以虛擬機為例
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擴容完成。