NetRock6
asked on
Chroot Bash script
Hi ..
I booted from an bootable media and trying to chroot the root file system on the local drive to run a bash script (bscript.sh) in /usr/local/bin/ with 3 parameters which are files inside the /usr/local/bin/
i made a bash script as below:
The bscript.sh runs without the parameters, since 'cat' cannot find the files that in the same directory as the bscript.sh (of course full path works with cat like: cat /mnt/s1/....)
Could someone help to correct the above procedures.
Thank You for your Quick Prompts.
I booted from an bootable media and trying to chroot the root file system on the local drive to run a bash script (bscript.sh) in /usr/local/bin/ with 3 parameters which are files inside the /usr/local/bin/
i made a bash script as below:
mkdir /mnt/s1
mount /dev/sda1 /mnt/s1
chroot /mnt/s1 ./etc/local/bin/bscript.sh $(cat paramfile1) $(cat paramfile2) $(cat paramfile3)
But I get the error:
cat cannot find file paramfile1
cat cannot find file paramfile2
cat cannot find file paramfile3
/bin/bash: line 1 file not found
The bscript.sh runs without the parameters, since 'cat' cannot find the files that in the same directory as the bscript.sh (of course full path works with cat like: cat /mnt/s1/....)
Could someone help to correct the above procedures.
Thank You for your Quick Prompts.
ASKER
I need to chroot the "/".
The 'bscript.sh' and the paramfiles are get created after booting, next with 'cp' command are copying to ' /mnt/s1/etc/local/bin/' once the drive is mounted (mount /dev/sda1 /mnt/s1). In the process the 'bscript.sh' runs but 'cat' not able to find the files unless the full path(/mnt/s1/etc/local/bin /paramfile s) is given.
What I am trying to understand is that the script runs but then, 'cat' is not able to find the paramfiles (although, there are already in /mnt/s1/etc/local/bin/).
Also, in my Q I corrected the typos.
Thank you for help.
Cheers ;)
The 'bscript.sh' and the paramfiles are get created after booting, next with 'cp' command are copying to ' /mnt/s1/etc/local/bin/' once the drive is mounted (mount /dev/sda1 /mnt/s1). In the process the 'bscript.sh' runs but 'cat' not able to find the files unless the full path(/mnt/s1/etc/local/bin
What I am trying to understand is that the script runs but then, 'cat' is not able to find the paramfiles (although, there are already in /mnt/s1/etc/local/bin/).
Also, in my Q I corrected the typos.
Thank you for help.
Cheers ;)
Each of your scripts run, like paramfile1, will only work if paramfile1 lives in the /mnt/s1 directory.
If paramfile1 lives in say /mnt/s1/mytools then you'll have to reference the file as /mytools/paramfile1 for your chroot invocation to find these files.
So specify an absolute path, rather than relative path.
If paramfile1 lives in say /mnt/s1/mytools then you'll have to reference the file as /mytools/paramfile1 for your chroot invocation to find these files.
So specify an absolute path, rather than relative path.
ASKER
Before chroot, I am copying the files. So before chroot the files are already there... ready for 'cat'
Oh wait...
Likely doesn't mean what you think.
Your above line says... before you chroot run all the cats + pass the results to bscript.sh in your chroot invocation.
So the above line expands all the cats before doing the chroot.
I'm guessing this isn't what your after.
Try this instead.
Likely something similar will work. In other words you have to defer the cats running till after chroot runs.
A better way to do this will be...
Then do all your cat(s) from within bscript.sh which will make your chroot invocation much more robust.
chroot /mnt/s1 ./etc/local/bin/bscript.sh $(cat paramfile1) $(cat paramfile2) $(cat paramfile3)
Likely doesn't mean what you think.
Your above line says... before you chroot run all the cats + pass the results to bscript.sh in your chroot invocation.
So the above line expands all the cats before doing the chroot.
I'm guessing this isn't what your after.
Try this instead.
chroot /mnt/s1 ./etc/local/bin/bscript.sh '$(cat paramfile1)' '$(cat paramfile2)' '$(cat paramfile3)'
Likely something similar will work. In other words you have to defer the cats running till after chroot runs.
A better way to do this will be...
chroot /mnt/s1 ./etc/local/bin/bscript.sh paramfile1 paramfile2 paramfile3
Then do all your cat(s) from within bscript.sh which will make your chroot invocation much more robust.
Test pwd, location and whether the referenced files would be seen.
The point, you are using relative references to the script though you are running the script from the new root
Not sure why you use the chroot to deal with files.
The files always are used, so including in the script without the needt o either pass their content in execution or as arguments.
Changing the working directory,
chdir /etc/local/bin
The point, you are using relative references to the script though you are running the script from the new root
Not sure why you use the chroot to deal with files.
The files always are used, so including in the script without the needt o either pass their content in execution or as arguments.
Changing the working directory,
chdir /etc/local/bin
It is unclear from your original Q whether bscript.sh is in /etc/local/bin or /usr/local/bin on your root file system. In your example, your script did not actually run at all: that's what the last line /bin/bash: line 1 file not found is telling you. Try this
Also I've tried this myself, with a simple pwd instead of ./bscript.sh ... and it did work.
chroot /mnt/s1 sh -c "cd /usr/local/bin; ./bscript.sh $(cat paramfile1) $(cat paramfile2) $(cat paramfile3)"
I have assumed that /usr/local/bin is correct.Also I've tried this myself, with a simple pwd instead of ./bscript.sh ... and it did work.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
@Duncan, small correction required to your solution.
Change the double quotes to single quotes, which is what I think you meant.
And yes, using single quotes around the entire shell command is, to me, a far more readable solution.
Change the double quotes to single quotes, which is what I think you meant.
And yes, using single quotes around the entire shell command is, to me, a far more readable solution.
ASKER
Thank you David and Duncan for you great help. I will try out.....
@David, the initial cd is so that double quotes work. With single quotes you could miss it out
ASKER
After trying your solution, I get the error:
I am really stuck!
Thank you for your help...
cd /usr/local/bin: No such file or directory
it seems 'chroot' is not working or I am doing something wrong....??!! ;(I am really stuck!
Thank you for your help...
do you have /usr/local/bin?
the path in your original had ./etc/local/bin which is relative to /mnt/s1/ .
since the path is uncommon, those who provided insight, defaulted to the common which is /usr/local that deals with local admin prefix for .....
You can not CD, sing the bash script seems to be relative to the /mnt/s1
using chrdir within the bash script to make sure the starting point is ......
where these files are.
the path in your original had ./etc/local/bin which is relative to /mnt/s1/ .
since the path is uncommon, those who provided insight, defaulted to the common which is /usr/local that deals with local admin prefix for .....
You can not CD, sing the bash script seems to be relative to the /mnt/s1
using chrdir within the bash script to make sure the starting point is ......
where these files are.
I tested cd in a chroot on my system and it worked fine. Please post exactly the chroot command you used. The line you posted is not an error message from cd : more like looking for a file or command called "cd /usr/local/bin"
Please try this minimal test on your system
Please try this minimal test on your system
chroot /mnt/s1 sh -c "cd /usr/local/bin ; pwd"
ASKER
Thank YOU David and Duncan for your excellent help.
Cheers ;)
Cheers ;)
you would need to copy what you need into the chroot environment in the correct structure?
seems peculiar that you are settings up something, but in the setup it is imune from external interference so not quite sure what you are trying to shield the mnt from, an errand directive in your bash script leaving the confines of the mount point and adversely affecting other parameters.?
where are the files, potentially before chroot, you would copy paramfile1-3 into the /mnt/s1