LVM磁碟建立與擴容
阿新 • • 發佈:2021-12-06
以虛擬機器為例
1、在虛擬機器上新增新磁碟,點選虛擬機器→設定->新增,最後如下圖。
2、進入系統fdisk -l,檢視當前磁碟資訊
[root@master shell]# fdisk -l Disk /dev/sda: 10.7 GB, 10737418240 bytes, 20971520 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk label type: dos Disk identifier: 0x000af178 Device Boot Start End Blocks Id System /dev/sda1 * 2048 2099199 1048576 83 Linux /dev/sda2 2099200 20971519 9436160 8e Linux LVM Disk /dev/mapper/centos-root: 8585 MB, 8585740288 bytes, 16769024 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk /dev/mapper/centos-swap: 1073 MB, 1073741824 bytes, 2097152 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes [root@master shell]#
發現沒有讀到我們新新增的磁碟,我們重啟下虛擬機器。
如果不想重啟,或者是生產上的伺服器,不允許重啟,可以使用partprobe,線上更新分割槽表。
# partprobe線上更新分割槽資訊表,獲取最新的磁碟資訊 [root@master ~]# partprobe
重新使用fdisk -l命令檢視,發現多了一塊21.5GB的/dev/sdb磁碟
# fdisk -l 重新檢視一下 [root@master ~]# df -Th Filesystem Type Size Used Avail Use% Mounted on devtmpfs devtmpfs 475M 0 475M 0% /dev tmpfs tmpfs 487M 0 487M 0% /dev/shm tmpfs tmpfs 487M 7.5M 479M 2% /run tmpfs tmpfs 487M 0 487M 0% /sys/fs/cgroup /dev/mapper/centos-root xfs 8.0G 4.8G 3.3G 60% / /dev/sda1 xfs 1014M 167M 848M 17% /boot tmpfs tmpfs 98M 0 98M 0% /run/user/0 [root@master ~]# fdisk -l Disk /dev/sda: 10.7 GB, 10737418240 bytes, 20971520 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk label type: dos Disk identifier: 0x000af178 Device Boot Start End Blocks Id System /dev/sda1 * 2048 2099199 1048576 83 Linux /dev/sda2 2099200 20971519 9436160 8e Linux LVM Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk /dev/mapper/centos-root: 8585 MB, 8585740288 bytes, 16769024 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk /dev/mapper/centos-swap: 1073 MB, 1073741824 bytes, 2097152 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes
3、fdisk在新的磁碟上建立分割槽
[root@master ~]# fdisk /dev/sdb # 注意,這裡的sdb是不帶數字的 Welcome to fdisk (util-linux 2.23.2). Changes will remain in memory only, until you decide to write them. Be careful before using the write command. Device does not contain a recognized partition table Building a new DOS disklabel with disk identifier 0xe1f69387. Command (m for help): n # new新建分割槽 Partition type: p primary (0 primary, 0 extended, 4 free) e extended Select (default p): p # 選擇主分割槽 Partition number (1-4, default 1): 1 # 分割槽編號,直接回車,選擇預設的1就好 First sector (2048-41943039, default 2048): # 設定起始扇區,直接回車,選擇預設的 Using default value 2048 Last sector, +sectors or +size{K,M,G} (2048-41943039, default 41943039): # 設定結束扇區,直接回車,預設選擇到磁碟的最後一個扇區。 Using default value 41943039 # 預設分割槽的容量就是整個磁碟的容量 Partition 1 of type Linux and of size 20 GiB is set Command (m for help): p # print列印當前設定好的磁碟資訊(注意,此時還沒物理生效) Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk label type: dos Disk identifier: 0xe1f69387 Device Boot Start End Blocks Id System /dev/sdb1 2048 41943039 20970496 83 Linux Command (m for help): t # 輸入t,修改磁碟格式為LVM格式 Selected partition 1 Hex code (type L to list all codes): 8e # 輸入8e,8e代表LVM格式 Changed type of partition 'Linux' to 'Linux LVM' Command (m for help): p # 再次列印磁碟資訊 Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk label type: dos Disk identifier: 0xe1f69387 Device Boot Start End Blocks Id System /dev/sdb1 2048 41943039 20970496 8e Linux LVM Command (m for help): w # w開始儲存設定的磁碟分割槽資訊,此時才是真正作用生效了。 The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks.
4、建立pv
建立LVM概念中的物理卷。
注意:這裡要選帶數字編號的磁碟,這裡的/dev/sdb1就是上面我們用fdisk建立好的磁碟分割槽,1表示我們的分割槽編號,上面設定的是1,所以這裡就是sdb1
[root@master ~]# pvscan # 檢視當前系統上的物理卷資訊 PV /dev/sda2 VG centos lvm2 [<9.00 GiB / 0 free] Total: 1 [<9.00 GiB] / in use: 1 [<9.00 GiB] / in no VG: 0 [0 ] [root@master ~]# pvcreate /dev/sdb1 # 建立物理卷,注意這裡的磁碟尾號sdb1必須帶數字 Physical volume "/dev/sdb1" successfully created. [root@master ~]# pvscan PV /dev/sda2 VG centos lvm2 [<9.00 GiB / 0 free] PV /dev/sdb1 lvm2 [<20.00 GiB] Total: 2 [<29.00 GiB] / in use: 1 [<9.00 GiB] / in no VG: 1 [<20.00 GiB]
5、將pv加入vg組,擴容vg
pv是物理卷,vg是卷組,多個pv組成一個vg,然後vg裡面去構建邏輯卷組lv
如果系統沒有vg,則建立vg組。
[root@master ~]# vgdisplay [root@master ~]# vgscan Reading volume groups from cache. # vgcreate 後面可以接多個磁碟,比如vgcreate vgdata /dev/xvde1 /dev/xvde2,因為這裡只有一塊,所以就用一個 [root@master ~]# vgcreate vgdata /dev/xvde1 # 建立vg,vg名字設定為vgdata,/dev/xvde1是我們上面建立的pv卷的名字 Volume group "vgdata" successfully created [root@master ~]# vgscan Reading volume groups from cache. Found volume group "vgdata" using metadata type lvm2 [root@master ~]# vgdisplay --- Volume group --- VG Name vgdata System ID Format lvm2 Metadata Areas 1 Metadata Sequence No 1 VG Access read/write VG Status resizable MAX LV 0 Cur LV 0 Open LV 0 Max PV 0 Cur PV 1 Act PV 1 VG Size <300.00 GiB PE Size 4.00 MiB Total PE 76799 Alloc PE / Size 0 / 0 Free PE / Size 76799 / <300.00 GiB VG UUID bY9vpW-ALyF-YDxB-WCjj-Tlvg-04Jp-DWXdkm [root@master ~]#
如果有vg,則擴容
[root@master ~]# vgdisplay --- Volume group --- VG Name centos System ID Format lvm2 Metadata Areas 1 Metadata Sequence No 3 VG Access read/write VG Status resizable MAX LV 0 Cur LV 2 Open LV 2 Max PV 0 Cur PV 1 Act PV 1 VG Size <9.00 GiB PE Size 4.00 MiB Total PE 2303 Alloc PE / Size 2303 / <9.00 GiB Free PE / Size 0 / 0 VG UUID YSauT8-zhj2-05Yc-c4rb-VNqs-OjrX-ZB8eeE [root@master ~]# vgextend centos /dev/sdb1 # centos是原來就已經存在的卷組名字,/dev/sdb1是我們剛剛建立的pv名字 Volume group "centos" successfully extended [root@master ~]# vgdisplay --- Volume group --- VG Name centos System ID Format lvm2 Metadata Areas 2 Metadata Sequence No 4 VG Access read/write VG Status resizable MAX LV 0 Cur LV 2 Open LV 2 Max PV 0 Cur PV 2 Act PV 2 VG Size 28.99 GiB PE Size 4.00 MiB Total PE 7422 Alloc PE / Size 2303 / <9.00 GiB Free PE / Size 5119 / <20.00 GiB VG UUID YSauT8-zhj2-05Yc-c4rb-VNqs-OjrX-ZB8eeE
6、擴容LV
如果LV邏輯卷不存在,則建立
[root@yzjgxh2529638940 ~]# lvcreate -n lvdata -L 300G vgdata Volume group "vgdata" has insufficient free space (76799 extents): 76800 required. # 一般無法用到300G,所以稍微小一點就好了 [root@yzjgxh2529638940 ~]# lvcreate -n lvdata -L 299G vgdata Logical volume "lvdata" created. [root@yzjgxh2529638940 ~]# lvscan ACTIVE '/dev/vgdata/lvdata' [299.00 GiB] inherit [root@yzjgxh2529638940 ~]# # 格式化該邏輯分割槽 [root@yzjgxh2529638940 ~]# mkfs.xfs /dev/vgdata/lvdata meta-data=/dev/vgdata/lvdata isize=512 agcount=4, agsize=19595264 blks = sectsz=512 attr=2, projid32bit=1 = crc=1 finobt=0, sparse=0 data = bsize=4096 blocks=78381056, imaxpct=25 = sunit=0 swidth=0 blks naming =version 2 bsize=4096 ascii-ci=0 ftype=1 log =internal log bsize=4096 blocks=38272, version=2 = sectsz=512 sunit=0 blks, lazy-count=1 realtime =none extsz=4096 blocks=0, rtextents=0 [root@yzjgxh2529638940 ~]#
如果LV邏輯卷存在,則擴容
[root@master ~]# lvscan ACTIVE '/dev/centos/swap' [1.00 GiB] inherit ACTIVE '/dev/centos/root' [<8.00 GiB] inherit [root@master ~]# lvextend -L +19.5G /dev/centos/root Size of logical volume centos/root changed from <8.00 GiB (2047 extents) to <27.50 GiB (7039 extents). Logical volume centos/root successfully resized.
7、resize使檔案系統生效(重要)
[root@master ~]# df -Th Filesystem Type Size Used Avail Use% Mounted on devtmpfs devtmpfs 475M 0 475M 0% /dev tmpfs tmpfs 487M 0 487M 0% /dev/shm tmpfs tmpfs 487M 7.5M 479M 2% /run tmpfs tmpfs 487M 0 487M 0% /sys/fs/cgroup /dev/mapper/centos-root xfs 8.0G 4.8G 3.3G 60% / /dev/sda1 xfs 1014M 167M 848M 17% /boot tmpfs tmpfs 98M 0 98M 0% /run/user/0 # ext3,ext4使用resize2fs [root@master ~]# resize2fs /dev/centos/root resize2fs 1.42.9 (28-Dec-2013) resize2fs: Bad magic number in super-block while trying to open /dev/centos/root Couldn't find valid filesystem superblock. # 這裡需要擴容的是xfs,所以用xfs_growfs命令 [root@master ~]# xfs_growfs /dev/centos/root meta-data=/dev/mapper/centos-root isize=512 agcount=4, agsize=524032 blks = sectsz=512 attr=2, projid32bit=1 = crc=1 finobt=0 spinodes=0 data = bsize=4096 blocks=2096128, imaxpct=25 = sunit=0 swidth=0 blks naming =version 2 bsize=4096 ascii-ci=0 ftype=1 log =internal bsize=4096 blocks=2560, version=2 = sectsz=512 sunit=0 blks, lazy-count=1 realtime =none extsz=4096 blocks=0, rtextents=0 data blocks changed from 2096128 to 7207936 [root@master ~]# df -Th Filesystem Type Size Used Avail Use% Mounted on devtmpfs devtmpfs 475M 0 475M 0% /dev tmpfs tmpfs 487M 0 487M 0% /dev/shm tmpfs tmpfs 487M 7.5M 479M 2% /run tmpfs tmpfs 487M 0 487M 0% /sys/fs/cgroup /dev/mapper/centos-root xfs 28G 4.8G 23G 18% / /dev/sda1 xfs 1014M 167M 848M 17% /boot tmpfs tmpfs 98M 0 98M 0% /run/user/0
8、掛載
# 建立掛載的目錄 [root@yzjgxh2529638940 ~]# mkdir /data # 開始掛載 [root@yzjgxh2529638940 ~]# mount /dev/vgdata/lvdata /data/ [root@yzjgxh2529638940 ~]# df -Th Filesystem Type Size Used Avail Use% Mounted on /dev/xvda2 xfs 36G 2.1G 34G 6% / devtmpfs devtmpfs 32G 0 32G 0% /dev tmpfs tmpfs 32G 24K 32G 1% /dev/shm tmpfs tmpfs 32G 18M 32G 1% /run tmpfs tmpfs 32G 0 32G 0% /sys/fs/cgroup tmpfs tmpfs 6.3G 0 6.3G 0% /run/user/0 /dev/mapper/vgdata-lvdata xfs 299G 33M 299G 1% /data # 加入開機自動掛載 [root@yzjgxh2529638940 ~]# vim /etc/fstab # # /etc/fstab # Created by anaconda on Thu May 9 01:56:05 2019 # # 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=a83f4fdc-2ea1-4fec-a1e2-a42016ce0afe / xfs defaults 0 0 UUID=9e33bd6f-c68c-41c7-95c8-703f4fe8c3d4 swap swap defaults 0 0 /dev/vgdata/lvdata /data xfs defaults 0 0
到此,LVM擴容完成。