Today, I was testing a vps, but the partitioning of his template system was too bad (not sprayed). I wanted to let the customer service take a look at it, but I just said it. That's why I have this note, and I can also review LVM by the way.
The total hard disk space is 30G SSD, the root partition is 10G, and the remaining 20G space is mounted under /home.
[ root@MyCloudServer ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/centos-root 8.5G 930M 7.6G 11%/ #The root partition gave 10G
devtmpfs 873M 0 873M 0%/dev
tmpfs 893M 0 893M 0%/dev/shm
tmpfs 893M 8.2M 885M 1%/run
tmpfs 893M 0 893M 0%/sys/fs/cgroup
/dev/xvda1 497M 161M 337M 33%/boot
/dev/mapper/Xvdbgroup-xvdb1 20G 45M 19G 1%/home #Another partition 20G mounted to/under home
My requirement is to give all the space to the root directory. After all, it only has 30G space. What's the point? First, let's check the current state of LVM division:
[ root@MyCloudServer ~]# pvscan
PV /dev/xvda2 VG centos lvm2 [9.51 GiB /40.00 MiB free]
PV /dev/xvdb1 VG Xvdbgroup lvm2 [20.00 GiB /96.00 MiB free]
Total:2[29.50 GiB]/in use:2[29.50 GiB]/in no VG:0[0][root@MyCloudServer ~]# vgscan
Reading all physical volumes. This may take a while...
Found volume group "centos" using metadata type lvm2
Found volume group "Xvdbgroup" using metadata type lvm2
[ root@MyCloudServer ~]# lvdisplay
- - - Logical volume ---
LV Path /dev/centos/swap #LV path
LV Name swap #LV's name
VG Name centos #VG's name
LV UUID Bs14H4-uzvw-o8NK-FwAH-6vlQ-wOR8-R7Yx0n #LV UUID
LV Write Access read/write
LV Creation host, time localhost,2015-07-2211:01:55+0800
LV Status available
# open 2
LV Size 1.00 GiB
Current LE 256
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:1--- Logical volume ---
LV Path /dev/centos/root
LV Name root
VG Name centos
LV UUID jH3frG-mRfk-hf0o-0KS6-rcth-aklH-BAciW4
LV Write Access read/write
LV Creation host, time localhost,2015-07-2211:01:55+0800
LV Status available
# open 1
LV Size 8.47 GiB
Current LE 2168
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:0--- Logical volume ---
LV Path /dev/Xvdbgroup/xvdb1
LV Name xvdb1
VG Name Xvdbgroup
LV UUID 4nLoAG-0xFr-1agn-K3D0-lihf-erDn-PNUx8K
LV Write Access read/write
LV Creation host, time MyCloudServer,2017-08-2917:10:30+0800
LV Status available
# open 1
LV Size 19.90 GiB
Current LE 5095
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:2
From the above results, a 10G hard disk is hung up, which is regarded as a system disk; a 20G hard disk is hung up, which should be regarded as a data disk. There are PV, VG, and LV in total. What needs to be done now is to remove which piece of 20G, and add it to the PV where the root partition is located.
It should be noted that LVM deletion is reverse deletion, that is, delete from LV, then delete VG, and then delete PV.
[ root@MyCloudServer ~]# lvremove /dev/Xvdbgroup/xvdb1 #lvremove deletes lv, prompting that the volume is in use. The reason is that there is no umount
Logical volume Xvdbgroup/xvdb1 contains a filesystem in use.[root@MyCloudServer ~]# umount /home/ #Uninstall the lv that needs to be deleted
[ root@MyCloudServer ~]# lvremove /dev/Xvdbgroup/xvdb1 #Delete lv
Do you really want to remove active logical volume xvdb1?[y/n]: y
Logical volume "xvdb1" successfully removed
[ root@MyCloudServer ~]# vgdisplay #View vg
- - - 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.51 GiB
PE Size 4.00 MiB
Total PE 2434
Alloc PE / Size 2424/9.47 GiB
Free PE / Size 10/40.00 MiB
VG UUID NnqUfE-nNgq-u0tY-rmV2-J7ku-nn5S-E3l26G
- - - Volume group ---
VG Name Xvdbgroup
System ID
Format lvm2
Metadata Areas 1
Metadata Sequence No 3
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 20.00 GiB
PE Size 4.00 MiB
Total PE 5119
Alloc PE / Size 0/0
Free PE / Size 5119/20.00 GiB
VG UUID YYmS9Z-XmaO-gRi0-rIUR-e0sU-AenG-KwUSoq
[ root@MyCloudServer ~]# vgremove Xvdbgroup #Delete vg
Volume group "Xvdbgroup" successfully removed
[ root@MyCloudServer ~]# pvdisplay #View pv
- - - Physical volume ---
PV Name /dev/xvda2
VG Name centos
PV Size 9.51 GiB / not usable 3.00 MiB
Allocatable yes
PE Size 4.00 MiB
Total PE 2434
Free PE 10
Allocated PE 2424
PV UUID lFsKe4-CXvT-KHR1-qhrV-ct6O-rvMJ-aDGoRW
" /dev/xvdb1" is a newphysical volume of"20.00 GiB"--- NEW Physical volume ---
PV Name /dev/xvdb1
VG Name
PV Size 20.00 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID 9WiyQq-36PK-sG2m-GrUc-AuFb-0BK3-ivM11V
[ root@MyCloudServer ~]# pvremove /dev/xvdb1 #Delete pv
Labels on physical volume "/dev/xvdb1" successfully wiped
After the lv is deleted to the pv is completed, the expansion operation is officially started. First use partprobe to re-read the partition table.
[ root@MyCloudServer ~]# partprobe
Use fdisk to partition and format a 20G disk.
< pre lang="sh" line="1" escaped="true">[root@MyCloudServer ~]# fdisk /dev/xvdb #Partition
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.Command(m for help): p #Print current partition
Disk /dev/xvdb:21.5 GB,21474836480 bytes,41943040 sectors
Units = sectors of1*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:0x25dec84b
Device Boot Start End Blocks Id System
/dev/xvdb1 2048419430392097049683 Linux
Command(m for help): d #Delete the first partition
Selected partition 1
Partition 1 is deleted
Command(m for help): n #New partition
Partition type:
p primary(0 primary,0 extended,4 free)
e extended
Select(default p): p #Primary partition
Partition number(1-4,default1): #Enter directly, the default is 1
First sector(2048-41943039,default2048): #Enter directly
Using default value 2048
Last sector,+sectors or +size{K,M,G}(2048-41943039,default41943039):
Using default value 41943039
Partition 1of type Linux and of size 20 GiB is setCommand(m for help): t #Set partition type
Selected partition 1
Hex code(type L to list all codes): 8e #8e is LVM
Changed type of partition 'Linux' to 'Linux LVM'Command(m for help): w #Write changes to hard drive
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.[root@MyCloudServer ~]# mkfs.ext4 /dev/xvdb1 #Format the partition just created
mke2fs 1.42.9(28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=4096(log=2)
Fragment size=4096(log=2)
Stride=0 blocks, Stripe width=0 blocks
1310720 inodes,5242624 blocks
262131 blocks(5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=2153775104160 block groups
32768 blocks per group,32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:32768,98304,163840,229376,294912,819200,884736,1605632,2654208,4096000
Allocating group tables: done
Writing inode tables: done
Creating journal(32768 blocks): done
Writing superblocks and filesystem accounting information: done
After the formatting is complete, we start to create LVM.
[ root@MyCloudServer ~]# pvcreate /dev/xvdb1 #Create LVM and delete are reversed, that is, create PV first
WARNING: ext4 signature detected on /dev/xvdb1 at offset 1080. Wipe it?[y/n]: y
Wiping ext4 signature on /dev/xvdb1.
Physical volume "/dev/xvdb1" successfully created
[ root@MyCloudServer ~]# pvscan #Check, xvdb1 has created PV, but it has not been added to VG.
PV /dev/xvda2 VG centos lvm2 [9.51 GiB /40.00 MiB free]
PV /dev/xvdb1 lvm2 [20.00 GiB]
Total:2[29.51 GiB]/in use:1[9.51 GiB]/in no VG:1[20.00 GiB][root@MyCloudServer ~]# vgextend centos /dev/xvdb1 #Then to expand the VG of the root partition, the VG name is centos
Volume group "centos" successfully extended
[ root@MyCloudServer ~]# pvscan #Check after the addition is successful, xvdb1 has been added to VG centos.
PV /dev/xvda2 VG centos lvm2 [9.51 GiB /40.00 MiB free]
PV /dev/xvdb1 VG centos lvm2 [20.00 GiB /20.00 GiB free]
Total:2[29.50 GiB]/in use:2[29.50 GiB]/in no VG:0[0][root@MyCloudServer ~]# pvs #View pv
PV VG Fmt Attr PSize PFree
/dev/xvda2 centos lvm2 a--9.51g 40.00m
/dev/xvdb1 centos lvm2 a--20.00g 20.00g
[ root@MyCloudServer ~]# lvdisplay #View lv
- - - Logical volume ---
LV Path /dev/centos/swap
LV Name swap
VG Name centos
LV UUID Bs14H4-uzvw-o8NK-FwAH-6vlQ-wOR8-R7Yx0n
LV Write Access read/write
LV Creation host, time localhost,2015-07-2211:01:55+0800
LV Status available
# open 2
LV Size 1.00 GiB
Current LE 256
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:1--- Logical volume ---
LV Path /dev/centos/root
LV Name root
VG Name centos
LV UUID jH3frG-mRfk-hf0o-0KS6-rcth-aklH-BAciW4
LV Write Access read/write
LV Creation host, time localhost,2015-07-2211:01:55+0800
LV Status available
# open 1
LV Size 8.47 GiB
Current LE 2168
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:0[root@MyCloudServer ~]# vgs #Check vg, see the size of vg centos is 29.5g, free space is 20.04g
VG #PV #LV #SN Attr VSize VFree
centos 220 wz--n-29.50g 20.04g
[ root@MyCloudServer ~]# lvs #View lv
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
root centos -wi-ao----8.47g
swap centos -wi-ao----1.00g
[ root@MyCloudServer ~]# lvextend -L +20G /dev/centos/root #-L: Specify the size of the logical volume, the unit is "kKmMgGtT" bytes;+20G means adding 20G space
Size of logical volume centos/root changed from8.47GiB(2168 extents) to 28.47GiB(7288 extents).
Logical volume root successfully resized
[ root@MyCloudServer ~]# lvs #Check lv, you have seen that 20G has been added
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
root centos -wi-ao----28.47g
swap centos -wi-ao----1.00g
At this point, the LVM of the root partition has been expanded. Next we have to test the results of our toss.
Checking the disk space at this time, there is no additional 20G space.
[ root@MyCloudServer ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/centos-root 8.5G 930M 7.6G 11% /
devtmpfs 873M 0 873M 0%/dev
tmpfs 893M 0 893M 0%/dev/shm
tmpfs 893M 8.2M 885M 1%/run
tmpfs 893M 0 893M 0%/sys/fs/cgroup
/dev/xvda1 497M 161M 337M 33%/boot
Because the system currently does not know that you have expanded the capacity, refresh the disk partition information. Note that the refresh command of extX partition format and xfs partition format is different.
[ root@MyCloudServer ~]# resize2fs /dev/centos/root #Suitable for extX partition format
resize2fs 1.42.9(28-Dec-2013)
resize2fs: Bad magic number insuper-block while trying to open /dev/centos/root
Couldn't find valid filesystem superblock.[root@MyCloudServer ~]# xfs_growfs /dev/centos/root #Suitable for xfs partition format
meta-data=/dev/mapper/centos-root isize=256 agcount=4, agsize=555008 blks
= sectsz=512 attr=2, projid32bit=1= crc=0 finobt=0
data = bsize=4096 blocks=2220032, imaxpct=25= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=0
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 from2220032 to 7462912
Check the hard disk information again, you can see that the space of the root partition has been added successfully.
[ root@MyCloudServer ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/centos-root 29G 930M 28G 4% /
devtmpfs 873M 0 873M 0%/dev
tmpfs 893M 0 893M 0%/dev/shm
tmpfs 893M 8.2M 885M 1%/run
tmpfs 893M 0 893M 0%/sys/fs/cgroup
/dev/xvda1 497M 161M 337M 33%/boot
Recommended Posts