Iso - remaster script trying to start chroot run commands then exit but host system gets messed up

The script works and creates a modified iso fine until I added the chrootbeg and chrootend functions and executed them. I'm sorry if I did something wrong this is my first post. I uploaded entire bash script for reference or in case you want to run it to debug it is called isoremast.txt.

 Please let me know if you need more information and I'll be happy to provide it. Thank you.

Summary of script

USAGE: isoremast (no options) - Will DL iso and uncompress image
isoramst m - Will modify the iso files by adding, deleting, etc
possibly by chrooting into the system
isoremast c - Will recreate the iso file with your modifications

Script was written to be run 'isoremast m' mult times in a row
and change the script to easily add or delete files without having
to recreate the iso each time - useful for rapid testing
Also several other proj do something similar but use a myraid of scripts
and programs to do so. Want all functionality need in 1 script this
is my goal along with simplicity so my 8 year old can run it
without having to type a myriad of commands into the terminal.

Setup - Running golang webserver on Win 7 host running Debian 10 as a VM guest running the script

# Begin chroot 
chrootbeg() {
  echo "chrootbeg - beg funct ..."
  sudo mount --bind /dev/ unsquash/dev
  sudo chroot unsquash

  mount -t proc none /proc/
  mount -t sysfs none /sys/
  mount -t devpts none /dev/pts
  export HOME=/root
  export LC_ALL=C
  dbus-uuidgen > /var/lib/dbus/machine-id
  dpkg-divert --local --rename --add /sbin/initctl   //not sure what this does...
  ln -s /bin/true /sbin/initctl
}

# End chroot
chrootend(){
  echo "chrootend - beg funct ..."
  ap_clean
  rm -rf /tmp/* ~/.bash_history
  rm /var/lib/dbus/machine-id
  rm /etc/resolv.conf
  rm /sbin/initctl
  dpkg-divert --rename --remove /sbin/initctl
  #### Clean older/non-used kernels...
  dpkg -l 'linux-*' | sed '/^ii/!d;/'"$(uname -r | sed "s/\(.*\)-\([^0-9]\+\)/\1/")"'/d;s/^[^ ]* [^ ]* \([^ ]*\).*/\1/;/[0-9]/!d' | xargs sudo apt-get -y purge
  umount /proc || umount -lf /proc
  umount /sys || umount -lf /sys
  umount /dev/pts  || umount -lf /dev/pts
  #HELP - Uncomment or comment out lines below?
  #HELP - will exit exit function early and not execute unmount command below?
  # Move 2 lines outside of function to be executed after function
  #exit
  #sudo umount unsquash/dev || umount -lf unsquash/dev
  #HELP - END
}


#HELP - after this script ends run with 'm' option -
  # my system is messed up. Not sure if never chrooted correct or 
  # never exited correctly. I have a 30 GB VB image i have backed up
  # that  I have to restore from every time after this script runs
  # when trying to run all code inbetween the chrootbeg and chrootend
  # functions. Can some expert please give me something specific to try
  # I think my chroot code is flawed but not sure why. I can't even run 
  # the sudo command on main system after this script run after chroot.
  echo "Chrooting into the system ..."
  chrootbeg
  fn="/etc/apt/sources.list"
  cat $fn | grep "non-free" 2>/dev/null >/dev/null
  if [ "$?" != "0" ]; then
    if [ ! -f $fn.org ]; then
      cp $fn $fn.org
    fi
    echo "Modifying file '"$fn"' adding contrib and non-free repos ..."
    sed -i 's/main/main contrib non-free/g' $fn
    cp $fn $fn.mod
    echo; ls -la $fn*; echo
  fi
  echo "apt-get update ..."
  apt-get update
  echo "Deleting libreoffice ..."
  apt-get remove --purge libreoffice-*
  echo "Deleting all non English language packs ..." 
  apt-get remove --purge `dpkg-query -W --showformat='${Package}\n' | grep language-pack | egrep -v '\-en'`
  echo "apt-get upgrade ..."
  apt-get upgrade
  echo "Installing various packages that I need ..."
  apt install curl wget apt-transport-https dirmngr
  echo "Chrooting the system  is ending ..."
  chrootend 
  echo "WARN WARN WARN - Make sure line below is executed because occurs after an exit ..."
  echo "sudo umount unsquash/dev || umount -lf unsquash/dev"
  echo "Run 'mount | grep \"unsquash/dev\"' to check after this script exits!"
  exit
  sudo umount unsquash/dev || umount -lf unsquash/dev
  #HELP - END

I've never heard of chrootbeg, google's never heard of chrootbeg, and my system doesn't have it. I think you took someone's instructions a little too literally. Those are labels for you, telling you 'run all this stuff in a chroot'.

Further, chroot doesn't work that way, neither does sudo nor any other shell. No interpreter will stop in the middle, start executing a totally different language, then return to where you were without being asked -- if you want to put commands into something else, you have to tell the shell to put them there.

chroot works like:

chroot /path/to/newroot /bin/sh

...and from there on out, reads interactively. Though simple commands will work noninteractively. (apt-get and the like are prone to prompt you for y/n, so your mileage may vary.) You could put the inside-chroot stuff into a script file and run it:

chroot /path/to/newroot /bin/sh < /path/to/scriptfile

Ok thank you. For clarification chrootbeg and chrootend were bash functions I created in the script not actual commands. If I do what you mentioned and create a separate script that contain just the commands to be run in a chroot env and put a exit command in it does that kills the script but doesn't exit the chroot environment? Then I have to type exit interactively at the prompt to exit the chroot env?

Sorry, my morning coffee hadn't kicked in.

Your chroot commands get things ready for a chroot, but don't actually run chroot, so nothing ends up happening inside the chroot.

If you call chroot the way I show it

chroot /path/to/folder /path/to/shell < inputfile

...it ought to finish by itself when it hits end-of-file. Interactive commands may foul this up.

So,

chrootbeg
chroot /path/to/folder /path/to/shell < /path/to/inside-chroot-script
chrootend

Below is the code solution I came up with. It seems to work fine regarding running the commands in a script non interactively from within a chroot.

Thank you again Corona68. Problem is SOLVED.

 #HELP - Function below creates a script and enters a chroot env then
#HELP - exec the script that the function created in the new chroot env
chrootexec unsquash
#HELP - END


# chrootexec - Creates a script then enters a chroot env then executes 
# the script in the chroot env then exits the chroot env
chrootexec() {
  # Creating the script to be executed in chroot env
  chrootdn=$1
  echo "chrootexec - beg funct - chrootdn = '"$chrootdn"' ..."
  currdn=`pwd`
  if [ ! -d "$currdn/$chrootdn" ]; then
    echo "ERROR - chrootdn '"$currdn/$chrootdn"' dir does NOT exist!"; exit 1
  fi
  chrootfn=`echo $currdn"/"$chrootdn"/"chroots`
  echo "Creating the chroot script to be run '"$chrootfn"' ..."
  sudo rm $chrootfn 2>/dev/null >/dev/null
  sudo touch $chrootfn
  sudo chmod 777 $chrootfn
  
  # Setup to enter chroot env
  echo "#!/bin/bash" >>$chrootfn
  echo "#PROGDESC - chroots - Script to be executed in chroot env ONLY !" >>$chrootfn
  echo "mount -t proc none /proc/" >>$chrootfn
  echo "mount -t sysfs none /sys/" >>$chrootfn
  echo "mount -t devpts none /dev/pts" >>$chrootfn
  echo "export HOME=/root" >>$chrootfn
  echo "export LC_ALL=C" >>$chrootfn
  echo "dbus-uuidgen > /var/lib/dbus/machine-id" >>$chrootfn
  echo "dpkg-divert --local --rename --add /sbin/initctl" >>$chrootfn   
  echo "ln -s /bin/true /sbin/initctl" >>$chrootfn
  
  # Put any custom code you want to execute below
  echo "echo \"chroot - my mods - beg ...\"" >>$chrootfn
  # https://superuser.com/questions/476512/how-do-i-permanently-reset-the-time-zone-in-debian
  echo "date | egrep \"PST|PDT\"" >>$chrootfn
  echo "if [ \"\$?\" != \"0\" ]; then" >>$chrootfn
  echo "  echo \"WARN - Timezone is != PDT or PST - Changing it!\"" >>$chrootfn
  echo "  echo 'America/Los_Angeles' > /etc/timezone" >>$chrootfn 
  echo "  rm /etc/localtime" >>$chrootfn
  echo "  dpkg-reconfigure -f noninteractive tzdata" >>$chrootfn
  echo "  `date`" >>$chrootfn
  echo "fi" >>$chrootfn
  echo "fn=\"/etc/apt/sources.list\"" >>$chrootfn
  echo "cat \$fn | grep \"non-free\" 2>/dev/null >/dev/null" >>$chrootfn
  echo "if [ \"\$?\" != \"0\" ]; then" >>$chrootfn
  echo "  if [ ! -f \$fn.org ]; then" >>$chrootfn
  echo "    cp \$fn \$fn.org" >>$chrootfn
  echo "  fi" >>$chrootfn
  echo "  echo \"Modifying file '\"\$fn\"' adding contrib and non-free repos ...\"" >>$chrootfn
  echo "  sed -i 's/main/main contrib non-free/g' "\$fn >>$chrootfn
  echo "  cp \$fn \$fn.mod" >>$chrootfn
  echo "  echo; ls -la \$fn*; echo" >>$chrootfn
  echo "fi" >>$chrootfn
  echo "echo \"apt-get update ...\"" >>$chrootfn
  echo "apt-get -y update" >>$chrootfn
  echo "apt -y --fix-broken install" >>$chrootfn
  echo "if [ -d /usr/lib/libreoffice ]; then" >>$chrootfn
  echo "  echo \"Deleting libreoffice ...\"" >>$chrootfn
  echo "  apt-get -y remove --purge libreoffice-*" >>$chrootfn
  echo "fi" >>$chrootfn
  echo "# Save over 2 GB of hard drive space from live install deleting progs below" >>$chrootfn
  echo "if [ -d /usr/share/doc/fonts-noto-extra ]; then" >>$chrootfn
  echo "  apt purge ubertooth-firmware-source" >>$chrootfn
  echo "  apt purge -y fonts-noto-extra" >>$chrootfn
  echo "  apt purge -y fonts-noto-cjk-extra" >>$chrootfn
  echo "  apt purge -y fonts-noto-cjk" >>$chrootfn
  echo "  apt purge -y fonts-noto-ui-extra" >>$chrootfn
  echo "  apt purge -y fonts-noto-core" >>$chrootfn
  echo "  apt purge -y fonts-noto-color-emoji" >>$chrootfn
  echo "  apt purge -y fonts-noto-ui-core" >>$chrootfn
  echo "  apt purge -y fonts-noto-unhinted" >>$chrootfn
  echo "  apt purge -y fonts-noto-mono" >>$chrootfn
  echo "  # Remove all foreign dictionaries then install only american" >>$chrootfn
  echo "  apt purge -y aspell" >>$chrootfn
  echo "  apt-get -y install iamerican" >>$chrootfn
  echo "  apt-get -y autoremove" >>$chrootfn
  echo "fi" >>$chrootfn
  echo "if [ -f /usr/bin/clipit ]; then" >>$chrootfn
  echo "  apt-get -y purge clipit" >>$chrootfn
  echo "  pkill clipit" >>$chrootfn
  echo "fi" >>$chrootfn
  echo "echo \"Deleting all non English language packs ...\"" >>$chrootfn
  echo "apt-get -y remove --purge \`dpkg-query -W --showformat='${Package}\n' | grep language-pack | egrep -v '\-en'\`" >>$chrootfn
  echo "apt -y autoremove" >>$chrootfn
  echo "echo \"apt-get upgrade ...\"" >>$chrootfn
  echo "apt-get -y upgrade" >>$chrootfn
  echo "echo \"Installing various packages that I need ...\"" >>$chrootfn
  echo "apt -y install curl wget apt-transport-https dirmngr" >>$chrootfn
  echo "if [ ! -f /usr/bin/sensors ]; then" >>$chrootfn
  echo "  sudo apt-get -y install lm-sensors" >>$chrootfn 
  echo "fi" >>$chrootfn
  echo "if [ ! -f /usr/sbin/hddtemp ]; then" >>$chrootfn
  echo "  sudo apt-get -y install hddtemp" >>$chrootfn
  echo "fi" >>$chrootfn
  echo "if [ ! -f /usr/sbin/rfkill ]; then" >>$chrootfn
  echo "  sudo apt-get -y install rfkill" >>$chrootfn 
  echo "fi" >>$chrootfn
  echo "if [ ! -f /usr/bin/dos2unix ]; then" >>$chrootfn
  echo "  sudo apt-get -y install dos2unix" >>$chrootfn
  echo "fi" >>$chrootfn
  echo "if [ ! -d /etc/cifs-utils ]; then" >>$chrootfn 
  echo "  sudo apt-get -y install cifs-utils # for mapdrv routine to work" >>$chrootfn
  echo "fi" >>$chrootfn
  echo "if [ ! -f /usr/bin/upower ]; then" >>$chrootfn
  echo "  sudo apt-get -y install upower" >>$chrootfn
  echo "fi" >>$chrootfn
  echo "if [ ! -f /usr/bin/adb ]; then" >>$chrootfn
  echo "  sudo apt-get -y install adb" >>$chrootfn
  echo "fi" >>$chrootfn
  echo "if [ ! -d /usr/lib/ntp ]; then" >>$chrootfn
  echo "  sudo apt-get -y install ntp" >>$chrootfn
  echo "fi" >>$chrootfn
  echo "if [ ! -f /usr/bin/geany ]; then" >>$chrootfn
  echo "  apt-get -y install geany" >>$chrootfn
  echo "fi" >>$chrootfn
  echo "if [ ! -f /usr/bin/xdiskusage ]; then" >>$chrootfn
  echo "  sudo apt-get -y install xdiskusage" >>$chrootfn 
  echo "fi" >>$chrootfn 
  echo "if [ ! -f /usr/bin/meld ]; then" >>$chrootfn
  echo "  sudo apt-get -y install meld" >>$chrootfn
  echo "fi" >>$chrootfn
  echo "if [ ! -f /usr/bin/gdebi ]; then" >>$chrootfn
  echo "  sudo apt-get -y install gdebi" >>$chrootfn
  echo "fi" >>$chrootfn
  echo "if [ ! -d /usr/share/doc/firmware-linux-nonfree ]; then" >>$chrootfn
  echo "  apt-get -y install firmware-linux-nonfree" >>$chrootfn
  echo "fi" >>$chrootfn
  echo "if [ ! -d /usr/share/bug/firmware-iwlwifi ]; then" >>$chrootfn
  echo "  # NOTES - Need this for my wireless AC card in laptop is recognized" >>$chrootfn
  echo "  apt-get -y install firmware-iwlwifi" >>$chrootfn
  echo "fi" >>$chrootfn
  echo "if [ ! -f /usr/bin/ristretto ]; then" >>$chrootfn
  echo "  sudo apt-get -y install ristretto # Open/Browse pictures quickly" >>$chrootfn
  echo "fi" >>$chrootfn
  echo "if [ ! -f /usr/bin/system-config-printer ]; then" >>$chrootfn
  echo "  apt-get -y install system-config-printer #printer support" >>$chrootfn
  echo "fi" >>$chrootfn
  # easytether made no internet on remast live cd
  # when i tried to uninstall resolvconf it prompted me with dialog
  # box that I couldn't bypass
  #echo "apt-get -y purge resolvconf" >>$chrootfn
  #echo "ieprog easytether" >>$chrootfn
  echo "ieprog bootiso" >>$chrootfn
  echo "ieprog rclone" >>$chrootfn
  echo "ieprog golang" >>$chrootfn
  echo "ieprog liteide" >>$chrootfn
  echo "echo \"INFO - Only install flashplayer on Debian NOT on Ubuntu - Install takes FOREVER !!!\"" >>$chrootfn
  echo "if [ ! -f /usr/lib/mozilla/plugins/libflashplayer.so ]; then" >>$chrootfn
  echo "  ieprog flashplayer" >>$chrootfn
  echo "fi" >>$chrootfn
  echo "echo \"chroot - my mods - end ...\"" >>$chrootfn
  
  # Setup to exit chroot env
  echo "echo \"\"" >>$chrootfn
  #echo "ap_clean" >>$chrootfn
  echo "rm -rf /tmp/* ~/.bash_history" >>$chrootfn
  echo "rm /var/lib/dbus/machine-id" >>$chrootfn
  echo "rm /etc/resolv.conf" >>$chrootfn
  echo "rm /sbin/initctl" >>$chrootfn
  echo "dpkg-divert --rename --remove /sbin/initctl" >>$chrootfn
  #echo "#### Clean older/non-used kernels..." >>$chrootfn
  #echo "dpkg -l 'linux-*' | sed '/^ii/!d;/'\"$(uname -r | sed \"s/\(.*\)-\([^0-9]\+\)/\1/\")\"'/d;s/^[^ ]* [^ ]* \([^ ]*\).*/\1/;/[0-9]/!d' | xargs sudo apt-get -y purge" >>$chrootfn
  echo "umount /proc || umount -lf /proc" >>$chrootfn
  echo "umount /sys || umount -lf /sys" >>$chrootfn
  echo "umount /dev/pts || umount -lf /dev/pts" >>$chrootfn
  
  echo; echo "Displaying chroot created script ..."; echo; cat $chrootfn; echo
  
  # Ensure we have a resolv.conf file so internet will work - for chroot only
  fn=`echo $xdn"/unsquash/etc/resolv.conf"`
  if [ ! -f $fn ]; then
    sudo cp /etc/resolv.conf $fn
  fi
  echo; ls -la $fn; echo
  
  if [ "$atrue" = "Y" ]; then
    # Actually entering chroot env and executing the script then
    # exit the chroot sys then returning to host system    
    sudo mount --bind /dev/ $chrootdn/dev
    echo "BEG - Running chroot created script ...";
    # Changed chroot from running interactive commands to run a script
    #sudo chroot $chrootdn
    sudo chroot $chrootdn /bin/sh < $chrootfn
    echo "DONE - Running chroot created script ...";
    sudo umount $chrootdn/dev || sudo umount -lf $chrootdn/dev
  fi
}
2 Likes