Posted by: richlyn | April 25, 2010

Chroot

A chroot on Unix operating systems is an operation that changes the apparent disk root directory for the current running process and its children. A program that is re-rooted to another directory cannot access or name files outside that directory, and the directory is called a “chroot jail”

A chroot environment can be used to create and host a separate virtualized copy of the operating system. This can be useful for  creating  a test environment  in the chroot for software that would otherwise be too risky to deploy on a production system. Should a system be rendered unbootable, a chroot can be used to move back into the damaged environment after bootstrapping from an alternate root file system (such as from installation media, or a Live CD). 32-bit applications can be run on 64-bit systems using chroot. Chroot is often used as a security measure. When chroot is implemented, programs running inside cannot access any system resources on the outside. Thus all system libraries, configuration files and even devices files should be recreated within the chroot jail. In many ways, a chroot is like installing another operating system inside your existing operating system. Note that it is possible for software from outside the chroot to access files inside the chroot. See Wikipedia – chroot for more information.

How to chroot  simple and fast:

1.        Create a mountpoint

Code: # mkdir /mount/point

2.         Mount mount-bind the existing file systems (dev,proc,sys,/dev/pts) from the non-chrooted environment to the chrooted environment

Code: # mount -o bind /proc /mount/point/proc # mount -o bind /dev /mount/point/dev # mount -o bind /dev/pts /mount/point/dev/pts # mount -o bind /sys /mount/point/sys

3.        Copy resolv.conf to networking to enable the chrooted environment to connect to the internet.

Code: # cp /etc/resolv.conf /mount/point/etc/resolv.conf

4.      Chroot in the mounted environment.

Code: # sudo chroot /mnt

5.      Upstart jobs cannot be started in a chroot because upstart acts as a service supervisor, and processes within the chroot are unable to communicate with the upstart running outside of the chroot, hence chrooted environment needs to be configures for upstart jobs.

Code:  # dpkg-divert --local --rename --add /sbin/initctl 
         ls -s /bin/true /sbin/initctl 

6.    Do what you have to do by running the commands and then undo the upstart confiruration

Code: #  rm /sbib/initctl
         dpkg-divert --local --rename /sbin/initcl

7.     Exit chroot

Code: #  exit

I had an issue where i had Burg as my boot loader on a dedicated partition (sda9) and was multi booting with Windows , Karmic(sda10) and Lucid testing(sda11). One fine day a lucid update went wrong which rewrote the MBR and i could not boot to Karmic or Lucid.

heres what i did…………

sudo mount /dev/sda9 /mnt

sudo mount –bind /dev /mnt/dev

sudo mount –bind /proc /mnt/proc

sudo mount –bind /dev/pts /mnt/disk/dev/pts

sudo cp /etc/resolv.conf /mnt/etc/resolv.conf

sudo chroot /mnt

dpkg-divert –local –rename –add /sbin/initctl

ln -s /bin/true /sbin/initctl

RUN NEEDED COMMANDS

rm /sbin/initctl

dpkg-divert –local –remove /sbin/initctl

exit

sudo umount /mnt/disk/dev/pts

sudo umount /mnt/dev

sudo umount /mnt/proc

sudo umount /mnt

sudo reboot

My Terminal:

custom@custom:~$ sudo fdisk -l

Disk /dev/sda: 160.0 GB, 160041885696 bytes

255 heads, 63 sectors/track, 19457 cylinders

Units = cylinders of 16065 * 512 = 8225280 bytes

Disk identifier: 0xa64ca64c

Device Boot Start End Blocks Id System

/dev/sda1 * 1 2550 20482843+ 7 HPFS/NTFS

/dev/sda2 2551 5099 20474842+ 7 HPFS/NTFS

/dev/sda3 5100 19457 115330635 f W95 Ext’d (LBA)

/dev/sda5 5100 8953 30957223+ 7 HPFS/NTFS

/dev/sda6 8954 12809 30973288+ 7 HPFS/NTFS

/dev/sda7 12810 16633 30716248+ 7 HPFS/NTFS

/dev/sda8 16634 16764 1052226 82 Linux swap / Solaris

/dev/sda9 16765 18110 10811713+ 83 Linux

/dev/sda10 18111 19325 9759456 83 Linux

/dev/sda11 19326 19457 1060258+ 83 Linux

Disk /dev/sdb: 500.1 GB, 500107862016 bytes

255 heads, 63 sectors/track, 60801 cylinders

Units = cylinders of 16065 * 512 = 8225280 bytes

Disk identifier: 0xae1f8566

Device Boot Start End Blocks Id System

/dev/sdb1 1 31871 256003776 83 Linux

/dev/sdb2 31872 60801 232380225 83 Linux

Disk /dev/sdc: 4016 MB, 4016046080 bytes

255 heads, 63 sectors/track, 488 cylinders

Units = cylinders of 16065 * 512 = 8225280 bytes

Disk identifier: 0x0001a359

Device Boot Start End Blocks Id System

/dev/sdc1 1 488 3919828+ b W95 FAT32

custom@custom:~$ sudo mount /dev/sda9 /mnt && sudo mount –bind /dev /mnt/dev && sudo

mount –bind /proc /mnt/proc && sudo mount –bind /sys /mnt/sys && sudo mount –bind

/dev/pts /mnt/dev/pts && sudo cp /etc/resolv.conf /mnt/etc/resolv.conf && sudo chroot /mnt

root@custom:/# dpkg-divert –local –rename –add /sbin/initctl

Adding `local diversion of /sbin/initctl to /sbin/initctl.distrib’

root@custom:/# ln -s /bin/true /sbin/initctl

root@custom:/# sudo burg-install –root-directory=/mnt/ /dev/sd11

sudo: unable to resolve host custom

/usr/sbin/burg-probe: error: cannot stat `/dev/sd11′.

Invalid device `/dev/sd11′.

Try `/usr/sbin/burg-setup –help’ for more information.

“I manually mounted the dedicated Burg partition ”

root@custom:/# sudo burg-install –root-directory=/mnt/ /dev/sd11

sudo: unable to resolve host custom

/usr/sbin/burg-probe: error: cannot stat `/dev/sd11′.

Invalid device `/dev/sd11′.

Try `/usr/sbin/burg-setup –help’ for more information.

root@custom:/# update-burg

Generating burg.cfg …

Found linux image: /boot/vmlinuz-2.6.31-17-generic

Found initrd image: /boot/initrd.img-2.6.31-17-generic

Found linux image: /boot/vmlinuz-2.6.31-16-generic

Found initrd image: /boot/initrd.img-2.6.31-16-generic

Found linux image: /boot/vmlinuz-2.6.31-15-generic

Found initrd image: /boot/initrd.img-2.6.31-15-generic

Found linux image: /boot/vmlinuz-2.6.31-14-generic

Found initrd image: /boot/initrd.img-2.6.31-14-generic

ls: cannot access /media/disk: No such file or directory

ls: cannot access /media/disk: No such file or directory

ls: cannot access /media/disk: No such file or directory

ls: cannot access /media/disk: No such file or directory

ls: cannot access /media/disk: No such file or directory

ls: cannot access /media/disk: No such file or directory

Found Microsoft Windows XP Professional on /dev/sda1

done

root@custom:/# lsb_release -a

No LSB modules are available.

Distributor ID: Ubuntu

Description: Ubuntu 9.10

Release: 9.10

Codename: karmic

root@custom:/# uname -m

i686

root@custom:/# sudo umount /mnt/dev/pts && sudo umount /mnt/sys && sudo umount

/mnt/proc && sudo umount /mnt/dev && sudo umount /mnt

sudo: unable to resolve host custom

umount: /mnt/dev/pts: not found

root@custom:/# exit

exit

custom@custom:~$ lsb_release -a

No LSB modules are available.

Distributor ID: Ubuntu

Description: Ubuntu 8.10

Release: 8.10

Codename: intrepid

custom@custom:~$


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: