/etc/init.d/umountfs is run during shutdown to unmount most filesystems. It contains this:PROTECTED_MOUNTS=$(sed -n ':a;/^[^ ]* \(\/\|\/usr\) /!{H;n;ba};{H;s/.*//;x;s/\n//;p}' /proc/mounts)This command apparently selects a subset of the lines in /proc/mounts, which then become exempt from being unmounted later in the script.It matches lines with / or /usr in the second field (the mount point) and then what it does next involves too many exotic sed features for me to figure out what the intent is.It would make sense if this was just selecting the / and /usr lines, since those get unmounted in a different script. But it doesn't do that. When I run it on my actual /proc/mounts, $PROTECTED_MOUNTS ends up containing almost all the lines, including the /home line. It's not good if /home doesn't get unmounted, so that can't be right.What could the writer of that sed command possibly have meant to do, with all that branching and hold space stuff?
What is the sed command in Debian's /etc/init.d/umountfs supposed to do?
sed
I'll give it a shot...This will return every line up to and including the last occurrence of / or /usr. AKA this strips everything after the last occurrence of either of those. :a;/^[^ ]* \(\/\|\/usr\) /!{H;n;ba};This says, for lines that don't contain exactly / or /usr in the second (whitespace delimited) column, append to Hold space, fetch next line, loop back to label a.{H;s/.*//;x;s/\n//;p}We'll only get here if we encounter a line with / or /usr. Then we append to Hold space (which has everything from the first section), clear pattern space, exchange hold space with pattern space, remove first newline from pattern space and print it.After we've seen the last / or /usr line (and we've printed everything up to that point), we loop through the first part and suck all the remaining non-matching lines up...but they never get printed since we don't enter the second part.I did a bit of testing and that has been consistent with my claim.Clearly this depends on some kind of ordering being maintained in /proc/mounts. But the list is simply built according to the order that the mount points are created. So wherever that is done must be maintained or this stuff will break, I guess. It all seems awfully brittle to me.