有同仁看了上文《Linux添加硬盘扩充已有分区存储空间方式》一文后,提出疑问,现在很多云服务器本来没有逻辑卷,添加数据盘后需要自行添加,如何处理?
此文将以某云服务器为例,详细进行解说。
总体步骤
-
磁盘初始化分区
-
创建物理卷
-
创建卷组
-
创建逻辑卷
-
通知文件系统生效
磁盘初始化分区
1 [root@VM_15_115_centos ~]# df -h 2 文件系统 容量 已用 可用 已用% 挂载点 3 /dev/vda1 50G 1.6G 45G 4% / 4 devtmpfs 7.8G 0 7.8G 0% /dev 5 tmpfs 7.8G 24K 7.8G 1% /dev/shm 6 tmpfs 7.8G 464K 7.8G 1% /run 7 tmpfs 7.8G 0 7.8G 0% /sys/fs/cgroup 8 tmpfs 1.6G 0 1.6G 0% /run/user/0 9 [root@VM_15_115_centos ~]# fdisk -l 10 11 磁盘 /dev/vda:53.7 GB, 53687091200 字节,104857600 个扇区 12 Units = 扇区 of 1 * 512 = 512 bytes 13 扇区大小(逻辑/物理):512 字节 / 512 字节 14 I/O 大小(最小/最佳):512 字节 / 512 字节 15 磁盘标签类型:dos 16 磁盘标识符:0x0009ac89 17 18 设备 Boot Start End Blocks Id System 19 /dev/vda1 * 2048 104857599 52427776 83 Linux 20 21 磁盘 /dev/vdb:536.9 GB, 536870912000 字节,1048576000 个扇区 22 Units = 扇区 of 1 * 512 = 512 bytes 23 扇区大小(逻辑/物理):512 字节 / 512 字节 24 I/O 大小(最小/最佳):512 字节 / 512 字节 25 26 [root@VM_15_115_centos ~]# fdisk /dev/vdb 27 欢迎使用 fdisk (util-linux 2.23.2)。 28 29 更改将停留在内存中,直到您决定将更改写入磁盘。 30 使用写入命令前请三思。 31 32 Device does not contain a recognized partition table 33 使用磁盘标识符 0xae10029f 创建新的 DOS 磁盘标签。 34 35 命令(输入 m 获取帮助):m 36 命令操作 37 a toggle a bootable flag 38 b edit bsd disklabel 39 c toggle the dos compatibility flag 40 d delete a partition 41 g create a new empty GPT partition table 42 G create an IRIX (SGI) partition table 43 l list known partition types 44 m print this menu 45 n add a new partition 46 o create a new empty DOS partition table 47 p print the partition table 48 q quit without saving changes 49 s create a new empty Sun disklabel 50 t change a partition's system id 51 u change display/entry units 52 v verify the partition table 53 w write table to disk and exit 54 x extra functionality (experts only) 55 56 命令(输入 m 获取帮助):g 57 Building a new GPT disklabel (GUID: AB878B71-5712-455A-9C62-3EBA92ECA888) 58 59 60 命令(输入 m 获取帮助):p 61 62 磁盘 /dev/vdb:536.9 GB, 536870912000 字节,1048576000 个扇区 63 Units = 扇区 of 1 * 512 = 512 bytes 64 扇区大小(逻辑/物理):512 字节 / 512 字节 65 I/O 大小(最小/最佳):512 字节 / 512 字节 66 磁盘标签类型:gpt 67 Disk identifier: AB878B71-5712-455A-9C62-3EBA92ECA888 68 69 70 # Start End Size Type Name 71 72 命令(输入 m 获取帮助):m 73 命令操作 74 d delete a partition 75 g create a new empty GPT partition table 76 G create an IRIX (SGI) partition table 77 l list known partition types 78 m print this menu 79 n add a new partition 80 o create a new empty DOS partition table 81 p print the partition table 82 q quit without saving changes 83 s create a new empty Sun disklabel 84 t change a partition's system id 85 v verify the partition table 86 w write table to disk and exit 87 x extra functionality (experts only) 88 89 命令(输入 m 获取帮助):n 90 分区号 (1-128,默认 1): 91 第一个扇区 (2048-1048575966,默认 2048): 92 Last sector, +sectors or +size{K,M,G,T,P} (2048-1048575966,默认 1048575966): 93 已创建分区 1 94 95 96 命令(输入 m 获取帮助):p 97 98 磁盘 /dev/vdb:536.9 GB, 536870912000 字节,1048576000 个扇区 99 Units = 扇区 of 1 * 512 = 512 bytes 100 扇区大小(逻辑/物理):512 字节 / 512 字节 101 I/O 大小(最小/最佳):512 字节 / 512 字节 102 磁盘标签类型:gpt 103 Disk identifier: AB878B71-5712-455A-9C62-3EBA92ECA888 104 105 106 # Start End Size Type Name 107 1 2048 1048575966 500G Linux filesyste 108 109 命令(输入 m 获取帮助):w 110 The partition table has been altered! 111 112 Calling ioctl() to re-read partition table. 113 正在同步磁盘。
执行结果
1 [root@VM_15_115_centos ~]# fdisk -l 2 3 磁盘 /dev/vda:53.7 GB, 53687091200 字节,104857600 个扇区 4 Units = 扇区 of 1 * 512 = 512 bytes 5 扇区大小(逻辑/物理):512 字节 / 512 字节 6 I/O 大小(最小/最佳):512 字节 / 512 字节 7 磁盘标签类型:dos 8 磁盘标识符:0x0009ac89 9 10 设备 Boot Start End Blocks Id System 11 /dev/vda1 * 2048 104857599 52427776 83 Linux 12 WARNING: fdisk GPT support is currently new, and therefore in an experimental phase. Use at your own discretion. 13 14 磁盘 /dev/vdb:536.9 GB, 536870912000 字节,1048576000 个扇区 15 Units = 扇区 of 1 * 512 = 512 bytes 16 扇区大小(逻辑/物理):512 字节 / 512 字节 17 I/O 大小(最小/最佳):512 字节 / 512 字节 18 磁盘标签类型:gpt 19 Disk identifier: AB878B71-5712-455A-9C62-3EBA92ECA888 20 21 22 # Start End Size Type Name 23 1 2048 1048575966 500G Linux filesyste
第二块磁盘已经初始化完毕,空间500G
创建物理卷
1 [root@VM_15_115_centos ~]# pvdisplay 2 [root@VM_15_115_centos ~]# pvcreate /dev/sdb1 3 Device /dev/sdb1 not found. 4 [root@VM_15_115_centos ~]# pvcreate /dev/vdb1 5 Physical volume "/dev/vdb1" successfully created. 6 [root@VM_15_115_centos ~]# pvdisplay 7 "/dev/vdb1" is a new physical volume of "<500.00 GiB" 8 --- NEW Physical volume --- 9 PV Name /dev/vdb1 10 VG Name 11 PV Size <500.00 GiB 12 Allocatable NO 13 PE Size 0 14 Total PE 0 15 Free PE 0 16 Allocated PE 0 17 PV UUID gV2Rlx-sFHV-9nJT-biZn-iXYf-rl1H-442MZF
注意:设备路径,有些设备是 /dev/sdb 有些是 /dev/vdb ,需要具体根据 fdisk -l 的结果确定。
创建卷组
1 [root@VM_15_115_centos ~]# vgdisplay 2 [root@VM_15_115_centos ~]# 3 [root@VM_15_115_centos ~]# vgcreate centos /dev/vdb1 4 Volume group "centos" successfully created
创建结果
1 [root@VM_15_115_centos ~]# vgdisplay 2 --- Volume group --- 3 VG Name centos 4 System ID 5 Format lvm2 6 Metadata Areas 1 7 Metadata Sequence No 1 8 VG Access read/write 9 VG Status resizable 10 MAX LV 0 11 Cur LV 0 12 Open LV 0 13 Max PV 0 14 Cur PV 1 15 Act PV 1 16 VG Size <500.00 GiB 17 PE Size 4.00 MiB 18 Total PE 127999 19 Alloc PE / Size 0 / 0 20 Free PE / Size 127999 / <500.00 GiB 21 VG UUID ndS3xn-MIrv-cn2Y-TTxb-pWYi-EBhB-f9wCp2
创建逻辑卷
1 [root@VM_15_115_centos ~]# lvdisplay 2 [root@VM_15_115_centos ~]# 3 [root@VM_15_115_centos ~]# lvcreate -l +100%FREE centos 4 Logical volume "lvol0" created.
注意:centos为上文创建的卷组的名字
此处将该卷组全部剩余空间划入本逻辑卷
创建结果:
1 [root@VM_15_115_centos ~]# lvdisplay 2 --- Logical volume --- 3 LV Path /dev/centos/lvol0 4 LV Name lvol0 5 VG Name centos 6 LV UUID ulWla4-C4SA-WE2e-HnjW-kHbB-TVSp-hstVYQ 7 LV Write Access read/write 8 LV Creation host, time VM_15_115_centos, 2019-07-06 08:52:24 +0800 9 LV Status available 10 # open 0 11 LV Size <500.00 GiB 12 Current LE 127999 13 Segments 1 14 Allocation inherit 15 Read ahead sectors auto 16 - currently set to 8192 17 Block device 252:0 18 19 [root@VM_15_115_centos ~]#
通知文件系统生效
新卷则建议采用 xfs 类型,当然,采用 ext4 亦可。
但需要注意的是,xfs文件系统使用 xfs_growfs,ext文件系统使用 resize2fs;xfs文件系统只支持增大不支持缩小。
先格式化逻辑卷磁盘分区,卷的名字从 lvdisplay 中查找
1 [root@VM_15_115_centos ~]# mkfs.xfs /dev/centos/lvol0 2 meta-data=/dev/centos/lvol0 isize=512 agcount=4, agsize=32767744 blks 3 = sectsz=512 attr=2, projid32bit=1 4 = crc=1 finobt=0, sparse=0 5 data = bsize=4096 blocks=131070976, imaxpct=25 6 = sunit=0 swidth=0 blks 7 naming =version 2 bsize=4096 ascii-ci=0 ftype=1 8 log =internal log bsize=4096 blocks=63999, version=2 9 = sectsz=512 sunit=0 blks, lazy-count=1 10 realtime =none extsz=4096 blocks=0, rtextents=0 11 [root@VM_15_115_centos ~]#
然后修改文件系统挂载表文件/etc/fstab
1 [root@VM_15_115_centos ~]# cat /etc/fstab 2 UUID=4b499d76-769a-40a0-93dc-4a31a59add28 / ext4 noatime,acl,user_xattr 1 1 3 proc /proc proc defaults 0 0 4 sysfs /sys sysfs noauto 0 0 5 debugfs /sys/kernel/debug debugfs noauto 0 0 6 devpts /dev/pts devpts mode=0620,gid=5 0 0 7 [root@VM_15_115_centos ~]# 8 [root@VM_15_115_centos ~]# 9 [root@VM_15_115_centos ~]# 10 [root@VM_15_115_centos ~]# 11 [root@VM_15_115_centos ~]# 12 [root@VM_15_115_centos ~]# vi /etc/fstab 13 [root@VM_15_115_centos ~]# 14 [root@VM_15_115_centos ~]# 15 [root@VM_15_115_centos ~]# 16 [root@VM_15_115_centos ~]# cat /etc/fstab 17 UUID=4b499d76-769a-40a0-93dc-4a31a59add28 / ext4 noatime,acl,user_xattr 1 1 18 proc /proc proc defaults 0 0 19 sysfs /sys sysfs noauto 0 0 20 debugfs /sys/kernel/debug debugfs noauto 0 0 21 devpts /dev/pts devpts mode=0620,gid=5 0 0 22 /dev/centos/lvol0 /data xfs defaults 0 0 23 [root@VM_15_115_centos ~]#
再挂载,然后查看挂载结果
1 [root@VM_15_115_centos ~]# mount -a 2 [root@VM_15_115_centos ~]# 3 [root@VM_15_115_centos ~]# df -h 4 文件系统 容量 已用 可用 已用% 挂载点 5 /dev/vda1 50G 1.6G 45G 4% / 6 devtmpfs 7.8G 0 7.8G 0% /dev 7 tmpfs 7.8G 24K 7.8G 1% /dev/shm 8 tmpfs 7.8G 496K 7.8G 1% /run 9 tmpfs 7.8G 0 7.8G 0% /sys/fs/cgroup 10 tmpfs 1.6G 0 1.6G 0% /run/user/0 11 /dev/mapper/centos-lvol0 500G 33M 500G 1% /data 12 [root@VM_15_115_centos ~]#
至此,操作成功。