From 9dcc8ca722422b9bfc44fddc2bd735ae8cc73941 Mon Sep 17 00:00:00 2001 From: Bert Van Vreckem Date: Sat, 23 Nov 2024 12:05:39 +0100 Subject: [PATCH] Add section about findmnt, update information about mount info under /proc --- .../storage_mounting/030_mounting_theory.md | 93 ++++++++++++++++--- 1 file changed, 81 insertions(+), 12 deletions(-) diff --git a/modules/storage_mounting/030_mounting_theory.md b/modules/storage_mounting/030_mounting_theory.md index ff1df59..0dc95d2 100644 --- a/modules/storage_mounting/030_mounting_theory.md +++ b/modules/storage_mounting/030_mounting_theory.md @@ -73,24 +73,95 @@ student@linux:~$ mount | grep /dev/sdb /dev/sdb1 on /mnt/project42 type ext2 (rw) ``` -### /proc/mounts +The output of `mount` used to be simple enough, but these days, with all the pseudo filesystems under `/sys`, `/run` and the like, you have to pipe it through `grep` to find useful information through all the noise. -The kernel provides the info in `/proc/mounts` in file form, but `/proc/mounts` does not exist as a file on any hard disk. Looking at `/proc/mounts` is looking at information that comes directly from the kernel. +However, as it turns out, using `mount` to *list* mounted filesystems is *deprecated*. From the man page `mount(8)`: -```console -student@linux:~$ cat /proc/mounts | grep /dev/sdb - /dev/sdb1 /mnt/project42 ext2 rw 0 0 +```text + Listing the mounts + The listing mode is maintained for backward compatibility only. + + For more robust and customizable output use findmnt(8), especially in your scripts. ``` -### /etc/mtab +### findmnt + +The `findmnt` command is way more useful to show mounted filesystems. Even the default output looks better structured than `mount` and there are a lot of options and arguments to customize the output. Try the default output yourself, as it's quite verbose. + +A few examples of useful options: + +- `--real`: Only show "real" filesystems (i.e. not pseudo filesystems): + + ```console + [vagrant@el ~]$ findmnt --real + TARGET SOURCE FSTYPE OPTIONS + / /dev/sda2 xfs rw,relatime,seclabel,attr2,inode64,logbufs=8,logbsize=32k,noquota + ├─/mnt/part3 /dev/sdc5 xfs rw,relatime,seclabel,attr2,inode64,logbufs=8,logbsize=32k,noquota + ├─/mnt/part4 /dev/sdc6 fuseblk rw,relatime,user_id=0,group_id=0,allow_other,blksize=4096 + ├─/mnt/part2 /dev/sdc2 ext4 rw,relatime,seclabel + └─/mnt/part1 /dev/sdc1 ext3 rw,relatime,seclabel + ``` + + Note the tree structure and aligned columns! + +- `--fstab`, `-s`: Show only filesystems listed in `/etc/fstab`: + + ```console + [vagrant@el ~]$ findmnt --fstab + TARGET SOURCE FSTYPE OPTIONS + / UUID=303db791-9236-4ac4-a176-e2a033576d89 xfs defaults + none UUID=a4814ebe-c0b2-4819-8129-30f32b3e8772 swap defaults + /mnt/part1 UUID="f89d27bf-94d3-40a4-9fca-2af9cbe94856" ext3 defaults + /mnt/part2 UUID="cb44e6c4-a49e-43e1-b9a8-ed5d272b8efb" ext4 defaults + /mnt/part3 UUID="6b306b44-23fe-4cef-8c3e-423188e5c088" xfs defaults + /mnt/part4 UUID="2856B0906FE06278" ntfs defaults + /vagrant vagrant vboxsf uid=1000,gid=1000,_netdev + ``` -The `/etc/mtab` file is not updated by the kernel, but is maintained by the `mount` command. Do not edit `/etc/mtab` manually. +- `--json`, `-J`: Output in JSON format + +- `--pairs`, `-P`: Output in key-value pairs + + ```console + [vagrant@el ~]$ findmnt --pairs + TARGET="/proc" SOURCE="proc" FSTYPE="proc" OPTIONS="rw,nosuid,nodev,noexec,relatime" + TARGET="/sys" SOURCE="sysfs" FSTYPE="sysfs" OPTIONS="rw,nosuid,nodev,noexec,relatime,seclabel" + [...] + ``` + +- `--df`, `-D`: imitate the output of `df(1)` + + ```console + [vagrant@el ~]$ findmnt --df + SOURCE FSTYPE SIZE USED AVAIL USE% TARGET + devtmpfs devtmpfs 4M 0 4M 0% /dev + tmpfs tmpfs 384.5M 0 384.5M 0% /dev/shm + tmpfs tmpfs 153.8M 4.4M 149.4M 3% /run + /dev/sda2 xfs 61.9G 1.8G 60G 3% / + /dev/sdc5 xfs 4.9G 67.5M 4.9G 1% /mnt/part3 + /dev/sdc6 fuseblk 5G 26.2M 5G 1% /mnt/part4 + /dev/sdc2 ext4 5.8G 24K 5.5G 0% /mnt/part2 + /dev/sdc1 ext3 3.9G 96K 3.7G 0% /mnt/part1 + tmpfs tmpfs 76.9M 0 76.9M 0% /run/user/1000 + ``` + +### Mount info in /proc + +The kernel provides the info through the `/proc` pseudo file system (i.e., `/proc` does not exist as a file on any hard disk but provides a file-like interface to information about the kernel). + +Several files contain relevant information: + +- `/proc/self/mounts`: a list of all mounted file systems, the content is like the output of `mount`. For backwards compatibility, `/etc/mtab` and `/proc/mounts` are symlinks to this file. +- `/proc/self/mountinfo`: a more detailed list of mounted file systems +- `/proc/self/mountstats`: statistics about mounted file systems ```console -student@linux:~$ cat /etc/mtab | grep /dev/sdb +student@linux:~$ cat /proc/mounts | grep /dev/sdb /dev/sdb1 /mnt/project42 ext2 rw 0 0 ``` +However, `findmnt` is a better way to get and present this information. + ### df A more user friendly way to look at mounted file systems is `df`. The `df` (*diskfree*) command has the added benefit of showing you the free space on each mounted disk. Like a lot of Linux commands, `df` supports the `-h` switch to make the output more *human readable*. @@ -136,16 +207,14 @@ student@linux:~$ du -sh /boot /srv/wolf ## from start to finish -Below is a screenshot that show a summary roadmap starting with -detection of the hardware (/dev/sdb) up until mounting on `/mnt`. +Below is a screenshot that show a summary roadmap starting with detection of the hardware (/dev/sdb) up until mounting on `/mnt`. ```console student@linux:~$ sudo dmesg | grep '\[sdb\]' sd 3:0:0:0: [sdb] 150994944 512-byte logical blocks: (77.3 GB/72.0 GiB) sd 3:0:0:0: [sdb] Write Protect is off sd 3:0:0:0: [sdb] Mode Sense: 00 3a 00 00 -sd 3:0:0:0: [sdb] Write cache: enabled, read cache: enabled, doesn't support \ -DPO or FUA +sd 3:0:0:0: [sdb] Write cache: enabled, read cache: enabled, doesn't support DPO or FUA sd 3:0:0:0: [sdb] Attached SCSI disk student@linux:~$ sudo parted /dev/sdb