综合比较可知, blkid 命令是最可靠的获取方式。
root@ubuntu:~# blkid
/dev/sda1: UUID="05c72939-7771-405b-a417-3d2e9e34c3b7" TYPE="ext4"
/dev/sda5: UUID="7c9bd388-581b-49f3-996f-8d4c26932739" TYPE="swap"
/dev/sdb1: LABEL="BOOT" UUID="68AB-85A4" TYPE="vfat"
/dev/sdb2: LABEL="ROOTFS" UUID="2c884457-40a0-4d3e-9681-e866bcec34b2" TYPE="ext3"
root@ubuntu:~#
root@ubuntu:~# e2label /dev/sdb2
ROOTFS
root@ubuntu:~# e2label /dev/sdb1
e2label: Bad magic number in super-block 当尝试打开 /dev/sdb1 时
找不到有效的文件系统超级块.
root@ubuntu:~#
root@ubuntu:~# tune2fs -l /dev/sdb2
tune2fs 1.41.11 (14-Mar-2010)
Filesystem volume name: ROOTFS
Last mounted on: /media/video/ROOTFS
Filesystem UUID: 2c884457-40a0-4d3e-9681-e866bcec34b2
Filesystem magic number: 0xEF53
Filesystem revision #: 1 (dynamic)
Filesystem features: has_journal ext_attr resize_inode dir_index filetype needs_recovery sparse_super large_file
Filesystem flags: signed_directory_hash
Default mount options: user_xattr acl
Filesystem state: clean
Errors behavior: Continue
Filesystem OS type: Linux
Inode count: 435024
Block count: 1736704
Reserved block count: 86834
Free blocks: 1458029
Free inodes: 418691
First block: 0
Block size: 4096
Fragment size: 4096
Reserved GDT blocks: 423
Blocks per group: 32768
Fragments per group: 32768
Inodes per group: 8208
Inode blocks per group: 513
Filesystem created: Thu Mar 23 18:42:23 2017
Last mount time: Mon Nov 6 13:51:59 2017
Last write time: Mon Nov 6 13:51:59 2017
Mount count: 62
Maximum mount count: -1
Last checked: Thu Mar 23 18:42:23 2017
Check interval: 0 (<none>)
Lifetime writes: 882 MB
Reserved blocks uid: 0 (user root)
Reserved blocks gid: 0 (group root)
First inode: 11
Inode size: 256
required extra isize: 28
Desired extra isize: 28
Journal inode: 8
Default directory hash: half_md4
Directory Hash Seed: e9e08be3-d7ea-47dd-b9d5-efeb56e744fc
Journal backup: inode blocks
root@ubuntu:~#
配合 awk 或者 cut 命令 提取LABEL
root@ubuntu:~#
root@ubuntu:~# blkid /dev/sdb2 | awk -F " " '{print $2}' | awk -F "\"" '{print $2}'
ROOTFS
root@ubuntu:~# blkid /dev/sdb1 | awk -F " " '{print $2}' | awk -F "\"" '{print $2}'
BOOT
root@ubuntu:~#
root@ubuntu:~# blkid /dev/sdb2 | cut -d " " -f 2
LABEL="ROOTFS"
root@ubuntu:~# blkid /dev/sdb2 | cut -d " " -f 2 | cut -d "\"" -f 2
ROOTFS
root@ubuntu:~# blkid /dev/sdb1 | cut -d " " -f 2 | cut -d "\"" -f 2
BOOT
root@ubuntu:~# blkid /dev/sda1 | cut -d " " -f 2 | cut -d "\"" -f 2
05c72939-7771-405b-a417-3d2e9e34c3b7
root@ubuntu:~# blkid /dev/sdb1 | cut -d "LABEL=" -f 2
cut: 分界符必须是单个字符
请尝试执行"cut --help"来获取更多信息。
root@ubuntu:~# blkid /dev/sdb1 | awk -F "LABEL=" '{print $2}'
"BOOT" UUID="68AB-85A4" TYPE="vfat"
root@ubuntu:~#
root@ubuntu:~# blkid /dev/sdb1 | awk -F "LABEL=" '{print $2}' | awk -F "\"" '{print $2}'
BOOT
root@ubuntu:~# blkid /dev/sda1 | awk -F "LABEL=" '{print $2}' | awk -F "\"" '{print $2}'
root@ubuntu:~#