“device is busy…”

I’m working on a fun little project to set up a custom-made bootable usb key. But I ran into a little trouble after using a chroot, due to /dev and mount --rbind.

# mount -t proc none /mnt/gentoo/proc
# mount --rbind /sys /mnt/gentoo/sys
# mount --rbind /dev /mnt/gentoo/dev
# mount -t tmpfs tmpfs /mnt/gentoo/tmp
# mount -t tmpfs tmpfs /mnt/gentoo/var/tmp
# mount -t tmpfs tmpfs /mnt/gentoo/usr/src
# mount | grep gentoo
/dev/sde1 on /mnt/gentoo type ext2 (rw)
none on /mnt/gentoo/proc type proc (rw)
/sys on /mnt/gentoo/sys type none (rw,bind,rbind)
/dev on /mnt/gentoo/dev type none (rw,bind,rbind)
tmpfs on /mnt/gentoo/tmp type tmpfs (rw)
tmpfs on /mnt/gentoo/var/tmp type tmpfs (rw)
tmpfs on /mnt/gentoo/usr/src type tmpfs (rw)
# chroot /mnt/gentoo

All’s well, until it’s time to exit the chroot and unmount everything..

# umount /mnt/gentoo/usr/src /mnt/gentoo/var/tmp \
/mnt/gentoo/tmp /mnt/gentoo/sys /mnt/gentoo/proc \
/mnt/gentoo/dev /mnt/gentoo
umount: /mnt/gentoo/dev: device is busy.
       (In some cases useful info about processes that use
        the device is found by lsof(8) or fuser(1))

I don’t see anything mounted under there. Fuuuu…

# mount | grep gentoo
/dev/sde1 on /mnt/gentoo type ext2 (rw)
# lsof|grep gentoo
# fuser -m /mnt/gentoo
/mnt/gentoo:
#

Rebooting at this stage is inconvenient, but will certainly solve the problem. But what is left using /mnt/gentoo/dev?
Googling around, and seeing some people with similar problems, finally lit a light bulb above my head:

# cat /proc/mounts | awk '{print $2}' | grep gentoo
/mnt/gentoo
/mnt/gentoo/dev
/mnt/gentoo/dev/pts
/mnt/gentoo/dev/shm

So mount --rbind worked as advertised and recursively mounted /dev and everything sub-mounted. And I didn’t realize that udev mounted other things under /dev without updating /etc/mtab. Sigh.

# umount /mnt/gentoo/dev/shm /mnt/gentoo/dev/pts /mnt/gentoo/dev /mnt/gentoo
#

And now I can get on with my life.