Advertisement
s243a

mk_adrv.sh

Feb 8th, 2020
489
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 15.60 KB | None | 0 0
  1. #!/bin/bash
  2. #WKGMNTPT=/mnt/sb
  3. set -x
  4. WKGMNTPT=/mnt/home
  5. ISO_BLD_FLDR=puppylivecdbuild
  6. INST_PKG_PREFIX=${INST_PKG_PREFIX:-woof}
  7. PUP_LAYER_MNT="" #e.g. /initrd${PUP_LAYER}
  8. MKZDRV=${MKZDRV:-yes} #Setting this to yes will kep kernal modules out of the iso
  9. CLEARPKGLOG=${CLEARPKGLOG:-yes}
  10. [ "`whoami`" != "root" ] && exec sudo -A ${0} ${@} #110505
  11.  
  12. #NOTE: rename to avoid clash with 'remasterpup2.mo' used by previous i18n method.
  13. export TEXTDOMAIN=remasterpup2x
  14. export OUTPUT_CHARSET=UTF-8
  15. . gettext.sh
  16.  
  17.  
  18. #LANG=C #faster, plus Xdialog happier.
  19. KERNELVER="`uname -r`"
  20.  
  21. ##variables created at bootup by /initrd/usr/sbin/init...
  22. #. /etc/rc.d/PUPSTATE
  23. [ "$PUP_LAYER" = "" ] && PUP_LAYER="/pup_ro2"
  24.  
  25. #. /etc/DISTRO_SPECS
  26. #PUPPYSFS="$DISTRO_PUPPYSFS" #ex: puppy.sfs
  27. #PUPSFS_ROOT="${PUPPYSFS%.sfs}"
  28. #ZDRVSFS="$DISTRO_ZDRVSFS"   #ex: zdrv.sfs
  29. #SFSBASE="`basename $PUPPYSFS .sfs`" #ex: puppy
  30.  
  31.  
  32. PPATTERN="/initrd${PUP_LAYER}"
  33.  
  34. SAVEPART="$PDEV1" #from PUPSTATE.
  35. CDR="/dev/$SAVEPART"
  36.  
  37. #XPID=$!
  38. if [ 1 -eq 0 ]; then
  39.     SIZEOPT=0
  40.     SIZEBIN=`du -sk /bin | cut -f 1`
  41.     SIZESBIN=`du -sk /sbin | cut -f 1`
  42.     SIZELIB=`du -sk /lib | cut -f 1`
  43.     SIZEUSR=`du -sk /usr | cut -f 1`
  44.     SIZEOPT=0   # 01jul09
  45.     [ -d /opt ] && SIZEOPT=`du -sk /opt | cut -f 1`
  46.     sync
  47.     SIZETOTALK=`LANG=C dc $SIZEBIN $SIZESBIN + $SIZELIB + $SIZEUSR + $SIZEOPT + p`
  48.     SIZETOTALM=`LANG=C dc $SIZETOTALK 1024 \/ p| cut -d'.' -f1` # 01jul09
  49.     [ "$SIZETOTALM" ] || SIZETOTALM=1
  50.     #estimate a compressed size...
  51.     SIZENEEDEDM=`expr $SIZETOTALM \/ 3`
  52.     SIZESLACKM=`expr $SIZENEEDEDM \/ 3` #guess
  53.     SIZENEEDEDM=`expr $SIZENEEDEDM + $SIZESLACKM`
  54.     SIZENEEDEDM=`expr $SIZENEEDEDM + 25` #space for vmlinuz, initrd.gz, zdrv, etc
  55. fi 
  56. #now choose working partition... v431 add ext4...  130216 add f2fs...
  57. #PARTSLIST="`probepart -m 2> /dev/null | grep '^/dev/' | grep -E 'f2fs|ext2|ext3|ext4|reiserfs|btrfs|minix|msdos|vfat|exfat|ntfs' | cut -f 1-3 -d '|'`"
  58.  
  59. #add tmpfs ramdisk choice...
  60.  
  61. Yes_lbl="$(gettext 'Yes')"
  62. No_lbl="$(gettext 'No')"
  63. m_01="$(gettext 'Puppy simple CD remaster')" #window title.
  64. m_02="$(gettext 'ERROR')"
  65. m_07="$(gettext 'currently mounted')"
  66. m_08="$(gettext 'not mounted')"
  67. m_09="$(gettext 'Filesystem')" #130302
  68. m_10="$(gettext 'Size')" #130302
  69. m_11="$(gettext 'Free')" #130302
  70.  
  71.     #create new puppy.sfs file...
  72.     squash() {
  73.         echo $0 $@
  74.         rxvt -bg orange -fg black -title "$m_01" -geometry 80x6 -e mksquashfs $@ 2> /dev/null
  75.     }
  76.     fk_squash() {
  77.         echo $0 $@
  78.         #s243a: use set -x to echo cpio commands in fk_mksquashfs and pass to rxvt
  79.         #rxvt -bg orange -fg black -title "$m_01" -geometry 80x6 -e mksquashfs $@ 2> /dev/null
  80.         fk_mksquashfs $@ #Short term hack
  81.     }
  82.     do_squash(){
  83.       if [ "$mode" = dir ]; then
  84.         fk_squash $@    
  85.       else
  86.         squash $@
  87.       fi
  88.     }  
  89.     fk_mksquashfs(){
  90.       #source_dir=$1; shift
  91.       #target_dir=$2; shift
  92.       option=""
  93.       out=() #Currently not used
  94.       args=()
  95.       declare -A exludes
  96.       for arg in "$@";do
  97.         if [[ "$arg" == -* ]]; then
  98.           case "$arg" in
  99.           -*)
  100.           option="$arg" ;;
  101.           esac  
  102.         else
  103.           case option in
  104.           -e)
  105.             exludes+=( ["$arg"]=1 ) ;;
  106.           '')
  107.             args+=( "$arg" ) ;;
  108.           *)
  109.             out+=( "$arg" ) ;;
  110.           esac
  111.         fi
  112.       done
  113.       n_args=${#args}
  114.       target_dir=$args[$n_args]
  115.       target_dir="${target_dir%.sfs}"
  116.      
  117.       mkdir -p "$target_dir"
  118.       unset 'args[$n_args-1]' #https://stackoverflow.com/questions/8247433/remove-the-last-element-from-an-array
  119.       #while read aDir; do
  120.       if [ realpath "$target_dir" != "/" ]; then
  121.         for aDir in "$args[@]"; do
  122.           excluded="${excludes[$aDir]}"
  123.           [ -z "$excluded" ] && excluded=0
  124.           if [ ! $excluded -eq 1 ]; then
  125.             cd $aDir
  126.             cpio -pd "$target_dir"
  127.           fi
  128.         done
  129.       fi
  130.       #done < <(ls -a -1)
  131.      
  132.     }  
  133.     do_mksquashfs(){
  134.       if [ "$mode" = dir ]; then
  135.         fk_mksquashfs $@    
  136.       else
  137.         mksquashfs $@
  138.       fi
  139.     }  
  140. #Use plugin_fns to over_ride functions 
  141.  
  142. #for a_root in /mnt/home/sb/fakeroot   
  143. function print_str_or_file(){
  144.     if [ "$1" = "-f" ]; then
  145.        cat "$2";
  146.   elif [ "$1" = "-s" ]; then
  147.        echo "$2";
  148.   elif [ "$1" = "-p" ]; then
  149.     while read -r line; do
  150.       echo "$line"
  151.     done
  152.   fi;  
  153. }
  154. #TODO, source this when processing options.
  155. #source "$plugin_fns"  
  156.  
  157. #Over ride this function to process more complex file inputs.
  158. function get_a_root(){
  159.    a_root=$(echo "$1" | cut -d ' ' -f1)
  160.     [ -z "$a_root" ] && a_root=/mnt/home/sb/fakeroot
  161.    PUPPYSFS=$(echo "$1" | cut -d ' ' -f2)  
  162.    [ -z "$PUPPYSFS" ] && PUPPYSFS=puppy.sfs
  163.    INST_PKG_PREFIX_maybe=$(echo "$1" | cut -d ' ' -f3)
  164.    [ ! -z "$INST_PKG_PREFIX_maybe" ] && INST_PKG_PREFIX="$INST_PKG_PREFIX_maybe"  
  165. }
  166. function mk_layer_sfs(){
  167.     #Feed print_str_or_file "$@" into the loop below
  168.   #for a_root in /mnt/home/sb/fakeroot
  169.   while read -r a_root_line; do
  170.     DIRHOME=""
  171.     echo "a_root_line=$a_root_line"
  172.     #We need to exclude the following directories
  173.     DIRHOME=''; DIRSYS=''; DIRLOST=''
  174.     [ -d "${a_root}/home" ] && DIRHOME="${a_root}/home"
  175.     [ -d "${a_root}/sys" ] && DIRSYS="${a_root}/sys"
  176.     [ -d "${a_root}/lost+found" ] && DIRLOST="${a_root}/lost+found"
  177.     PUPSFS='' #Override get_pupsfs_name to set pupsfs_name
  178.    
  179.     #type get_pupsfs_name && PUPPYSFS="$(get_pupsfs_name)"
  180.    
  181.     #Set a_root
  182.     get_a_root "$a_root_line"
  183.  
  184.     rm -f ${WKGMNTPT}/${ISO_BLD_FLDR}/$PUPPYSFS 2> /dev/null
  185.     sync
  186.     #note, /puppy.sfs is not normally there, i relocated it to a separate tmpfs,
  187.     #however have not yet done that for multisession-cd/dvd (PUPMODE=77).
  188.     #note, /home could be in underdog linux...
  189.  
  190.     # modules copied from initrd
  191.     ANOTHER_REMOVE=("${a_root}/lib/modules/$KERNELVER/initrd" "${a_root}/lib/modules/*/modules.*")  # 28dec09 modules.*
  192.     if [ "$MKZDRV" = "yes" ]; then
  193.       rm -f $WKGMNTPT/${ISO_BLD_FLDR}/$ZDRVSFS 2> /dev/null
  194.       do_mksquashfs /lib $WKGMNTPT/${ISO_BLD_FLDR}/$ZDRVSFS -keep-as-directory -e /lib/[^m]* $ANOTHER_REMOVE
  195.       ANOTHER_REMOVE=("${a_root}/lib/modules")
  196.     fi
  197.     #120605 Omit certain /dev subdir content and modem components loaded from firmware tarballs...
  198.     [ -d "${a_root}/dev/snd" ] && [ "$(ls "${a_root}/dev/snd")" != "" ] && DIRDEVSNDFILES="${a_root}/dev/snd/*" #120721
  199.     [ -d "${a_root}/dev/.udev" ] && DIRDEVUDEV="${a_root}/dev/.udev"
  200.     #121021 modem daemons now left in place.
  201.     [ -f "${a_root}/usr/share/icons/hicolor/icon-theme.cache" ] && ICONCACHE="${a_root}/usr/share/icons/hicolor/icon-theme.cache" #120721
  202.     TOPPLCDB=''
  203.     [ -e "${a_root}/${ISO_BLD_FLDR}" ] && TOPPLCDB="${a_root}/${ISO_BLD_FLDR}"
  204.     TOPPUPSFS=''
  205.     [ -e "${a_root}/${PUPPYSFS}" ] && TOPPUPSFS="${a_root}/${PUPPYSFS}"
  206.    
  207.  
  208.  
  209.     # display terminal only for the first stage because it takes the most of time. 'squash' is a function, see above. 120512 $COMP added...
  210.     do_squash ${a_root} $WKGMNTPT/${ISO_BLD_FLDR}/$PUPPYSFS ${COMP} -e "${a_root}/media" "${a_root}/proc" "${a_root}/initrd" \
  211.       "${a_root}/var" "${a_root}/tmp" "${a_root}/archive" "${a_root}/mnt" "${a_root}/root" "$TOPPLCDB" ${ANOTHER_REMOVE[@]} \
  212.       "$DIRHOME" "$DIRSYS" "$DIRLOST" "$TOPPUPSFS" "$DIRDEVSNDFILES" \
  213.        "$DIRDEVUDEV" "$ICONCACHE"  #120605 end #120721 avoid wildecards option, icon-theme.cache 121021
  214.     sync
  215.  
  216.     #add pristine folders (out of current puppy.sfs)...
  217.     #do_mksquashfs "${PUP_LAYER_MNT}"/home $WKGMNTPT/${ISO_BLD_FLDR}/$PUPPYSFS -keep-as-directory
  218.     sync
  219.     if [ ! -z "${PUP_LAYER_MNT}" ]; then
  220.       do_mksquashfs "${PUP_LAYER_MNT}/proc" $WKGMNTPT/${ISO_BLD_FLDR}/$PUPPYSFS -keep-as-directory
  221.       sync
  222.       do_mksquashfs "${PUP_LAYER_MNT}/tmp" $WKGMNTPT/${ISO_BLD_FLDR}/$PUPPYSFS -keep-as-directory
  223.       sync
  224.       do_mksquashfs "${PUP_LAYER_MNT}/mnt" $WKGMNTPT/${ISO_BLD_FLDR}/$PUPPYSFS -keep-as-directory
  225.       sync
  226.       do_mksquashfs "${PUP_LAYER_MNT}"/media $WKGMNTPT/${ISO_BLD_FLDR}/$PUPPYSFS -keep-as-directory
  227.       sync
  228.       kill $XPID
  229.     fi
  230.    
  231.    
  232.  
  233.     #######START WORKING ON /root#######
  234.     rm -rf /tmp/root 2> /dev/null
  235.     cp -arfv --remove-destination "${a_root}"/root* /tmp/root
  236.     rm "/tmp/root/.bashrc"
  237.     rm "/tmp/root/recently-used.xbel"
  238.     rm /tmp"${PKGS_DIR}"/*-installed-packages
  239.     #
  240.  
  241.     if [ -d "${a_root}/var/packages" ]; then
  242.       PKGS_DIR=/var/packages
  243.     elif [ -d "${a_root}/root/.packages" ]; then
  244.       PKGS_DIR=/root/.packages
  245.     else
  246.       PKGS_DIR="$(realpath -m "${a_root}"/root/.pacakges)" #THis doesn't work right : TODO fix
  247.       echo sed "$PKGS_DIR" | sed 's#^'"${a_root}"'##' #TODO make this more robust
  248.       [-z "$PKGS_DIR" ] && PKGS_DIR=/var/packages
  249.     fi
  250.    
  251.     if [ -e "${a_root}$PKGS_DIR/package-files" ]; then
  252.       PKG_FILES_DIR="$PKGS_DIR/package-files"
  253.     else
  254.       PKG_FILES_DIR="$PKGS_DIR"
  255.     fi
  256.     BUILTIN_FILES_DIR="$PKGS_DIR/builtin_files"
  257.  
  258.  
  259.     [ -f /tmp/root/.XLOADED ] && rm -f /tmp/root/.XLOADED #130527
  260.       #######END WORKING ON /root (Part #1)#######
  261.  
  262.  
  263.     #######START WORKING ON /etc#######
  264.     rm -rf /tmp/etc 2> /dev/null
  265.     cp -arfv --remove-destination "${a_root}"/etc/* /tmp/etc
  266.     #rm /tmp/etc/resolve.conf
  267.     echo "# nameservers go in here" > /tmp/etc/resolve.conf
  268.     #do some work on /etc before add it to the .sfs...
  269.  
  270.  
  271.  
  272.     #######START WORKING ON /var#######
  273.     rm -rf /tmp/var 2> /dev/null
  274.     [ ! -z "${PUP_LAYER_MNT}" ] && cp -a ${PUP_LAYER_MNT}/var /tmp/var #pristine var
  275.      
  276.     #.packages/ .files, copy any files installed to /var...
  277.     echo -n "" > /tmp/allpkgs.files
  278.     #for ONEPKG in `ls -1 "$PKG_FILES_DIR/"*.files 2>/dev/null | tr "\n" " "`
  279.     #do
  280.     #   grep '^/var/' $ONEPKG | \
  281.  
  282.     [ ! -d /tmp/var/packages ] && mkdir -p /tmp/var/packages #This is probably already done
  283.  
  284.     [ -d "${a_root}/var/packages" ] && cp -arf --remove-destination "${a_root}/var/packages/"* /tmp/var/packages
  285.     rm /tmp"${PKGS_DIR}"/*-installed-packages
  286.     sync
  287.     [ "$CLEARPKGLOG" = "yes" ] && rm -f /tmp/var/log/packages/* #120607
  288.  
  289.       #######END WORKING ON /var (Part #1)#######
  290.  
  291.     ####### COPY PACKAGE METADATA #######
  292.      
  293.     # 141008: move *.files to ~/.packages/builtin_files/
  294.  
  295.       #touch "${a_root}"/root/.packages/user-installed-packages
  296.      
  297.       mkdir -p /tmp${PKGS_DIR}/builtin_files
  298.      
  299.       #cat /root/.packages/user-installed-packages | \
  300.      
  301.       while read -r ONEPKG
  302.       do
  303.       ONEFILE="/tmp$PKG_FILES_DIR/`echo "$ONEPKG" | cut -f1 -d '|'`.files"
  304.       ONENAME="/tmp${PKGS_DIR}/builtin_files/`echo "$ONEPKG" | cut -f2 -d '|'`"
  305.       [ -f "$ONEFILE" ] && mv -f "$ONEFILE" "$ONENAME"
  306.       [ -f "$ONENAME" ] && echo "$ONEPKG" >> /tmp${PKGS_DIR}/${INST_PKG_PREFIX}-installed-packages
  307.       done < <(cat "${a_root}${PKGS_DIR}"/*-installed-packages)
  308.       #TODO, maybe look up the meta info of any package that wasn't moved
  309.      
  310.       #cat "${a_root}${PKGS_DIR}"/*-installed-packages >> /tmp${PKGS_DIR}/${INST_PKG_PREFIX}-installed-packages
  311.       sort -u --key=1 --field-separator="|" /tmp${PKGS_DIR}/${INST_PKG_PREFIX}-installed-packages > /tmp/${INST_PKG_PREFIX}-installed-packages-tmp #110722
  312.       mv -f /tmp/${INST_PKG_PREFIX}-installed-packages-tmp /tmp${PKGS_DIR}/${INST_PKG_PREFIX}-installed-packages
  313.       echo -n "" > /tmp${PKGS_DIR}/user-installed-packages #v431     
  314.  
  315.  
  316.     sync #120607
  317.     rm -f /tmp${PKG_FILES_DIR}/*.files #120607
  318.     rm -f /tmp${PKG_FILES_DIR}/*.remove #120607
  319.    
  320.     ####### END PACKAGE METADATA #######
  321.    
  322.     #######Start WORKING ON /root (Part #2) #######
  323.       m_19="$(eval_gettext 'This program has created folder /tmp/root, which has everything that is now going to be added as /root in the ${PUPPYSFS} file.')
  324.    $(gettext "This is mostly 'pristine', as obviously you do not want all your cache files, temp files, email files, and other working/temporary files to be the ISO. However, if you are familiar with the workings of Puppy, you might like to take a look at /tmp/root right now, and possibly add anything that you want from /root (or remove something!)")
  325.     $(gettext '(if you think that this program has missed out something important, please let us know..')
  326.    
  327.     $(eval_gettext "After examining /tmp/root, click 'Ok' to add /root in \${PUPPYSFS} file...")"
  328.    Xdialog --wrap --left --title "$m_01" --msgbox "$m_19" 0 80
  329.    sync
  330.    [ "`ls /tmp/root/.packages/*.files`" = "" ] && CLEARPKGLOG="yes" || CLEARPKGLOG="no" #120607 in case user copied entire /root to /tmp for boot disk.
  331.    do_mksquashfs /tmp/root $WKGMNTPT/${ISO_BLD_FLDR}/$PUPPYSFS -keep-as-directory
  332.    sync
  333.    rm -rf /tmp/root
  334.     #######END WORKING ON /root (Part #2) #######  
  335.      #######Start WORKING ON /etc (Part #2) #######
  336.    m_23="${MSG1}
  337.  
  338.     $(gettext 'If you know what you are doing, you can now modify any files in /tmp/etc folder. This is just about to be added to /etc in the .sfs file.')
  339.     $(gettext "Do anything you want before clicking 'Ok'.")
  340.     $(gettext '(If this program has missed something important, let me know -- Barry Kauler)')
  341.    
  342.     $(eval_gettext "Click 'Ok' to add /etc in \${PUPPYSFS} file...")"
  343.    Xdialog --wrap --left  --title "$m_01" --msgbox "$m_23" 0 80
  344.  
  345.    #120606 in case user just now replaced the /etc directory...
  346.    sync
  347.    #130527 .XLOADED moved to /root (see /usr/bin/xwin), change test...
  348.    MODIFETC="$(find /tmp/etc/modules -mindepth 1 -maxdepth 1 -name 'firmware.dep.inst.*')"
  349.    if [ "$MODIFETC" != "" ];then
  350.      #rm -f /tmp/etc/.XLOADED
  351.      rm -f /tmp/etc/modules/firmware.dep.inst.*
  352.      touch /tmp/etc/personal_settings_popup_disabled
  353.      touch /tmp/etc/personal_data_save_disabled
  354.    fi
  355.    [ -f /tmp/etc/.XLOADED ] && rm -f /tmp/etc/.XLOADED #130527 just in case old file still there.
  356.  
  357.    sync
  358.  
  359.    do_mksquashfs /tmp/etc $WKGMNTPT/${ISO_BLD_FLDR}/$PUPPYSFS -keep-as-directory
  360.    sync
  361.    rm -rf /tmp/etc
  362.     #######END WORKING ON /etc (Part #2) #######   
  363.      
  364.      #######Start WORKING ON /var (Part #2) #######
  365.  
  366.    sync
  367.    do_mksquashfs /tmp/var $WKGMNTPT/${ISO_BLD_FLDR}/$PUPPYSFS -keep-as-directory
  368.    sync
  369.    rm -rf /tmp/var
  370.    
  371.    #s243a: TODO: add prompt to modify the var folder like was done in the etc folder
  372.    
  373.    #######END WORKING ON /var#######
  374.    
  375.    #chmod a+r $WKGMNTPT/${ISO_BLD_FLDR}/* &>/dev/null
  376.    #chmod a-x $WKGMNTPT/${ISO_BLD_FLDR}/*.sfs &>/dev/null
  377.  
  378.    #fi ###### end of long skip if, cleating new sfs
  379.    
  380.    #=================================================================
  381.  
  382. m_25="$(gettehttp://www.murga-linux.com/puppy/index.phpxt 'Almost ready to create the new ISO file!')
  383.  
  384. $(gettext "If you want to add any more files, say extra SFS files, or to edit or modify the files in any way, do it now. Note, if you add an extra SFS file, say 'devx.sfs' then it will be available for use when you boot the new live-CD.")
  385.  
  386. $(eval_gettext "If you want to make any changes, use ROX-Filer to open \${WKGMNTPT}/${ISO_BLD_FLDR}/ and do so now, before clicking the 'OK' button.")"
  387.    Xdialog --wrap --left  --title "$m_01" --msgbox "$m_25" 0 80
  388.    sync
  389.  done < <(print_str_or_file "$@")
  390. }
  391. declare -a options="$(getopt -o f:,s:,p: --long input-file:,input-string:,pipe-input,plugin-fns: -- "$@")"
  392. eval set -- "$options"
  393. while [ $# -gt 0 ]; do
  394.  case "$1" in
  395.  -f|--input-file)
  396.    mk_layer_sfs -f "$2"
  397.    shift 2; ;;      
  398.  -s|--input-string)
  399.    mk_layer_sfs -s "$2"
  400.    shift 2; ;;  
  401.  -p|--pipe-input)
  402.    mk_layer_sfs -p
  403.    shift 1; ;;
  404.  --plugin-fns)
  405.    source $2
  406.    shift 2; ;;
  407.  --)
  408.    shift 1
  409.    options2+=( "$@" )
  410.    break; ;;
  411.  *)
  412.     options2+=( "$1" )
  413.     shift 1; ;;
  414.  esac
  415. done
  416. for item_source in "${LAYER_SOURCES[@]}"; do
  417.    case "$item_source" in
  418.    input-file)
  419.    mount_items "$INPUT_FILE"
  420.  items+="$(get_items -f "$INPUT_FILE")"; ;;
  421.  esac
  422. done
  423.  
  424. ### END ###
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement