ESXI虛擬機(jī)擴(kuò)容/目錄(添加新硬盤)
背景:
線上服務(wù)器,磁盤Linux的虛擬機(jī)根分區(qū)已經(jīng)使用90%,觸發(fā)了磁盤告警,再一頓操作刪除后,勉勉強(qiáng)強(qiáng)回到了82%,現(xiàn)在需要對根目錄進(jìn)行擴(kuò)容。
進(jìn)入到EXSI管理平臺,看到原來的sda磁盤只有30G【圖一】,新添加一塊100G 的新硬盤【圖二】

圖一

圖二
登錄機(jī)器,查看磁盤,發(fā)現(xiàn)了多了一塊sdb
- [root@localhost ~]# lsblk
- NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
- sda 8:0 0 30G 0 disk
- ├─sda1 8:1 0 1G 0 part /boot
- └─sda2 8:2 0 29G 0 part
- ├─centos-root 253:0 0 26G 0 lvm /
- └─centos-swap 253:1 0 3G 0 lvm [SWAP]
- sdb 8:16 0 100G 0 disk
- sr0 11:0 1 1024M 0 rom
第一步:分區(qū)
- [root@localhost ~]# fdisk /dev/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 0x3cb140d1.
- Command (m for help): n
- Partition type:
- p primary (0 primary, 0 extended, 4 free)
- e extended
- Select (default p): p
- Partition number (1-4, default 1): 1
- First sector (2048-209715199, default 2048):
- Using default value 2048
- Last sector, +sectors or +size{K,M,G} (2048-209715199, default 209715199):
- Using default value 209715199
- Partition 1 of type Linux and of size 100 GiB is set
- Command (m for help): w
- The partition table has been altered!
- Calling ioctl() to re-read partition table.
- Syncing disks.
再次查看磁盤,發(fā)現(xiàn)多了個sdb1
- [root@localhost ~]# lsblk
- NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
- sda 8:0 0 30G 0 disk
- ├─sda1 8:1 0 1G 0 part /boot
- └─sda2 8:2 0 29G 0 part
- ├─centos-root 253:0 0 26G 0 lvm /
- └─centos-swap 253:1 0 3G 0 lvm [SWAP]
- sdb 8:16 0 100G 0 disk
- └─sdb1 8:17 0 100G 0 part
- sr0 11:0 1 1024M 0 rom
第二步:格式化
格式化硬盤,這里使用xfs格式,建議使用和系統(tǒng)一樣的文件格式
可以看到/分區(qū) 【/dev/mapper/centos-root】 使用的是xfs 的文件系統(tǒng)
- [root@localhost ~]# blkid
- /dev/sda1: UUID="e1e8eda5-8b7a-45f0-a763-8868777e3f5f" TYPE="xfs"
- /dev/sda2: UUID="DQGZrB-2WRU-JneT-xKKw-WFSN-ozM9-rnW0S4" TYPE="LVM2_member"
- /dev/mapper/centos-root: UUID="a8e67b40-8e6e-4010-a911-21b4f95b90bf" TYPE="xfs"
- /dev/mapper/centos-swap: UUID="326cb3bd-ea93-460d-9f2b-cded2d3836c8" TYPE="swap"
把/dev/sdb1 格式化成 xfs文件系統(tǒng)
- [root@localhost ~]# mkfs.xfs /dev/sdb1
- meta-data=/dev/sdb1 isize=512 agcount=4, agsize=6553536 blks
- = sectsz=512 attr=2, projid32bit=1
- = crc=1 finobt=0, sparse=0
- data = bsize=4096 blocks=26214144, imaxpct=25
- = sunit=0 swidth=0 blks
- naming =version 2 bsize=4096 ascii-ci=0 ftype=1
- log =internal log bsize=4096 blocks=12799, version=2
- = sectsz=512 sunit=0 blks, lazy-count=1
- realtime =none extsz=4096 blocks=0, rtextents=0
可以看到/dev/sdb1 的文件系統(tǒng)為 xfs
- [root@localhost ~]# blkid
- /dev/sda1: UUID="e1e8eda5-8b7a-45f0-a763-8868777e3f5f" TYPE="xfs"
- /dev/sda2: UUID="DQGZrB-2WRU-JneT-xKKw-WFSN-ozM9-rnW0S4" TYPE="LVM2_member"
- /dev/mapper/centos-root: UUID="a8e67b40-8e6e-4010-a911-21b4f95b90bf" TYPE="xfs"
- /dev/mapper/centos-swap: UUID="326cb3bd-ea93-460d-9f2b-cded2d3836c8" TYPE="swap"
- /dev/sdb1: UUID="2eca6693-8885-4efe-a09e-320a0fe4eefa" TYPE="xfs"
第三步:lvm擴(kuò)容
通過lvdisplay 可以看到 / 分區(qū) 的lv 名稱為 root
- [root@localhost ~]# lvdisplay
- ..............
- --- Logical volume ---
- LV Path /dev/centos/root
- LV Name root
- VG Name centos
- LV UUID upKlP7-AbiZ-e3Yh-aYYN-5Eel-ULsG-TVXVer
- LV Write Access read/write
- LV Creation host, time localhost, 2021-09-26 05:16:02 -0400
- LV Status available
- # open 1
- LV Size <26.00 GiB
- Current LE 6655
- Segments 1
- Allocation inherit
- Read ahead sectors auto
- - currently set to 8192
- Block device 253:0
1、創(chuàng)建pv物理卷
- [root@localhost ~]# pvcreate /dev/sdb1
- WARNING: xfs signature detected on /dev/sdb1 at offset 0. Wipe it? [y/n]: y
- Wiping xfs signature on /dev/sdb1.
- Physical volume "/dev/sdb1" successfully created.
查看下pv物理卷
- [root@localhost ~]# pvs
- PV VG Fmt Attr PSize PFree
- /dev/sda2 centos lvm2 a-- <29.00g 0
- /dev/sdb1 lvm2 --- <100.00g <100.00g
2、向vg卷組 添加 pv物理卷
使用vgdisplay 查看下vg卷組名稱為 centos
- [root@localhost ~]# 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 <29.00 GiB
- PE Size 4.00 MiB
- Total PE 7423
- Alloc PE / Size 7423 / <29.00 GiB
- Free PE / Size 0 / 0
- VG UUID GzMH6f-8Hne-1SF4-h89q-n95E-0xft-x8JRkb
使用vgextend 添加pv到vg中
- [root@localhost ~]# vgextend centos /dev/sdb1
- Volume group "centos" successfully extended
使用vgdisplay,可以看到空閑的PE 容量大約為100G
- [root@localhost ~]# 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 128.99 GiB
- PE Size 4.00 MiB
- Total PE 33022
- Alloc PE / Size 7423 / <29.00 GiB
- Free PE / Size 25599 / <100.00 GiB
- VG UUID GzMH6f-8Hne-1SF4-h89q-n95E-0xft-x8JRkb
3、擴(kuò)容邏輯卷 lvm
通過lvdisplay 可以看到 / 分區(qū) 的lv 名稱為 root
- [root@localhost ~]# lvdisplay
- .....................
- --- Logical volume ---
- LV Path /dev/centos/root
- LV Name root
- VG Name centos
- LV UUID upKlP7-AbiZ-e3Yh-aYYN-5Eel-ULsG-TVXVer
- LV Write Access read/write
- LV Creation host, time localhost, 2021-09-26 05:16:02 -0400
- LV Status available
- # open 1
- LV Size <26.00 GiB
- Current LE 6655
- Segments 1
- Allocation inherit
- Read ahead sectors auto
- - currently set to 8192
- Block device 253:0
使用lvextend 增加邏輯卷 100G
- [root@localhost ~]# lvextend -L +100G /dev/centos/root
- Insufficient free space: 25600 extents needed, but only 25599 available
提示沒有那么多的空閑的pe去添加,那我們就添加99G 【其實,上面的操作在往vg卷組里添加pv 的時候,也說明了 可用容量< 100G】
- [root@localhost ~]# lvextend -L +99G /dev/centos/root
- Size of logical volume centos/root changed from <26.00 GiB (6655 extents) to <125.00 GiB (31999 extents).
- Logical volume centos/root successfully resized.
添加成功,我們看下磁盤大小,和掛載的情況
- [root@localhost ~]# lsblk
- NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
- sda 8:0 0 30G 0 disk
- ├─sda1 8:1 0 1G 0 part /boot
- └─sda2 8:2 0 29G 0 part
- ├─centos-root 253:0 0 125G 0 lvm /
- └─centos-swap 253:1 0 3G 0 lvm [SWAP]
- sdb 8:16 0 100G 0 disk
- └─sdb1 8:17 0 100G 0 part
- └─centos-root 253:0 0 125G 0 lvm /
- sr0 11:0 1 1024M 0 rom
- [root@localhost ~]# df -h
- Filesystem Size Used Avail Use% Mounted on
- devtmpfs 1.9G 0 1.9G 0% /dev
- tmpfs 1.9G 0 1.9G 0% /dev/shm
- tmpfs 1.9G 8.9M 1.9G 1% /run
- tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup
- /dev/mapper/centos-root 26G 1.5G 25G 6% /
- /dev/sda1 1014M 150M 865M 15% /boot
- tmpfs 379M 0 379M 0% /run/user/0
通過 lsblk 可以看到/ 分區(qū)磁盤的容量變成了125G 了,但是 磁盤掛載那邊還是沒有變化
有兩個解決方法:
1、重啟
2、刷新下文件系統(tǒng)
這里,我們采用第二種方法,刷新文件系統(tǒng)
注意:不同的文件系統(tǒng)的刷新命令不一樣
resize2fs 邏輯卷名 (只適用于ext文件系統(tǒng))
xfs_growfs 掛載點 (只適用于xfs文件系統(tǒng))
通過blkid 查看到/ 分區(qū)的文件系統(tǒng)為 xfs 格式【/dev/mapper/centos-root】
- [root@localhost ~]# df -h
- Filesystem Size Used Avail Use% Mounted on
- devtmpfs 1.9G 0 1.9G 0% /dev
- tmpfs 1.9G 0 1.9G 0% /dev/shm
- tmpfs 1.9G 8.9M 1.9G 1% /run
- tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup
- /dev/mapper/centos-root 26G 1.5G 25G 6% /
- /dev/sda1 1014M 150M 865M 15% /boot
- tmpfs 379M 0 379M 0% /run/user/0
- [root@localhost ~]# blkid
- /dev/sda1: UUID="e1e8eda5-8b7a-45f0-a763-8868777e3f5f" TYPE="xfs"
- /dev/sda2: UUID="DQGZrB-2WRU-JneT-xKKw-WFSN-ozM9-rnW0S4" TYPE="LVM2_member"
- /dev/mapper/centos-root: UUID="a8e67b40-8e6e-4010-a911-21b4f95b90bf" TYPE="xfs"
- /dev/mapper/centos-swap: UUID="326cb3bd-ea93-460d-9f2b-cded2d3836c8" TYPE="swap"
- /dev/sdb1: UUID="kuZdno-Kaue-WMEV-d6jh-zTjc-0eAa-YKcDxB" TYPE="LVM2_member"
使用xfs_growfs 刷新下文件系統(tǒng)
- [root@localhost ~]# xfs_growfs /
- meta-data=/dev/mapper/centos-root isize=512 agcount=4, agsize=1703680 blks
- = sectsz=512 attr=2, projid32bit=1
- = crc=1 finobt=0 spinodes=0
- data = bsize=4096 blocks=6814720, imaxpct=25
- = sunit=0 swidth=0 blks
- naming =version 2 bsize=4096 ascii-ci=0 ftype=1
- log =internal bsize=4096 blocks=3327, version=2
- = sectsz=512 sunit=0 blks, lazy-count=1
- realtime =none extsz=4096 blocks=0, rtextents=0
- data blocks changed from 6814720 to 32766976
刷新后發(fā)現(xiàn),/ 分區(qū)總的磁盤大小變成了125G
- [root@localhost ~]# df -h
- Filesystem Size Used Avail Use% Mounted on
- devtmpfs 1.9G 0 1.9G 0% /dev
- tmpfs 1.9G 0 1.9G 0% /dev/shm
- tmpfs 1.9G 8.9M 1.9G 1% /run
- tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup
- /dev/mapper/centos-root 125G 1.5G 124G 2% /
- /dev/sda1 1014M 150M 865M 15% /boot
- tmpfs 379M 0 379M 0% /run/user/0
- [root@localhost ~]# lsblk
- NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
- sda 8:0 0 30G 0 disk
- ├─sda1 8:1 0 1G 0 part /boot
- └─sda2 8:2 0 29G 0 part
- ├─centos-root 253:0 0 125G 0 lvm /
- └─centos-swap 253:1 0 3G 0 lvm [SWAP]
- sdb 8:16 0 100G 0 disk
- └─sdb1 8:17 0 100G 0 part
- └─centos-root 253:0 0 125G 0 lvm /
- sr0 11:0 1 1024M 0 rom
好了,本次ESXI虛擬機(jī)擴(kuò)容/目錄 成功