Linux系統LVM擴容一個分區相對於Windows來講沒有那麼直觀,可是熟悉命令後,擴容起來也是蠻方便的。git
擴容場景以下:shell
1 [root@rhel06 ~]# df -Th 2 Filesystem Type Size Used Avail Use% Mounted on 3 /dev/sda2 ext4 9.9G 2.2G 7.3G 23% / 4 tmpfs tmpfs 491M 0 491M 0% /dev/shm 5 /dev/sda1 ext4 194M 29M 155M 16% /boot 6 /dev/sr0 iso9660 3.6G 3.6G 0 100% /mnt 7 /dev/mapper/VG001-lv1 ext4 2.0G 67M 1.9G 4% /lv1
現有一個lvm分區lv1容量2G,但願擴容此分區空間bash
1 [root@rhel06 home]# cat /proc/partitions 2 major minor #blocks name 3 4 8 0 20971520 sda 5 8 1 204800 sda1 6 8 2 10485760 sda2 7 8 3 2097152 sda3 8 8 4 1 sda4 9 8 5 2103487 sda5 10 253 0 2097152 dm-0
查看partitions 獲取當前分區狀況和磁盤總空間狀況。須要計算20971520-204800-10485760-2097152-2103487-1=6080320,約6G的未使用空間。(dm-0是一個lv分區)app
固然使用這種手動方式獲取未分區空間太過麻煩。能夠用這個shell來完成大量的計算ide
1 [root@rhel06 home]# cat sda.sh 2 #!/bin/bash 3 total=$(grep 'sda$' /proc/partitions |awk '{print $3}'); 4 used=0 5 for i in $(grep 'sda[[:digit:]]\+$' /proc/partitions |awk '{print $3}' |xargs) 6 do 7 used=$(( used + i )); 8 done 9 echo $((( total - used ) / 1024 )) "MB" 10 [root@rhel06 home]# ./sda.sh 11 5937 MB
接下來執行具體的分區步驟:ui
1、劃分一個新分區並格式化spa
1 [root@rhel06 ~]# fdisk /dev/sda 2 3 Command (m for help): p 4 5 Disk /dev/sda: 21.5 GB, 21474836480 bytes 6 255 heads, 63 sectors/track, 2610 cylinders 7 Units = cylinders of 16065 * 512 = 8225280 bytes 8 Sector size (logical/physical): 512 bytes / 512 bytes 9 I/O size (minimum/optimal): 512 bytes / 512 bytes 10 Disk identifier: 0x00029f55 11 12 Device Boot Start End Blocks Id System 13 /dev/sda1 * 1 26 204800 83 Linux 14 Partition 1 does not end on cylinder boundary. 15 /dev/sda2 26 1332 10485760 83 Linux 16 /dev/sda3 1332 1593 2097152 82 Linux swap / Solaris 17 /dev/sda4 1593 2610 8176089 5 Extended 18 /dev/sda5 1593 1854 2103487+ 8e Linux LVM 19 20 Command (m for help): n #新建分區,因爲我已新建了四個分區(主分區和邏輯分區),因此這裏只能新建擴展分區,並無出現分區類型選項 21 First cylinder (1855-2610, default 1855): #開始點,直接回車便可 22 Using default value 1855 23 Last cylinder, +cylinders or +size{K,M,G} (1855-2610, default 2610): +2G #+2G,劃分一個2G的新分區 24 25 Command (m for help): p 26 27 Disk /dev/sda: 21.5 GB, 21474836480 bytes 28 255 heads, 63 sectors/track, 2610 cylinders 29 Units = cylinders of 16065 * 512 = 8225280 bytes 30 Sector size (logical/physical): 512 bytes / 512 bytes 31 I/O size (minimum/optimal): 512 bytes / 512 bytes 32 Disk identifier: 0x00029f55 33 34 Device Boot Start End Blocks Id System 35 /dev/sda1 * 1 26 204800 83 Linux 36 Partition 1 does not end on cylinder boundary. 37 /dev/sda2 26 1332 10485760 83 Linux 38 /dev/sda3 1332 1593 2097152 82 Linux swap / Solaris 39 /dev/sda4 1593 2610 8176089 5 Extended 40 /dev/sda5 1593 1854 2103487+ 8e Linux LVM 41 /dev/sda6 1855 2116 2104483+ 83 Linux 42 43 Command (m for help): w #保存退出
1 [root@rhel06 ~]# partprobe #不要忘了刷新分區表,虛擬機仍是須要重啓才能刷新。
1 [root@rhel06 ~]# mkfs.ext4 /dev/sda6 #格式化
2、轉換sda6分區爲pvcode
1 [root@rhel06 ~]# pvcreate /dev/sda6 #轉換sda6分區 2 dev_is_mpath: failed to get device for 8:6 3 Physical volume "/dev/sda6" successfully created 4 [root@rhel06 ~]# pvdisplay #查看系統全部pv,這裏sda6已成功轉換 5 --- Physical volume --- 6 PV Name /dev/sda5 7 VG Name VG001 8 PV Size 2.01 GiB / not usable 2.19 MiB 9 Allocatable yes 10 PE Size 4.00 MiB 11 Total PE 513 12 Free PE 1 13 Allocated PE 512 14 PV UUID dSHFLX-9Lui-0ZDb-6J2g-pp6A-azuX-vg5yGV 15 16 "/dev/sda6" is a new physical volume of "2.01 GiB" 17 --- NEW Physical volume --- 18 PV Name /dev/sda6 19 VG Name 20 PV Size 2.01 GiB 21 Allocatable NO 22 PE Size 0 23 Total PE 0 24 Free PE 0 25 Allocated PE 0 26 PV UUID TEGKht-876S-8MMd-vWkX-rjeI-4UqG-JqRsT2
3、添加新pv到vgorm
1 [root@rhel06 ~]# vgdisplay |grep Name #查看當前vg名稱 2 VG Name VG001 3 [root@rhel06 ~]# vgextend VG001 /dev/sda6 #添加「sda6」pv到此vg 4 Volume group "VG001" successfully extended 5 [root@rhel06 ~]# vgdisplay #查看vg詳情,能夠看到vg已經擴容到4g 6 --- Volume group --- 7 VG Name VG001 8 System ID 9 Format lvm2 10 Metadata Areas 2 11 Metadata Sequence No 3 12 VG Access read/write 13 VG Status resizable 14 MAX LV 0 15 Cur LV 1 16 Open LV 1 17 Max PV 0 18 Cur PV 2 19 Act PV 2 20 VG Size 4.01 GiB 21 PE Size 4.00 MiB 22 Total PE 1026 23 Alloc PE / Size 512 / 2.00 GiB 24 Free PE / Size 514 / 2.01 GiB 25 VG UUID qG18Fp-agAP-gaTp-0crS-Ughm-UilM-2ZftQt
4、調整lv1容量blog
1 [root@rhel06 ~]# lvextend -L 4G /dev/VG001/lv1 #調整lv1分區容量爲4G,原來是2G 2 Extending logical volume lv1 to 4.00 GiB 3 Logical volume lv1 successfully resized 4 [root@rhel06 ~]# resize2fs /dev/VG001/lv1 #執行重設大小,對當前lv1有效 5 resize2fs 1.41.12 (17-May-2010) 6 Filesystem at /dev/VG001/lv1 is mounted on /lv1; on-line resizing required 7 old desc_blocks = 1, new_desc_blocks = 1 8 Performing an on-line resize of /dev/VG001/lv1 to 1048576 (4k) blocks. 9 The filesystem on /dev/VG001/lv1 is now 1048576 blocks long.
5、到此設置已完成,df命令查看已擴容到4G
1 [root@rhel06 ~]# df -h 2 Filesystem Size Used Avail Use% Mounted on 3 /dev/sda2 9.9G 2.2G 7.3G 23% / 4 tmpfs 491M 0 491M 0% /dev/shm 5 /dev/sda1 194M 29M 155M 16% /boot 6 /dev/sr0 3.6G 3.6G 0 100% /mnt 7 /dev/mapper/VG001-lv1 4.0G 68M 3.7G 2% /lv1
6、新增lv分區
[root@rhel06 ~]# lvcreate -L 4G -n lv0data lv1 #在lv1邏輯卷新增lv0data分區 -L指定大小 -n指定名稱
問題1:執行partprobe刷新分區表時會出現如下報錯:
[root@rhel06 ~]# partprobe Warning: WARNING: the kernel failed to re-read the partition table on /dev/sda (設備或資源忙). As a result, it may not reflect all of your changes until after reboot.
這個問題貌似只存在於虛擬機上面,物理機上並無發現這個問題。重啓虛擬機便可。
問題2:fdisk能夠查看到sda5分區,可是使用mkfs格式化是提示「沒有這個文件或目錄」,那是分區表沒有刷新或者刷新失敗,刷新步驟可參考上一個問題。
1 Device Boot Start End Blocks Id System 2 /dev/sda1 * 1 26 204800 83 Linux 3 Partition 1 does not end on cylinder boundary. 4 /dev/sda2 26 1332 10485760 83 Linux 5 /dev/sda3 1332 1593 2097152 82 Linux swap / Solaris 6 /dev/sda4 1593 2610 8176089 5 Extended 7 /dev/sda5 1593 1854 2103487+ 83 Linux 8 [root@rhel06 ~]# mkfs.ext4 /dev/sda5 9 mke2fs 1.41.12 (17-May-2010) 10 沒法對 /dev/sda5 進行 stat 調用 --- 沒有那個文件或目錄