[arch-projects] [PATCH 1/2] rc.shutdown: reorder a bit
The new ordering is now: 1) book-keeping 2) shutting down and killing processes 3) swapoff 4) unmounting and tearing down of block devices The reason to move 1) to the top is so it will not interfere with the killing of processes. The reason for not moving 3) to the top as well is that we want to minimize the amount of stuff that has to be moved from swap to ram. However, swapoff must happen before 4) as we don't know what is under the swap device/file. Added a TODO: unmount any devices backed by swap (tmpfs/ramfs) to make sure that their contents is thrown away rather than moved to ram. In principle we might get OOM on shutdown due to the current situation, but in most cases the only problem should be that shutdown is unecessarily slow. Thanks to Dan and Dave for prompting me to look into this. Cc: Dan McGee <dpmcgee@gmail.com> Cc: Dave Reisner <d@falconindy.com> Signed-off-by: Tom Gundersen <teg@jklm.no> --- rc.shutdown | 34 ++++++++++++++++++++-------------- 1 files changed, 20 insertions(+), 14 deletions(-) diff --git a/rc.shutdown b/rc.shutdown index 5928b2d..1946068 100755 --- a/rc.shutdown +++ b/rc.shutdown @@ -19,20 +19,6 @@ run_hook shutdown_start stop_all_daemons -status "Shutting down UDev" udevadm control --exit - -status "Deactivating Swap" swapoff -a - -# stop monitoring of lvm2 groups before unmounting filesystems -[[ $USELVM = [Yy][Ee][Ss] && -x $(type -P lvm) ]] && - status "Deactivating monitoring of LVM2 groups" vgchange --monitor n - -run_hook shutdown_prekillall - -kill_all - -run_hook shutdown_postkillall - stat_busy "Saving Random Seed" RANDOM_SEED=/var/lib/misc/random-seed install -TDm 0600 /dev/null $RANDOM_SEED @@ -51,6 +37,26 @@ stat_done # Write to wtmp file before unmounting halt -w +# stop monitoring of lvm2 groups before unmounting filesystems +[[ $USELVM = [Yy][Ee][Ss] && -x $(type -P lvm) ]] && + status "Deactivating monitoring of LVM2 groups" vgchange --monitor n + +# any future uevents can and should be ignored +status "Shutting down UDev" udevadm control --exit + +run_hook shutdown_prekillall + +kill_all + +run_hook shutdown_postkillall + +# TODO: unmount any non-api partitions that are backed by swap, we don't want to +# move their contents into memory (waste of time and might caues OOM). + +# almost everything is dead now, so the swap should hopefully be relatively +# empty, and quick to switch off +status "Deactivating Swap" swapoff -a + # if we don't have devtmpfs support, /dev is mounted as tmpfs, so don't unmount it status "Unmounting Filesystems" \ umount -a -r -t nodevtmpfs,notmpfs,nosysfs,noproc,nodevpts -O no_netdev -- 1.7.7.1
Factor out umounting. Explicitly skip umounting API directories, rather than matching on fs type. This allows us to e.g. unmount all tmpfs but leaving /run and /dev/shm alone. Signed-off-by: Tom Gundersen <teg@jklm.no> --- functions | 6 ++++++ rc.shutdown | 9 ++++----- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/functions b/functions index 05d8f56..47146ea 100644 --- a/functions +++ b/functions @@ -460,6 +460,12 @@ mount_all() { mount -a -t "nosysfs,no${NETFS//,/,no}" -O no_netdev } +umount_all() { + # $1: restrict to fstype + umount `mount | grep -e "^\(${1}\)" | awk '{ print $3 }' | grep -v -e "^/\(proc\|sys\|dev\|run\)" | sort -r` \ + -O no_netdev -r +} + remove_leftover() { stat_busy "Removing Leftover Files" # handle this separately until we declare the non-symlinks obsoleted diff --git a/rc.shutdown b/rc.shutdown index 1946068..c042948 100755 --- a/rc.shutdown +++ b/rc.shutdown @@ -50,16 +50,15 @@ kill_all run_hook shutdown_postkillall -# TODO: unmount any non-api partitions that are backed by swap, we don't want to -# move their contents into memory (waste of time and might caues OOM). +# unmount any non-api partitions that are backed by swap, we don't want to +# move their contents into memory (waste of time and might caues OOM). +status "Unmounting Swap-backed Filesystems" umount_all "tmpfs\|ramfs" # almost everything is dead now, so the swap should hopefully be relatively # empty, and quick to switch off status "Deactivating Swap" swapoff -a -# if we don't have devtmpfs support, /dev is mounted as tmpfs, so don't unmount it -status "Unmounting Filesystems" \ - umount -a -r -t nodevtmpfs,notmpfs,nosysfs,noproc,nodevpts -O no_netdev +status "Unmounting Non-API Filesystems" umount_all run_hook shutdown_postumount -- 1.7.7.1
On Sun, Oct 30, 2011 at 12:00:09AM +0200, Tom Gundersen wrote:
Factor out umounting. Explicitly skip umounting API directories, rather than matching on fs type. This allows us to e.g. unmount all tmpfs but leaving /run and /dev/shm alone.
Signed-off-by: Tom Gundersen <teg@jklm.no> --- functions | 6 ++++++ rc.shutdown | 9 ++++----- 2 files changed, 10 insertions(+), 5 deletions(-)
diff --git a/functions b/functions index 05d8f56..47146ea 100644 --- a/functions +++ b/functions @@ -460,6 +460,12 @@ mount_all() { mount -a -t "nosysfs,no${NETFS//,/,no}" -O no_netdev }
+umount_all() { + # $1: restrict to fstype + umount `mount | grep -e "^\(${1}\)" | awk '{ print $3 }' | grep -v -e "^/\(proc\|sys\|dev\|run\)" | sort -r` \ + -O no_netdev -r +} + remove_leftover() { stat_busy "Removing Leftover Files" # handle this separately until we declare the non-symlinks obsoleted diff --git a/rc.shutdown b/rc.shutdown index 1946068..c042948 100755 --- a/rc.shutdown +++ b/rc.shutdown @@ -50,16 +50,15 @@ kill_all
run_hook shutdown_postkillall
-# TODO: unmount any non-api partitions that are backed by swap, we don't want to -# move their contents into memory (waste of time and might caues OOM). +# unmount any non-api partitions that are backed by swap, we don't want to +# move their contents into memory (waste of time and might caues OOM). +status "Unmounting Swap-backed Filesystems" umount_all "tmpfs\|ramfs"
# almost everything is dead now, so the swap should hopefully be relatively # empty, and quick to switch off status "Deactivating Swap" swapoff -a
-# if we don't have devtmpfs support, /dev is mounted as tmpfs, so don't unmount it -status "Unmounting Filesystems" \ - umount -a -r -t nodevtmpfs,notmpfs,nosysfs,noproc,nodevpts -O no_netdev +status "Unmounting Non-API Filesystems" umount_all
run_hook shutdown_postumount
-- 1.7.7.1
So, as we discussed, I does not like your umount_all. It'll be really nice when umount can do this on its own, but until then, something such as the following should suffice: umount_all() { while read -r fstype options target; do # match only targetted fstypes if [[ $1 && $1 != "$fstype" ]]; then continue fi # don't unmount API filesystems if [[ $target = /@(proc|sys|run|dev)?(/*) ]]; then continue fi # avoid networked devices IFS=, read -ra opts <<< "$options" if in_array _netdev "${opts[@]}"; then continue fi mounts+=("$target") done < <(findmnt -runRo FSTYPE,OPTIONS,TARGET / | tac) } d
On Sat, Oct 29, 2011 at 8:54 PM, Dave Reisner <d@falconindy.com> wrote:
So, as we discussed, I does not like your umount_all. It'll be really nice when umount can do this on its own, but until then, something such as the following should suffice:
umount_all() { while read -r fstype options target; do # match only targetted fstypes if [[ $1 && $1 != "$fstype" ]]; then continue fi
# don't unmount API filesystems if [[ $target = /@(proc|sys|run|dev)?(/*) ]]; then continue fi
# avoid networked devices IFS=, read -ra opts <<< "$options" if in_array _netdev "${opts[@]}"; then continue fi
mounts+=("$target") done < <(findmnt -runRo FSTYPE,OPTIONS,TARGET / | tac) }
hey, i only recently joined/realized i could post to this list ... if there is an established etiquette/procedure that i'm breaking (now or ever), just lmk. ... btw, you should be able to eliminate the call to `tac` via `findmnt -d backward [...]`. however, i wrote a simple routine semi-recently for a project that may be useful -- it reads `/proc/self/mountinfo` directly (like findmnt), but it also makes use of `tsort` to automatically order the umounts properly (findmnt doesn't seem to do this) -- it can handle any arbitrary depth, original mount order, `--bind`, and `--move` (the latter will unsort the mountinfo table): ------------------------------------------------------------------- # cat umountall.sh #!/bin/bash IFS=; mnts=$(sort -k1n /proc/self/mountinfo | tr -s ' ' '\t'); topo=$(echo ${mnts} | cut -f1,2 | tsort | nl -w1 | sort -k2n); join -o1.1,2.5 -t$'\t' -12 -21 - <(echo ${mnts}) <<<${topo} | sort -k1n; # ./umountall.sh 1 /run 2 /dev/shm 3 /dev/pts 4 /sys/fs/cgroup/systemd 5 /sys/fs/cgroup/cpuset 6 /sys/fs/cgroup/cpu,cpuacct 7 /sys/fs/cgroup/memory 8 /sys/fs/cgroup/devices 9 /sys/fs/cgroup/freezer 10 /sys/fs/cgroup/net_cls 11 /sys/fs/cgroup/blkio 12 /proc/sys/fs/binfmt_misc 13 /var/run 14 /var/lock 15 /dev/mqueue 16 /dev/hugepages 17 /sys/kernel/security 18 /sys/kernel/debug 19 /media 20 /boot 21 /home/anthony/.gvfs 22 /sys/fs/fuse/connections 23 /sys/fs/cgroup 24 /proc 25 /dev 26 /boot 27 /sys 28 / ------------------------------------------------------------------- execution shows you [a] proper umount order -- not the best example because my system is not that interesting, but it really should work for *anything* so long as the kernel doesn't lie. more fields added by modifying the `join` command, eg. `join -o1.1,2.5,2.6` additionally outputs the mount options. pathnames are octal(?) encoded, thus easy to handle, but need to be expanded before actual umount (`echo -e` or `printf`[?] or ???) all binaries are in `coreutils` package there was mention of a shudown hook after a pivot_root recently -- this should work well for that. if there is a simpler way to accomplish this, i'd love to here that as well. this isn't my original but it's tested and functions the same. the original just did a little more like unmount everything under a specific path, or filter stuff out (eg. /proc, /dev, ...) by using the unique mount id; can provide all that if interested. -- C Anthony
On Mon, Oct 31, 2011 at 6:22 AM, C Anthony Risinger <anthony@xtfx.me> wrote:
i only recently joined/realized i could post to this list ...
Welcome!
if there is an established etiquette/procedure that i'm breaking (now or ever), just lmk.
Trust me, you will be told ;-)
... btw, you should be able to eliminate the call to `tac` via `findmnt -d backward [...]`.
Thanks! I added this to the newest version.
however, i wrote a simple routine semi-recently for a project that may be useful -- it reads `/proc/self/mountinfo` directly (like findmnt), but it also makes use of `tsort` to automatically order the umounts properly (findmnt doesn't seem to do this)
From what I could see findmnt does this if you put TARGET as your first column (but I couldn't find this in the docs, so maybe I was just lucky). If it does not, we should consider asking upstream to add this as it seems like a very useful feature. I'd rather avoid home-made solutions if we can...
there was mention of a shudown hook after a pivot_root recently -- this should work well for that. if there is a simpler way to accomplish this, i'd love to here that as well.
We have a simple/hacky implementation of umount_all for the shutdownramfs (using only busybox features). I think we should consider moving this to using findmnt as well though...
this isn't my original
Just as a general remark: I guess we should try to avoid copying code that doesn't come with a license. Cheers, Tom
On Mon, Oct 31, 2011 at 5:38 AM, Tom Gundersen <teg@jklm.no> wrote:
On Mon, Oct 31, 2011 at 6:22 AM, C Anthony Risinger <anthony@xtfx.me> wrote:
it's a bit ... delayed, but shouldn't matter. i've had this message flagged for 2 weeks and just never wrote a response for some reason ...
From what I could see findmnt does this if you put TARGET as your first column (but I couldn't find this in the docs, so maybe I was just lucky). If it does not, we should consider asking upstream to add this as it seems like a very useful feature. I'd rather avoid home-made solutions if we can...
i'm not sure either ... IIRC i tested after doing some --binds and --moves and it *didn't* do it correctly, but i'd have to double check. generally the mountinfo table is in the correct order (mount order, usually works), but in the presence of --move/--bind's this doesn't always hold. ... probably won't affect -- my use case was far from normal.
there was mention of a shudown hook after a pivot_root recently -- this should work well for that. if there is a simpler way to accomplish this, i'd love to here that as well.
We have a simple/hacky implementation of umount_all for the shutdownramfs (using only busybox features). I think we should consider moving this to using findmnt as well though...
has the shutdown hook already landed? my systemd machines always complain about not being able to free the / device (md+lvm) ... unrelated though.
this isn't my original
Just as a general remark: I guess we should try to avoid copying code that doesn't come with a license.
ok, i actually responded specifically for this line ;-) what do you mean exactly? copying from your perspective or mine? i wrote all of it on my my own time ... should i send license info along with any code/patches sent to this list? i just want to make sure i understand, thats all ... unless otherwise noted, anything posted is hereby Apache2, MIT, BSD, or public domain at will ... i own it, and so do you :-) -- C Anthony
Factor out umounting. Explicitly skip umounting API directories, rather than matching on fs type. This allows us to e.g. unmount all tmpfs but leaving /run and /dev/shm alone. v2: new umount_all implementation based on proposal by Dave and input from Anthony. Also, ignore ramfs as it is not backed by swap. Cc: Dave Reisner <d@falconindy.com> Cc: C Anthony Risinger <anthony@xtfx.me> Signed-off-by: Tom Gundersen <teg@jklm.no> --- functions | 30 ++++++++++++++++++++++++++++++ rc.shutdown | 9 ++++----- 2 files changed, 34 insertions(+), 5 deletions(-) diff --git a/functions b/functions index 05d8f56..a7d8204 100644 --- a/functions +++ b/functions @@ -460,6 +460,36 @@ mount_all() { mount -a -t "nosysfs,no${NETFS//,/,no}" -O no_netdev } +umount_all() { + # $1: restrict to fstype + + local mounts + + while read -r target fstype options; do + + # match only targetted fstypes + if [[ $1 && $1 != "$fstype" ]]; then + continue + fi + + # don't unmount API filesystems + if [[ $target = /@(proc|sys|run|dev)?(/*) ]]; then + continue + fi + + # avoid networked devices + IFS=, read -ra opts <<< "$options" + if in_array _netdev "${opts[@]}"; then + continue + fi + + mounts+=("$target") + done < <(findmnt -runRo TARGET,FSTYPE,OPTIONS -d backward /) + + umount -r $mounts + +} + remove_leftover() { stat_busy "Removing Leftover Files" # handle this separately until we declare the non-symlinks obsoleted diff --git a/rc.shutdown b/rc.shutdown index 1946068..86ff799 100755 --- a/rc.shutdown +++ b/rc.shutdown @@ -50,16 +50,15 @@ kill_all run_hook shutdown_postkillall -# TODO: unmount any non-api partitions that are backed by swap, we don't want to -# move their contents into memory (waste of time and might caues OOM). +# unmount any non-api partitions that are backed by swap, we don't want to +# move their contents into memory (waste of time and might caues OOM). +status "Unmounting Swap-backed Filesystems" umount_all "tmpfs" # almost everything is dead now, so the swap should hopefully be relatively # empty, and quick to switch off status "Deactivating Swap" swapoff -a -# if we don't have devtmpfs support, /dev is mounted as tmpfs, so don't unmount it -status "Unmounting Filesystems" \ - umount -a -r -t nodevtmpfs,notmpfs,nosysfs,noproc,nodevpts -O no_netdev +status "Unmounting Non-API Filesystems" umount_all run_hook shutdown_postumount -- 1.7.7.1
participants (3)
-
C Anthony Risinger
-
Dave Reisner
-
Tom Gundersen