Loading

Linux虚机磁盘和lvm扩容

Linux虚机磁盘和lvm扩容

一台RedHat8.2 Linux虚拟机将原来的磁盘从60G扩展到80G,且希望将磁盘的空闲部分添加到现有PV和VG,再扩容给/var 5G、根目录5G,剩余空间分配和/home。当前的环境是:除了boot其他分区都采用lvm。

当前环境#

[root@Redhat82-template ~]# 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 9.0M 1.9G 1% /run
tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup
/dev/mapper/rhel-root 15G 5.3G 9.8G 36% /
/dev/mapper/rhel-tmp 5.0G 69M 5.0G 2% /tmp
/dev/mapper/rhel-home 31G 282M 31G 1% /home
/dev/mapper/rhel-var 5.0G 418M 4.6G 9% /var
/dev/sda1 495M 283M 213M 58% /boot
tmpfs 378M 0 378M 0% /run/user/1010
tmpfs 378M 0 378M 0% /run/user/800
tmpfs 378M 0 378M 0% /run/user/0
[root@Redhat82-template ~]# vgdisplay 
--- Volume group ---
VG Name rhel
System ID 
Format lvm2
Metadata Areas 1
Metadata Sequence No 6
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 5
Open LV 5
Max PV 0
Cur PV 1
Act PV 1
VG Size 59.50 GiB
PE Size 4.00 MiB
Total PE 15233
Alloc PE / Size 15232 / 59.50 GiB
Free PE / Size 1 / 4.00 MiB
VG UUID figpM5-yN3p-Vic6-x2ys-rj73-wx19-yQ4lEJ

虚拟化平台扩容磁盘 #

在虚拟化平台扩容/dev/sda磁盘重启系统,sda容量已经变成80G了。

[root@Redhat82-template ~]# fdisk -l
Disk /dev/sda: 80 GiB, 85899345920 bytes, 167772160 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
Disklabel type: dos
Disk identifier: 0x31237ad5

Device Boot Start End Sectors Size Id Type
/dev/sda1 * 2048 1026047 1024000 500M 83 Linux
/dev/sda2 1026048 125822975 124796928 59.5G 8e Linux LVM

略……

resize LVM所在磁盘分区#

[root@Redhat82-template ~]# parted /dev/sda
GNU Parted 3.2
Using /dev/sda
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) p                                                                
Model: VMware Virtual disk (scsi)
Disk /dev/sda: 85.9GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags: 

Number  Start   End     Size    Type     File system  Flags
 1      1049kB  525MB   524MB   primary  xfs          boot
 2      525MB   64.4GB  63.9GB  primary               lvm

(parted)                                                                  
(parted) ?                                                                
  align-check TYPE N                        check partition N for TYPE(min|opt) alignment
  help [COMMAND]                           print general help, or help on COMMAND
  mklabel,mktable LABEL-TYPE               create a new disklabel (partition table)
  mkpart PART-TYPE [FS-TYPE] START END     make a partition
  name NUMBER NAME                         name partition NUMBER as NAME
  print [devices|free|list,all|NUMBER]     display the partition table, available devices, free space, all found partitions, or a particular partition
  quit                                     exit program
  rescue START END                         rescue a lost partition near START and END
  resizepart NUMBER END                    resize partition NUMBER
  rm NUMBER                                delete partition NUMBER
  select DEVICE                            choose the device to edit
  disk_set FLAG STATE                      change the FLAG on selected device
  disk_toggle [FLAG]                       toggle the state of FLAG on selected device
  set NUMBER FLAG STATE                    change the FLAG on partition NUMBER
  toggle [NUMBER [FLAG]]                   toggle the state of FLAG on partition NUMBER
  unit UNIT                                set the default unit to UNIT
  version                                  display the version number and copyright information of GNU Parted
(parted) resizepart 2 80G              #扩容分区 resizepart “分区号” “分区结束位置”                                                
(parted) p                                                                
Model: VMware Virtual disk (scsi)
Disk /dev/sda: 85.9GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags: 

Number  Start   End     Size    Type     File system  Flags
 1      1049kB  525MB   524MB   primary  xfs          boot
 2      525MB   80.0GB  79.5GB  primary               lvm

(parted) resizepart 2 85.9G                                               
(parted)                                                                  
(parted)                                                                  
(parted) p                                                                
Model: VMware Virtual disk (scsi)
Disk /dev/sda: 85.9GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags: 

Number  Start   End     Size    Type     File system  Flags
 1      1049kB  525MB   524MB   primary  xfs          boot
 2      525MB   85.9GB  85.4GB  primary               lvm                                                                 
(parted) quit                                                             
Information: You may need to update /etc/fstab.
                         

resize PV和VG[#](https://www.cnblogs.com/my-show-time/p/14357675.html#resize pv和vg)

虽然分区/dev/sda2容量已经更新,但VG Size和PV Size还是和之前一样没有任何变化。需要对PV和VG进行resize操作。

[root@Redhat82-template ~]# vgdisplay 
--- Volume group ---
VG Name rhel
System ID 
Format lvm2
Metadata Areas 1
Metadata Sequence No 6
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 5
Open LV 5
Max PV 0
Cur PV 1
Act PV 1
VG Size 59.50 GiB
PE Size 4.00 MiB
Total PE 15233
Alloc PE / Size 15232 / 59.50 GiB
Free PE / Size 1 / 4.00 MiB
VG UUID figpM5-yN3p-Vic6-x2ys-rj73-wx19-yQ4lEJ 
[root@Redhat82-template ~]# pvdisplay 
--- Physical volume ---
PV Name /dev/sda2
VG Name rhel
PV Size <59.51 GiB / not usable 4.00 MiB
Allocatable yes 
PE Size 4.00 MiB
Total PE 15233
Free PE 1
Allocated PE 15232
PV UUID Dj4YUN-21Qw-azH2-0DeA-U2tg-ehk9-JabmHy

pvresize

[root@Redhat82-template ~]# pvresize /dev/sda2
Physical volume "/dev/sda2" changed
1 physical volume(s) resized or updated / 0 physical volume(s) not resized
pvchange pvck pvcreate pvdisplay pvmove pvremove pvresize pvs pvscan 
[root@Redhat82-template ~]# pvscan 
PV /dev/sda2 VG rhel lvm2 [<79.51 GiB / <20.01 GiB free]
Total: 1 [<79.51 GiB] / in use: 1 [<79.51 GiB] / in no VG: 0 [0 ]
[root@Redhat82-template ~]# pvdisplay 
--- Physical volume ---
PV Name /dev/sda2
VG Name rhel
PV Size <79.51 GiB / not usable 2.00 MiB        #可以看到PV容量已经更新
Allocatable yes 
PE Size 4.00 MiB
Total PE 20354
Free PE 5122
Allocated PE 15232
PV UUID Dj4YUN-21Qw-azH2-0DeA-U2tg-ehk9-JabmHy

PV容量更新后VG容量也自动更新为79.51 GiB

[root@Redhat82-template ~]# vgdisplay 
--- Volume group ---
VG Name rhel
System ID 
Format lvm2
Metadata Areas 1
Metadata Sequence No 9
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 5
Open LV 5
Max PV 0
Cur PV 1
Act PV 1
VG Size <79.51 GiB
PE Size 4.00 MiB
Total PE 20354
Alloc PE / Size 15232 / 59.50 GiB
Free PE / Size 5122 / <20.01 GiB
VG UUID figpM5-yN3p-Vic6-x2ys-rj73-wx19-yQ4lEJ

LV扩容#

var分区所在lv添加5G空间

[root@Redhat82-template ~]# 
[root@Redhat82-template ~]# lvresize -L +5G /dev/rhel/var
Size of logical volume rhel/var changed from 5.00 GiB (1280 extents) to 10.00 GiB (2560 extents).
Logical volume rhel/var successfully resized.
[root@Redhat82-template ~]# 
[root@Redhat82-template ~]# 
[root@Redhat82-template ~]# lvdisplay 
--- Logical volume ---
LV Path /dev/rhel/var
LV Name var
VG Name rhel
LV UUID JU6gwJ-nXCK-z3Gf-zbtO-fp5u-9x7j-3q379J
LV Write Access read/write
LV Creation host, time localhost, 2020-06-15 17:09:09 +0800
LV Status available
# open 1
LV Size 10.00 GiB
Current LE 2560
Segments 2
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:2
略……

resize文件系统#

使用resize2fs更新文件系统报错。原来resize2fs只适用于ext*的文件系统。当前文件系统为xfs,要用xfs_growfs。

[root@Redhat82-template ~]# resize2fs /dev/rhel/var
resize2fs 1.45.4 (23-Sep-2019)
resize2fs: Bad magic number in super-block while trying to open /dev/rhel/var
Couldn't find valid filesystem superblock.

[root@Redhat82-template var]# xfs_growfs /dev/rhel/var
meta-data=/dev/mapper/rhel-var isize=512 agcount=4, agsize=327680 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=1, sparse=1, rmapbt=0
= reflink=1
data = bsize=4096 blocks=1310720, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0, ftype=1
log =internal log 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 1310720 to 2621440
[root@Redhat82-template var]# 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 9.0M 1.9G 1% /run
tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup
/dev/mapper/rhel-root 15G 5.3G 9.8G 36% /
/dev/mapper/rhel-tmp 5.0G 69M 5.0G 2% /tmp
/dev/mapper/rhel-home 31G 282M 31G 1% /home
/dev/mapper/rhel-var 10G 455M 9.6G 5% /var
/dev/sda1 495M 283M 213M 58% /boot
tmpfs 378M 0 378M 0% /run/user/1010
tmpfs 378M 0 378M 0% /run/user/800
tmpfs 378M 0 378M 0% /run/user/0

剩下的/home/ 和根分区如法炮制即可。

总结:LVM扩容可能更多时候我们想到是添加磁盘创建PV扩容VG最后再扩容LV。这样做当然没有什么问题,但毕竟多出一个磁盘和PV显得有些乱。其实对于虚拟机大都是支持在原盘基础上扩容的,那么更好的方式是将扩容的原磁盘空闲区域添加到LVM所在分区再resize PV,后面就和普通的LVM扩容没啥区别了,完成LV扩容和文件系统resize就可以了。如此,我们的虚机就不会因为20G的空间多出一块盘和一个PV。

posted @ 2021-02-01 17:11  五月的麦田  阅读(571)  评论(0编辑  收藏  举报