欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  科技

详解Linux中获取全球唯一标示符UUID的方法

程序员文章站 2022-11-24 11:50:18
这篇文章主要介绍了Linux中获取全球唯一标示符UUID的方法,UUID经常被用于分布式系统中,需要的朋友可以参考下... 16-02-26...

uuid(universally unique identifiers),全球唯一标示符.它是一个标识系统中的存储设备的字符串,使其确定系统中的所有存储设备。

为什么要使用uuid?因为系统自动分配的设备名称并非总是一致的,它们依赖于启动时内核加载模块的顺序.相同的名称可能代表不同的硬盘分区.如果每个分区有一个唯一的uuid值,尤其是在grub中的kernel设置,这样就不会发生分区识别混乱的问题.

下面介绍几种方法来获得uuid.

1.最简单的方法就是使用blkid命令,它主要用来对系统的块设备(包括交换分区)所使用的文件系统类型,label,uuid等信息查询. 使用这个命令需要安装e2fsprogs包.

复制代码
代码如下:

root@10.1.1.200:~# dpkg -l | grep e2fsprogs
ii e2fsprogs 1.41.3-1 ext2/ext3/ext4 file system utilities
ii libuuid-perl 0.02-4 perl extension for using uuid interfaces as defined in e2fsprogs
root@10.1.1.200:~# blkid
/dev/sda1: uuid="b20e80f1-c88d-4918-9d9b-75cd7906629e" type="ext3"
/dev/sda5: type="swap" uuid="1fb3d17b-b2fe-470d-b39c-f00c4a30efbf"
/dev/sda6: uuid="c7050200-7efb-468c-81d1-a3add309bee1" type="ext3" sec_type="ext2"
/dev/sda7: uuid="934e4e22-3431-4707-8d47-dca47e76f448" type="xfs"
/dev/hioa: uuid="de0af117-ad92-4867-aa21-3e7d423e8864" type="xfs"
root@10.1.1.200:~# blkid /dev/sda1
/dev/sda1: uuid="b20e80f1-c88d-4918-9d9b-75cd7906629e" type="ext3"
root@10.1.1.200:~# cat /etc/blkid.tab
<device devno="0x0801" time="1364787338" uuid="b20e80f1-c88d-4918-9d9b-75cd7906629e" type="ext3">/dev/sda1</device>
<device devno="0x0805" time="1364787338" type="swap" uuid="1fb3d17b-b2fe-470d-b39c-f00c4a30efbf">/dev/sda5</device>
<device devno="0x0806" time="1364787338" uuid="c7050200-7efb-468c-81d1-a3add309bee1" type="ext3" sec_type="ext2">/dev/sda6</device>
<device devno="0x0807" time="1364787338" uuid="934e4e22-3431-4707-8d47-dca47e76f448" type="xfs">/dev/sda7</device>
<device devno="0xfd00" time="1364787338" uuid="de0af117-ad92-4867-aa21-3e7d423e8864" type="xfs">/dev/hioa</device>

2.通过浏览/dev/disk/by-uuid/下的设备文件信息.

复制代码
代码如下:

root@10.1.1.200:~# ls -l /dev/disk/by-uuid/
total 0
lrwxrwxrwx 1 root root 10 2013-03-19 11:01 1fb3d17b-b2fe-470d-b39c-f00c4a30efbf -> ../../sda5
lrwxrwxrwx 1 root root 10 2013-03-19 11:01 934e4e22-3431-4707-8d47-dca47e76f448 -> ../../sda7
lrwxrwxrwx 1 root root 10 2013-03-19 11:01 b20e80f1-c88d-4918-9d9b-75cd7906629e -> ../../sda1
lrwxrwxrwx 1 root root 10 2013-03-19 11:01 c7050200-7efb-468c-81d1-a3add309bee1 -> ../../sda6
lrwxrwxrwx 1 root root 10 2013-03-19 11:02 de0af117-ad92-4867-aa21-3e7d423e8864 -> ../../hioa
root@10.1.1.200:~# ls -l /dev/disk/by-uuid/ | grep sda1 | awk '{print $8}'
b20e80f1-c88d-4918-9d9b-75cd7906629e

3.通过vol_id命令查看

复制代码
代码如下:

root@10.1.1.200:~# vol_id /dev/sda1
id_fs_usage=filesystem
id_fs_type=ext3
id_fs_version=1.0
id_fs_uuid=b20e80f1-c88d-4918-9d9b-75cd7906629e
id_fs_uuid_enc=b20e80f1-c88d-4918-9d9b-75cd7906629e
id_fs_label=
id_fs_label_enc=
id_fs_label_safe=
root@10.1.1.200:~# vol_id /dev/sda1 | grep 'uuid'
id_fs_uuid=b20e80f1-c88d-4918-9d9b-75cd7906629e
id_fs_uuid_enc=b20e80f1-c88d-4918-9d9b-75cd7906629e、

4.使用tune2fs,,它是linux下文件系统调整工具

复制代码
代码如下:

root@10.1.1.200:~# tune2fs -l /dev/sda1
tune2fs 1.41.3 (12-oct-2008)
filesystem volume name: <none>
last mounted on: <not available>
filesystem uuid: b20e80f1-c88d-4918-9d9b-75cd7906629e
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: (none)
filesystem state: clean
errors behavior: continue
filesystem os type: linux
inode count: 1864128
block count: 7442103
reserved block count: 372105
free blocks: 6884819
free inodes: 1712582
first block: 0
root@10.1.1.200:~# tune2fs -l /dev/sda1 | grep 'uuid'
filesystem uuid: b20e80f1-c88d-4918-9d9b-75cd7906629e

这里额外在看下tune2fs一些常用参数:

复制代码
代码如下:

root@192.168.2.80:~# tune2fs --help
tune2fs 1.41.3 (12-oct-2008)
tune2fs: invalid option -- '-'
usage: tune2fs [-e errors_behavior] [-g group]

[-i interval[d|m|w]] 文件系统的检查间隔时间,系统在达到时间间隔,自动检查文件系统.
[-j] [-j journal_options] 转换文件系统
[-l] 显示文件系统参数
[-m reserved_blocks_percent] 设置保留的空间百分比
[-o [^]mount_options[,...]] 设置默认加载参数.
[-c max_mounts_count] 表示文件系统在mount次数达到设定后,需要运行fsck检查文件系统。
 
通常如果使用ext3文件系统的话,使用-c 0关掉mount次数达到后的文件系统检查。
禁止强制的文件系统检查:

复制代码
代码如下:

root@10.1.1.200:~# tune2fs -i0 -c0 /dev/sda1
tune2fs 1.41.3 (12-oct-2008)
setting maximal mount count to -1
setting interval between checks to 0 seconds

还有一个dump2fs也是文件系统调整的工具.

dump2fs显示当前的磁盘状态:

复制代码
代码如下:

root@10.1.1.200:~# dumpe2fs /dev/sda1
filesystem volume name: <none>
last mounted on: <not available>
filesystem uuid: b20e80f1-c88d-4918-9d9b-75cd7906629e
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: (none)
filesystem state: clean
errors behavior: continue
filesystem os type: linux
inode count: 1864128
block count: 7442103
reserved block count: 372105
free blocks: 6884819
free inodes: 1712582
first block: 0
block size: 4096
fragment size: 4096
reserved gdt blocks: 1022
blocks per group: 32768
fragments per group: 32768
inodes per group: 8176
inode blocks per group: 511
filesystem created: fri nov 16 15:36:59 2012
last mount time: tue mar 19 11:02:58 2013
last write time: wed mar 27 14:09:25 2013
mount count: 17
maximum mount count: -1
last checked: fri nov 16 15:36:59 2012
check interval: 0 (<none>)
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
first orphan inode: 1618875
default directory hash: half_md4
directory hash seed: 3090ccf1-625c-4934-9ec4-be3f74767f98
journal backup: inode blocks
journal size: 128m

 
复制代码
代码如下:

group 0: (blocks 0-32767)
primary superblock at 0, group descriptors at 1-2
reserved gdt blocks at 3-1024
block bitmap at 1025 (+1025), inode bitmap at 1026 (+1026)
inode table at 1027-1537 (+1027)
0 free blocks, 8152 free inodes, 2 directories
free blocks:
free inodes: 22, 25, 27-8176
group 1: (blocks 32768-65535)
backup superblock at 32768, group descriptors at 32769-32770
reserved gdt blocks at 32771-33792
block bitmap at 33793 (+1025), inode bitmap at 33794 (+1026)
inode table at 33795-34305 (+1027)
3 free blocks, 8176 free inodes, 0 directories
free blocks: 34309-34311
free inodes: 8177-16352
group 2: (blocks 65536-98303)
block bitmap at 65536 (+0), inode bitmap at 65537 (+1)
inode table at 65538-66048 (+2)
7 free blocks, 8176 free inodes, 0 directories
free blocks: 66049-66055
free inodes: 16353-24528
group 3: (blocks 98304-131071)
backup superblock at 98304, group descriptors at 98305-98306
reserved gdt blocks at 98307-99328
block bitmap at 99329 (+1025), inode bitmap at 99330 (+1026)
inode table at 99331-99841 (+1027)
6 free blocks, 8176 free inodes, 0 directories
free blocks: 99842-99847
free inodes: 24529-32704
group 4: (blocks 131072-163839)
block bitmap at 131072 (+0), inode bitmap at 131073 (+1)
inode table at 131074-131584 (+2)
63 free blocks, 0 free inodes, 0 directories
free blocks: 132074-132079, 133643-133647, 135671, 137739-137743, 139811-139815, 141814-141815, 143879, 145922-145927, 147933-147935, 150022-150023, 15207
1, 154107-154111, 156169-156175, 158202-158207, 160251-160255, 162293-162295
free inodes:
group 5: (blocks 163840-196607)
backup superblock at 163840, group descriptors at 163841-163842
reserved gdt blocks at 163843-164864
block bitmap at 164865 (+1025), inode bitmap at 164866 (+1026)
inode table at 164867-165377 (+1027)
0 free blocks, 8130 free inodes, 0 directories
free blocks:
free inodes: 40881, 40928-49056