Advertisement
Guest User

Untitled

a guest
Aug 30th, 2020
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 53.31 KB | None | 0 0
  1. #!/bin/sh
  2. # see also /sbin scripts: usablefs, set_plang, wait4usb, switch
  3. #
  4. # _FN = _FILENAME
  5. # _MP = _MOUNTPOINT
  6. #
  7.  
  8. #Aufs layers setup by this script...
  9. #aufs layers:            RW (top)      RO1             RO2              PUPMODE
  10. #First boot (or pfix=ram):  tmpfs                         pup_xxx.sfs      5
  11. #Normal running puppy:      pup_save.3fs                  pup_xxx.sfs      12
  12. #ditto, but flash drive:    tmpfs         pup_save.3fs    pup_xxx.sfs      13
  13. #Multisession cd/dvd:       tmpfs         folders         pup_xxx.sfs      77
  14.  
  15. ######################## localization ########################
  16. #ex: /locale/<locale>/init.mo
  17. L_WINDOWS_HIBERNATED="ERROR: Windows NTFS hibernated partition, cannot mount"
  18. L_DONE="done"
  19. L_FAILED="failed"
  20. L_DUMPING_BOOTINIT_LOG="Dumping last lines of /tmp/bootinit.log..."
  21. L_DUMPING_KERNEL_LOG="Dumping last lines of kernel log..."
  22. L_ERROR_IS_TOO_CRITICAL="Error is too critical, dropping out to console..."
  23. L_PAUSING_60_SECONDS="Pausing for 60 seconds..."
  24. L_LOADING_FILE="Loading the '%s' %s file..." #printf
  25. L_CANNOT_RESIZE_PUPSAVE="SORRY, cannot resize %s" #printf
  26. L_INCREASING_PUPSAVE='Increasing %s by %s Kbytes, please wait...' #printf
  27. L_LOADING_PUPSAVE='Loading savefolder %s [%s]...' #printf
  28. L_LOADING_PUPSAVE_FILE='Loading savefile %s [%s] (%s)...' #printf
  29. L_UPGRADE_NO="Backing off, not using personal storage, booting in RAM only, PUPMODE=5..."
  30. L_LOADING_FROM_CD="Loading folder %s from CD/DVD..." #printf
  31. L_RAM_DISK_FULL="RAM disk full, copy failed at %s" #printf
  32. L_ADDING_SAVE_LAYER_FAILED="adding %s to aufs stack failed." #printf
  33. L_WAITING_FOR_USB="Waiting for USB storage."
  34. L_LOADING_KEYBOARD_LAYOUT="Loading '%s' keyboard layout..." #printf
  35. L_COPY_MESSAGE="copying to ram"
  36. L_LOAD_MESSAGE="main"
  37. L_PRE_MESSAGE="Loading puppy main sfs file."
  38. L_ERR_PDRV_INCOMPLETE="%s information is incomplete." #printf
  39. L_ERR_ONEPART_NOT_MOUNTED="%s is not mounted." #printf
  40. L_ERR_ONEPART_NOT_OK="%s %s is not Ok." #printf
  41. L_ERR_ONEPART_MOUNT_SFS_FAILED="%s %s mount of sfs failed." #printf
  42. L_ERR_AUFS_SFS_FAILED="aufs mount of %s failed." #printf
  43. L_ERR_TYPE_PUPSAVE="Type a number to choose which personal file to use:"
  44. L_DROPPED_TO_INITRD_SHELL="Dropped to initramfs shell. Type 'exec switch' to continue booting Puppy."
  45. L_SWITCH_ROOT="Performing a 'switch_root' to the layered filesystem..."
  46. L_CONTINUING_LOADING="...continuing with loading %s..." #printf
  47. L_FOLDER_MARKED_BAD="Folder %s marked bad." #printf
  48. L_0_NONE="0  none"
  49. L_ERROR_FAILED_AUFS_STACK='Failed to create empty aufs stack'
  50. L_ADDING_MODULE="Adding module %s" #printf
  51. L_NO_DISTRO_SPECS="No DISTRO_SPECS file found, assuming that personal storage is empty."
  52. L_DEBUG_SAVE="To save debug info to a partition, type 'debugsave'"
  53. L_PASSWORD="Password:"
  54. L_PASSWORD_MSG_1="NOTICE: As you type your password nothing will be displayed on the screen."
  55. L_PASSWORD_MSG_2="This is a security measure. Just type it in then press ENTER key..."
  56. L_SFFS_ERROR="ERROR: savefile filesystem is not ext2/3/4"
  57.  
  58. ##############################################################
  59.  
  60. /sbin/usablefs # mount: /proc /sys /dev / (proc sysfs devtmpfs rootfs)
  61.  
  62. # a full-install OS has 'fullinstall' param in the cmdline
  63. # it's added by puppyinstaller (or you can add it manually)
  64. for i in $(cat /proc/cmdline) ; do
  65.   [ "$i" = "fullinstall" ] && exec /init_full_install
  66. done
  67.  
  68. export TERM="xterm"
  69. export TERMINFO="/usr/share/terminfo"
  70. export LANG=C
  71. PATH="/bin:/sbin"
  72. export KERNELVER="$(uname -r)"
  73. VFAT_OUT_PARAM='noatime,shortname=mixed,quiet,utf8' #see also /sbin/set_plang
  74. . /DISTRO_SPECS #v412 has DISTRO_VERSION, DISTRO_FILE_PREFIX
  75. . /etc/rc.d/functions_x
  76. export DISTRODESC="${DISTRO_NAME} ${DISTRO_VERSION} - Linux ${KERNELVER} - `uname -m`"
  77.  
  78. #precaution - if DISTRO_SPECS was not processed by 3builddistro...
  79. [ ! "$DISTRO_ZDRVSFS" ] && DISTRO_ZDRVSFS="zdrv_${DISTRO_FILE_PREFIX}_${DISTRO_VERSION}.sfs"
  80. [ ! "$DISTRO_FDRVSFS" ] && DISTRO_FDRVSFS="fdrv_${DISTRO_FILE_PREFIX}_${DISTRO_VERSION}.sfs"
  81. [ ! "$DISTRO_ADRVSFS" ] && DISTRO_ADRVSFS="adrv_${DISTRO_FILE_PREFIX}_${DISTRO_VERSION}.sfs"
  82. [ ! "$DISTRO_YDRVSFS" ] && DISTRO_YDRVSFS="adrv_${DISTRO_FILE_PREFIX}_${DISTRO_VERSION}.sfs"
  83. [ ! "$DISTRO_PUPPYSFS" ] && DISTRO_PUPPYSFS="puppy_${DISTRO_FILE_PREFIX}_${DISTRO_VERSION}.sfs"
  84.  
  85. # filenames specified in DISTRO_SPECS: DISTRO_ZDRVSFS, DISTRO_PUPPYSFS...
  86. Z_DEF_FN="$DISTRO_ZDRVSFS"
  87. F_DEF_FN="$DISTRO_FDRVSFS"
  88. A_DEF_FN="$DISTRO_ADRVSFS"
  89. Y_DEF_FN="$DISTRO_YDRVSFS"
  90. P_DEF_FN="$DISTRO_PUPPYSFS"
  91.  
  92. if [ $loglevel ] ; then
  93.   LOGLEVEL=$loglevel
  94. else
  95.   # If no loglevel is specified, the kernel uses its default loglevel..
  96.   # but it may be too verbose and flood the screen.
  97.   # '3' is the standard loglevel.
  98.   echo '3' > /proc/sys/kernel/printk
  99. fi
  100.  
  101. #=============================================================
  102. #                        FUNCTIONS
  103. #=============================================================
  104.  
  105. fsck_func() {
  106. # "$1" - partition device - ex: /dev/sdb2
  107. # "$2" - fstype - ex: ext2
  108.  case $2 in
  109.   ext2|ext3|ext4) fsck_app='e2fsck' ; fsck_OPT='-y' ;;
  110.   #vfat|msdos) fsck_app='fsck.fat' ; fsck_OPT='-y' ;;
  111.   #exfat) fsck_app='exfatfsck' ; fsck_OPT='' ;;
  112.   *) return ;;
  113.  esac
  114.  which "${fsck_app}" || {
  115.    echo "WARNING: '${fsck_app}' not found"
  116.    FSCKDPARTS="${FSCKDPARTS}${1}|"
  117.    return
  118.  }
  119.  if [ "$(echo "$FSCKDPARTS" | grep "${1}|")" = "" ];then
  120.   ${fsck_app} ${fsck_OPT} ${1} > /dev/console 2>&1 || sleep 3 #so the user has time to read the error msg..
  121.   FSCKDPARTS="${FSCKDPARTS}${1}|"
  122.  fi
  123. }
  124.  
  125. mntfunc() {
  126.  MNT_T="$1"    #ex: vfat
  127.  MNT_DEV="$2"  #ex: /dev/sda1
  128.  MNT_DIR="$3"  #ex: /mnt/sda1
  129.  MNT_O='noatime'
  130.  MNT_DSK="${MNT_DEV:5:3}"
  131.  if [ "$PTRIM" = "yes" -a "$MNT_DSK" != "" -a "$(cat /sys/block/$MNT_DSK/queue/rotational)" = "0" ];then
  132.   if [ "$MNT_T" = "ext4" -o "$MNT_T" = "f2fs" ];then
  133.     MNT_O="${MNT_O},discard"
  134.   fi
  135.  fi
  136.  [ "$PFSCKP" = "yes" ] && fsck_func "$MNT_DEV" "$MNT_T"
  137.  case $MNT_T in
  138.   ntfs)
  139.    ntfs-3g $MNT_DEV $MNT_DIR -o umask=0,no_def_opts,noatime,rw,silent 2>/dev/null #default is rw. 130211 add silent.
  140.    ntfsRETVAL=$?
  141.    [ $ntfsRETVAL -eq 0 ] && return 0
  142.    if [ $ntfsRETVAL -eq 14 ];then
  143.     echo -e "\\033[1;31m${L_WINDOWS_HIBERNATED}\\033[0;39m" >/dev/console #31=red
  144.     return 14
  145.    else
  146.     ntfs-3g $MNT_DEV $MNT_DIR -o umask=0,no_def_opts,noatime,rw,force,silent 2>/dev/null #130211 add silent.
  147.    fi
  148.   ;;
  149.   vfat)  mount -t $MNT_T -o $VFAT_OUT_PARAM $MNT_DEV $MNT_DIR ;;
  150.   exfat) mount.exfat-fuse -o $MNT_O $MNT_DEV $MNT_DIR ;;
  151.   *) mount -t $MNT_T -o $MNT_O $MNT_DEV $MNT_DIR ;;
  152.  esac
  153.  return $?
  154. }
  155.  
  156. check_status() #args: [-critical] $RET_VAL "$ERROR_MSG"
  157. {
  158.   ERROR_CRITICAL=""
  159.   [ "$1" = "-critical" ] && { ERROR_CRITICAL="yes" ; shift; }
  160.   RET_VAL=$1
  161.   shift
  162.   ERROR_MSG="$@"
  163.   if [ $RET_VAL -eq 0 ] ; then
  164.     echo -en "\\033[74G" >/dev/console #move to column 72. 110426: 74
  165.     echo -e "\\033[1;32m${L_DONE}\\033[0;39m" >/dev/console #32=green
  166.   else
  167.     echo -en "\\033[72G" >/dev/console #move to column 72.
  168.     echo -e "\\033[1;31m${L_FAILED}\\033[0;39m" >/dev/console #31=red
  169.     if [ "$FLAG_NO_TIMEOUT" ] ; then
  170.       FLAG_NO_TIMEOUT=
  171.       return
  172.     fi
  173.     echo -e "\\033[1;35m${L_DUMPING_BOOTINIT_LOG}\\033[0;39m" >/dev/console #35=purple
  174.     echo -en "\\033[1;31m" >/dev/console #31=red
  175.     cat /tmp/bootinit.log | tail -n 4 >/dev/console
  176.     echo -en "\\033[0;39m" >/dev/console
  177.     echo -e "\\033[1;35m${L_DUMPING_KERNEL_LOG}\\033[0;39m" >/dev/console #35=purple
  178.     echo -en "\\033[1;31m" >/dev/console #31=red
  179.     dmesg | tail -n 4 >/dev/console
  180.     echo -en "\\033[0;39m" >/dev/console
  181.     #exit to initial ramdisk shell...
  182.     [ "$RDSH" != "" ] && exec /bin/sh >/dev/console 2>&1
  183.     if [ "$ERROR_CRITICAL" = "yes" ] ; then
  184.       echo -en "\\033[1;35m" >/dev/console #35=purple
  185.       [ "$ERROR_MSG" != "" ] && echo "*** $ERROR_MSG" >/dev/console
  186.       echo "*** ${L_ERROR_IS_TOO_CRITICAL}" >/dev/console
  187.       echo -en "\\033[0;39m" >/dev/console
  188.       echo -e "\\033[1;32m*** $L_DEBUG_SAVE\\033[0;39m" >/dev/console
  189.       exec /bin/sh >/dev/console 2>&1
  190.     else
  191.       echo "${L_PAUSING_60_SECONDS}" >/dev/console
  192.       sleep 60
  193.     fi
  194.   fi
  195. }
  196.  
  197. decode_spec() {
  198.  #${1} = an sfs spec e.g. sdb2:/pup/test_it.sfs
  199.  ONE_BP_ID=""; ONE_BP_FN=""
  200.  [ "${1}" ] || return
  201.  case "${1}" in
  202.   *:*) ONE_BP_ID="${1%%:*}"; ONE_BP_FN="${1#*:}" ;;
  203.   *) ONE_BP_ID="${1}" ;;
  204.  esac
  205.  if [ "$ONE_BP_FN" ];then
  206.   case "$ONE_BP_FN" in
  207.    */*) [ "${ONE_BP_FN:0:1}" != "/" ] && ONE_BP_FN="/$ONE_BP_FN" ;;
  208.    *) ONE_BP_FN="$PSUBDIR/$ONE_BP_FN" ;;
  209.   esac
  210.  fi
  211. }
  212.  
  213. decode_id() {
  214.  ONE_PART=""
  215.  [ "${1}" ] || return
  216.  if [ "$(echo -n ${1} | grep -E '^[a-z]+[0-9]')" -a "$(grep -m1 "${1}$" /proc/partitions)" ];then
  217.   ONE_PART="$1"  #is a real partition
  218.  else
  219.   [ "$BLKIDOUT" ] || BLKIDOUT="$(blkid)"
  220.   ONE_PART="$(echo "$BLKIDOUT" | grep -m1 -E " LABEL=.${1}| UUID=.${1}" | cut -f1 -d: | cut -f3 -d/)" #is LABEL or UUID
  221.  fi
  222. }
  223.  
  224. decode_other_ids() {
  225.  #get ONE_PART from DEV or ID specified in sfs boot params
  226.  decode_id "$Z_BP_ID"
  227.  [ "$ONE_PART" ] && { Z_PART="$ONE_PART"; Z_BP_ID=""; }
  228.  decode_id "$F_BP_ID"
  229.  [ "$ONE_PART" ] && { F_PART="$ONE_PART"; F_BP_ID=""; }
  230.  decode_id "$A_BP_ID"
  231.  [ "$ONE_PART" ] && { A_PART="$ONE_PART"; A_BP_ID=""; }
  232.  decode_id "$Y_BP_ID"
  233.  [ "$ONE_PART" ] && { Y_PART="$ONE_PART"; Y_BP_ID=""; }
  234.  decode_id "$SAVE_BP_ID"
  235.  [ "$ONE_PART" ] && { SAVEPART="$ONE_PART"; PSAVEPART="$ONE_PART"; SAVE_BP_ID=""; }
  236. }
  237.  
  238. log_part_id() {
  239. # "$1" - partition id - ex: Work or sdc4
  240.  echo "Partition ${1} not found."
  241. }
  242.  
  243. ensure_mounted() {
  244. # "$1" - partition - ex: sda3
  245. # "$2" - mountpoint - ex: /mnt/pdrv
  246.  ONE_MP="$(mount | grep -m1 "/dev/${1} " | cut -f 3 -d ' ')"
  247.  [ "$ONE_MP" ] && return
  248.  ONE_FS="$(echo "$HAVE_PARTS" | grep -m1 "${1}|" | cut -f 2 -d '|')"
  249.  ONE_MP="${2}"
  250.  [ -d "$ONE_MP" ] || mkdir -p $ONE_MP
  251.  mntfunc $ONE_FS /dev/${1} $ONE_MP #-t $ONE_FS /dev/$ONE_PART $ONE_MP
  252.  if [ $? -ne 0 ] ; then
  253.   sleep 3 # usb optical drive showing as /sys/block/sr0, but won't mount, needs more delay...
  254.   mntfunc $ONE_FS /dev/${1} $ONE_MP
  255.   [ $? -ne 0 ] && { echo "${1} on $ONE_MP as $ONE_FS mount failed."; ONE_MP=""; return 1; }
  256.  fi
  257.  # fsckme.flg is created by rc.sysinit and deleted by rc.shutdown
  258.  # this flag will be set for any prior improper shutdown. if have lots of installations
  259.  # of puppy on the pc, the flag may not even be for this install of puppy, however, this is
  260.  # the simplest implementation...
  261.  if [ -f ${ONE_MP}/fsckme.flg ] ; then
  262.   #sda1,ext3,/PUPPYBOOT/precise/precisesave.4fs
  263.   FSCKME="`cat ${ONE_MP}/fsckme.flg`"
  264.   echo -e "${ONE_MP}/fsckme.flg\n  $FSCKME"
  265.   FSCK_PART="$(echo "$FSCKME" | cut -f 1 -d ",")"
  266.   FSCK_EXT="$(echo "$FSCKME" | cut -f 2 -d ",")"
  267.   FSCK_SAVEFILE="$(echo "$FSCKME" | cut -f 3 -d ",")"
  268.   rm -f ${ONE_MP}/fsckme.flg
  269.   [ "$FSCK_SAVEFILE" ] && PFSCK="yes"
  270.   [ "$PFSCKP" = "yes" ] && return # boot param, partition already fsck'ed
  271.   case ${FSCK_EXT} in ext2|ext3|ext4|vfat|msdos|exfat)
  272.     umount ${ONE_MP}
  273.     [ "$FSCKME" ] && fsck_func /dev/${FSCK_PART} ${FSCK_EXT}
  274.     ensure_mounted "$1" "$2" ;;
  275.   esac
  276.  fi
  277. }
  278.  
  279. ensure_save_mounted() {
  280.  SAVE_MP="/mnt/dev_save"
  281.  ensure_mounted "$SAVEPART" "$SAVE_MP"
  282.  if [ "$ONE_MP" ];then
  283.   if [ "$ONE_MP" != "$SAVE_MP" ];then
  284.    #ensure SAVEPART is mounted on /mnt/dev_save
  285.    [ -d "$SAVE_MP" ] || mkdir -p $SAVE_MP
  286.    echo "mount -o move $ONE_MP $SAVE_MP" #debug
  287.    mount -o move $ONE_MP $SAVE_MP
  288.   fi
  289.   SAVE_FS="$ONE_FS"
  290.  else
  291.   SAVE_MP=""
  292.  fi
  293. }
  294.  
  295. find_drv_file() {
  296. # "$1" - specified filename - ex: /pup/mydrv-1.2.3.sfs
  297. # "$2" - default filename - ex: adrv_tahr_6.0.5.sfs
  298.  ONE_FN=""
  299.  [ "${1}" ] || [ "${2}" ] || return
  300.  if [ "${1}" ];then
  301.   ONE_TRY_FN="${1}"
  302.   [ "${ONE_TRY_FN:$((${#ONE_TRY_FN} - 1))}" = "/" ] && ONE_TRY_FN="${ONE_TRY_FN}${2}" #last char
  303.  else
  304.   ONE_TRY_FN="${PSUBDIR}/${2}"
  305.  fi
  306.  [ -f "${ONE_MP}${ONE_TRY_FN}" ] && ONE_FN="$ONE_TRY_FN"
  307. }
  308.  
  309. find_onepupdrv() {
  310. # "$1" - partition - ex: sda3
  311. # "$2" - specified filename - ex: /pup/mydrv-1.2.3.sfs
  312. # "$3" - default filename - ex: adrv_tahr_6.0.5.sfs
  313. # "$4" - prefix for "drv" mountpoint - ex: a
  314.  ONE_FN=""
  315.  [ "${2}" ] || [ "${3}" ] || return
  316.  [ "${4}" ] || return
  317.  if [ "${1}" ];then
  318.   ONE_PART="${1}"
  319.  else
  320.   ONE_PART="$P_PART"
  321.  fi
  322.  [ "$ONE_PART" ] || return
  323.  ensure_mounted "$ONE_PART" "/mnt/${4}drv"
  324.  [ "$ONE_MP" ] || return
  325.  find_drv_file "${2}" "${3}"
  326.  [ "$ONE_FN" = "" -a "${2}" ] && echo "$ONE_PART, $ONE_TRY_FN file not found."
  327. }
  328.  
  329. setup_loop_sfs() {
  330. # "$1" - sfs filename - ex: /mnt/dev_save/puppy/tahr/puppy_tahr_6.0.5.sfs
  331.  ONE_LOOP="$(losetup -f)"
  332.  losetup $LO_OPT $ONE_LOOP ${1}
  333.  [ $? -eq 0 ] || { LO_OPT=""; losetup $LO_OPT $ONE_LOOP ${1}; }
  334. }
  335.  
  336. load_sfs_file() {
  337.  [ "$LOADMSG" ] && echo -n "$(printf "${L_LOADING_FILE}" "$ONE_BASENAME" "$LOADMSG")" > /dev/console
  338.  if [ "$COPY2RAM" = "" ];then
  339.   COPY2RAM="no"
  340.   #if there's heaps of ram, copy puppy.sfs to a tmpfs...
  341.   #v405 fast media plus more than 256MB ram then definitely worth copying to ram...
  342.   SIZESFSK=$(du -k $ONE_FN | cut -f 1)
  343.   SIZESFSK=$(($SIZESFSK + 1000)) #some slack.
  344.   MINRAM2CPY=$(($SIZESFSK * 2)) #100222 technosaurus: in case of very big puppies.
  345.   #decide whether to copy .sfs's to ram
  346.   [ "$PNOCOPY" = "yes" ] || COPYCONTENDER='yes'
  347.   [ "$PCOPY" = "yes" ] && COPYCONTENDER='yes'
  348.   [ "$PRAMONLY" = "yes" ] && COPYCONTENDER='yes'
  349.   [ "$COPYCONTENDER" = "yes" ] && [ $RAMSIZE -gt 400000 ] && [ $RAMSIZE -gt $MINRAM2CPY ] && COPY2RAM="yes"
  350.  fi
  351.  if [ "$COPY2RAM" = "yes" ];then
  352.   SIZEZK=$(du -k $ONE_FN | cut -f 1)
  353.   TFREEK=$(df | grep -m1 ' /mnt/tmpfs' | tr -s ' ' | cut -f 4 -d ' ')
  354.   if [ $TFREEK -gt $SIZEZK ];then
  355.    if [ "$ONE_MP" = "" ];then #101101 humongous initrd.
  356.     mv -f $ONE_FN /mnt/tmpfs/
  357.    else
  358.     [ "$LOADMSG" ] && [ "$COPYMSG" ] && echo -en " \\033[1;35m${COPYMSG}\\033[0;39m" > /dev/console #purple.
  359.     TOTAL_SIZEK_SFS_RAM=$(($TOTAL_SIZEK_SFS_RAM + $SIZEZK))
  360.     cp -af $ONE_FN /mnt/tmpfs/
  361.    fi
  362.    sync
  363.    setup_loop_sfs /mnt/tmpfs/$ONE_BASENAME
  364.   else
  365.    setup_loop_sfs $ONE_FN
  366.    [ "$ONE_PART" != "rootfs" ] && KEEPMOUNTED="${KEEPMOUNTED}${ONE_PART} "
  367.   fi
  368.  else
  369.   setup_loop_sfs $ONE_FN
  370.   [ "$ONE_PART" != "rootfs" ] && KEEPMOUNTED="${KEEPMOUNTED}${ONE_PART} "
  371.  fi
  372.  SFS_MP="/pup_${ONE_SFX}"
  373.  [ "$ONE_SFX" = "p" ] && SFS_MP="/pup_ro2"
  374.  [ -d "$SFS_MP" ] || mkdir $SFS_MP
  375.  mount -r -t squashfs -o noatime $ONE_LOOP $SFS_MP > /dev/console 2>&1
  376.  STATUS=$?
  377.  [ $STATUS -eq 0 ] && ONE_LAYER="$SFS_MP=rr"
  378.  [ "$LOADMSG" ] && check_status $STATUS
  379. }
  380.  
  381. setup_onepupdrv() {
  382. # "$1" - sfs spec - ex: sdb2,ext4,/pup/mydrv-1.2.3.sfs
  383. # "$2" - suffix for "pup_" branch directory - ex: a
  384. # "$3" - prepend after rw layer indicator - ex: p
  385.  ONE_LAYER=""
  386.  [ "$1" ] || return 1
  387.  [ "$2" ] || return 1
  388.  ONE_PART="$(echo "${1}" | cut -f 1 -d ',')"
  389.  [ "$ONE_PART" ] || return 1
  390.  ONE_REL_FN="$(echo "${1}" | cut -f 3 -d ',')"
  391.  [ "$ONE_REL_FN" ] || return 1
  392.  ONE_SFX="${2}"
  393.  ONE_PREP="${3}"
  394.  if [ "$ONE_PART" = "rootfs" ];then #humongous initrd.
  395.   ONE_MP="" #actually it's '/'.
  396.   COPY2RAM='yes' #actually it is already in ram, but load_sfs_file code puts it in a tmpfs.
  397.  else
  398.   if [ "$ONE_PART" = "$P_PART" ];then
  399.    ONE_MP="$P_MP"
  400.   else
  401.    ONE_MP="$(mount | grep -m1 "/dev/$ONE_PART " | cut -f 3 -d ' ')"
  402.    [ "$ONE_MP" ] || return 2
  403.   fi
  404.  fi
  405.  ONE_FN="${ONE_MP}${ONE_REL_FN}"
  406.  ONE_BASENAME="$(basename $ONE_REL_FN)"
  407.  #validate sfs here
  408.  [ -s "$ONE_FN" ] || return 3 #sfs not Ok
  409.  load_sfs_file
  410.  [ "$ONE_LAYER" ] || return 4 #sfs mount failed
  411.  if [ "$ONE_PREP" ];then
  412.   echo "mount -o remount,add:1:$ONE_LAYER /pup_new" #debug
  413.   mount -o remount,add:1:$ONE_LAYER /pup_new
  414.   [ $? -eq 0 ] || return 5
  415.  else
  416.   echo "mount -o remount,append:$ONE_LAYER /pup_new" #debug
  417.   mount -o remount,append:$ONE_LAYER /pup_new
  418.   [ $? -eq 0 ] || return 5
  419.  fi
  420.  NEWUNIONRECORD="${NEWUNIONRECORD}${ONE_BASENAME} "
  421.  return 0
  422. }
  423.  
  424.  
  425. load_ext_file() { #ex: stretchsave.4fs
  426.  SAVE_SZ_FN=""
  427.  RESIZE_FN="$(dirname $SAVE_FN)/pupsaveresizenew.txt"
  428.  #delete following line when resize file location is fixed
  429.  [ -f "$RESIZE_FN" ] || RESIZE_FN="${SAVE_MP}/pupsaveresizenew.txt"
  430.  #-- resize savefile?.. see /usr/sbin/resizepfile.sh
  431.  if [ -f "$RESIZE_FN" ] ; then
  432.    #(1) resize extX file. see below (2)
  433.    . $RESIZE_FN #$PUPSAVEFILEX $KILOBIG
  434.    if [ "$PUPSAVEFILEX" = "$SAVE_REL_FN" ] ; then
  435.     if [ ! -e /bin/resize2fs ];then
  436.      echo -en "\\033[1;31m" > /dev/console
  437.      echo -n "$(printf "${L_CANNOT_RESIZE_PUPSAVE}" "$PUPSAVEFILEX")" > /dev/console #31=red
  438.      echo -en "\\033[0;39m" > /dev/console
  439.      rm -f $RESIZE_FN
  440.     else
  441.      SAVE_SZ_FN="$SAVE_FN"
  442.      rm -f $RESIZE_FN
  443.      echo > /dev/console
  444.      echo -n "$(printf "${L_INCREASING_PUPSAVE}" "$PUPSAVEFILEX" "$KILOBIG")" >/dev/console
  445.      dd if=/dev/zero bs=1024 count=$KILOBIG >> $SAVE_SZ_FN
  446.      sync
  447.     fi
  448.    fi
  449.  fi
  450.  #--
  451.  # is the ${DISTRO_FILE_PREFIX}save encrypted?...
  452.  SFFS=$(blkid "$SAVE_FN" | grep -o ' TYPE=".*' | cut -f 2 -d '"')
  453.  #--
  454.  if [ "$SFFS" = "crypto_LUKS" ] ; then
  455.    ln -sv /pup_new/lib/modules/${KERNELVER} /lib/modules/${KERNELVER}
  456.    modprobe dm-crypt || ERROR_M_DESC="Or maybe the kernel doesn't properly support dm-crypt"
  457.    modprobe xts || ERROR_M_DESC="Or maybe the kernel doesn't properly support dm-crypt"
  458.    while [ 1 ]
  459.    do
  460.      ONE_LOOP="$(losetup -f)"
  461.      #mount encrypted savefile
  462.      echo >/dev/console
  463.      MYPASS=$(dialog --backtitle "$DISTRODESC" --stdout --title "Password required" --passwordbox "$L_PASSWORD_MSG_1 $L_PASSWORD_MSG_2" 0 0 2>/dev/console) ; clear
  464.      losetup $ONE_LOOP $SAVE_FN
  465.      echo -n "$MYPASS" | cryptsetup luksOpen -v $ONE_LOOP savefile -
  466.      if [ $? -ne 0 ] ; then
  467.        cryptsetup luksClose savefile
  468.        losetup -d $ONE_LOOP
  469.        dialog --backtitle "$DISTRODESC" --title "Wrong password" --ok-label "Retry" --yes-label "Retry" \
  470.        --cancel-label "Drop to console" --no-label "Drop to console" \
  471.         --extra-button --extra-label "Reboot" --yesno "You entered a wrong password (most likely). $ERROR_M_DESC"  0 0 &>/dev/console
  472.        case $? in
  473.           0) continue ;;
  474.           3) reboot ;;
  475.           *) exec /bin/sh >/dev/console 2>&1 ;;
  476.         esac
  477.      fi
  478.      ONE_LOOP_PREV=$ONE_LOOP
  479.      ONE_LOOP=/dev/mapper/savefile #hack
  480.      SFFS=ext4
  481.      break
  482.    done
  483.  else # unencrypted savefile
  484.    if echo "$SAVE_FN" | grep -q '_crypt' ; then
  485.      dialog --backtitle "$DISTRODESC" --msgbox "Your savefile name contains: *_crypt*. It's probably an old cryptoloop savefile. No longer supported. Will not use it. However you can open ${PUPSAVE##*/} to see its contents by clicking on it (filemnt)" 0 0 &>/dev/console
  486.      echo 'Warning: old cryptoloop savefile... setting PUPSAVE=""' #debug
  487.      FLAG_NO_TIMEOUT=1
  488.      PUPSAVE=""
  489.      return
  490.    fi
  491.    ONE_LOOP="$(losetup -f)"
  492.    losetup $ONE_LOOP $SAVE_FN
  493.  fi
  494.  #--
  495.  if [ "$PFSCK" = "yes" ] ; then
  496.    echo "" > /dev/console
  497.    fsck_func "$ONE_LOOP" "$SFFS"
  498.  fi
  499.  #--
  500.  if [ "$SAVE_SZ_FN" ] ; then
  501.    #(2) resize extX filesystem - fill extX file. see above (1)
  502.    [ "$PFSCK" != "yes" ] && e2fsck -y "$ONE_LOOP" #debug
  503.    resize2fs -pf "$ONE_LOOP" #no size, will fill all of file.
  504.    echo -n "$(printf "${L_CONTINUING_LOADING}" "$PUPSAVEFILE")" > /dev/console
  505.    sync
  506.  fi
  507.  #--
  508.  [ -d "$SAVE_LAYER" ] || mkdir $SAVE_LAYER
  509.  echo "mount -t $SFFS -o noatime $ONE_LOOP $SAVE_LAYER" #debug
  510.  mount -t $SFFS -o noatime $ONE_LOOP $SAVE_LAYER
  511.  if [ $? -ne 0 ] ; then
  512.    echo 'Warning: setting PUPSAVE=""' #debug
  513.    PUPSAVE=""
  514.  fi
  515. }
  516.  
  517.  
  518. setup_save_file(){
  519.  SAVE_REL_FN="$(echo "$PUPSAVE" | cut -f 3 -d ',')"
  520.  if [ -f "${SAVE_MP}${SAVE_REL_FN}" ];then #savefile
  521.   PUPSAVE_SIZE="$(fx_format_bytes $(stat -c %s "${SAVE_MP}${SAVE_REL_FN}"))"
  522.   echo -n "$(printf "${L_LOADING_PUPSAVE_FILE}" "$SAVE_REL_FN" "$SAVEPART" "$PUPSAVE_SIZE")" > /dev/console
  523.  else
  524.   echo -n "$(printf "${L_LOADING_PUPSAVE}" "$SAVE_REL_FN" "$SAVEPART")" > /dev/console
  525.  fi
  526.  rm -r -f "$SAVE_LAYER"
  527.  SAVE_FN="${SAVE_MP}${SAVE_REL_FN}"
  528.  if [ -f "$SAVE_FN" ];then #savefile. ex: stretchsave.4fs
  529.   echo "--SAVEFILE-- $SAVE_FN" #debug
  530.   load_ext_file
  531.  elif [ -d "$SAVE_FN" ];then #savefolder
  532.   echo "--SAVEFOLDER-- $SAVE_FN" #debug
  533.   ln -sv "$SAVE_FN" "$SAVE_LAYER"
  534.  else
  535.   PUPSAVE=""
  536.  fi
  537.  #[ "$PUPSAVE" ] && decrypt directory at SAVE_LAYER
  538.  if [ "$PUPSAVE" ];then
  539.   SAVE_NAME="$(basename $SAVE_REL_FN)"
  540.   #- fix for empty pupsaves (missing /initrd/DISTRO_SPECS)
  541.   if [ ! -f "$SAVE_LAYER/initrd/DISTRO_SPECS" ] ; then
  542.     touch /tmp/rc_update_force_pm5 # see /etc/rc.d/rc.update
  543.     PCLEAN=""
  544.   fi
  545.   #-
  546.   if [ "$PCLEAN" != "yes" -a -f "$SAVE_LAYER/initrd/DISTRO_SPECS" ];then
  547.     validate_pupsave_upgrade /DISTRO_SPECS $SAVE_LAYER/initrd/DISTRO_SPECS
  548.     case $? in
  549.       0) PCLEAN="yes" ;; #(dialog) perform upgrade
  550.       1) PUPSAVE=""   ;; #(dialog) do not perform upgrade
  551.     esac
  552.   fi
  553.   if [ "$PCLEAN" = "yes" ];then
  554.    #do upgrade processing
  555.    touch /tmp/version_update_flag
  556.    CURDIR="$(pwd)"
  557.    cd "$SAVE_LAYER"
  558.    #delete critical system files
  559.    #keep rc.local - user customisations and/or fixes.
  560.    echo "Removing $(ls ./lib/modules/$KERNELVER/modules.*)"
  561.    rm -f ./lib/modules/$KERNELVER/modules.*
  562.    RC_STUFF="$(ls ./etc/rc.d/rc.* | grep -v "/rc.local$" | tr '\n' ' ')"
  563.    rm -fv $RC_STUFF
  564.    rm -fv ./etc/rc.d/funct*
  565.    #iterate over all files in save
  566.    for ONE_FILE in $(find . \( -type f -or -type l \) -print); do
  567.     ONE_BASE="${ONE_FILE##*/}" #basename
  568.     if [ "${ONE_BASE:0:4}" = ".wh." ]; then #remove most whiteout files
  569.      [ "${ONE_BASE:4:4}" = ".wh." ] && continue #internal aufs files
  570.      [ "${ONE_FILE%/*}" = "./root/.config/autostart" ] && continue #keep autostart changes
  571.      echo "Removing ${ONE_FILE}"
  572.      rm -f "$ONE_FILE" #remove whiteout file
  573.     fi
  574.    done
  575.    cd "$CURDIR"
  576.    sync
  577.   fi
  578.   check_status 0
  579.  else
  580.   check_status 1
  581.  fi
  582.  if [ ! "$PUPSAVE" ] ; then
  583.    echo "No pupsave. Setting PUPMODE 5" #debug
  584.    if ! [ -L $SAVE_LAYER ] ; then # $SAVE_LAYER is a true mp
  585.      echo -e "Unmounting savefile\numount -d $SAVE_LAYER" #debug
  586.      umount -d $SAVE_LAYER
  587.    fi
  588.    PUPMODE=5
  589.    SAVE_MP=""
  590.    SAVE_LAYER=""
  591.  fi
  592. }
  593.  
  594. copy_folders() { #SAVE_LAYER=/pup_ro1
  595.  echo -e "\n-------------------- copy_folders" #debug
  596.  echo "SAVE_MP=$SAVE_MP"
  597.  DESTDIR="/mnt/tmpfs/pup_ro1"
  598.  [ -d "$DESTDIR" ] || mkdir -p $DESTDIR
  599.  rm -r -f "$SAVE_LAYER"
  600.  SAVE_FN="$DESTDIR"
  601.  ln -s $DESTDIR $SAVE_LAYER
  602.  #BKFOLDERS now set in check for pupmode=77
  603.  BKLASTFOLDER="$(echo "${BKFOLDERS}" | head -n 1)"
  604.  #a boot option allows ignore last n sessions, also need to create a badlist...
  605.  if [ "$PIGNORELAST" ];then
  606.   BKBADLIST="$(echo "${BKFOLDERS}" | head -n ${PIGNORELAST})"
  607.   if [ -f ${SAVE_MP}/${BKLASTFOLDER}/.badfolders ];then
  608.    cp ${SAVE_MP}/${BKLASTFOLDER}/.badfolders ${DESTDIR}/
  609.    if [ $? -ne 0 ];then
  610.     #fallback, in case last folder badly corrupted...
  611.     BKPREVFOLDER="$(echo "${BKFOLDERS}" | head -n 2 | tail -n 1)"
  612.     [ -f ${SAVE_MP}/${BKPREVFOLDER}/.badfolders ] && cp ${SAVE_MP}/${BKPREVFOLDER}/.badfolders ${DESTDIR}/
  613.    fi
  614.   fi
  615.   echo "$BKBADLIST" >> ${DESTDIR}/.badfolders
  616.   #note, rc.shutdown and savesession-dvd 'touch' this file so it will get saved.
  617.   sync
  618.  else
  619.   [ -f ${SAVE_MP}/${BKLASTFOLDER}/.badfolders ] && cp ${SAVE_MP}/${BKLASTFOLDER}/.badfolders ${DESTDIR}/
  620.  fi
  621.  [ -f ${DESTDIR}/.badfolders ] && BKBADLIST="$(cat ${DESTDIR}/.badfolders | tr "\n" " ")"
  622.  echo "BKBADLIST=${BKBADLIST}" #debug
  623.  BKFOLDERS="$(echo -n "${BKFOLDERS}" | tr "\n" " ")"
  624.  for ONEFOLDER in ${BKFOLDERS}
  625.  do
  626.   if [ "$(echo -n "$BKBADLIST" | grep "${ONEFOLDER}")" != "" -o -L "${SAVE_MP}/${ONEFOLDER}" ];then
  627.    echo "$(printf "${L_FOLDER_MARKED_BAD}" "${ONEFOLDER}")" >/dev/console
  628.    continue #ignore bad folder.
  629.   fi
  630.   echo "$ONEFOLDER" #debug
  631.   echo -n "$(printf "${L_LOADING_FROM_CD}" "${ONEFOLDER}")" >/dev/console
  632.   #need to be careful not to overfill the ramdisk...
  633.   FREERAMDISK=$(df 2>/dev/null | grep /mnt/tmpfs | head -n 1 | tr -s " " | cut -f 4 -d " ")
  634.   SIZEFOLDER=$(du -k -s ${SAVE_MP}/${ONEFOLDER} | cut -f 1)
  635.   SIZEARCHIVE=0
  636.   if [ -d ${SAVE_MP}/${ONEFOLDER}/archive ];then
  637.    SIZEARCHIVE=$(du -k -s ${SAVE_MP}/${ONEFOLDER}/archive | cut -f 1)
  638.   fi
  639.   SIZESOURCE=$(expr $SIZEFOLDER - $SIZEARCHIVE)
  640.   if [ $FREERAMDISK -gt $SIZESOURCE ];then
  641.    #well, -u will only copy if files newer, so less stuff may get copied than calc'd above.
  642.    #need to copy everything except archive folder...
  643.    [ -d ${SAVE_MP}/${ONEFOLDER}/bin ] && cp -a -u ${SAVE_MP}/${ONEFOLDER}/bin ${DESTDIR}/
  644.    [ -d ${SAVE_MP}/${ONEFOLDER}/sbin ] && cp -a -u ${SAVE_MP}/${ONEFOLDER}/sbin ${DESTDIR}/
  645.    [ -d ${SAVE_MP}/${ONEFOLDER}/etc ] && cp -a -u ${SAVE_MP}/${ONEFOLDER}/etc ${DESTDIR}/
  646.    [ -d ${SAVE_MP}/${ONEFOLDER}/lib ] && cp -a -u ${SAVE_MP}/${ONEFOLDER}/lib ${DESTDIR}/
  647.    [ -d ${SAVE_MP}/${ONEFOLDER}/opt ] && cp -a -u ${SAVE_MP}/${ONEFOLDER}/opt ${DESTDIR}/
  648.    [ -d ${SAVE_MP}/${ONEFOLDER}/var ] && cp -a -u ${SAVE_MP}/${ONEFOLDER}/var ${DESTDIR}/
  649.    [ -d ${SAVE_MP}/${ONEFOLDER}/root ] && cp -a -u ${SAVE_MP}/${ONEFOLDER}/root ${DESTDIR}/
  650.    [ -d ${SAVE_MP}/${ONEFOLDER}/home ] && cp -a -u ${SAVE_MP}/${ONEFOLDER}/home ${DESTDIR}/
  651.    [ -d ${SAVE_MP}/${ONEFOLDER}/usr ] && cp -a -u ${SAVE_MP}/${ONEFOLDER}/usr ${DESTDIR}/
  652.    [ -f ${DESTDIR}/root/.XLOADED ] && rm -fv ${DESTDIR}/root/.XLOADED #toxic
  653.    [ -f ${DESTDIR}/var/local/shared/.XLOADED ] && rm -fv ${DESTDIR}/var/local/shared/.XLOADED #toxic
  654.    cp -a -u ${SAVE_MP}/${ONEFOLDER}/*.sfs ${DESTDIR}/ 2>/dev/null
  655.    #delete deleted files (.wh.filename)... 110212 screen out .wh..wh..opq
  656.    WHITEOUTS="$(find ${DESTDIR} -xdev -type f -name .wh.* | grep -v '__dir_opaque' | grep -v '.wh..wh.')" #110212
  657.    echo "$WHITEOUTS" | while read DELWHITE ; do
  658.     DELFILE="$(echo -n "$DELWHITE" | sed -e 's/\.wh\.//g')"
  659.     if [ -e "$DELFILE" ];then
  660.      [ "$PDEBUG" ] && echo "Deleting $DELFILE" #debug
  661.      rm -rf "$DELFILE"
  662.      rm -rf "$DELWHITE"
  663.     fi
  664.    done
  665.    check_status 0 #display 'done' for each folder loaded.
  666.   else
  667.    printf "${L_RAM_DISK_FULL}\n" "${ONEFOLDER}"
  668.    check_status 1
  669.    break
  670.   fi
  671.  done
  672.  sync
  673.  echo -e "--------------------\n" # debug
  674. }
  675.  
  676. prepend_dir() { #SAVE_LAYER=/pup_ro1
  677.  rm -rf ${SAVE_LAYER}/tmp #in case last boot was pupmode=12
  678.  #adjust stack
  679.  echo "mount -o remount,add:1:${SAVE_LAYER}=ro+wh /pup_new" #debug
  680.  mount -o remount,add:1:${SAVE_LAYER}=ro+wh /pup_new #ro+wh = Readonly branch and it has/might have whiteouts on it
  681.  if [ $? -eq 0 ];then
  682.   [ $PUPMODE -ne 77 ] && KEEPMOUNTED="${KEEPMOUNTED}${SAVEPART} "
  683.   [ "$SAVE_NAME" ] && NEWUNIONRECORD="$SAVE_NAME $NEWUNIONRECORD"
  684.  else
  685.   printf "${L_ADDING_SAVE_LAYER_FAILED}\n" "$SAVE_LAYER"
  686.  fi
  687. }
  688.  
  689. replace_dir() { #SAVE_LAYER=/pup_rw
  690.  #setup empty /tmp on tmpfs for in stack
  691.  rm -rf ${SAVE_LAYER}/tmp
  692.  #adjust stack
  693.  echo "mount -o remount,prepend:${SAVE_LAYER}=rw,mod:/mnt/tmpfs/pup_rw=ro,del:/mnt/tmpfs/pup_rw /pup_new" #debug
  694.  mount -o remount,prepend:${SAVE_LAYER}=rw,mod:/mnt/tmpfs/pup_rw=ro,del:/mnt/tmpfs/pup_rw /pup_new
  695.  if [ $? -eq 0 ];then
  696.   rm -rf /mnt/tmpfs/pup_rw
  697.   KEEPMOUNTED="${KEEPMOUNTED}${SAVEPART} "
  698.   [ "$SAVE_NAME" ] && NEWUNIONRECORD="$SAVE_NAME $NEWUNIONRECORD"
  699.  else
  700.   printf "${L_ADDING_SAVE_LAYER_FAILED}\n" "$SAVE_LAYER"
  701.  fi
  702. }
  703.  
  704. set_fs_linux() {
  705. # "$1" - file system type - ex: ext4
  706.  ONE_FS_IS_LINUX=""
  707.  case ${1} in
  708.   ext2|ext3|ext4|reiserfs|minix|f2fs) ONE_FS_IS_LINUX="yes" ;;
  709.  esac
  710. }
  711.  
  712. find_save_file() {
  713. # "$1" - mount point - ex:  /mnt/pdrv
  714. # "$2" - partition name - ex: sdb2
  715. # "$3" - file system type - ex: ext4
  716.  if [ "$SAVE_BP_DIR" ];then
  717.   SAVE_DIR="$SAVE_BP_DIR"
  718.  else
  719.   SAVE_DIR="${PSUBDIR}/"
  720.  fi
  721.  SAVE_FILES="$(ls -d ${1}${SAVE_DIR}${DISTRO_FILE_PREFIX}save* 2>/dev/null)"
  722.  for ONE_SAVE in $SAVE_FILES;do
  723.   #validate savefiles here
  724.   IS_OK=""
  725.   case $ONE_SAVE in
  726.    *.2fs|*.3fs|*.4fs)
  727.     [ -s "$ONE_SAVE" ] && IS_OK="yes"
  728.    ;;
  729.    *.BKP-*) #written by /usr/sbin/pupsave-backup
  730.     case $ONE_SAVE in
  731.      *.2fs*|*.3fs*|*.4fs*) IS_OK="$PSAVEBKP" ;;
  732.      *) [ "$ONE_FS_IS_LINUX" = "yes" ] && IS_OK="$PSAVEBKP" ;;
  733.     esac
  734.    ;;
  735.    *)
  736.     [ "$ONE_FS_IS_LINUX" = "yes" ] && [ -d "$ONE_SAVE" ] && IS_OK="yes"
  737.    ;;
  738.   esac
  739.   [ "$IS_OK" = "yes" ] && PUP_SAVES="${PUP_SAVES}${2},${3},${SAVE_DIR}$(basename $ONE_SAVE)
  740. "
  741.  done
  742.  PUP_SAVES="$(echo "$PUP_SAVES")" #remove trailing newline char
  743.  echo "MENU_MAX_PUPSAVES=${MENU_MAX_PUPSAVES}" #debug
  744.  if [ "$MENU_MAX_PUPSAVES" ] ; then
  745.    PUP_SAVES="$(echo "$PUP_SAVES" | head -n $MENU_MAX_PUPSAVES)"
  746.  fi
  747. }
  748.  
  749. search_func() {
  750.  [ "$PDEBUG" -a "$LOOK_SAVE" ] && echo "3: filename=${PSUBDIR}/${DISTRO_FILE_PREFIX}save"
  751.  [ "$PDEBUG" -a "$LOOK_PUP" ] && echo "3: PSUBDIR=$PSUBDIR P_BP_FN=$P_BP_FN P_DEF_FN=$P_DEF_FN"
  752.  echo "HAVE_PARTS='${HAVE_PARTS}'" #debug
  753.  if [ "${1}" ];then
  754.   echo "param='${1}'" #debug
  755.   TRY_PARTS_FIRST="$(echo "$HAVE_PARTS" | grep -E "${1}")"
  756.   TRY_PARTS_LAST="$(echo "$HAVE_PARTS" | grep -Ev "${1}")"
  757.   TRY_PARTS="$TRY_PARTS_FIRST
  758. $TRY_PARTS_LAST"
  759.  else
  760.   TRY_PARTS="$HAVE_PARTS"
  761.  fi
  762.  echo "TRY_PARTS='${TRY_PARTS}'" #debug
  763.  [ "$TRY_PARTS" ] || return
  764.  for ONETRY in $TRY_PARTS;do
  765.   ONE_PART="$(echo -n "$ONETRY" | cut -f 1 -d '|')"
  766.   [ "$PDEBUG" ] && echo "4: ONE_PART=$ONE_PART"
  767.   ensure_mounted "$ONE_PART" "/mnt/pdrv"
  768.   if [ "$ONE_MP" ];then
  769.    if [ "$LOOK_SAVE" -a "$SAVEPART" = "" ];then
  770.     set_fs_linux "$ONE_FS"
  771.     find_save_file "$ONE_MP" "$ONE_PART" "$ONE_FS"
  772.     if [ "$PUP_SAVES" ];then
  773.      SAVEPART="$ONE_PART"
  774.      [ "$PDEBUG" ] && echo "5: ONE_PART=$ONE_PART filename=${PSUBDIR}/${DISTRO_FILE_PREFIX}save"
  775.     fi
  776.    fi
  777.    if [ "$LOOK_PUP" ];then
  778.     find_drv_file "$P_BP_FN" "$P_DEF_FN"
  779.     if [ "$ONE_FN" ];then
  780.      PDRV="$ONE_PART,$ONE_FS,$ONE_FN"
  781.      P_MP="$ONE_MP"
  782.      P_PART="$ONE_PART"
  783.      [ "$PDEBUG" ] && echo "5: ONE_PART=$ONE_PART PSUBDIR=$PSUBDIR P_BP_FN=$P_BP_FN P_DEF_FN=$P_DEF_FN"
  784.      break;
  785.     fi
  786.    fi
  787.    umount $ONE_MP
  788.   fi
  789.  done
  790. }
  791.  
  792. get_part_info() {
  793.  probedisk -hr > /tmp/probedisk.log
  794.  probepart -hr > /tmp/probepart.log
  795.  sed 's%|.*%% ; s%.*/%%' /tmp/probedisk.log > /tmp/ALLDRVS0
  796.  HAVE_PARTS="$(cat /tmp/probepart.log | cut -f 1-2 -d '|' | sed -e 's%/dev/%%')"
  797.  [ "$PDEBUG" ] && echo "$HAVE_PARTS" > /tmp/HAVE_PARTS
  798. }
  799.  
  800. wait_for_usb() {
  801.  [ -e /tmp/flag-usb-ready ] && return
  802.  echo -n "${L_WAITING_FOR_USB}" > /dev/console
  803.  /sbin/wait4usb
  804.  get_part_info
  805.  BLKIDOUT=""
  806.  check_status 0
  807. }
  808.  
  809. umount_unneeded() {
  810.  MTD_PARTS="$(mount | cut -f1 -d' ' | grep '^/dev' | grep -v loop | cut -f3 -d'/')"
  811.  for ONE_PART in $MTD_PARTS;do
  812.   [ "$(echo -n "$KEEPMOUNTED" | grep "$ONE_PART")" ] || umount /dev/$ONE_PART
  813.  done
  814. }
  815.  
  816. fatal_error() {
  817. # "$1" - message - ex: puppy_tahr_6.0.5.sfs not found
  818. # "$2" - pre-status-message - ex: Finding puppy
  819.  KEEPMOUNTED=""
  820.  umount_unneeded
  821.  [ "${2}" ] && echo -n "${2}" > /dev/console
  822.  check_status -critical 1 "$1"
  823. }
  824.  
  825. #=============================================================
  826. #                           MAIN
  827. #=============================================================
  828.  
  829. clear #clear the screen.
  830.  
  831. echo -en "\\033[0;34m***\\033[0;37m ${DISTRO_NAME} ${DISTRO_VERSION}"
  832. echo -en "\\033[0;34m -\\033[0;37m Linux ${KERNELVER} "
  833. echo -en "\\033[0;31m[\\033[0;37m`uname -m`\\033[0;31m]"
  834. echo -e "\\033[0;34m ***\\033[0;39m"
  835.  
  836. [ ! "$LOGLEVEL" ] && exec 1>/tmp/bootinit.log 2>&1 #remove o/p from console. v2.22 loglevel added.
  837.  
  838. [ ! -f /bin/resize2fs ] && touch /tmp/no_resize2fs
  839.  
  840. # sets PLANG, PKEYS, VFAT_OUT_PARAM, FONTMAP, KMAP, CODEPAGE
  841. [ -f /sbin/set_plang ] && . /sbin/set_plang
  842.  
  843. #pmedia= usbflash|usbhd|usbcd|ataflash|atahd|atacd|atazip|scsihd|scsicd|cd
  844. [ $pmedia ] && PMEDIA=$pmedia #boot parameter, broad category of boot media. ex: cd.
  845. [ $psubdir ] && PSUBDIR=$psubdir #boot parameter, directory for puppy files. ex: puppy220
  846. [ $psavemark ] && PSAVEMARK=$psavemark #100913 partition number that has/will-have save-file.
  847.  
  848. [ $PSUBDIR ] && [ "${PSUBDIR:0:1}" != "/" ] && PSUBDIR="/${PSUBDIR}" #add leading /.
  849.  
  850. [ $pdev1 ] && PDRV=$pdev1 #boot parameter, partition have booted off. ex: hda3
  851. #100915 requested by technosaurus (formats get changed further down)...
  852. [ $pdrv ] && PDRV=$pdrv #format partition:<path><filename> ex: sda2:/slacko/puppy_slacko_6.3.0.sfs
  853. [ $pupsfs ] && PDRV=$pupsfs
  854. [ $zdrv ] && ZDRV=$zdrv #ex: sda2:/slacko/zdrv_slacko_6.3.0.sfs
  855. [ $fdrv ] && FDRV=$fdrv
  856. [ $adrv ] && ADRV=$adrv
  857. [ $ydrv ] && YDRV=$ydrv
  858. #<partition>:<filename>, for savefile/savefolder. <partition> can be a name or Label or UUID
  859. [ $psave ] && PSAVE=$psave #ex: sdb4:/puppy/tahr/tahrsave or smark or 49baa82d-8c69:tahrsave
  860. #list of kernel modules to load, ex: pimod=hid-logitech-dj.ko,kernel/drivers/hid/hid-multitouch.ko
  861. [ $pimod ] && PIMOD=$pimod
  862. #specify partition for Underdog Linux (refer also underdog.lnx).
  863. [ $underdog ] && UNDERDOG=$underdog
  864. #[ $pdebug ] && PDEBUG=$pdebug
  865. PDEBUG=1
  866. TOTAL_SIZEK_SFS_RAM=0
  867.  
  868. # show menu with pupsaves
  869. [ $psavemenu ] && PSAVEMENU=$psavemenu
  870.  
  871. # Booting from external USB disks may not find files on time. Wait early.
  872. [ "${PMEDIA:0:3}" = "usb" ] && wait_for_usb
  873.  
  874. RDSH=""
  875. if [ "$pfix" ];then
  876.  for ONEFIX in $(echo -n "$pfix" | tr ',' ' ')
  877.  do
  878.   case $ONEFIX in
  879.    ram)     PRAMONLY="yes";;      #run in ram only (do not load ${DISTRO_FILE_PREFIX}save).
  880.    rdsh)    RDSH="yes";;          #exit to shell in initial ramdisk.
  881.    xorgwizard) PXORGWIZARD="yes";;#force xorgwizard for this session
  882.    nox)     PNOX="yes";;          #do not start X.
  883.    clean)   PCLEAN="yes";;        #force version upgrade and cleanup.
  884.    trim)    PTRIM="yes";;         #add "discard" to mount options if SSD
  885.    copy)    PCOPY="yes";;         #copy .sfs files into ram.
  886.    nocopy)  PNOCOPY="yes";;        #do not copy .sfs files into ram (default is copy if enough ram).
  887.    fsck)    PFSCK="yes";;         #do a fsck of ${DISTRO_FILE_PREFIX}save file.
  888.    fsckp)   PFSCKP="yes";;        #do fsck before first mount of ext partitions
  889.    [0-9]*)  PIGNORELAST=$ONEFIX;; #blacklist last $ONEFIX folders (multisession).
  890.    *)       echo "pfix=$ONEFIX is not a known boot parameter";;
  891.   esac
  892.  done
  893. fi
  894.  
  895. [ "$PRAMONLY" != "yes" ] && [ -f /BOOT_SPECS ] && . /BOOT_SPECS
  896.  
  897. if [ "$PSAVEMENU" ] ; then
  898.  #MENU_MAX_PUPSAVES=number
  899.  MENU_MAX_PUPSAVES=$(echo "$PSAVEMENU" | grep -o '[0-9]' | tr '\n' ' ' | sed 's| ||g')
  900. fi
  901.  
  902. [ "$TZ" ] && export TZ
  903. hwclock -l -s
  904.  
  905. [ "$PDEBUG" ] && echo "0: PMEDIA=$PMEDIA PDRV=$PDRV PSUBDIR=$PSUBDIR pfix=$pfix"
  906.  
  907. #100915 technosaurus recommendation (see above)...
  908. [ "$PDRV" ] && { decode_spec "$PDRV"; P_BP_ID="$ONE_BP_ID"; P_BP_FN="$ONE_BP_FN"; PDRV=""; }
  909. [ "$ZDRV" ] && { decode_spec "$ZDRV"; Z_BP_ID="$ONE_BP_ID"; Z_BP_FN="$ONE_BP_FN"; ZDRV=""; }
  910. [ "$FDRV" ] && { decode_spec "$FDRV"; F_BP_ID="$ONE_BP_ID"; F_BP_FN="$ONE_BP_FN"; FDRV=""; }   
  911. [ "$ADRV" ] && { decode_spec "$ADRV"; A_BP_ID="$ONE_BP_ID"; A_BP_FN="$ONE_BP_FN"; ADRV=""; }
  912. [ "$YDRV" ] && { decode_spec "$YDRV"; Y_BP_ID="$ONE_BP_ID"; Y_BP_FN="$ONE_BP_FN"; YDRV=""; }
  913. [ "$PSAVE" ] && { decode_spec "$PSAVE"; SAVE_BP_ID="$ONE_BP_ID"; SAVE_BP_FN="$ONE_BP_FN"; }
  914. SAVE_BP_DIR=""
  915. [ "$SAVE_BP_FN" ] && [ "${SAVE_BP_FN:$((${#SAVE_BP_FN} - 1))}" = "/" ] && SAVE_BP_DIR="$SAVE_BP_FN" #last char
  916.  
  917. #first look inside initrd...
  918. #unset x_DEF_FN so that find_onepupdrv() won't look for it
  919. [ -f /${P_DEF_FN} ] && { PDRV="rootfs,rootfs,/$P_DEF_FN"; P_DEF_FN=""; }
  920. [ -f /${Z_DEF_FN} ] && { ZDRV="rootfs,rootfs,/$Z_DEF_FN"; Z_DEF_FN=""; }
  921. [ -f /${F_DEF_FN} ] && { FDRV="rootfs,rootfs,/$F_DEF_FN"; F_DEF_FN=""; }
  922. [ -f /${A_DEF_FN} ] && { ADRV="rootfs,rootfs,/$A_DEF_FN"; A_DEF_FN=""; }
  923. [ -f /${Y_DEF_FN} ] && { YDRV="rootfs,rootfs,/$Y_DEF_FN"; Y_DEF_FN=""; }
  924.  
  925. #-----------------------------
  926. # see if actually a non-huge kernel is being used..
  927. not_a_huge_kernel_stuff && check_status 0
  928. #-----------------------------
  929.  
  930. get_part_info
  931. grep -v '^sr' /tmp/ALLDRVS0 > /tmp/ATADRIVES0
  932. ATAOPTICALDRIVES="$(grep '^sr' /tmp/ALLDRVS0 | tr '\n' ' ')"
  933.  
  934. PUP_SAVES=""
  935. KEEPMOUNTED=""
  936. FSCKDPARTS=""
  937. BLKIDOUT=""
  938. LO_OPT="-r"
  939.  
  940. [ "$PDEBUG" ] && echo "1: PDRV=$PDRV P_BP_ID=$P_BP_ID P_BP_FN=$P_BP_FN"
  941.  
  942. #establish PDRV
  943. P_PART=""; LOOK_PUP=""; LOOK_SAVE=""
  944. if [ "$P_BP_ID" ];then #specified as parameter
  945.  decode_id "$P_BP_ID"
  946.  [ "$ONE_PART" ] && { P_PART="$ONE_PART"; P_BP_ID=""; }
  947.  if [ "$P_PART"  = "" ];then
  948.   wait_for_usb
  949.   decode_id "$P_BP_ID"
  950.   [ "$ONE_PART" ] && { P_PART="$ONE_PART"; P_BP_ID=""; }
  951.  fi
  952.  find_onepupdrv "$P_PART" "$P_BP_FN" "$P_DEF_FN" "p"
  953.  [ "$ONE_FN" ] && { PDRV="$ONE_PART,$ONE_FS,$ONE_FN"; P_MP="$ONE_MP"; }
  954.  [ "$PDEBUG" ] && echo "2: ONE_PART=$ONE_PART ONE_FN=$ONE_FN ONE_MP=$ONE_MP"
  955. elif [ "$PDRV" = "" ];then #not specified anywhere
  956.  #determine what to search for
  957.  [ "$PMEDIA" = "cd" ] && LOOK_SAVE="yes"
  958.  [ "$SAVE_BP_ID" -o "$PSAVEMARK" ] && LOOK_SAVE=""
  959.  LOOK_PUP=yes
  960.  [ "$PDEBUG" ] && echo "2: LOOK_PUP=$LOOK_PUP LOOK_SAVE=$LOOK_SAVE PMEDIA=$PMEDIA"
  961. fi
  962. if [ "$LOOK_PUP" -o "$LOOK_SAVE" ];then #something to search for
  963.  [ "${PMEDIA:0:3}" != "usb" ] && search_func
  964.  if [ "$P_PART" = "" ];then
  965.   wait_for_usb
  966.   USBDRVS="$(find /sys/block -maxdepth 1 -name 'sd*' -o -name 'sr*' | xargs -n 1 readlink 2>/dev/null | grep '/usb[0-9]' | rev | cut -f 1 -d '/' | rev | tr '\n' '|')"
  967.   [ "$PDEBUG" ] && echo "2: USBDRVS=$USBDRVS -> ${USBDRVS%|}"
  968.   if [ "$USBDRVS" ] ; then
  969.     search_func "${USBDRVS%|}"
  970.   else
  971.     search_func
  972.   fi
  973.  fi
  974. fi
  975. [ "$P_BP_ID" ] && { log_part_id "$P_BP_ID"; ONE_PART="$P_BP_ID"; }
  976. [ "$PDEBUG" ] && echo "6: ONE_PART=$ONE_PART ONE_TRY_FN=$ONE_TRY_FN PDRV=$PDRV"
  977. if [ "$PDRV" = "" ];then
  978.  [ "$ONE_TRY_FN" ] || ONE_TRY_FN="$PSUBDIR/$P_DEF_FN"
  979.  fatal_error "$ONE_PART $ONE_TRY_FN not found." "Finding puppy main sfs file."
  980. fi
  981.  
  982. #establish SAVEPART
  983. decode_other_ids
  984. if [ "$Z_BP_ID" -o "$F_BP_ID" -o "$A_BP_ID" -o "$Y_BP_ID" -o "$SAVE_BP_ID" ];then
  985.  wait_for_usb
  986.  decode_other_ids
  987. fi
  988. if [ "$P_PART" ];then
  989.  BOOTDRV="$(echo -n "$P_PART" | grep -o -f /tmp/ALLDRVS0)" #110205 ex: sda1 becomes sda.
  990.  if [ "$SAVEPART" = "" ];then #setup SAVEPART
  991.   [ "$P_MP" ] || P_MP="$(mount | grep -m1 "/dev/$P_PART " | cut -f 3 -d ' ')"
  992.   [ -f ${P_MP}${PSUBDIR}/SAVEMARK ] && PSAVEMARK="$(cat ${P_MP}${PSUBDIR}/SAVEMARK)" #partition no. that has or will-have ${DISTRO_FILE_PREFIX}save.2fs. 101020
  993.   if [ "$PSAVEMARK" ];then
  994.    SAVEPART="${BOOTDRV}${PSAVEMARK}" #note, PSAVEMARK could also be a kernel boot param. ex: 2
  995.    if [ "$(echo $HAVE_PARTS | grep "$SAVEPART")" = "" ];then #partition not exist
  996.     SAVEPART=''
  997.     PSAVEMARK=''
  998.    fi
  999.   fi
  1000.   [ "$SAVEPART" ] || SAVEPART="$P_PART"
  1001.  fi
  1002. fi
  1003.  
  1004. #have pup...sfs, now try to load it
  1005. RAMSIZE=$(free | grep -o 'Mem: .*' | tr -s ' ' | cut -f 2 -d ' ') #total physical ram (less shared video). 110405
  1006.  
  1007. mount -t tmpfs tmpfs /mnt/tmpfs
  1008. [ -d "/mnt/tmpfs/pup_rw" ] || mkdir /mnt/tmpfs/pup_rw
  1009. mount -t aufs -o udba=reval,diropq=w,br=/mnt/tmpfs/pup_rw=rw,xino=/mnt/tmpfs/.aufs.xino unionfs /pup_new
  1010. [ $? -eq 0 ] || fatal_error "${L_ERROR_FAILED_AUFS_STACK}"
  1011.  
  1012. NEWUNIONRECORD=""
  1013. COPY2RAM=""
  1014.  
  1015. COPYMSG="${L_COPY_MESSAGE}" #purple
  1016. LOADMSG="${L_LOAD_MESSAGE}"
  1017. setup_onepupdrv "$PDRV" "p"
  1018. STATUS=$?
  1019. if [ $STATUS -gt 0 ];then
  1020.  P_PRE_MSG="${L_PRE_MESSAGE}"
  1021.  case $STATUS in
  1022.   1) fatal_error "$(printf "${L_ERR_PDRV_INCOMPLETE}" "$PDRV")" "$P_PRE_MSG" ;;
  1023.   2) fatal_error "$(printf "${L_ERR_ONEPART_NOT_MOUNTED}" "$ONE_PART")" "$P_PRE_MSG" ;;
  1024.   3) fatal_error "$(printf "${L_ERR_ONEPART_NOT_OK}" "$ONE_PART" "$ONE_REL_FN")" "$P_PRE_MSG" ;;
  1025.   4) fatal_error "$(printf "${L_ERR_ONEPART_MOUNT_SFS_FAILED}" "$ONE_PART" "$ONE_REL_FN")" "$P_PRE_MSG" ;;
  1026.   5) fatal_error "$(printf "${L_ERR_AUFS_SFS_FAILED}" "$ONE_LAYER")" "$P_PRE_MSG" ;;
  1027.  esac
  1028. fi
  1029. PUP_LAYER="$SFS_MP"
  1030.  
  1031. #all partition id's should be decoded by now
  1032. [ "$Z_BP_ID" ] && log_part_id "$Z_BP_ID"
  1033. [ "$F_BP_ID" ] && log_part_id "$F_BP_ID"
  1034. [ "$A_BP_ID" ] && log_part_id "$A_BP_ID"
  1035. [ "$Y_BP_ID" ] && log_part_id "$Y_BP_ID"
  1036. [ "$SAVE_BP_ID" ] && log_part_id "$SAVE_BP_ID"
  1037.  
  1038. #have basic system, now try to add optional stuff
  1039. find_onepupdrv "$F_PART" "$F_BP_FN" "$F_DEF_FN" "f"
  1040. [ "$ONE_FN" ] && FDRV="$ONE_PART,$ONE_FS,$ONE_FN"
  1041. [ "$FDRV" ] && { LOADMSG="fdrv"; setup_onepupdrv "$FDRV" "f"; }
  1042.  
  1043. find_onepupdrv "$Z_PART" "$Z_BP_FN" "$Z_DEF_FN" "z"
  1044. [ "$ONE_FN" ] && ZDRV="$ONE_PART,$ONE_FS,$ONE_FN"
  1045. [ "$ZDRV" ] && { LOADMSG="zdrv"; setup_onepupdrv "$ZDRV" "z"; }
  1046.  
  1047. find_onepupdrv "$Y_PART" "$Y_BP_FN" "$Y_DEF_FN" "y"
  1048. [ "$ONE_FN" ] && YDRV="$ONE_PART,$ONE_FS,$ONE_FN"
  1049. [ "$YDRV" ] && { LOADMSG="ydrv"; setup_onepupdrv "$YDRV" "y" "p"; }
  1050.  
  1051. find_onepupdrv "$A_PART" "$A_BP_FN" "$A_DEF_FN" "a"
  1052. [ "$ONE_FN" ] && ADRV="$ONE_PART,$ONE_FS,$ONE_FN"
  1053. [ "$ADRV" ] && { LOADMSG="adrv"; setup_onepupdrv "$ADRV" "a" "p"; }
  1054.  
  1055. #ensure that save partition is mounted
  1056. #if SAVEPART is not the same as P_PART
  1057. #moved up here to ensure config files are available
  1058. [ "$SAVEPART" ] && ensure_save_mounted
  1059.  
  1060. #bring back Underdog Linux...
  1061. if [ ! "$UNDERDOG" ] ; then
  1062.   [ -s "${SAVE_MP}${PSUBDIR}/underdog.lnx" ] && UNDERDOG="`cat ${SAVE_MP}${PSUBDIR}/underdog.lnx`"
  1063. fi
  1064. [ "$UNDERDOG" ] && { decode_id "$UNDERDOG"; UNDERDOG="$ONE_PART"; }
  1065. if [ "$UNDERDOG" ];then #boot parameter
  1066.  ensure_mounted "$UNDERDOG" "/pup_ud"
  1067.  if [ "$ONE_MP" ];then
  1068.   mount -o remount,append:${ONE_MP}=rr /pup_new
  1069.   #if [ $? -eq 0 ];then
  1070.    #do any necessary underdog fiddles
  1071.   #fi
  1072.  fi
  1073. fi
  1074.  
  1075. #support initmodules.txt
  1076. if [ "$PIMOD" = "" ]; then
  1077.   PIMOD_DIR=""
  1078.   case $SAVE_BP_FN in
  1079.     */*) PIMOD_DIR="${SAVE_BP_FN%/*}" ;;
  1080.     *) PIMOD_DIR="${PSUBDIR}" ;;
  1081.   esac
  1082.   PIMOD_FN="${SAVE_MP}${PIMOD_DIR}/${DISTRO_FILE_PREFIX}initmodules.txt"
  1083.   if [ -s "$PIMOD_FN" ]; then
  1084.    PIMOD="$(cat "$PIMOD_FN")"
  1085.    echo "Using $PIMOD_FN"
  1086.   fi
  1087. fi
  1088. #modprobe modules needed by keyboard
  1089. if [ "$PIMOD" != "" ]; then
  1090.  [ -L "/lib/modules/$KERNELVER" ] || ln -sv /pup_new/lib/modules/$KERNELVER /lib/modules/$KERNELVER
  1091.  [ -L "/lib/firmware" ] || ln -sv /pup_new/lib/firmware /lib/firmware
  1092.  for ONEMOD in `echo -n "$PIMOD" | tr ',' ' '`;do
  1093.   echo -n "$(printf "${L_ADDING_MODULE}" "$ONEMOD")" > /dev/console
  1094.   modprobe "$ONEMOD" > /dev/console 2>&1
  1095.   check_status $?
  1096.  done
  1097. fi
  1098.  
  1099. #find PUPSAVE, sortout pupmode
  1100. PUPMODE=5
  1101.  
  1102. #debug
  1103. echo --------------------
  1104. echo "SAVE_MP=${SAVE_MP}"
  1105. echo "PRAMONLY=${PRAMONLY}"
  1106. echo "BOOTDRV=${BOOTDRV}"
  1107. mount
  1108. echo --------------------
  1109.  
  1110. PUPSAVE=""
  1111. if [ "${SAVE_MP}" != "" -a "$PRAMONLY" != "yes" ];then #have mounted save? partition
  1112.  #check if save partition is linux
  1113.  set_fs_linux "$SAVE_FS"
  1114.  #check for cd multisession
  1115.  if [ "$SAVE_FS" = "iso9660" ];then #booting on optical, is it multisession?...
  1116.   BKFOLDERS="$(find $SAVE_MP -maxdepth 1 -xdev -type d -name '20[0-9][0-9]-[0-9][0-9]-[0-9][0-9]-[0-9][0-9]-[0-9][0-9]' | sed -e s%^${SAVE_MP}/%% | sort -r)"
  1117.   if [ "$BKFOLDERS" ];then #multisession cd/dvd
  1118.    FND_MULTIFOLDER="/$(echo "$BKFOLDERS" | head -n 1)"
  1119.    if [ "$FND_MULTIFOLDER" ];then #multisession cd/dvd
  1120.     PUPMODE=77
  1121.     PUPSAVE="${SAVEPART},${SAVE_FS},${FND_MULTIFOLDER}"
  1122.    fi
  1123.   fi
  1124.  fi
  1125.  echo "FND_MULTIFOLDER=${FND_MULTIFOLDER}" #debug
  1126.  #check for save to partition
  1127.  if [ "$ONE_FS_IS_LINUX" = "yes" -a "$PSUBDIR" = "" -a -f "${SAVE_MP}/etc/rc.d/PUPSTATE" ];then
  1128.   # make sure it's not a full install
  1129.   if ! grep -q 'PUPMODE=2' ${SAVE_MP}/etc/rc.d/PUPSTATE ; then
  1130.     # PUPMODE=6 PUPMODE=7 - unsupported
  1131.     if [ "$ONE_FS_IS_LINUX" = "yes" ] ; then #savefolder is a full partition
  1132.       PUPSAVE="$SAVEPART,$SAVE_FS,"
  1133.       PUPMODE=12
  1134.       ONE_PART="$(echo -n "$PUPSAVE" | cut -f 1 -d ',')"
  1135.       [ "$ONE_PART" != "$SAVEPART" ] && { SAVEPART="$ONE_PART"; ensure_save_mounted; }
  1136.     fi
  1137.   fi
  1138.  fi
  1139.  #not sorted yet, may be pupmode=12
  1140.  if [ $PUPMODE -eq 5 ];then
  1141.   #check boot spec
  1142.   if [ "$SAVE_BP_FN" -a "$SAVE_BP_DIR" = "" ];then
  1143.    if [ -d "${SAVE_MP}${SAVE_BP_FN}" ];then
  1144.     [ "$ONE_FS_IS_LINUX" = "yes" ] && PUPSAVE="$SAVEPART,$SAVE_FS,$SAVE_BP_FN"
  1145.    else
  1146.     [ -s "${SAVE_MP}${SAVE_BP_FN}" ] && PUPSAVE="$SAVEPART,$SAVE_FS,$SAVE_BP_FN"
  1147.    fi
  1148.    [ "$PUPSAVE" ] || echo "$SAVEPART, $SAVE_BP_FN file not found."
  1149.   else
  1150.    #have to find savefile/savefolder
  1151.    [ "$PUP_SAVES" ] || find_save_file "${SAVE_MP}" "$SAVEPART" "$SAVE_FS"
  1152.    echo "PUP_SAVES=${PUP_SAVES}" #debug
  1153.    if [ "$PUP_SAVES" ];then
  1154.     NUMPUP_SAVES=0
  1155.     for ONE_SAVE in $PUP_SAVES;do
  1156.      NUMPUP_SAVES=$(expr $NUMPUP_SAVES + 1)
  1157.     done
  1158.     echo "NUMPUP_SAVES=${NUMPUP_SAVES}" #debug
  1159.     if [ $NUMPUP_SAVES -eq 1 ];then
  1160.      PUPSAVE="$(echo -n $PUP_SAVES)"
  1161.     elif [ $NUMPUP_SAVES -gt 1 ];then
  1162.      CNTSAVE=1
  1163.      echo -e "\\033[1;36m" >/dev/console #36=aquablue
  1164.      echo "${L_ERR_TYPE_PUPSAVE}" > /dev/console
  1165.      echo "${L_0_NONE}" > /dev/console
  1166.      for ONETRY in $PUP_SAVES
  1167.      do
  1168.       ONE_PART="$(echo -n "$ONETRY" | cut -f 1 -d ',')"
  1169.       ONE_FILE="$(echo -n "$ONETRY" | cut -f 3 -d ',')"
  1170.       echo -e "${CNTSAVE}  ${ONE_PART}\\033[10G${ONE_FILE}" > /dev/console #10 means move to that column.
  1171.       CNTSAVE=$(expr $CNTSAVE + 1)
  1172.      done
  1173.      echo -en "\\033[0;39m" >/dev/console
  1174.      read NUMSAVE
  1175.      #--  $NUMSAVE -ne 0 = have selected a PUPSAVE ...
  1176.      if [ $NUMSAVE -ne 0 ] ; then
  1177.       echo -e "\n----" #debug
  1178.       echo "Have selected pupsave $NUMSAVE" #debug
  1179.       PUPSAVE="$(echo -n $PUP_SAVES | cut -f $NUMSAVE -d ' ')"
  1180.       PSPATH="${SAVE_MP}/$(echo "$PUPSAVE" | cut -f 3 -d ',')"
  1181.       echo "PSPATH=${PSPATH}" #debug
  1182.       case "$PSPATH" in *.tar.[gx]z) #compressed archive
  1183.        PSPATH_NOTGZ=${PSPATH%.tar.[gx]z} #remove trailing 'tar.[gx]z'
  1184.        echo "PSPATH_NOTGZ=${PSPATH_NOTGZ}" #debug
  1185.        case "$PSPATH" in
  1186.          *".2fs"*|*".3fs"*|*".4fs"*) savefile="yes" ;;
  1187.          *) savefolder="yes" ;;
  1188.        esac
  1189.        case "$PSPATH" in
  1190.          *.tar.gz) TAROPT="-z" ;;
  1191.          *.tar.xz) TAROPT="-J" ;;
  1192.        esac
  1193.        echo "savefile=${savefile}"     #debug
  1194.        echo "savefolder=${savefolder}" #debug
  1195.        cdir=$(pwd)
  1196.        #--
  1197.        if [ "$savefolder" = "yes" ] ; then
  1198.          mkdir -p "$PSPATH_NOTGZ"
  1199.          cd "$PSPATH_NOTGZ"
  1200.          echo "Extracting ${PSPATH##*/}..." >/dev/console #basename
  1201.          tar -x $TAROPT -f "$PSPATH" && TAROK=yes
  1202.          itsinside=$(ls -d ${DISTRO_FILE_PREFIX}save* | head -n 1)
  1203.          if [ "$itsinside" ] ; then
  1204.            cd "$cdir"
  1205.            mv -fv "$PSPATH_NOTGZ" "$PSPATH_NOTGZ".2
  1206.            sync
  1207.            mv -fv "$PSPATH_NOTGZ".2/${itsinside} "$PSPATH_NOTGZ"
  1208.            sync
  1209.            rm -rfv "$PSPATH_NOTGZ".2
  1210.          fi
  1211.        else #savefile
  1212.          cd "${PSPATH%/*}" #dirname $PSPATH
  1213.          echo "Extracting ${PSPATH##*/}..." >/dev/console #basename $PSPATH
  1214.          tar -x $TAROPT -f "$PSPATH" && TAROK=yes
  1215.        fi
  1216.        sync
  1217.        #--
  1218.        echo "TAROK=${TAROK}" #debug
  1219.        if [ "$TAROK" = "yes" ] ; then
  1220.          PUPSAVE=${PUPSAVE%.tar.[gx]z} #remove trailing 'tar.[gx]z'
  1221.          rm -fv "$PSPATH"
  1222.        else
  1223.          PUPSAVE=""
  1224.          echo "Error extracting ${PSPATH##*/}"
  1225.          echo -e "\\033[1;31mError extracting ${PSPATH##*/}\\033[0;39m" >/dev/console #31=red
  1226.          [ "$savefolder" = "yes" ] && rm -rf "$PSPATH_NOTGZ"
  1227.          [ "$savefile" = "yes" ] && rm -fv "$PSPATH_NOTGZ"
  1228.        fi
  1229.        cd $cdir
  1230.        echo -e "----\n" #debug
  1231.        ;;
  1232.       esac
  1233.      fi
  1234.      #----------------------------------------------------
  1235.     fi
  1236.    fi
  1237.   fi
  1238.   echo "PUPSAVE=${PUPSAVE}" #debug
  1239.   if [ "$PUPSAVE" ];then
  1240.    echo "Setting PUPMODE 12" #debug
  1241.    PUPMODE=12
  1242.    ONE_PART="$(echo -n "$PUPSAVE" | cut -f 1 -d ',')"
  1243.    [ "$ONE_PART" != "$SAVEPART" ] && { SAVEPART="$ONE_PART"; ensure_save_mounted; }
  1244.   fi
  1245.  fi
  1246. fi
  1247.  
  1248. SAVE_LAYER=""
  1249. if [ "$PUPSAVE" ];then #refine pupmode
  1250.  # refine pupmode
  1251.  if [ $PUPMODE -eq 12 ];then
  1252.   SAVE_LAYER="/pup_rw"
  1253.   if ! [ "$PMEDIA" ] ; then
  1254.    [ "`cat /sys/block/$(fx_get_drvname $SAVEPART)/removable`" = "1" ] && xPM=13
  1255.   fi
  1256.   if [ "${PMEDIA:3}" = "flash" -o "$xPM" = "13" ];then
  1257.    PUPMODE=$(($PUPMODE + 1)) #PUPMODE=13
  1258.    echo "Setting PUPMODE $PUPMODE" #debug
  1259.    SAVE_LAYER="/pup_ro1"
  1260.   fi
  1261.   echo "SAVE_LAYER=$SAVE_LAYER" #debug
  1262.  elif [ $PUPMODE -eq 77 ];then
  1263.   SAVE_LAYER="/pup_ro1"
  1264.  fi
  1265. fi
  1266.  
  1267. #load savefile if required, then setup stack
  1268. case $PUPMODE in
  1269.  77) #load folders
  1270.   copy_folders #to /pup_ro1 (part of the tmpfs)
  1271.   prepend_dir  #prepend ro1
  1272.  ;;
  1273.  13) #prepend ro1
  1274.   setup_save_file
  1275.   [ "$PUPSAVE" ] && prepend_dir
  1276.  ;;
  1277.  12) #replace rw
  1278.   setup_save_file
  1279.   [ "$PUPSAVE" ] && replace_dir
  1280.  ;;
  1281.  5)
  1282.   SAVE_MP=""
  1283.  ;;
  1284. esac
  1285.  
  1286. umount_unneeded
  1287.  
  1288. echo -n $(($TOTAL_SIZEK_SFS_RAM - 5)) > /tmp/sfs_ram_sizek
  1289.  
  1290. [ "$DEV1FS" ] || DEV1FS="$(echo "$PDRV" | cut -f 2 -d ',')"
  1291. [ "$ATADRIVES" ] || ATADRIVES="$(cat /tmp/ATADRIVES0 | tr '\n' ' ')"
  1292.  
  1293. mkdir -p /pup_new/etc/rc.d
  1294. ( # > /pup_new/etc/rc.d/PUPSTATE
  1295. echo "PUPMODE=$PUPMODE"
  1296. echo "PDEV1='$P_PART'"
  1297. echo "DEV1FS='$DEV1FS'"
  1298. echo "PUPSFS='$PDRV'"
  1299. echo "PUPSAVE='$PUPSAVE'"
  1300. echo "PMEDIA='$PMEDIA'"
  1301. echo '#ATADRIVES is all internal ide/pata/sata drives, excluding optical, excluding usb...'
  1302. echo "ATADRIVES='$ATADRIVES'"
  1303. echo '#ATAOPTICALDRIVES is list of non-usb optical drives...'
  1304. echo "ATAOPTICALDRIVES='$ATAOPTICALDRIVES'"
  1305. echo '#these directories are unionfs/aufs layers in /initrd...'
  1306. echo "RW_LAYER='/pup_rw'"
  1307. echo "SAVE_LAYER='$SAVE_LAYER'"
  1308. echo "PUP_LAYER='$PUP_LAYER'"
  1309. echo "#The partition that has the ${DISTRO_FILE_PREFIX}save file is mounted here..."
  1310. echo "PUP_HOME='${SAVE_MP}'"
  1311. echo '#(in /initrd) ...note, /mnt/home is a link to it.'
  1312. echo '#this file has extra kernel drivers and firmware...'
  1313. echo "ZDRV='$ZDRV'"
  1314. echo "FDRV='$FDRV'"
  1315. echo "ADRV='$ADRV'"
  1316. echo "YDRV='$YDRV'"
  1317. echo '#Partition no. override on boot drive to which session is (or will be) saved...'
  1318. echo "PSAVEMARK='$PSAVEMARK'"
  1319. echo "PSAVEPART='$PSAVEPART'"
  1320. echo "PSAVEDIR='$SAVE_BP_DIR'"
  1321. echo "PSUBDIR='$PSUBDIR'"
  1322. echo "PUNIONFS='aufs'"
  1323. echo "DOIMODS='yes'"
  1324. echo "DOMIBS='no'"
  1325. [ -f /sbin/set_plang ] && plang_pupstate #echo
  1326. [ "$UNDERDOG" ] && echo "UNDERDOG='$UNDERDOG'"
  1327. ) > /pup_new/etc/rc.d/PUPSTATE
  1328.  
  1329. if [ "$PUPSAVE" ];then
  1330.  xBOOTCONFIG="$(grep -v '^PREVUNIONRECORD' /pup_new/etc/rc.d/BOOTCONFIG | sed -e 's/^LASTUNIONRECORD/PREVUNIONRECORD/')"
  1331.  echo "$xBOOTCONFIG" > /pup_new/etc/rc.d/BOOTCONFIG
  1332.  echo "LASTUNIONRECORD='$(echo -n $NEWUNIONRECORD)'" >> /pup_new/etc/rc.d/BOOTCONFIG
  1333. else
  1334.  echo "" > /pup_new/etc/rc.d/BOOTCONFIG
  1335.  echo "LASTUNIONRECORD=''" >> /pup_new/etc/rc.d/BOOTCONFIG
  1336. fi
  1337.  
  1338. echo -n "${L_SWITCH_ROOT}" > /dev/console
  1339.  
  1340. echo "Moving mountpoints to /pup_new/initrd for after switch..." #debug
  1341. rm -r -f "/pup_new/initrd${SAVE_LAYER}"
  1342. for ONEMNT in $(mount | cut -f 3 -d ' ' | grep -v 'pup_new' | grep '^/pup_' | tr '\n' ' ');do
  1343.  mkdir -p /pup_new/initrd${ONEMNT}
  1344.  echo "mount -o move $ONEMNT /pup_new/initrd${ONEMNT}" #debug
  1345.  mount -o move $ONEMNT /pup_new/initrd${ONEMNT}
  1346. done
  1347. for ONEMNT in $(mount | cut -f 3 -d ' ' | grep '^/mnt/' | tr '\n' ' ');do
  1348.  mkdir -p /pup_new/initrd${ONEMNT}
  1349.  echo "mount -o move $ONEMNT /pup_new/initrd${ONEMNT}" #debug
  1350.  mount -o move $ONEMNT /pup_new/initrd${ONEMNT}
  1351. done
  1352.  
  1353. echo -------------------- # debug
  1354. mount
  1355. echo -------------------- # debug
  1356.  
  1357. echo "SAVE_FN=$SAVE_FN"         #debug
  1358. echo "SAVE_LAYER=${SAVE_LAYER}" #debug
  1359.  
  1360. # --- create/fix symlinks to /pup_new/initrd/* for after switch
  1361.  
  1362. # remove symbolic links for after switch, that might be leftover
  1363. [ -L /pup_new/initrd/pup_rw ] && rm -rf /pup_new/initrd/pup_rw
  1364. [ -L /pup_new/initrd/pup_ro1 ] && rm -rf /pup_new/initrd/pup_ro1
  1365. [ -L /pup_new/tmp ] && rm -fv /pup_new/tmp
  1366.  
  1367. # SAVE_LAYER: /pup_rw or /pup_ro1
  1368. # savefolder: create symlink to the save layer mountpoint in /pup_new/initrd
  1369. # (for savefiles and savepartitions the SAVE_LAYER is not a symlink)
  1370. if [ "$PUPSAVE" ];then
  1371.  if [ "$SAVE_LAYER" -a -L "$SAVE_LAYER" ];then
  1372.   ln -sv "/initrd${SAVE_FN}" "/pup_new/initrd${SAVE_LAYER}" #for after switch
  1373.  fi
  1374. fi
  1375.  
  1376. # symlinks to /pup_new/initrd/tmpfs subdirs
  1377. # * /pup_rw:  pupmodes 5,13,77)
  1378. # * /pup_ro1: in PUPMODE 77 pup_ro1 is part of the tmpfs but an aufs read-only branch
  1379. #             where the previous session is stored to build the final filesystem
  1380. # * (rm -rf ... is an extra measure to ensure deletion
  1381. #     since an actual directory might be there and therefore was not deleted.)
  1382. if ! grep -q '/pup_new/initrd/pup_rw' /proc/mounts ; then
  1383.   if [ -d /pup_new/initrd/mnt/tmpfs/pup_rw ] ; then
  1384.     rm -rf /pup_new/initrd/pup_rw
  1385.     ln -sv /initrd/mnt/tmpfs/pup_rw /pup_new/initrd/pup_rw #for after switch
  1386.   fi
  1387. fi
  1388. if [ -d /pup_new/initrd/mnt/tmpfs/pup_ro1 ] ; then
  1389.   rm -rf /pup_new/initrd/pup_ro1
  1390.   ln -sv /initrd/mnt/tmpfs/pup_ro1 /pup_new/initrd/pup_ro1 #for after switch
  1391. fi
  1392. mkdir -p /pup_new/initrd/mnt/tmpfs/tmp
  1393. if [ -d /pup_new/initrd/mnt/tmpfs/tmp ];then #extra paranoid precaution
  1394.  chmod 1777 /pup_new/initrd/mnt/tmpfs/tmpb, switch
  1395. #
  1396.  rm -rf /pup_new/tmp
  1397.  ln -sv /initrd/mnt/tmpfs/tmp /pup_new/tmp #for after switch
  1398. fi
  1399.  
  1400. #---
  1401.  
  1402. [ "$PXORGWIZARD" = "yes" ] && touch /pup_new/initrd/mnt/tmpfs/tmp/xwin_xorgwizard_cli
  1403. #PNOX is a boot param. /etc/profile prevents X from starting if this file exists...
  1404. [ "$PNOX" = "yes" ] && touch /pup_new/initrd/mnt/tmpfs/tmp/bootcnt.txt
  1405.  
  1406. cp -a /DISTRO_SPECS /pup_new/initrd/
  1407. cp /init* /README.txt /pup_new/initrd/
  1408. chmod -x /pup_new/initrd/init
  1409. dmesg > /tmp/dmesg.txt
  1410.  
  1411. [ -d "/pup_new/initrd/tmp" ] && rm -rf /pup_new/initrd/tmp
  1412. mkdir -p /pup_new/initrd/tmp
  1413. cp -af /tmp/* /pup_new/initrd/tmp/ #keep any log files.
  1414.  
  1415. [ -f /sbin/set_plang ] && plang_copy_to_newroot #$LANG, /etc/keymap|fontmap|codepage
  1416.  
  1417. if [ "$RDSH" = "yes" ];then
  1418.  echo > /dev/console
  1419.  echo "${L_DROPPED_TO_INITRD_SHELL}" > /dev/console
  1420.  exec /bin/sh >/dev/console 2>&1
  1421. fi
  1422.  
  1423. #--
  1424. TMPFS=/initrd/files
  1425. mkdir -p /pup_new${TMPFS}
  1426. mount -t tmpfs tmpfs -o mode=0755 /pup_new${TMPFS}
  1427. #cp -a bin /pup_new${TMPFS} 2>/dev/null
  1428. cp -a bin sbin etc dev lib locale usr var /pup_new${TMPFS} 2>/dev/null
  1429. sync
  1430. size=$(du -c /pup_new${TMPFS} | tail -n 1 | cut -f 1)
  1431. size=$(($size + 512))
  1432. mount -o remount,ro,size=${size}k /pup_new${TMPFS}
  1433. #--
  1434.  
  1435. sync
  1436. [ -d "/proc/bus/usb" ] && umount /proc/bus/usb
  1437. umount /sys
  1438. umount /dev
  1439. umount /proc
  1440.  
  1441. #now using cpio archive for initramfs 'initial ramdisk'...
  1442. exec switch_root /pup_new /sbin/init
  1443.  
  1444. ###END###
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement