Quote:
Originally Posted by musther
I'm just not quite sure what this line is doing:
Code:
if ! /bin/grep '^/dev/root' /proc/mounts | /usr/bin/cut -d' ' -f4 | /bin/grep -v 'rw'; then
Can you explain it please, so that I can learn from and modify it?
|
Basically, the intent of that line of code was to check whether the remount worked. Unfortunately for you, my brain seems to have been dead at the time it was written. That line not only contains an error—the negative (
!) of an inverse grep (
-v)—it could be much better. I have fixed the old post, and will try to explain this new line as much as possible here:
Code:
if /bin/grep '^/dev/root' /proc/mounts | /usr/bin/cut -d' ' -f4 | /bin/grep '^ro'; then
Since
/etc/mtab should not have been written to if it’s on a read-only filesystem, we use properties of the
/proc filesystem instead. In particular,
/proc/mounts keeps a list of mounted filesystems. When that file is read, the entries are formatted as follows:
Code:
mnt_devname mnt_root sb_type[.sb_subtype] r{o,w}[,fs_info1,fs_info2,…] 0 0
Where square brackets signify optional fields and curly braces signify that either one or the other of the comma-separated values appear. For example, you might have this on a running system:
Code:
$ cat /proc/self/mounts
rootfs / rootfs rw 0 0
/dev/root / ext3 rw,noatime 0 0
proc /proc proc rw 0 0
sys /sys sysfs rw 0 0
…
At the head of the mount tree is always
rootfs which is always read-writable.
/dev/root (which is not necessarily an actual device node) is the root filesystem for the process you are considering. If you are in a chroot, it represents the root of that. If you are in an initrd, it represents that root (not
real_root_device). For most intents and purposes (especial for the init process), it is the actual root filesystem.
So what the new line does is this: use
grep to search for a line beginning with
/dev/root in the file
/proc/mounts. Grep will write the entire line containing the match to stdout. This is piped to
cut, which extracts the fourth field delimited by spaces. The fourth field is then passed to
grep, which searches for
ro at the beginning. If a match is found, the exit code of the final
grep indicates success, so the conditional proceeds to the
then statement. Otherwise, the exit code will indicate failure, and the conditional will proceed to the
else statement.