是否有一个简单的命令将磁盘的设备节点作为输入,并告诉我该磁盘的安装位置(以及是否安装)?是否可以自己获取挂载点,以便我可以将其传递给另一个命令?
我正在使用最少安装的 Debian Squeeze 实时系统(如果需要,我可以安装额外的软件包)。
Sté*_*las 33
在 Linux 上,您现在可以使用以下findmnt命令util-linux(自 2.18 版起):
$ findmnt -S /dev/VG_SC/home
TARGET SOURCE FSTYPE OPTIONS
/home /dev/mapper/VG_SC-home ext4 rw,relatime,errors=remount-ro,data=ordered
Run Code Online (Sandbox Code Playgroud)
或lsblk(也来自util-linux,自 2.19 起):
$ lsblk /dev/VG_SC/home
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
VG_SC-home 254:2 0 200G 0 lvm /home
Run Code Online (Sandbox Code Playgroud)
这对于查找特定设备(磁盘或分区...)下安装的所有文件系统也很有用:
$ lsblk /dev/sda2
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda2 8:2 0 59.5G 0 part
??linux-debian64 (dm-1) 252:1 0 15G 0 lvm
??linux-mint (dm-2) 252:2 0 15G 0 lvm /
Run Code Online (Sandbox Code Playgroud)
仅获取挂载点:
$ findmnt -nr -o target -S /dev/storage/home
/home
$ lsblk -o MOUNTPOINT -nr /dev/storage/home
/home
Run Code Online (Sandbox Code Playgroud)
findmnt如果设备未安装,则上面会返回失败退出状态,而不是lsblk.
所以:
if mountpoint=$(findmnt -nr -o target -S "$device"); then
printf '"%s" is mounted on "%s"\n' "$device" "$mountpoint"
else
printf '"%s" does not appear to be directly mounted\n' "$device"
fi
Run Code Online (Sandbox Code Playgroud)
Gil*_*il' 17
在Linux下,可以直接从内核中获取挂载点信息/proc/mounts。该mount程序将类似的信息记录在/etc/mtab. 路径和选项可能不同,/etc/mtab代表mount传递给内核的内容,而/proc/mounts显示内核内部的数据。/proc/mounts始终是最新的,而/etc/mtab如果/etc在引导脚本不期望的某个时间点是只读的,则可能不是。格式类似于/etc/fstab.
在这两个文件中,第一个空格分隔的字段包含设备路径,第二个字段包含安装点。
awk -v needle="$device_path" '$1==needle {print $2}' /proc/mounts
Run Code Online (Sandbox Code Playgroud)
或者如果你没有 awk:
grep "^$device_path " /proc/mounts | cut -d ' ' -f 2
Run Code Online (Sandbox Code Playgroud)
在许多边缘情况下,您可能无法获得预期的结果。如果设备是通过/dev指定相同设备的不同路径安装的,您不会以这种方式注意到它。在 中/proc/mounts,绑定安装与原始安装没有区别。如果一个挂载点遮蔽另一个挂载点,则可能会有多个匹配项(这是不寻常的)。
在/proc/selfor 中/proc/$pid,有一个mounts模拟全局文件的每个进程文件。挂载信息可能因进程而异,例如由于chroot. 还有一个名为的附加文件mountinfo,它具有不同的格式并包含更多信息,特别是设备主要和次要编号。从文档:
36 35 98:0 /mnt1 /mnt2 rw,noatime master:1 - ext3 /dev/root rw,errors=continue
(1)(2)(3) (4) (5) (6) (7) (8) (9) (10) (11)
(1) mount ID: unique identifier of the mount (may be reused after umount)
(2) parent ID: ID of parent (or of self for the top of the mount tree)
(3) major:minor: value of st_dev for files on filesystem
(4) root: root of the mount within the filesystem
(5) mount point: mount point relative to the process's root
(6) mount options: per mount options
(7) optional fields: zero or more fields of the form "tag[:value]"
(8) separator: marks the end of the optional fields
(9) filesystem type: name of filesystem of the form "type[.subtype]"
(10) mount source: filesystem specific information or "none"
(11) super options: per super block options
Run Code Online (Sandbox Code Playgroud)
因此,如果您要按编号查找设备,您可以这样做:
awk -v dev="$major:minor" '$3==dev {print $5}'
awk -v dev="$(stat -L -c %t:%T /dev/block/something)" '$3==dev {print $5}'
Run Code Online (Sandbox Code Playgroud)
mount不带参数的命令将列出所有当前挂载的文件系统;你可以grep为你想要的磁盘(或者grep /etc/mtab,这是mount从中读取信息的文件):
$ grep /dev/sda /etc/mtab
/dev/sda3 /boot ext2 rw,noatime 0 0
Run Code Online (Sandbox Code Playgroud)