scripting chroot

Alain De Vos

Aspiring Daemon

Reaction score: 256
Messages: 984

Sometimes I chroot into something and I do a repetitive task afterwards.
How do I script after chroot ?
I know the input, I can foresee the output.
Something very basic like "adduser" after chroot.
 

memreflect

Active Member

Reaction score: 173
Messages: 194

chroot(8) says:
Code:
     Example 2: Execution of a Command with a Changed Root Directory

       The following command changes a root directory with chroot and then
       runs ls(1) to list the contents of /sbin.

         # chroot /tmp/testroot ls /sbin

For multiple commands, I'd recommend executing a script in the target root directory in most cases, as in chroot /tmp/testroot sh /filename.sh, particularly if you're dealing with text manipulation or if the use of '$', quote chars, and backslashes becomes problematic.
 

PMc

Daemon

Reaction score: 560
Messages: 1,145

Code:
# n="1 2 3"
# chroot /j sh -c 'for i in '"$n"'; do echo $i; done'
1
2
3
#

Basic rules:
1. "string" and 'string' can be glued together
2. Variables from the outside should be in such "$string".
3. Variables local within the chroot should be in such '$string'.
 
Top