一、Linux磁盘分区UUID的获取方法

1、# fdisk -l

Disk /dev/sda: 1000.2 GB, 1000204886016 bytes
240 heads, 63 sectors/track, 129201 cylinders
Units = cylinders of 15120 * 512 = 7741440 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x54f054f0

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1        6772    51196288+   7  HPFS/NTFS
/dev/sda2            6773      129200   925555680    f  W95 Ext'd (LBA)
/dev/sda5            6773       33862   204800368+   7  HPFS/NTFS
/dev/sda6           33863       47408   102400000    7  HPFS/NTFS
/dev/sda7           47408       61476   106354688    7  HPFS/NTFS
/dev/sda8           61476       61503      204800   83  Linux
/dev/sda9           61503       75048   102400000   83  Linux
/dev/sda10          75048       88593   102400000   83  Linux
/dev/sda11          88593       91302    20480000   83  Linux
/dev/sda12          91302       92657    10240000   82  Linux swap / Solaris

2、# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda11             20G  1.6G   17G   9% /
tmpfs                 3.9G  260K  3.9G   1% /dev/shm
/dev/sda8             194M   30M  155M  16% /boot
/dev/sda10             97G  188M   92G   1% /opt
/dev/sda9              97G  298M   91G   1% /var

3、# cat /etc/fstab

#
# /etc/fstab
# Created by anaconda on Wed Nov 23 10:11:32 2011
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
UUID=15d68d9f-e1a9-45dd-8823-37b7ecafce47 /                       ext4    defaults        1 1
UUID=575c18d8-4776-4ecf-abd6-1209293d0737 /boot                   ext4    defaults        1 2
UUID=98b4689f-d70e-4903-a8b4-8054e5a968ee /opt                    ext4    defaults        1 2
UUID=a9558fcf-9e8b-448f-8003-212716694bce /var                    ext4    defaults        1 2
UUID=49446740-94ac-46fa-a80d-f072932e9a8a swap                    swap    defaults        0 0
tmpfs                   /dev/shm                tmpfs   defaults        0 0
devpts                  /dev/pts                devpts  gid=5,mode=620  0 0
sysfs                   /sys                    sysfs   defaults        0 0
proc                    /proc                   proc    defaults        0 0

4、我这里要格式化/dev/sda10

# umount /opt

# mkfs.ext4 /dev/sda10

# mount -a 这里报错了,uuid不匹配

# blkid /dev/sda10  得到当前的UUID

# vi /etc/fstab 修改其中/opt那栏的uuid

# mount -a 解决

二、Linux UUID的作用及意义

1、它是真正的唯一标志符

UUID为系统中的存储设备提供唯一的标识字符串,不管这个设备是什么类型的。如果你在系统中添加了新的存储设备如硬盘,很可能会造成一些麻烦,比如说启动的时候因为找不到设备而失败,而使用UUID则不会有这样的问题。

2、设备名并非总是不变的

自动分配的设备名称并非总是一致的,它们依赖于启动时内核加载模块的顺序。如果你在插入了USB盘时启动了系统,而下次启动时又把它拔掉了,就有可能导致设备名分配不一致。

3、rhel6中的许多关键功能现在开始依赖于UUID

例如grub:

# cat /boot/grub/grub.conf
# grub.conf generated by anaconda
#
# Note that you do not have to rerun grub after making changes to this file
# NOTICE:  You have a /boot partition.  This means that
#          all kernel and initrd paths are relative to /boot/, eg.
#          root (hd0,0)
#          kernel /vmlinuz-version ro root=/dev/sdb3
#          initrd /initrd-[generic-]version.img
#boot=/dev/sdb
default=0
timeout=1
splashp_w_picpath=(hd0,0)/grub/splash.xpm.gz
hiddenmenu
title ECData (2.6.32-71.el6.x86_64)
    root (hd0,0)
    kernel /vmlinuz-2.6.32-71.el6.x86_64 ro root=UUID=72e9d119-3bdc-4f06-b84c-0e70a92483c9 rd_NO_LUKS rd_NO_LVM rd_NO_MD rd_NO_DM LANG=en_US.UTF-8 SYSFONT=latarcyrheb-sun16 KEYBOARDTYPE=pc KEYTABLE=us crashkernel=auto rhgb quiet vga=0x317
    initrd /initramfs-2.6.32-71.el6.x86_64.img