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:
Code:
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:
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?
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)
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)
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)
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)
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)
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)
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)
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)