PDA

View Full Version : [HOWTO] How to boot a chroot directory ...



Fabianx
07-04-2003, 10:16 PM
This is actuaqlly a copy of a mail I sent to debian-knoppix earlier this day ...

I hope this is interesting for some of you:

---

Hi,

in my last mail I said, that there are problems with chroot, that you are not
able to unmount or remount ...

I also had an idea, and this idea did work ...

What you need:

- Linux partition
- Lilo (Grub could also be possible)

Create an ext2-image with 2560000 bytes. (Should be enough).

Mount it to /mnt.

-rwxr-xr-x 1 root root 150580 2002-07-26 14:09 /mnt/static/ash
lrwxrwxrwx 1 root root 3 2003-04-01 23:46 /mnt/static/init
-> ash
lrwxrwxrwx 1 root root 3 2003-04-01 23:46 /mnt/static/sh ->
ash

Copy ash to /mnt/static and create symlinks ...

Copy needed modules (for ext3 that are jbd.o and ext3.o) to /mnt/modules

-rw-r--r-- 1 root root 87626 2003-04-01 23:46
/mnt/modules/ext3.o
-rw-r--r-- 1 root root 74090 2003-04-01 23:46 /mnt/modules/jbd.o

Copy device that needs to be mounted to /mnt/dev (or copy whole /dev)

cp libraries for non-static binaries ("real" mount/pivot_root are needed)

-rwxr-xr-x 1 root root 82456 2003-05-07 09:27
/mnt/lib/ld-linux.so.2
-rwxr-xr-x 1 root root 1104072 2003-05-07 09:27 /mnt/lib/libc.so.6

mkdir -p /mnt/mnt /mnt/mnt2 /mnt/proc

copy mount and pivot_root to /bin:


-rwsr-xr-x 1 root root 81080 2003-05-07 09:27 /mnt/bin/mount
-rwsr-xr-x 1 root root 4832 2003-05-07 09:27
/mnt/bin/pivot_root

Last but not least create the /mnt/linuxrc the heart of our operation :-))

#!/static/sh

# load needed modules
insmod /modules/jbd.o
insmod /modules/ext3.o

# mount proc, say that root is pivot_rooted and unmount it
mount -t proc proc /proc
echo 256 > /proc/sys/kernel/real-root-dev
umount /proc

# mount the root-filesystem
mount -t ext3 /dev/hda5 /mnt

# tricky: We now bind our chroot environment to /mnt2
/bin/mount --bind /mnt/home/knoppix/newroot /mnt2

# now we unmount the root-filesystem; believe me, its possible and /mnt2 is
still there
umount /mnt

# Now we change the root permanently to the chroot-dir and we can do every
operation that is needed (remount, unmount, ...)

/bin/pivot_root /mnt2 /mnt2/initrd

Of course you can sue the comments and just have the script so compact :

#!/static/sh
insmod /modules/jbd.o
insmod /modules/ext3.o
mount -t proc proc /proc
echo 256 > /proc/sys/kernel/real-root-dev
umount /proc
mount -t ext3 /dev/hda5 /mnt
/bin/mount --bind /mnt/home/knoppix/newroot /mnt2
umount /mnt
/bin/pivot_root /mnt2 /mnt2/initrd

I tried this and it did work ... I was really suprised, but I think its an
kind of easy way to test several copied knoppix systems or bootstrapped and
then knoppised debian's :-)).

If anyone knows an easier way, I'm interested to learn it :-) (Or if anyone
knows this way from another source on the web ...)

btw. lilo.conf:

image=/boot/vmlinuz-2.4.20-xfs
label=Chroot
append="hda=scsi hdb=scsi hdc=scsi hdd=scsi hde=scsi hdf=scsi hdg=scsi
hdh=scsi apm=power-off nomce ramdisk_size=100000 lang=de init=/etc/init"
initrd=/boot/chroot_initrd
read-only

I think it would be possible to also have all this pointing to
/home/knoppix/newroot/vmlinuz|boot/chroot_initrd ...

But as I had the same kernel, I'm lazy ... :-))

cu

Fabian

PS: I hope this is useful to someone ...
PPS: So you can finally start your changed system and such USE sortlists and
so optimise the system more and more without the need of an extra-partition
...

garyng
07-05-2003, 06:05 AM
I am confused. What do you want to achieve ? I use pivot_root in the normal initrd to boot into a real root fs(say a clooped device, or a loopback device on FAT/NTFS) but don't quite understand what you want to do in this post. pivot_root is sort of like a 'chain boot' to me and there is no way one can get back to the previous root system. I believe UML is a better way to test out a new distribution within a running system.

BTW, in general people seldom do a pivot_root to absolute path "/..." but rather :

cd /your_new_root
pivot_root . etc/init(this is a under the new root system and note the absence of / before etc)

Fabianx
07-05-2003, 01:52 PM
I am confused. What do you want to achieve ? I use pivot_root in the normal initrd to boot into a real root fs(say a clooped device, or a loopback device on FAT/NTFS) but don't quite understand what you want to do in this post. pivot_root is sort of like a 'chain boot' to me and there is no way one can get back to the previous root system. I believe UML is a better way to test out a new distribution within a running system.

BTW, in general people seldom do a pivot_root to absolute path "/..." but rather :

cd /your_new_root
pivot_root . etc/init(this is a under the new root system and note the absence of / before etc)

If you want to test Knoppix-features like hw-detection, I think UML is no choice as you need real test-circumstances ...

I want to boot a directory /home/knoppix/newroot as if everything in newroot was in its own partition!

So /mnt/home/knoppix/newroot -> /

But with pivot_root this is not possible ... (Neither with relative nor absolute path)

You need the trick, I've described above ...

cu

Fabian

PS: Btw and yes, you CAN change the System, while running with pivot_root and you can also go back (if you don
't unmount the initrd) ...