#!/bin/sh
#(c) Copyright Barry Kauler, 30 January 2017. Licence: GPL v3 (/usr/share/doc/legal).
#simple script in initramfs to boot Easy Linux.
#deloyed as a drive image, with one 519MB fat32 partition, containing vmlinuz, initrd.q, q.sfs
#text colors: 34=blue, 33=yellow, 32=green, 31=red, 35=purple, 36=aquablue, 38=black

export TEXTDOMAIN=easyinitrd
export OUTPUT_CHARSET=UTF-8
mount -t proc none /proc
mount -t sysfs none /sys
mount -t rootfs -o remount,rw rootfs /
ln -s /proc/mounts /etc/mtab 2> /dev/null
export PATH="/bin:/sbin"

err_exit() {
 echo -e "\\033[1;31mERROR: ${1}\\033[0;39m" #red
 echo 'Have now dropped into a shell in the initramfs.'
 echo 'Note1: Type "exit", the init script will attempt to continue, however,'
 echo '       only an expert who is debugging the bootup should do this.'
 echo 'Note2: On some PCs the keyboard does not work at this stage of bootup.'
 echo 'PLEASE HOLD DOWN THE POWER BUTTON TO SHUTDOWN'
 /bin/sh
}

#170206 reintroducing aufs in the kernel:
if grep -qw aufs /proc/filesystems; then
 LAYERFS='aufs'
 RO='=ro'
else
 LAYERFS='overlay'
 RO=''
fi

SESSIONSFSflag=0 #170525 see also /usr/sbin/easy_version_control. 170924 change 1 to 0.
export SESSIONSFSflag

###find drives###
#find the drive we are booting on (has vmlinuz, initrd.q, q.sfs), and working drv...
. /BOOT_SPECS #has BOOT_DISKID, BOOT_PARTNUM, BOOT_FS, BOOT_DIR, WKG_DISKID, WKG_PARTNUM, WKG_FS, WKG_DIR, Q_DISTRO_VERSION
echo -n -e "\\033[1;35mFinding drives\\033[0;39m\n " #purple
CNT=0; Pb=''; Pw=''; BOOT_DRV=''; WKG_DRV=''

if [ $WKG_PARTNUM -eq 0 ];then #zram0
 FREEK=`grep '^MemFree:' /proc/meminfo | tr -s ' ' | cut -f 2 -d ' '`
 #allocate 3/4 of free ram times 2 (as compression is approx 2:1)...
 HALFK=$(($FREEK/2))
 QTRK=$(($HALFK/2))
 USEK=$(($HALFK+$QTRK)) #3/4
 ALLOCK=$(($USEK*2))
 echo "  Creating compressed zram, using ${USEK}K of RAM"
 echo "${ALLOCK}K" > /sys/block/zram0/disksize
 echo "${USEK}K" > /sys/block/zram0/mem_limit
 WKG_DRV='zram'
 WKG_DISKID='unknown'
 WKG_FS='ext4'
fi

while [ $CNT -lt 8 ];do #drives may take couple seconds to become available.
 sleep 1
 CNT=$(($CNT+1))
 for aDRV in /sys/block/sd[a-z] /sys/block/mmcblk[0-9]
 do
  [ "${aDRV/*]/]}" == "]" ] && continue #170731
  DRV=${aDRV:11:7} #extract drv from /sys/block/<drv>
  echo -n " ${DRV}"
  fdisk -l /dev/${DRV} > diskinfo-${DRV} 2>/dev/null
  if grep "$BOOT_DISKID" diskinfo-${DRV} >/dev/null;then BOOT_DRV="$DRV"; fi
  if grep "$WKG_DISKID" diskinfo-${DRV} >/dev/null;then WKG_DRV="$DRV"; fi
  [ "$BOOT_DRV" -a "$WKG_DRV" ] && break 2
 done
done
[ ! "$BOOT_DRV" ] && err_exit 'Boot drive not found'
[ ! "$WKG_DRV" ] && err_exit 'Working drive not found'
[ "${BOOT_DRV:0:3}" == "mmc" ] && Pb='p'
[ "${WKG_DRV:0:3}" == "mmc" ] && Pw='p'
BOOT_DEV="${BOOT_DRV}${Pb}${BOOT_PARTNUM}"
WKG_DEV="${WKG_DRV}${Pw}${WKG_PARTNUM}"
echo -e "\n  Boot-partition: ${BOOT_DEV}  Working-partition: ${WKG_DEV}"
mkdir -p /mnt/${BOOT_DEV}
mkdir -p /mnt/${WKG_DEV}

###performance measurements###
FREEK=`grep '^MemFree:' /proc/meminfo | tr -s ' ' | cut -f 2 -d ' '` #free RAM
#echo 3 > /proc/sys/vm/drop_caches #clear memory caches. note, could use "hdparm -t ..." but it is slower.
TIMEs="$(dd if=/dev/${WKG_DRV} of=/dev/null bs=1024 count=64 iflag=skip_bytes skip=1048576 2>&1 | grep -o '[0-9.]* seconds,' | cut -f 1 -d ' ')" #read speed.
TIME10k=$(dc ${TIMEs} 10000 mul p | cut -f 1 -d '.') #cheap flash stick: usb2=140 usb3=77, internal eMMC: 

###[create and] mount working partition###
if ! grep " ${WKG_DEV}$" /proc/partitions >/dev/null;then
 if [ $WKG_PARTNUM -ne 0 ];then
  echo -e "\\033[1;35mCreating partition ${WKG_DEV} to fill drive\\033[0;39m" #purple
  #170730 now mbr, not gpt, need to specify primary (p) or extended (e)...
  echo -e "n\np\n${WKG_PARTNUM}\n\n\nw" | fdisk -u /dev/$WKG_DRV > err.log 2>&1
  sync
  if grep -i 'failed' err.log ;then err_exit "Failed to create working partition ${WKG_DEV}" ; fi
 fi
 echo "  Creating ${WKG_FS} filesystem in partition ${WKG_DEV}"
 mke2fs -q -t ${WKG_FS} -O ^has_journal -L easy2 -m 0 -b 4096 /dev/${WKG_DEV} #only supporting ext2/3/4
fi
mount -t ${WKG_FS} /dev/${WKG_DEV} /mnt/${WKG_DEV}
[ $? -ne 0 ] && err_exit "Unable to mount working-partition ${WKG_DEV}"
if [ ! -d /mnt/${WKG_DEV}/${WKG_DIR}repository ];then #populate with skeleton directory hierarchy.
 cp -a -f /skeleton/* /mnt/${WKG_DEV}/${WKG_DIR}
 cp -a -f /skeleton/.[a-z]* /mnt/${WKG_DEV}/${WKG_DIR}
fi

###set date and time###
#could read .session/etc/clock and run hwclock, but for now this probably good enough (refer: rc.shutdown)...
[ -s /mnt/${WKG_DEV}/${WKG_DIR}.session/root/.var/local/shutdown_date_saved ] && date -s "`cat /mnt/${WKG_DEV}/${WKG_DIR}.session/root/.var/local/shutdown_date_saved`" > /dev/null

###mount boot partition###
if [ "$BOOT_DEV" != "$WKG_DEV" ];then
 mount -t ${BOOT_FS} /dev/$BOOT_DEV /mnt/$BOOT_DEV
 [ $? -ne 0 ] && err_exit "Unable to mount boot-partition ${BOOT_DEV}"
fi
[ ! -f /mnt/$BOOT_DEV/${BOOT_DIR}q.sfs ] && err_exit "$(LANG=${wkgLANG} gettext 'Boot-partition does not have file q.sfs')"

###recovery, maintenance###
[ "$qfix" ] && QFIX=$qfix #kernel boot param
if [ "$QFIX" ];then
 for ONEFIX in `echo -n "$QFIX" | tr ',' ' '`
 do
  case $ONEFIX in
   fsck|FSCK) echo -n "${WKG_DEV},${WKG_FS},REQUEST" > /mnt/${WKG_DEV}/${WKG_DIR}.session/.fsckme.flg ;;
   back|bak|BACK|BAK) echo -n ",last" > /mnt/${WKG_DEV}/${WKG_DIR}.session/.rollback.flg ;;
  esac
 done
fi
export wkgLANG="$(grep '^LANG=' /mnt/${WKG_DEV}/${WKG_DIR}.session/etc/profile 2>/dev/null | cut -f 2 -d '=')"
[ ! "$wkgLANG" ] && export wkgLANG=C
[ -s /mnt/${WKG_DEV}/${WKG_DIR}.session/.fsckme.flg ] && fscheck ${WKG_DRV} ${WKG_DEV} #ex: improper shutdown
[ -s /mnt/${WKG_DEV}/${WKG_DIR}.session/.rollback.flg ] && rollback ${WKG_DRV} ${WKG_DEV}

###version control###
prevVER="" #170919
if [ ! -d /mnt/${WKG_DEV}/${WKG_DIR}repository/easy-${Q_DISTRO_VERSION} ];then
 echo -e "\\033[1;35m$(LANG=${wkgLANG} gettext 'One-time only operation, creating a snapshot of Easy OS')\\033[0;39m"
 echo "  $(LANG=${wkgLANG} gettext 'This will allow future rollback with the Easy Version Control Manager')"
 echo "  $(LANG=${wkgLANG} gettext 'Creating:') /mnt/${WKG_DEV}/${WKG_DIR}repository/easy-${Q_DISTRO_VERSION}"
 mkdir /mnt/${WKG_DEV}/${WKG_DIR}repository/easy-${Q_DISTRO_VERSION}
 
 #170816 there is a limit on history...
 if [ -s /mnt/${WKG_DEV}/${WKG_DIR}.session/root/.var/local/version-history-depth ];then
  DEPTH_MAX="$(cat /mnt/${WKG_DEV}/${WKG_DIR}.session/root/.var/local/version-history-depth)"
  VERS="$(ls -l /mnt/${WKG_DEV}/${WKG_DIR}repository/easy-* | rev | cut -f 1 -d '-' | rev)"
  sortedVERS="$(echo "$VERS" | sort -t. -k 1,1n -k 2,2n -k 3,3n -k 4,4n)" #lowest to highest.
  for ADEL in `echo "$sortedVERS" | head -n -${DEPTH_MAX} | tr '\n' ' '`
  do
   if [ -d /mnt/${WKG_DEV}/${WKG_DIR}repositories/easy-${ADEL} ];then
    MSGdel="$(gettext 'Warning, deleting old version:')"
    echo -e "  \\033[1;31m${MSGdel} easy-${ADEL}\\033[0;39m" #red
    rm -rf /mnt/${WKG_DEV}/${WKG_DIR}repositories/easy-${ADEL}
   fi
  done
 fi

 if [ -f /mnt/${WKG_DEV}/${WKG_DIR}.session/etc/DISTRO_SPECS ];then
  prevVER="$(grep '^DISTRO_VERSION=' /mnt/${WKG_DEV}/${WKG_DIR}.session/etc/DISTRO_SPECS | cut -f 2 -d '=' | cut -f 1 -d ' ')"
  touch /mnt/${WKG_DEV}/${WKG_DIR}.session/.delayedrun_version_upgrade #see /usr/sbin/delayedrun
 else
  prevVER=""
 fi
 echo -n '  initrd.q'; cp -f /mnt/${BOOT_DEV}/${BOOT_DIR}initrd.q /mnt/${WKG_DEV}/${WKG_DIR}repository/easy-${Q_DISTRO_VERSION}/
 echo -n ' vmlinuz'; cp -f /mnt/${BOOT_DEV}/${BOOT_DIR}vmlinuz /mnt/${WKG_DEV}/${WKG_DIR}repository/easy-${Q_DISTRO_VERSION}/
 echo -n ' q.sfs'; cp -f /mnt/${BOOT_DEV}/${BOOT_DIR}q.sfs /mnt/${WKG_DEV}/${WKG_DIR}repository/easy-${Q_DISTRO_VERSION}/
 rm -rf /mnt/${WKG_DEV}/${WKG_DIR}.session/.[a-z]* 2>/dev/null #precaution.
 ##170919 delete *all* whiteout files (see also /sbin/fixlayers)...
 #find /mnt/${WKG_DEV}/${WKG_DIR}.session -mindepth 1 -type f -name '.wh.*' -delete 2>/dev/null
 if [ $SESSIONSFSflag -eq 1 ];then #170525
  if [ ! "$prevVER" ];then
   #.session folder (rw layer) is empty, but need a session.sfs anyway...
   echo -n ' session'; mksquashfs /mnt/${WKG_DEV}/${WKG_DIR}.session /mnt/${WKG_DEV}/${WKG_DIR}repository/easy-${Q_DISTRO_VERSION}/session.sfs -comp gzip >/dev/null
  else
   #need to merge .session (rw layer) with easy-$prevVER/session.sfs, create new session.sfs...
   mkdir sro snew
   mount -t squashfs -o loop,noatime /mnt/${WKG_DEV}/${WKG_DIR}repository/easy-${prevVER}/session.sfs sro
   [ $? -ne 0 ] && err_exit "FAILED: mount -t squashfs -o loop,noatime /mnt/${WKG_DEV}/${WKG_DIR}repository/easy-${prevVER}/session.sfs sro"
   if [ "$LAYERFS" == "aufs" ];then
    mount -t aufs -o br=/mnt/${WKG_DEV}/${WKG_DIR}.session=rw:sro=ro aufs snew
    [ $? -ne 0 ] && err_exit "FAILED: mount -t aufs -o br=/mnt/${WKG_DEV}/${WKG_DIR}.session=rw:sro=ro aufs snew"
   else
    mkdir -p /mnt/${WKG_DEV}/${WKG_DIR}swork
    mount -t overlay -o lowerdir=sro,upperdir=/mnt/${WKG_DEV}/${WKG_DIR}.session,workdir=/mnt/${WKG_DEV}/${WKG_DIR}swork overlay snew
    [ $? -ne 0 ] && err_exit "mount -t overlay -o lowerdir=sro,upperdir=/mnt/${WKG_DEV}/${WKG_DIR}.session,workdir=/mnt/${WKG_DEV}/${WKG_DIR}swork overlay snew"
   fi
   echo -e 'dev\nmnt\nproc\nrun\nsys\ntmp\nvar\nlost+found\n.XLOADED' > exclusions1 #exclusions.
   mksquashfs snew /mnt/${WKG_DEV}/${WKG_DIR}repository/easy-${Q_DISTRO_VERSION}/session.sfs -ef exclusions1 -comp gzip #>/dev/null
   echo -e "\\033[1;33m$(LANG=${wkgLANG} gettext 'Please wait, synchronizing...')\\033[0;39m" #yellow
   sync
   umount snew
   umount sro
   [ -d /mnt/${WKG_DEV}/${WKG_DIR}swork ] && rm -rf /mnt/${WKG_DEV}/${WKG_DIR}swork
   rm -rf /mnt/${WKG_DEV}/${WKG_DIR}.session/*
   rm -f /mnt/${WKG_DEV}/${WKG_DIR}repository/easy-${prevVER}/session.sfs #remove session.sfs in prev. version.
  fi
 else
  #echo -n "rw-${Q_DISTRO_VERSION}.sfs"; 
  mksquashfs /mnt/${WKG_DEV}/${WKG_DIR}.session /mnt/${WKG_DEV}/${WKG_DIR}repository/easy-${Q_DISTRO_VERSION}/rw-${Q_DISTRO_VERSION}.sfs -comp gzip #>/dev/null
 fi
 mkdir -p /mnt/${WKG_DEV}/${WKG_DIR}.session/etc
 #also save any container sessions:
 for EXE in `ls -1 /mnt/${WKG_DEV}/${WKG_DIR}containers | tr '\n' ' '`
 do
  [ ! -d /mnt/${WKG_DEV}/${WKG_DIR}containers/${EXE}/.session ] && continue
  #echo -n " ec-${EXE}"; mksquashfs /mnt/${WKG_DEV}/${WKG_DIR}containers/${EXE}/.session /mnt/${WKG_DEV}/${WKG_DIR}containers/${EXE}/rw-${prevVER}.sfs -comp gzip >/dev/null
  cp -f /mnt/${WKG_DEV}/${WKG_DIR}containers/${EXE}/configuration /mnt/${WKG_DEV}/${WKG_DIR}containers/${EXE}/configuration-${Q_DISTRO_VERSION}
 done
 echo
 touch /mnt/${WKG_DEV}/${WKG_DIR}repository/easy-${Q_DISTRO_VERSION}/configuration
 touch /mnt/${WKG_DEV}/${WKG_DIR}repository/easy-${Q_DISTRO_VERSION}/configuration-${Q_DISTRO_VERSION}
fi

###recompress q.sfs### improve operating speed, recompress q.sfs (refer quicksetup)
if [ -s /mnt/${WKG_DEV}/${WKG_DIR}.session/.qsfs.flg ];then
 echo -e "\\033[1;35m$(LANG=${wkgLANG} gettext 'Recompressing q.sfs, xz to gz, please wait')\\033[0;39m" #purple
 mkdir /mntsfs
 mount -r -t squashfs -o loop,noatime /mnt/${WKG_DEV}/${WKG_DIR}repository/easy-${Q_DISTRO_VERSION}/q.sfs /mntsfs
 mksquashfs /mntsfs /mnt/${WKG_DEV}/${WKG_DIR}q-gz.sfs -comp gzip #>/dev/null
 sync
 umount /mntsfs
 mv -f /mnt/${WKG_DEV}/${WKG_DIR}q-gz.sfs /mnt/${WKG_DEV}/${WKG_DIR}repository/easy-${Q_DISTRO_VERSION}/q.sfs
 sync
 rm -f /mnt/${WKG_DEV}/${WKG_DIR}.session/.qsfs.flg
fi

###setup bottom ro layer, with q.sfs###
echo -e "\\033[1;35m$(LANG=${wkgLANG} gettext 'Mounting read-only layer of layered filesystem')\\033[0;39m" #purple
QSFSbytes0=`stat -c %s /mnt/${WKG_DEV}/${WKG_DIR}repository/easy-${Q_DISTRO_VERSION}/q.sfs`
QSFSbytes1=$(($QSFSbytes0+500000))
mount -t tmpfs -o size=${QSFSbytes1} tmpfs /q_ro
mkdir /q_ro/q_sfs
#decide whether to copy q.sfs to RAM, or not...
CPYflg=0
[ $FREEK -gt 1100000 ] && CPYflg=1 #>1GB ram then copy
[ $TIME10k -lt 100 ] && CPYflg=0   #but fast drive so don't copy.
[ $FREEK -gt 3100000 ] && CPYflg=1 #but heaps of ram, so copy.
if [ $CPYflg -eq 0 ];then
 #do not copy q.sfs into ram, mount where it is...
 echo "  $(LANG=${wkgLANG} gettext 'Mounting squashfs file q.sfs')"
 QSFS_PATH="/mnt/${WKG_DEV}/${WKG_DIR}repository/easy-${Q_DISTRO_VERSION}/" #need this for setting up containers.
 mount -r -t squashfs -o noatime,loop /mnt/${WKG_DEV}/${WKG_DIR}repository/easy-${Q_DISTRO_VERSION}/q.sfs /q_ro/q_sfs
else
 echo "  $(LANG=${wkgLANG} gettext 'Copying q.sfs to RAM, then mounting')"
 [ ! -f /q_ro/q.sfs ] && cp -a /mnt/${WKG_DEV}/${WKG_DIR}repository/easy-${Q_DISTRO_VERSION}/q.sfs /q_ro/
 QSFS_PATH="/mnt/q_ro/" #need this for setting up containers.
 mount -r -t squashfs -o noatime,loop /q_ro/q.sfs /q_ro/q_sfs
fi
[ $? -ne 0 ] && err_exit "$(LANG=${wkgLANG} gettext 'Failed to mount q.sfs')"
mkdir -p /mnt/${WKG_DEV}/${WKG_DIR}.session/etc #170927 missing if have erased session.
cp -f /q_ro/q_sfs/etc/DISTRO_SPECS /mnt/${WKG_DEV}/${WKG_DIR}.session/etc/ #need to be sure correct one is on top.
EXTRASFS=""; sessionSFS=""; NEWEXTRASFSLIST=""
if [ $SESSIONSFSflag -eq 1 ];then #170525
 mkdir /q_ro/session
 mount -r -t squashfs -o loop,noatime /mnt/${WKG_DEV}/${WKG_DIR}repository/easy-${Q_DISTRO_VERSION}/session.sfs /q_ro/session
 sessionSFS="/q_ro/session${RO}:"
fi
#precaution...
. /q_ro/q_sfs/etc/DISTRO_SPECS
[ "$DISTRO_VERSION" != "$Q_DISTRO_VERSION" ] && echo -e "\\033[1;31m$(gettext 'WARNING, versions do not match.') initrd.q: ${Q_DISTRO_VERSION}, q.sfs: ${DISTRO_VERSION}\\033[0;39m" #red

###load extra sfs on ro layer###
#the configuration file defines any more sfs files to load. q.sfs is always loaded
# (on bottom), and session.sfs on top of ro layers.
. /mnt/${WKG_DEV}/${WKG_DIR}repository/easy-${Q_DISTRO_VERSION}/configuration
for NUM in 4 3 2 1
do
 eval "ROsfs=\$EASY_LAYER_RO${NUM}" #implements indirection. EASY_LAYER_RO1='devx*.sfs' in configuration file, will assign 'devx*.sfs' to ROsfs. 170320 170523
 if [ "$ROsfs" ];then
  #170523 check file exists. note, may have a glob wildcard...
  FNDSFSS="$(ls -1 /mnt/${WKG_DEV}/${WKG_DIR}repository/easy-${Q_DISTRO_VERSION}/${ROsfs} | tr '\n' ' ')"
  for FNDSFS in $FNDSFSS
  do
   ANAME="$(basename $FNDSFS .sfs)"
   mkdir /q_ro/$ANAME
   echo "  $(LANG=${wkgLANG} gettext 'Mounting extra squashfs file:') ${ANAME}.sfs"
   mount -r -t squashfs -o noatime,loop /mnt/${WKG_DEV}/${WKG_DIR}repository/easy-${Q_DISTRO_VERSION}/${ANAME}.sfs /q_ro/${ANAME}
   EXTRASFS="/q_ro/${ANAME}${RO}:${EXTRASFS}" #170206
   NEWEXTRASFSLIST="${ANAME}:${NEWEXTRASFSLIST}" #170919
  done
 fi
done

sync
[ "$BOOT_DEV" != "$WKG_DEV" ] && umount /dev/${BOOT_DEV}

###fix layers change###
OLDEXTRASFSLIST="$(grep '^EXTRASFSLIST=' /mnt/${WKG_DEV}/${WKG_DIR}.session/etc/rc.d/PUPSTATE 2>/dev/null | cut -f 2 -d "'")"
[ -s /mnt/${WKG_DEV}/${WKG_DIR}.session/etc/rc.d/PUPSTATE ] && [ "$NEWEXTRASFSLIST" != "$OLDEXTRASFSLIST" -o "$prevVER" != "" ] && /sbin/fixlayers ${WKG_DRV} ${WKG_DEV} "${NEWEXTRASFSLIST}" "${prevVER}" #170919

###the big moment, create layered f.s.###
#echo -e "\\033[1;35m$(LANG=${wkgLANG} gettext 'Creating layered filesystem, with read-write folder:') /mnt/${WKG_DEV}/.session\\033[0;39m"
echo -e "\\033[1;35m$(LANG=${wkgLANG} gettext 'Creating layered filesystem, type:') ${LAYERFS}\\033[0;39m"
if [ "$LAYERFS" == "aufs" ];then #170525
 mount -t aufs -o br=/mnt/${WKG_DEV}/${WKG_DIR}.session=rw:${sessionSFS}${EXTRASFS}/q_ro/q_sfs=ro aufs /q_new
else
 mount -t overlay -o lowerdir=${sessionSFS}${EXTRASFS}/q_ro/q_sfs,upperdir=/mnt/${WKG_DEV}/${WKG_DIR}.session,workdir=/mnt/${WKG_DEV}/${WKG_DIR}.tempwork overlay /q_new
fi
[ $? -ne 0 ] && err_exit "$(LANG=${wkgLANG} gettext 'Failed to create layered filesystem')"

#/etc/rc.d/rc.sysinit will append to PUPSTATE, get the ball rolling here...
#PUPMODE, bit-1 (partition has session), bit-2 (bottom layer is sfs) --rough equiv to puppy
echo -e "PUPMODE=6\nBOOT_DEV='${BOOT_DEV}'\nBOOT_FS='${BOOT_FS}'\nBOOT_DIR='${BOOT_DIR}'\nWKG_DEV='${WKG_DEV}'\nWKG_FS='${WKG_FS}'\nWKG_DIR='${WKG_DIR}'\nQSFS_PATH='${QSFS_PATH}'" > /q_new/etc/rc.d/PUPSTATE
echo -e "EXTRASFSLIST='${NEWEXTRASFSLIST}'\nSESSIONSFSflag=${SESSIONSFSflag}" >> /q_new/etc/rc.d/PUPSTATE #170919 170924

###relocate mount-points prior to switch_root###
echo -e "\\033[1;35m$(LANG=${wkgLANG} gettext 'Performing a switch_root onto the layered filesystem')\\033[0;39m" #purple
#move the mount points...
[ ! -d /q_new/mnt/q_ro ] && mkdir /q_new/mnt/q_ro
[ ! -d /q_new/mnt/${WKG_DEV} ] && mkdir /q_new/mnt/${WKG_DEV}
[ ! -d /q_new/mnt/${BOOT_DEV} ] && mkdir /q_new/mnt/${BOOT_DEV}
mount -o move /q_ro /q_new/mnt/q_ro
mount -o move /mnt/${WKG_DEV} /q_new/mnt/${WKG_DEV}

mount -t devtmpfs devtmpfs /q_new/dev #need to do this before switch_root.
sync
umount /sys
umount /proc
exec switch_root /q_new /sbin/init

###END###
