slitaz-doc-wiki-data view pages/en/handbook/chroot.txt @ rev 7

Add pages/en folder.
author Christopher Rogers <slaxemulator@gmail.com>
date Sat Feb 26 12:17:18 2011 +0000 (2011-02-26)
parents
children
line source
1 ====== Chroot environment ======
3 This document describes the necessary steps to create a chrooted environment, in order to change the root of the system so that you can work. This makes it possible to compile, test and develop SliTaz without any risk to the host system you're working on. The host system can be SliTaz installed to a hard drive or any other GNU/Linux system such as Debian, Fedora, PCLinuxOS and so on. You can also create a chrooted environment in LiveCD mode associated with USB media. The only prerequisite is to have a SliTaz ISO image available and a little time. Note that all commands are carried out as system administrator (root).
5 ===== Prepare the environment =====
7 To begin, we must extract the contents of the ISO image into the directory that will serve as our chroot. The directory can be created any place you choose, we'll use a directory ///home/slitaz/chroot-env//. To extract the contents of an ISO image, we must mount it in a loop directory and then copy the compressed root filesystem (rootfs.gz) into the chroot directory. Assuming the ISO is in the current directory:
9 <code>
10 # mkdir /tmp/loop
11 # mount -o loop slitaz-cooking.iso /tmp/loop
12 # mkdir -p /home/slitaz/chroot-env
13 # cp /tmp/loop/boot/rootfs.gz \
14 /home/slitaz/chroot-env
15 # umount /tmp/loop
16 </code>
18 Now we have a copy of the compressed filesystem, we must extract and unpack it (this is a cpio archive compressed with either gzip or lzma). To complete this stage, we can remove the rootfs which is no longer required:
20 <code>
21 # cd /home/slitaz/chroot-env
22 # (zcat rootfs.gz 2>/dev/null || lzma d rootfs.gz -so) | cpio -id
23 # rm rootfs rootfs.gz
24 </code>
26 If the unpacking of the rootfs compressed with lzma fails; you can use the following method:
28 <code>
29 # unlzma rootfs.gz -S .gz
30 # cat rootfs | cpio -id
31 </code>
33 ===== Using the environment =====
35 To begin using the chrooted environment, you just need to mount some virtual filesystems and use the chroot command. To simplify things, we can write a small script automating the process. Example using the chroot directory ///home/slitaz/chroot-env// and creating a script chroot_in_env.sh in ///home/slitaz//. On any systems other than SliTaz you can uncomment the lines about ///dev// and ///tmp// - Note to save typing you can copy and paste:
37 <code> # cat > /home/slitaz/chroot_in_env.sh << "EOF" </code>
39 <file>
40 #!/bin/sh
41 # Chroot in SliTaz to hack.
42 #
43 ROOTFS="/home/slitaz/chroot-env"
45 # Mount virtual Kernel file systems and chroot.
46 #
47 #mount --bind /dev $ROOTFS/dev
48 #mount --bind /tmp $ROOTFS/tmp
49 mount -t proc proc $ROOTFS/proc
50 mount -t sysfs sysfs $ROOTFS/sys
51 mount -t devpts devpts $ROOTFS/dev/pts
52 mount -t tmpfs shm $ROOTFS/dev/shm
54 echo "Chrooting into $ROOTFS... "
55 chroot $ROOTFS /bin/sh --login
57 # Unmount virtual Kernel file systems on exit.
58 #
59 umount $ROOTFS/dev/shm
60 umount $ROOTFS/dev/pts
61 umount $ROOTFS/sys
62 umount $ROOTFS/proc
63 #umount $ROOTFS/tmp
64 #umount $ROOTFS/dev
66 echo "Exiting $ROOTFS chroot environment... "
68 EOF
69 </file>
71 To finish and test the environment, you just make the script executable and run:
73 <code>
74 # chmod +x /home/slitaz/chroot_in_env.sh
75 # sh /home/slitaz/chroot_in_env.sh
76 </code>
78 === To activate the network ===
80 In order to have the network up to download and install some development packages, just start the DHCP client on the correct interface. Example using eth1:
82 <code> # udhcpc -i eth1 </code>
84 === Installing packages ===
86 If the network is functional, just reload the list of packages and use tazpkg get-install to install them. If a connection is not possible, you can download the packages from another system, copy them to the chrooted environment and install them with the tazpkg install command. To install the basic compilation tools:
88 <code> # tazpkg recharge
89 # tazpkg get-install slitaz-toolchain </code>
91 Once the environment is configured, you can compile applications from source to create packages, test scripts, etc. The Cookbook should help you out here:
93 === Exit the environment ===
95 To exit the chrooted environment, just type exit, the //chroot_in_env.sh// script will then end by unmounting the virtual filesystems from the Linux Kernel:
97 <code>
98 # exit
99 #
100 </code>