Sponsored Content
Top Forums UNIX for Beginners Questions & Answers Iso - remaster script trying to start chroot run commands then exit but host system gets messed up Post 303040012 by paulhoffusa on Monday 21st of October 2019 10:32:10 PM
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.


Code:
 #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
}

This User Gave Thanks to paulhoffusa For This Post:
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
All times are GMT -4. The time now is 08:26 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy