SHARE
TWEET

/initrd/init s243a's mods to tahrpup6.0.6

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