因为KVM虚拟化架构,如果您增加了硬盘,请进入系统自行扩展分区。

 

虚拟机磁盘在创建时就固定了大小,后期要新增磁盘空间的话,只能扩容当前分区空间或者新增磁盘。

一、当前硬盘扩容

kvm虚拟机磁盘格式分为raw和qcow2两种,扩容方式命令相同,区别是后缀名

查看当前硬盘空间并扩容镜像磁盘空间

# qemu-img info vm5.qcow2
image: vm5.qcow2
file format: qcow2
virtual size: 20G (21474836480 bytes)
disk size: 1.6G
cluster_size: 65536
Format specific information:
compat: 1.1
lazy refcounts: true

# qemu-img resize /data/kvm/vm5.qcow2 +10G //镜像空间加10G
Image resized.

# qemu-img info vm5.qcow2
image: vm5.qcow2
file format: qcow2
virtual size: 30G (32212254720 bytes)
disk size: 1.6G
cluster_size: 65536
Format specific information:
compat: 1.1
lazy refcounts: true

重启虚拟机,查看磁盘空间已经调整,挂载空间还需要调整

# virsh shutdown vm5
# virsh start vm5
# virsh console vm5
# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root
19G 697M 17G 4% /
tmpfs 246M 0 246M 0% /dev/shm
/dev/vda1 477M 34M 418M 8% /boot

# fdisk -l
Disk /dev/vda: 32.2 GB, 32212254720 bytes
16 heads, 63 sectors/track, 62415 cylinders
Units = cylinders of 1008 * 512 = 516096 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00070304

Device Boot Start End Blocks Id System
/dev/vda1 * 3 1018 512000 83 Linux
/dev/vda2 1018 41611 20458496 8e Linux LVM

Disk /dev/mapper/VolGroup-lv_root: 18.8 GB, 18798870528 bytes
255 heads, 63 sectors/track, 2285 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

Disk /dev/mapper/VolGroup-lv_swap: 2147 MB, 2147483648 bytes
255 heads, 63 sectors/track, 261 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

 

新增的空间进行分区

# fdisk /dev/vda

WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
switch off the mode (command 'c') and change display units to
sectors (command 'u').

Command (m for help): p

Disk /dev/vda: 32.2 GB, 32212254720 bytes
16 heads, 63 sectors/track, 62415 cylinders
Units = cylinders of 1008 * 512 = 516096 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00070304

Device Boot Start End Blocks Id System
/dev/vda1 * 3 1018 512000 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/vda2 1018 41611 20458496 8e Linux LVM
Partition 2 does not end on cylinder boundary.

Command (m for help): n //新增分区
Command action
e extended
p primary partition (1-4)
p //主分区
Partition number (1-4): 3
First cylinder (1-62415, default 1): 41611
Last cylinder, +cylinders or +size{K,M,G} (41611-62415, default 62415):
Using default value 62415

Command (m for help): p

Disk /dev/vda: 32.2 GB, 32212254720 bytes
16 heads, 63 sectors/track, 62415 cylinders
Units = cylinders of 1008 * 512 = 516096 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00070304

Device Boot Start End Blocks Id System
/dev/vda1 * 3 1018 512000 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/vda2 1018 41611 20458496 8e Linux LVM
Partition 2 does not end on cylinder boundary.
/dev/vda3 41611 62415 10485640 83 Linux

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.

把 /dev/vda3 加入到 lvm 里面去,如 /dev/vda3 设备不存在则重启虚拟机即可

# pvcreate /dev/vda3 //创建物理卷
Physical volume "/dev/vda3" successfully created
# pvs //查看卷
PV VG Fmt Attr PSize PFree
/dev/vda2 VolGroup lvm2 a--u 19.51g 0
/dev/vda3 lvm2 ---- 10.00g 10.00g
# vgextend VolGroup /dev/vda3 //VolGroup虚拟卷扩展,vda3加入到VolGroup(这个名字从上面pvs中VG字段获取)
Volume group "VolGroup" successfully extended
# pvs
PV VG Fmt Attr PSize PFree
/dev/vda2 VolGroup lvm2 a--u 19.51g 0
/dev/vda3 VolGroup lvm2 a--u 10.00g 10.00g
# vgs
VG #PV #LV #SN Attr VSize VFree
VolGroup 2 2 0 wz--n- 29.50g 10.00g
# lvs //查看逻辑卷
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
lv_root VolGroup -wi-ao---- 17.51g
lv_swap VolGroup -wi-ao---- 2.00g
# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root
18G 696M 16G 5% /
tmpfs 939M 0 939M 0% /dev/shm
/dev/vda1 477M 40M 413M 9% /boot
# lvextend -l +100%FREE /dev/mapper/VolGroup-lv_root //扩展卷,卷名从 df -h 获取
Size of logical volume VolGroup/lv_root changed from 17.51 GiB (4482 extents) to 27.50 GiB (7041 extents).
Logical volume lv_root successfully resized.
# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root
18G 696M 16G 5% /
tmpfs 939M 0 939M 0% /dev/shm
/dev/vda1 477M 40M 413M 9% /boot

使用 resize2fs 或 xfs_growfs 命令进行最后扩容

# resize2fs /dev/mapper/VolGroup-lv_root
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/mapper/VolGroup-lv_root is mounted on /; on-line resizing required
old desc_blocks = 2, new_desc_blocks = 2
Performing an on-line resize of /dev/mapper/VolGroup-lv_root to 7209984 (4k) blocks.
The filesystem on /dev/mapper/VolGroup-lv_root is now 7209984 blocks long.

# xfs (CentOS 7)文件系统格式使用 xfs_growfs 命令扩容
# xfs_growfs /dev/mapper/VolGroup-lv_root

# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root
27G 696M 25G 3% /
tmpfs 939M 0 939M 0% /dev/shm
/dev/vda1 477M 40M 413M 9% /boot

二、新增硬盘

创建磁盘

# qemu-img create -f qcow2 vm5_2.qcow2 5G
Formatting 'vm5_2.qcow2', fmt=qcow2 size=5368709120 encryption=off cluster_size=65536 lazy_refcounts=off

1
2

关闭虚拟机并修改配置文件,添加如下配置

# virsh destroy vm5
Domain vm5 destroyed
# virsh edit vm5
......
<disk type='file' device='disk'>
<driver name='qemu' type='qcow2' cache='none'/>
<source file='/data/kvm/vm5_2.qcow2'/>
<target dev='vdb' bus='virtio'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x08' function='0x0'/>
</disk>
......

开机虚拟机,格式化硬盘并挂载

# virsh start vm5
# virsh console vm5
# mkfs.ext4 /dev/vdb
# mount /dev/vdb /data
————————————————
版权声明:本文为CSDN博主「小王格子」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_25854057/article/details/109813314

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。