5

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

  • 1
    What's the output of ls -l /proc/1/ after the pivot_root? (My guess is that it's not your init process doing the pivoting ...) – frostschutz Aug 29 '16 at 12:40
  • @frostschutz Indeed... ls: /proc/1: No such file or directory – Zermingore Aug 29 '16 at 12:59
  • 1
    You do have to mount /proc for that obviously... anyway if your init won't free up the / then you won't be able to umount it. Other possibilities are loop/md/dm devices... – frostschutz Aug 29 '16 at 14:00
  • @frostschutz I tried to mount /proc but I am not so sure about the mount options (--move would make sense to me but I still cannot unmount after the pivot_root). Is there any other mount point I should mount (I tried with /proc and /sys) – Zermingore Aug 29 '16 at 14:09
  • 1
    mount -t proc none /proc. if there are other mounts in the old root (check /proc/mounts) you have to umount those first too ... there is not too much point in moving proc sys ... and it's just --move old new, no need for -t... – frostschutz Aug 29 '16 at 19:27
5

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/.

  • Actually, I think my problem is related to the /dev/root entry in /proc/mounts I cannot manage to umount / mount --move. (the rootfs is not umontable as far as I know but I'm not sure if it's an issue or not) – Zermingore Aug 31 '16 at 7:18
0

Actually, my issue was easy to solve, I just needed to relaunch init running init u

Pitfalls:

  • Concerning the unmount of the old partition, we don't care about the rootfs neither about the /dev/root

  • once I chrooted, I didn't mount a /proc and consequently, fuser -m /old_root gave no output

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.