CentOS下kvm虚拟机磁盘扩展
时间:2016-01-12 21:01 来源:linux.it.net.cn 作者:IT
两种方式:
1.添加一块新的虚拟磁盘
2.扩充原有磁盘的容量大小
注意:
只有磁盘格式为qcow2的磁盘才支持磁盘快照和磁盘动态扩展.
测试的虚拟机磁盘大小和格式.
方法一:添加新磁盘
1.查看已存在盘的大小
[root@kvm-node154 vm]# qemu-img info api1-qcow2.img
image: api1-qcow2.img
file format: qcow2
virtual size: 8.0G (8589934592 bytes)
disk size: 1.5G
cluster_size: 65536
<disk type='file' device='disk'>
<driver name='qemu' type='qcow2' cache='none'/>
<source file='/vm/api1-qcow2.img'/>
<target dev='vda' bus='virtio'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x0'/>
</disk>
2.添加一块qcow2的盘,大小为10G
cd /vm && qemu-img create -f qcow2 api1-qcow2_add1.img 8G
[root@kvm-node154 vm]# qemu-img info api1-qcow2_add1.img
image: api1-qcow2_add1.img
file format: qcow2
virtual size: 10G (10737418240 bytes)
disk size: 140K
cluster_size: 65536
[root@kvm-node154 vm]# ll /vm
总用量 3161404
-rwxr-xr-x 1 qemu qemu 1580662784 1月 11 14:55 api2-qcow2.img
-rw-r--r-- 1 root root 197120 1月 11 14:53 api1-qcow2_add1.img
-rw-r--r-- 1 qemu qemu 1653407744 1月 11 14:55 api1-qcow2.img
3.添加api1-qcow2_add1.img到配置文件中
[root@kvm-node154 vm]# virsh shutdown api1
域 api4 被关闭
[root@kvm-node154 vm]# virsh list --all
Id 名称 状态
----------------------------------------------------
2 api2 running
- api1 关闭
virsh edit api1,添加新磁盘
<disk type='file' device='disk'>
<driver name='qemu' type='qcow2' cache='none'/>
<source file='/vm/api1-qcow2_add1.img'/>
<target dev='vdb' bus='virtio'/>
</disk>
4.登录到api1终端,然后查看,并分区/dev/vdb
[root@vm-api1 ~]# fdisk -l
Disk /dev/vdb: 8589 MB, 8589934592 bytes
16 heads, 63 sectors/track, 16644 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: 0x00000000
5.格式化分区/dev/vdb,并且转换为lvm格式.
fdisk /dev/vdb >> lvm
[root@vm-api1 ~]# fdisk -l
Disk /dev/vdb: 8589 MB, 8589934592 bytes
16 heads, 63 sectors/track, 16644 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: 0xa91642d5
Device Boot Start End Blocks Id System
/dev/vdb1 1 16644 8388544+ 8e Linux LVM
[root@vm-api1 ~]# pvcreate /dev/vdb1
Physical volume "/dev/vdb1" successfully created
[root@vm-api1 ~]# vgs
VG #PV #LV #SN Attr VSize VFree
VolGroup 1 2 0 wz--n- 7.51g 0
[root@vm-api1 ~]# vgextend VolGroup /dev/vdb1
Volume group "VolGroup" successfully extended
[root@vm-api1 ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
lv_root VolGroup -wi-ao---- 6.71g
lv_swap VolGroup -wi-ao---- 816.00m
[root@vm-api1 ~]# lvextend -l +100%FREE /dev/VolGroup/lv_root
Size of logical volume VolGroup/lv_root changed from 6.71 GiB (1718 extents) to 14.71 GiB (3765 extents).
Logical volume lv_root successfully resized
[root@vm-api1 ~]# resize2fs /dev/VolGroup/lv_root
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/VolGroup/lv_root is mounted on /; on-line resizing required
old desc_blocks = 1, new_desc_blocks = 1
Performing an on-line resize of /dev/VolGroup/lv_root to 3855360 (4k) blocks.
The filesystem on /dev/VolGroup/lv_root is now 3855360 blocks long.
6.再次查看vm的磁盘大小
[root@vm-api1 ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root
15G 652M 13G 5% /
tmpfs 246M 0 246M 0% /dev/shm
/dev/vda1 477M 25M 427M 6% /boot
方法二:扩充原有磁盘的大小
1.查看原磁盘的大小
[root@kvm-node154 vm]# qemu-img info /vm/api2-qcow2.img
image: /vm/api2-qcow2.img
file format: qcow2
virtual size: 8.0G (8589934592 bytes)
disk size: 2.4G
cluster_size: 65536
Snapshot list:
ID TAG VM SIZE DATE VM CLOCK
2 api2_ftp 320M 2016-01-08 16:59:52 01:17:18.639
3 api2_httpd 385M 2016-01-08 17:27:16 00:53:48.096
2.关闭vm,然后直接扩充api2-qcow2.img
[root@kvm-node154 vm]# virsh shutdown api2
域 api2 被关闭
[root@kvm-node154 vm]# virsh list --all
Id 名称 状态
----------------------------------------------------
3 api1 running
- api2 关闭
[root@kvm-node154 vm]# qemu-img resize api2-qcow2.img +8G
This image format does not support resize //提示不能这种格式不支持扩充
[root@kvm-node154 vm]# virsh
欢迎使用 virsh,虚拟化的交互式终端。
输入:'help' 来获得命令的帮助信息
'quit' 退出
virsh # snapshot-list api2
名称 Creation Time 状态
------------------------------------------------------------
api2_ftp 2016-01-08 16:59:52 +0800 running
api2_httpd 2016-01-08 17:27:16 +0800 running
virsh # snapshot-delete api
api1-qcow2_add1.img api1-qcow2.img api2-qcow2.img
virsh # snapshot-delete api2 api2_ftp
Domain snapshot api2_ftp deleted
virsh # snapshot-delete api2 api2_httpd
Domain snapshot api2_httpd deleted
[root@kvm-node154 vm]# qemu-img resize api2-qcow2.img +8G
Image resized.
3.查看已经扩充的磁盘
[root@kvm-node154 vm]# qemu-img info api2-qcow2.img
image: api2-qcow2.img
file format: qcow2
virtual size: 16G (17179869184 bytes)
disk size: 2.4G
cluster_size: 65536
4.启动vm,然后分区,并格式化
[root@kvm-node154 vm]# virsh start api2
域 api2 已开始
[root@vm-api2 ~]# pvcreate /dev/vda3
Physical volume "/dev/vda3" successfully created
[root@vm-api2 ~]# vgs
VG #PV #LV #SN Attr VSize VFree
VolGroup 1 2 0 wz--n- 7.51g 0
[root@vm-api2 ~]# vgextend VolGroup /dev/vda3
Volume group "VolGroup" successfully extended
[root@vm-api2 ~]# lvextend -l +100%FREE /dev/VolGroup/lv_root
Size of logical volume VolGroup/lv_root changed from 6.71 GiB (1718 extents) to 14.71 GiB (3765 extents).
Logical volume lv_root successfully resized
[root@vm-api2 ~]# resize2fs /dev/VolGroup/lv_root
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/VolGroup/lv_root is mounted on /; on-line resizing required
old desc_blocks = 1, new_desc_blocks = 1
Performing an on-line resize of /dev/VolGroup/lv_root to 3855360 (4k) blocks.
The filesystem on /dev/VolGroup/lv_root is now 3855360 blocks long.
[root@vm-api2 ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root
15G 655M 13G 5% /
tmpfs 246M 0 246M 0% /dev/shm
/dev/vda1 477M 25M 427M 6% /boot
成功添加了磁盘.
(责任编辑:IT)
两种方式:
1.添加一块新的虚拟磁盘 2.扩充原有磁盘的容量大小
注意: 只有磁盘格式为qcow2的磁盘才支持磁盘快照和磁盘动态扩展. 测试的虚拟机磁盘大小和格式.
方法一:添加新磁盘 1.查看已存在盘的大小
2.添加一块qcow2的盘,大小为10G
3.添加api1-qcow2_add1.img到配置文件中
virsh edit api1,添加新磁盘
4.登录到api1终端,然后查看,并分区/dev/vdb
5.格式化分区/dev/vdb,并且转换为lvm格式.
6.再次查看vm的磁盘大小
方法二:扩充原有磁盘的大小 1.查看原磁盘的大小
2.关闭vm,然后直接扩充api2-qcow2.img
域 api2 被关闭
3.查看已经扩充的磁盘
4.启动vm,然后分区,并格式化
成功添加了磁盘. (责任编辑:IT) |