lvm 虚拟机扩容

1.使用命令将qcow2 文件扩容

# 文件之前已经设置了50G,现在增加50G
qemu-img resize /data/img/rds1.qcow2 +50G

# 查看文件信息,执行此命令需要虚拟机关机
qemu-img info /data/img/rds1.qcow2 

root@yuuri-MS-7C94:/data/img# qemu-img info /data/img/rds1.qcow2
image: /data/img/rds1.qcow2
file format: qcow2
virtual size: 100 GiB (107374182400 bytes)
disk size: 37.3 GiB
cluster_size: 65536
Snapshot list:
ID        TAG                     VM SIZE                DATE       VM CLOCK
1         1651040447             3.27 GiB 2022-04-27 14:20:47   00:08:53.146
Format specific information:
    compat: 1.1
    lazy refcounts: true
    refcount bits: 16
    corrupt: false

2.添加完成后进入虚拟机

virsh console rds1

# 进入虚拟机后查看空间已经增加,但是无法使用

[root@localhost ~]# lsblk
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sr0              11:0    1 1024M  0 rom
vda             252:0    0  100G  0 disk
├─vda1          252:1    0    1G  0 part /boot
└─vda2          252:2    0   49G  0 part
  ├─centos-root 253:0    0 45.1G  0 lvm  /
  └─centos-swap 253:1    0  3.9G  0 lvm  [SWAP]

3.将未分配的空间进行初始格式化

[root@localhost ~]# fpdisk /dev/vda
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):



# 下一步选择n ,添加一个新的分区


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): n
Partition type:
   p   primary (2 primary, 0 extended, 2 free)
   e   extended
Select (default p): p
Partition number (3,4, default 3): 3
First sector (104857600-209715199, default 104857600):
Using default value 104857600
Last sector, +sectors or +size{K,M,G} (104857600-209715199, default 209715199):
Using default value 209715199
Partition 3 of type Linux and of size 50 GiB is set



# 打印分区信息,并将分区修改为lvm 格式

Command (m for help): p

Disk /dev/vda: 107.4 GB, 107374182400 bytes, 209715200 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: 0x00089f98

   Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *        2048     2099199     1048576   83  Linux
/dev/vda2         2099200   104857599    51379200   8e  Linux LVM
/dev/vda3       104857600   209715199    52428800   83  Linux

Command (m for help): t
Partition number (1-3, default 3):
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'



# 最后一步,输入w 退出并保存结果

Command (m for help): m
Command action
   a   toggle a bootable flag
   b   edit bsd disklabel
   c   toggle the dos compatibility flag
   d   delete a partition
   g   create a new empty GPT partition table
   G   create an IRIX (SGI) partition table
   l   list known partition types
   m   print this menu
   n   add a new partition
   o   create a new empty DOS partition table
   p   print the partition table
   q   quit without saving changes
   s   create a new empty Sun disklabel
   t   change a partition's system id
   u   change display/entry units
   v   verify the partition table
   w   write table to disk and exit
   x   extra functionality (experts only)

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.

4.使用lvm 进行扩容

# 首先执行partprobe
[root@localhost ~]# partprobe

# 执行lsblk 分区信息,已经增加
[root@localhost ~]# lsblk
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sr0              11:0    1 1024M  0 rom
vda             252:0    0  100G  0 disk
├─vda1          252:1    0    1G  0 part /boot
├─vda2          252:2    0   49G  0 part
│ ├─centos-root 253:0    0 45.1G  0 lvm  /
│ └─centos-swap 253:1    0  3.9G  0 lvm  [SWAP]
└─vda3          252:3    0   50G  0 part



# 添加pv
[root@localhost ~]# pvcreate /dev/vda3
  Physical volume "/dev/vda3" successfully created.


# 查看pv
[root@localhost ~]# pvs
  PV         VG     Fmt  Attr PSize   PFree
  /dev/vda2  centos lvm2 a--  <49.00g  4.00m
  /dev/vda3         lvm2 ---   50.00g 50.00g



# 查看vg
[root@localhost ~]# vgs
  VG     #PV #LV #SN Attr   VSize   VFree
  centos   1   2   0 wz--n- <49.00g 4.00m

# 将已存在的vg 扩容
[root@localhost ~]# vgextenld centos /dev/vda3
  Volume group "centos" successfully extended

[root@localhost ~]# vgs
  VG     #PV #LV #SN Attr   VSize  VFree
  centos   2   2   0 wz--n- 98.99g 50.00g

# 扩容lv 
[root@localhost ~]# lvextend -l  +100%FREE /dev/mapper/centos-root
  Size of logical volume centos/root changed from <45.12 GiB (11550 extents) to <95.12 GiB (24350 extents).l
  Logical volume centos/root successfully resized.

[root@localhost ~]# lvs
  LV   VG     Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  root centos -wi-ao---- <95.12g
  swap centos -wi-ao----  <3.88g


# lsblk 查看已经挂载
[root@localhost ~]# lsblk
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sr0              11:0    1 1024M  0 rom
vda             252:0    0  100G  0 disk
├─vda1          252:1    0    1G  0 part /boot
├─vda2          252:2    0   49G  0 part
│ ├─centos-root 253:0    0 95.1G  0 lvm  /
│ └─centos-swap 253:1    0  3.9G  0 lvm  [SWAP]
└─vda3          252:3    0   50G  0 part
  └─centos-root 253:0    0 95.1G  0 lvm  /

# df 查看还未更新
[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  9.5M  1.9G   1% /run
tmpfs                    1.9G     0  1.9G   0% /sys/fs/cgroup
/dev/mapper/centos-root   46G   32G   14G  70% /
/dev/vda1               1014M  150M  865M  15% /boot


# 使用命令xfs_growfs 更新

[root@localhost ~]# xfs_growfs /dev/mapper/centos-root
meta-data=/dev/mapper/centos-root isize=512    agcount=4, agsize=2956800 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0 spinodes=0
data     =                       bsize=4096   blocks=11827200, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal               bsize=4096   blocks=5775, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
data blocks changed from 11827200 to 24934400



# 执行完成后 成功更新

[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  9.5M  1.9G   1% /run
tmpfs                    1.9G     0  1.9G   0% /sys/fs/cgroup
/dev/mapper/centos-root   96G   32G   64G  34% /
/dev/vda1               1014M  150M  865M  15% /boot

All posts

Other pages