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

Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Iso - remaster script trying to start chroot run commands then exit but host system gets messed up
# 1  
Old 10-14-2019
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
# 2  
Old 10-14-2019
# 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
  echo "chrootend - beg funct ..."
  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
  #sudo umount unsquash/dev || umount -lf unsquash/dev

#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 ..."
  cat $fn | grep "non-free" 2>/dev/null >/dev/null
  if [ "$?" != "0" ]; then
    if [ ! -f $ ]; then
      cp $fn $
    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
  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 ..."
  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!"
  sudo umount unsquash/dev || umount -lf unsquash/dev

# 3  
Old 10-15-2019
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

Last edited by Corona688; 10-15-2019 at 02:15 PM..
# 4  
Old 10-15-2019
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?
# 5  
Old 10-15-2019
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 ought to finish by itself when it hits end-of-file. Interactive commands may foul this up.

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

# 6  
Old 10-21-2019
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

# 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
  echo "chrootexec - beg funct - chrootdn = '"$chrootdn"' ..."
  if [ ! -d "$currdn/$chrootdn" ]; then
    echo "ERROR - chrootdn '"$currdn/$chrootdn"' dir does NOT exist!"; exit 1
  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
  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 \$ ]; then" >>$chrootfn
  echo "    cp \$fn \$" >>$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/ ]; 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
  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

This User Gave Thanks to paulhoffusa For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Inner script run and its exit status

Main Script #!/bin/ksh echo "Maimn script" ./clocal/www/web-data/WAS/WebSphere7/scripts/DealerLocator/Scripts/secondscript.ksh echo "$? = status" Sdecond Script #!/bin/ksh echo "In second SCript" exit 1 Output: Maimn script ./testmain.ksh:... (4 Replies)
Discussion started by: dineshaila
4 Replies

2. Shell Programming and Scripting

Run script on remote host

Hi friends, I have two servers. Server A and B. I want to run one script on server A by logging in to server B. Can anyone provide me code for this.? I tried it by using following ssh username@serverA ./script Then it prompt me the password. I give correct password of the server A. but it... (7 Replies)
Discussion started by: Nakul_sh
7 Replies

3. UNIX for Dummies Questions & Answers

Creating chroot environment with an ISO file.

I decided to try creating a chroot environment with a BT5r2 iso file. I'm just wanting to run Backtrack from inside Debian without having to reboot into my other partition or use vmware. I found some documentation on how to do this with BT4 at this link: ... (0 Replies)
Discussion started by: Azrael
0 Replies

4. Shell Programming and Scripting

How to run a shell script on a remote host using ftp

Hi, is there a way I can run a shell script through ftp on a remote host? The remote host doesn't have ssh running so I can't use ssh. (7 Replies)
Discussion started by: mrskittles99
7 Replies

5. Shell Programming and Scripting

HTML Code to Run a Script from Remote Unix Host

Hi All, Noticed few posts around this but coudnt get exatcly what i wanted. Thanks for your help again. I have a script running on a remote machine and i normally ssh from putty and run the script manually. Is there anyway that i can write an HTML Code with a button so taht when I Click... (1 Reply)
Discussion started by: robinbannis
1 Replies

6. UNIX for Dummies Questions & Answers

Run a script on remote host

Hi, I wish to run a script located on a remote host machineB from machineA. I am using ssh and running the below on machineA. However, the ssh does not seem to work and freezes at ssh -l wlsadmin machineB -v Sun_SSH_1.1.2, SSH protocols 1.5/2.0, OpenSSL 0x0090704f debug1: Reading... (9 Replies)
Discussion started by: shifahim
9 Replies

7. Shell Programming and Scripting

fuser exit code different when script is run from cron

Hi, I am writing a bash script (running on Centos 5.4) to process video (.MTS) files which may have appeared in a certain directory. The files will be dragged and dropped there from a Windows box using Samba, and the script is to check periodically (i.e. run from cron) whether any new .MTS... (0 Replies)
Discussion started by: Palamedes
0 Replies

8. Shell Programming and Scripting

running commands to remote host from centralized host

Gurus/Experts We have a centralized UNIX/Solaris server from where we can actually ssh to all other UNIX/Solaris servers...I need to write a script that reside on this centerlized server and do FileSystem monitoring (basically run df -h or -k) of other remote servers and then send an email to me... (6 Replies)
Discussion started by: anjum.suri
6 Replies

9. UNIX for Dummies Questions & Answers

How to start a chroot jail?

I was reading an article on how it is very important to setup a chroot jail to run bind. I can follow what the article says but one thing I am unclear about is now on system boot the BIND process in the chroot jail will start since it the owner will no longer be root but some other user. Can... (1 Reply)
Discussion started by: mojoman
1 Replies

10. Shell Programming and Scripting

Run a shell script from one host which connext to remote host and run the commands

I want to write a script which would run from one host say A and connect to other remote host B and then run rest of commands in that host. I tried connecting from A host to B with SSH but after connecting to host B it just getting me inside Host B command prompt. Rest of the script is not running... (6 Replies)
Discussion started by: SN2009
6 Replies
Login or Register to Ask a Question

Featured Tech Videos