Linux下磁盘顺序混乱导致数据盘无法正常挂载
在Linux系统中,若存在多块硬盘,通常内核分配盘符的顺序是/dev/sda、/dev/sdb、/dev/sdc ......
在系统启动过程中,内核会按照扫描到硬盘的顺序分配盘符。
在系统启动后,热插拔硬盘硬盘,系统会顺序分配盘符。在同一个硬盘槽位,每次插入硬盘后,在系统中的盘符都可能不一致。第一次插入时,盘符可能为/dev/sdb,将硬盘拔除后,再次插入硬盘,盘符可能变为/dev/sde,就会出现盘符漂移。
NFS:~# fdisk -l
Disk /dev/sdc: 1024 GB, 1099511627776 bytes, 2147483648 sectors
133674 cylinders, 255 heads, 63 sectors/track
Units: sectors of 1 * 512 = 512 bytes
Device Boot StartCHS EndCHS StartLBA EndLBA Sectors Size Id Type
/dev/sdc1 0,1,1 1023,254,63 63 2147483647 2147483585 1023G 83 Linux
Disk /dev/sdb: 10 GB, 10737418240 bytes, 20971520 sectors
1305 cylinders, 255 heads, 63 sectors/track
Units: sectors of 1 * 512 = 512 bytes
Device Boot StartCHS EndCHS StartLBA EndLBA Sectors Size Id Type
/dev/sdb1 * 0,32,33 12,223,19 2048 206847 204800 100M 83 Linux
/dev/sdb2 12,223,20 339,58,39 206848 5449727 5242880 2560M 82 Linux swap
/dev/sdb3 339,58,40 281,106,17 5449728 20971519 15521792 7579M 83 Linux
Disk /dev/sda: 1024 GB, 1099511627776 bytes, 2147483648 sectors
133674 cylinders, 255 heads, 63 sectors/track
Units: sectors of 1 * 512 = 512 bytes
Device Boot StartCHS EndCHS StartLBA EndLBA Sectors Size Id Type
/dev/sda1 0,1,1 1023,254,63 63 2147483647 2147483585 1023G 83 Linux
通过fdisk
命令可以看到系统有三块磁盘分别是sda、sdb、sdc,正常是10GB的sda作为系统盘。现在sda分配给了1TB的数据盘,系统盘变为了sdb。导致原先1TB的数据盘无法正常挂载
解决
通过在fstab里按UUID来挂载磁盘,UUID对于磁盘来说是不变的,不论其盘符、标签是否变化;但使用UUID的缺陷在于灵活性不足,不利于大批量部署。
查看uuid
NFS:~# ls -al /dev/disk/by-uuid/
total 0
drwxrwxrwx 2 root root 160 Aug 18 17:20 .
drwxrwxrwx 5 root root 100 Aug 18 17:20 ..
lrwxrwxrwx 1 root root 9 Aug 18 17:20 28614fa3-2257-4089-8263-a492239b3cf8 -> ../../sda
lrwxrwxrwx 1 root root 10 Aug 18 17:20 36b278eb-e74c-4c6c-b0fa-3299d9b02e56 -> ../../sdb2
lrwxrwxrwx 1 root root 10 Aug 18 17:20 af5fa781-c9a6-476f-be4c-929d18446bdb -> ../../sdb1
lrwxrwxrwx 1 root root 10 Aug 18 17:20 d54255cf-db54-44aa-b46a-4720c7a215e6 -> ../../sda1
lrwxrwxrwx 1 root root 10 Aug 18 17:20 db97c791-9af7-4eaa-b800-a6eabce42931 -> ../../sdb3
lrwxrwxrwx 1 root root 10 Aug 18 17:20 dda3f621-4ed3-4431-93f3-cf4aa4cb8f4a -> ../../sdc1
通过查看UUID可以看到,sda1(原sdb1)的UUID为d54255cf-db54-44aa-b46a-4720c7a215e6
编辑/etc/fstab
,修改为通过UUID进行挂载
UUID=db97c791-9af7-4eaa-b800-a6eabce42931 / ext4 rw,relatime 0 1
UUID=af5fa781-c9a6-476f-be4c-929d18446bdb /boot ext4 rw,relatime 0 2
UUID=36b278eb-e74c-4c6c-b0fa-3299d9b02e56 swap swap defaults 0 0
/dev/cdrom /media/cdrom iso9660 noauto,ro 0 0
/dev/usbdisk /media/usb vfat noauto 0 0
UUID=d54255cf-db54-44aa-b46a-4720c7a215e6 /mnt/data ext4 defaults 0 0
Comments | NOTHING