Linux Mount – Unable to Umount After Pivot Root

chrootmount

On a board, I would like to flash the (NAND) partition where my current rootfs is without restarting and being able to check if the flash succeeded.

To do so, I try to:

  • kill every unnecessary processes
  • mount a SD card containing another rootfs (a mount point to the current rootfs will be available on the SD card)
  • run pivot_root between the rootfs of the SD card and the original one
  • run chroot to use the SD card root.
  • unmount the old root partition
  • flash a new rootfs
  • pivot_root back on the rootfs partition

Unfortunately, I cannot unmount the original root partition.

The beginning is basically the steps described in the pivot_root manual page:

mount /dev/hda1 /new-root
cd /new-root
pivot_root . old_root
exec chroot . sh <dev/console >dev/console 2>&1
umount /old-root # <== unable to unmount

Killing some processes:

ps
  PID USER       VSZ STAT COMMAND
        1 root      1524 S    init [3]
        2 root         0 SW<  [kthreadd]
        3 root         0 SW<  [ksoftirqd/0]
        4 root         0 SW<  [watchdog/0]
        5 root         0 SW<  [events/0]
        6 root         0 SW<  [khelper]
      126 root         0 SW<  [kblockd/0]
      132 root         0 SW<  [kseriod]
      136 root         0 SW<  [kmmcd]
      159 root         0 SW   [pdflush]
      160 root         0 SW   [pdflush]
      161 root         0 SW<  [kswapd0]
      209 root         0 SW<  [aio/0]
      213 root         0 SW<  [nfsiod]
      220 root         0 SW<  [cifsoplockd]
      807 root         0 SW<  [kapmd]
      873 root         0 SW<  [mtdblockd]
      919 root         0 SW<  [rpciod/0]
      925 root         0 SWN  [jffs2_gcd_mtd3]
      927 root         0 SW<  [mmcqd]
     1806 root      2908 R    -bash
     2456 root      2072 R    ps
mount /dev/mmcblk0p0 /mnt/disk
umount /sys
umount /tmp
cat /proc/mounts
 rootfs / rootfs rw 0 0
 /dev/root / jffs2 rw 0 0
 /proc /proc proc rw 0 0
 /dev/mmcblk0p0 /mnt/disk ext2 rw,errors=continue 0 0

I guess I have to find a way to unmount /dev/root and / or rootfs but won't /mnt/disk be a problem because it will still be in use but, it is the SD card I want to chroot to?

umount /proc

cd /mnt/disk
pivot_root . old-root
mount -t proc none /proc
ls -l /proc/1
-r--------    1 root     root             0 Nov 30 01:17 auxv
--w-------    1 root     root             0 Nov 30 01:17 clear_refs
-r--r--r--    1 root     root             0 Nov 30 01:16 cmdline
-rw-r--r--    1 root     root             0 Nov 30 01:17 coredump_filter
lrwxrwxrwx    1 root     root             0 Nov 30 01:17 cwd -> //
-r--------    1 root     root             0 Nov 30 01:17 environ
lrwxrwxrwx    1 root     root             0 Nov 30 01:16 exe -> /old_root  /sbin/init*
dr-x------    2 root     root             0 Nov 30 01:17 fd/
dr-x------    2 root     root             0 Nov 30 01:17 fdinfo/
-r--------    1 root     root             0 Nov 30 01:17 limits
-r--r--r--    1 root     root             0 Nov 30 01:17 maps
-rw-------    1 root     root             0 Nov 30 01:17 mem
-r--r--r--    1 root     root             0 Nov 30 01:17 mountinfo
-r--r--r--    1 root     root             0 Nov 30 01:17 mounts
-r--------    1 root     root             0 Nov 30 01:17 mountstats
dr-xr-xr-x    5 root     root             0 Nov 30 01:17 net/
-rw-r--r--    1 root     root             0 Nov 30 01:17 oom_adj
-r--r--r--    1 root     root             0 Nov 30 01:17 oom_score
-r--------    1 root     root             0 Nov 30 01:17 pagemap
-r--------    1 root     root             0 Nov 30 01:17 personality
lrwxrwxrwx    1 root     root             0 Nov 30 01:17 root -> //
-rw-r--r--    1 root     root             0 Nov 30 01:17 sched
-r--r--r--    1 root     root             0 Nov 30 01:17 smaps
-r--r--r--    1 root     root             0 Nov 30 01:16 stat
-r--r--r--    1 root     root             0 Nov 30 01:17 statm
-r--r--r--    1 root     root             0 Nov 30 01:17 status
dr-xr-xr-x    3 root     root             0 Nov 30 01:17 task/
-r--r--r--    1 root     root             0 Nov 30 01:17 wchan
cat /proc/mounts
rootfs / rootfs rw 0 0
/dev/root /old_root jffs2 rw 0 0
/dev/mmcblk0p0 / ext2 rw,errors=continue 0 0
none /proc proc rw 0 0
exec chroot . sh <dev/console >dev/console 2>&1
umount /old_root
umount: can't umount /old_root/: Device or resource busy
fuser -m /old_root/
#> 1 # <= issue here

I would like to know if I forgot something (maybe my strategy is simply broken?)

I tried with chroot / switch_root.
I also tried to mount --move /proc and /sys instead of using the SD card rootfs.

Kernel version: 2.6.29.4 ← a rootfs exists so, is it possible to umount the root?
Busybox version: 1.16.1

Best Answer

I believe, you are executing the command umount /old_root still from the old root, and therefore it is busy.

I once did a similar script, and the following worked for me:

#!/bin/sh

 mount -v -n -t proc  -onodev,noexec,nosuid proc  /proc
 mount -v -n -t sysfs -onodev,noexec,nosuid sysfs /sys

 mount -v -t ext4 /dev/sdb1 /mnt/root                           

 mount --move /dev  /mnt/root/dev/                                  
 mount --move /proc /mnt/root/proc/                                 
 mount --move /sys  /mnt/root/sys/                                  

 echo "Switching root filesystem..."
 cd /mnt/root                                               
 pivot_root . mnt/tmp/                                          

 exec chroot . /sbin/init   

then, inside the new root, the first command the new init executes is umount /mnt/tmp/.

Related Question