Personal tools
You are here: Home Documentation Tips Using "mount -o bind" for chroot enviroments
Document Actions

Using "mount -o bind" for chroot enviroments

by Troy Dawson last modified 2009-03-27 15:17

How to use mount -o bind to make your chroot environment better.

chroot is a marvelous tool. It allows you to have a separate environment to build and test on without doing any virtual machines. But it has it's drawbacks, and mounting disks is one of them.
This tip will show you how to use the "mount -o bind" option to allow your chroot environment to be easier to use.

From the mount man page

...
Since Linux 2.4.0 it is possible to remount part of the file hierarchy somewhere else. The call is

mount --bind olddir newdir
After this call the same contents is accessible in two places. One can also remount a single file (on a single file).
This call attaches only (part of) a single filesystem, not possible submounts. The entire file hierarchy including submounts is attached a second place using

mount --rbind olddir newdir

Note that the filesystem mount options will remain the same as those on the original mount point, and cannot be changed by passing the -o option along with --bind/--rbind.

A Build Enviroment Example

On this machine we have a SL4 i386 installation in the directory /mnt/sl4.i386.
On one screen we have chrooted into that area, on the other screen we are on the machine just normally.
A few things to point out. The normal machine does show the bind mounts when you do a df. But you do not see that anything different for df in the chroot area.

Normal Chroot
# df -h
Filesystem           1K-blocks      Used Available Use% Mounted on
/dev/sda1             19236308  15856816   2402340  87% /
none                   2029676         0   2029676   0% /dev/shm
/dev/sda2             19236340  12376224   5882964  68% /mnt/sl4.i386
linux:/export/linux  2147483648 2027811072 119672576  95% /mnt/linux

# ls /mnt/linux
centos   fermi         lts30x  lts45        lts4x       slf51        slf5x
contrib  iso           lts41   lts46        redhat      slf52
extra    lts309        lts42   lts47        scientific  slf53
fedora   lts30rolling  lts44   lts4rolling  slf50       slf5rolling
# df -h
Filesystem           1K-blocks      Used Available Use% Mounted on
/dev/sda2             19236340  12376224   5882964  68% /
none                   2029676       216   2029460   1% /dev/pts
none                   2029676       216   2029460   1% /dev/shm

# ls /mnt/linux
#
# mount -o bind /mnt/linux /mnt/sl4.i386/mnt/linux/
# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda1              19G   16G  2.3G  87% /
none                  2.0G     0  2.0G   0% /dev/shm
/dev/sda2              19G   12G  5.7G  68% /mnt/sl4.i386
linux:/export/linux   2.0T  1.9T  115G  95% /mnt/linux
/mnt/linux            2.0T  1.9T  115G  95% /mnt/sl4.i386/mnt/linux
#
# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda2              19G   12G  5.7G  68% /
none                  2.0G  216K  2.0G   1% /dev/pts
none                  2.0G  216K  2.0G   1% /dev/shm
# ls /mnt/linux
centos   fermi         lts30x  lts45        lts4x       slf51        slf5x
contrib  iso           lts41   lts46        redhat      slf52
extra    lts309        lts42   lts47        scientific  slf53
fedora   lts30rolling  lts44   lts4rolling  slf50       slf5rolling
#

A Rescue Example

Booting into a live CD is a great way to fix problems on a broken machine. But what if you need to do more than just edit a file or two.
Many times when you are fixing a machine, you need to chroot into it. Many of those times the programs you run (such as grub-install) will need the /proc, /dev, and /sys directories to be alive and well. This is when you need to use bind.

For this example, we will try to run "grub-install" to try to fix a machine who's grub didn't install properly.
You might notice, that when we bind /proc, /dev, and /sys, they do not show up when you do a "df". To see them you have to do a "mount" command.

# mount /dev/hda5 /mnt/hda5
# chroot /mnt/hda5/
[chroot]# grub-install /dev/hda
/dev/hda: Not found or not a block device.
[chroot]# exit
exit
# mount -o bind /proc /mnt/hda5/proc/
# mount -o bind /dev /mnt/hda5/dev/
# mount -o bind /sys /mnt/hda5/sys
# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/hda3             9.5G  3.9G  5.1G  44% /
tmpfs                 311M     0  311M   0% /dev/shm
AFS                   8.6G     0  8.6G   0% /afs
/dev/hda1             9.7G  5.0G  4.2G  55% /mnt/hda1
/dev/hda5             9.5G  3.8G  5.3G  42% /mnt/hda5
# chroot /mnt/hda5
[chroot]# grub-install /dev/hda
Installation finished. No error reported.
This is the contents of the device map /boot/grub/device.map.
Check if this is correct or not. If any of the lines is incorrect,
fix it and re-run the script `grub-install'.

# this device map was generated by anaconda
(hd0)     /dev/hda
[chroot]# exit
exit
#


Powered by Plone, the Open Source Content Management System

This site conforms to the following standards: