This site is now 100% read-only, and retired.

Mounting things in multiple locations

Posted by Steve on Thu 24 Aug 2006 at 11:39

mount is a command which most people take for granted, once they've mounted their local filesystems it doesn't get used very often. Recently I've come to appreciate one of the more unusual mount facilities; the ability to mount something in multiple locations.

As we recently saw it is possible to install Debian via debootstrap, and this works nicely for most things.

However if you were to boot a system using Knoppix, or similar, and attempt to install a new machine with debootstrap you'll soon realise that guide isn't complete - your new system has no kernel installed and is also missing a bootloader.

How do we fix this? Well the solution is very simple, we use chroot to change our root into the new installation, then we can use apt-get to install a kernel.

The process would look something like this:

# chroot /target
# apt-get update
# apt-get install kernel-image-2.6.8-3-686
# apt-get install grub

(Note: The kernel you install will depend upon which distribution of Debian you're working with, and upon your architecture.)

Now we're almost at a point where we have a real installation, the only thing remaining is that we've still to configure the bootloader.

There are a couple of steps to this if you're installing grub like this:

  • Copy the support files into place.
  • Configure a menu.lst file.
  • Do the boot-block magic.

It is that last step which reintroduced me to using mount. For the first to steps you should be able to just run this:

# mkdir -p /boot/grub
# cp /lib/grub/i386-pc/* /boot/grub
# $EDITOR /boot/grub/menu.lst

The contents of the menu.lst file will vary depending on what you're using, for me I used:


title           Debian GNU/Linux, kernel 2.6.16-2-k7
root            (hd0,1)
kernel          /boot/vmlinuz-2.6.16-3-686 root=/dev/sda2 ro
initrd          /boot/initrd.img-2.6.16-3-686

Now we need to use the grub interactive installer, and you'll discover that it won't run properly because the system does not have a fully-populated /dev directory - so grub cannot discover the device(s) to use.

(In my case I was relying upon the /dev directory being setup dynamically when the new system booted, so having it be missing on the newly installed debootstrap system wasn't a problem.)

The solution? Exit the chroot and make the /dev directory of the host available to the chroot using the bind option:

# mount --bind /dev  /target/dev
# mount --bind /proc /target/proc

Now if you return to the system, via "chroot /target", you can see /dev as you'd expect. The same directory is available in two locations without any symlinks or other complications!

Completing the setup with grub now becomes a simple job:

# grub
grub>
grub> device (hd0) /dev/sda
grub> root (hd0,1)
grub> setup (hd0)
grub> quit

(Note: Any setup of your own will use different devices/hd-strings. This is just a simple example.)

"mount --bind" is really just a shortcut for "mount .... -o bind", but it is a useful one.

There are probably a lot of uses for the --bind option which I've not yet discovered, but this one was good enough.

 

 


Re: Mounting things in multiple locations
Posted by ajt (204.193.xx.xx) on Thu 24 Aug 2006 at 14:37
[ View Weblogs ]
If you are running 32bit x86 apps on an AMD64 system via a chroot, you use the mount --bind option to make various file systems from the 64-bit host appear inside the chroot. Very useful, if messy.

--
"It's Not Magic, It's Work"
Adam

[ Parent ]

Setting bind mounts from fstab
Posted by Anonymous (132.248.xx.xx) on Thu 24 Aug 2006 at 18:36
I use some bind mounts to provide /proc, /dev and /tmp for some of my virtual servers - In your article, you didn't explain how to set this automatically at boot time. Just add to your /etc/fstab:
/proc           /var/sarge/proc                 auto bind 0 0
/dev            /var/sarge/dev                  auto bind 0 0

Of course, assuming your desired chroot is at /var/sarge :)

[ Parent ]

Re: Setting bind mounts from fstab
Posted by trakic (87.72.xx.xx) on Sun 27 Aug 2006 at 21:29
[ View Weblogs ]
Thx anonymous for your previous posting. Bu using your hint my vservers can do automounts via /net/"nfshostname".

BTW: I wonder why bind mounting on /proc /dev is not enabled (per default) on vserver creation script, nor even documented in vserver documentation site?

/regards,Admir Trakic

[ Parent ]

Re: Setting bind mounts from fstab
Posted by Anonymous (86.61.xx.xx) on Sat 2 Sep 2006 at 17:02
Because a sistem can run wihtout /proc and with really reduced /dev too. And if you want to have a really secure vserver, then you will be just angry if a stupid script enables this by default.

[ Parent ]

Re: Mounting things in multiple locations
Posted by suspended user tong (64.231.xx.xx) on Tue 29 Aug 2006 at 21:47
[ View Weblogs ]
I still can't install grub after bind mount /dev.

I got:

% grub-install /dev/$BDEV
Could not find device for /boot: Not found or not a block device.

Within grub, I got:

grub> device (hd0) /dev/sda
Error 12: Invalid device requested

My

BDEV=sda5

grub:
Installed: 0.97-13
Candidate: 0.97-13

within chroot:

$ ls -ld /boot
drwxr-xr-x 3 root root 4096 08-29 16:19 /boot

please help.

thanks

[ Parent ]

Re: Mounting things in multiple locations
Posted by Steve (62.30.xx.xx) on Tue 29 Aug 2006 at 21:50
[ View Weblogs ]

Sounds like your system is setup to have a seperate boot partition? If so you'll need to mount that in the chroot to.

Steve

[ Parent ]