接下来这篇文章主要介绍KVM虚拟机的基本管理:
一、virsh 命令常用操作
二、克隆虚拟机
三、磁盘镜像格式转换
四、快照管理
五、磁盘扩容
六、调整cpu和内存查看子机配置
七、不重启虚拟机在线增加网卡
八、虚拟机迁移
一、virsh 命令常用操作
1、开启并登入子机
[iyunv@centos ~]# virsh start tpp1
[iyunv@centos ~]# ssh 192.168.0.10
也可以在开启的同时进入子机:
[iyunv@centos ~]# virsh start tpp1 --console
2、关闭子机
[iyunv@centos ~]# virsh shutdown tpp1 //关闭
注意:默认我们没有办法在宿主机直接shutdown自己,我们需要借助于子机上的acpid服务,这个服务是让宿主机可以去调用子机的电源关闭的接口。所以,子机上需要安装并启动acpid服务,若子机没开启acpid服务,虽然显示关闭成功,但查看仍为running状态。
[iyunv@centos ~]# virsh console tpp1
[iyunv@localhost ~]# yum install -y acpid
[iyunv@localhost ~]# /etc/init.d/acpid start
然后Ctrl+] 退出子机
[iyunv@centos ~]# virsh shutdown tpp1
| [iyunv@centos ~]# virsh destroy tpp1 //另一种方法是直接从列表中删除
3、子机随宿主机开机自启
[iyunv@centos ~]# virsh autostart tpp1 //自动启动
[iyunv@centos ~]# virsh autostart --disable tpp1 //解除自动启动
4、列出子机
[iyunv@centos ~]# virsh list //只能列出启动的子机
[iyunv@centos ~]# virsh list --all //把所有子机都列出来
5、删除子机
[iyunv@centos ~]# virsh destroy tpp1
[iyunv@centos ~]# virsh undefine tpp1
[iyunv@centos ~]# rm -f /data/tpp1.img //若为qcow2格式的,删除tpp2.qcow2
6、挂起和恢复子机
[iyunv@centos ~]# virsh suspend tpp1 //挂起
[iyunv@centos ~]# virsh resume tpp1 //恢复
7、退出子机
按ctrl+] 可以返回到宿主机。
二、克隆虚拟机 [iyunv@centos ~]# virt-clone --original tpp1 --name tpp2 --file /data/tpp2.img
说明: 1)克隆之前先关闭待克隆的机器,不然会报错(ERROR 必须暂停或者关闭有要克隆设备的域。) 2)--original tpp1:指定待克隆的机器 3)--name tpp2:指定克隆后机器名 4)--file /data/tpp2.img :指定存放路径(命名可由样本的磁盘镜像格式决定是.qcow2或者.img) |
[iyunv@centos ~]# virsh list --all //可以看到克隆的子机
三、磁盘镜像格式转换 1)查看子机磁盘镜像格式
[iyunv@centos ~]# qemu-img info /data/tpp1.img
image: /data/tpp1.img
file format: raw
virtual size: 10G (10739318784 bytes)
disk size: 1.5G
|
2)raw格式转换为qcow2格式
[iyunv@centos ~]# qemu-img convert -f raw -O qcow2 /data/tpp1.img /data/tpp1.qcow2
说明:其实就是相当于复制了一份,会生成一个qcow2格式的文件(tpp1.qcow2)
3)再次查看子机磁盘镜像格式
[iyunv@centos ~]# qemu-img info /data/tpp1.qcow2
image: /data/tpp1.qcow2
file format: qcow2
virtual size: 10G (10739318784 bytes)
disk size: 392K
cluster_size: 65536
|
4)编辑子机配置文件
[iyunv@centos ~]# virsh edit tpp1
找到:
<driver name='qemu' type='raw' cache='none'/>
<source file='/data/tpp1.img'/>
改为:
<driver name='qemu' type='qcow2' cache='none'/>
<source file='/data/tpp1.qcow2'/> | 说明:编辑该子机配置文件,让它使用新格式的虚拟磁盘镜像文件。命令:virsh edit tpp1 就是进入了该子机的配置文件(/etc/libvirt/qemu/tpp1.xml),跟用vim编辑这个文件一样的用法。
四、快照管理 1、创建快照
[iyunv@centos ~]# virsh snapshot-create tpp1 //提示以下信息则成功
Domain snapshot 1447276326 created |
注意:若磁盘镜像格式为raw格式则会报下面错误:
unsupported configuration: internal snapshot for disk vda unsupported for storage type raw
2、列出快照
[iyunv@centos ~]# virsh snapshot-list tpp1
名称 Creation Time 状态 ------------------------------------------------------------ 1447276326 2015-11-12 05:12:06 +0800 shutoff |
3、查看当前子机的快照版本 [iyunv@centos ~]# virsh snapshot-current tpp1 说明:tpp1子机的快照文件在 /var/lib/libvirt/qemu/snapshot/tpp1/ 目录下 [iyunv@centos ~]# ls /var/lib/libvirt/qemu/snapshot/tpp1/
4、恢复快照
1)首先需要关闭子机
[iyunv@centos ~]# virsh destroy tpp1 2)确认子机是否关闭 [iyunv@centos ~]# virsh domstate tpp1
3) 列出快照 [iyunv@centos ~]# virsh snapshot-list tpp1
名称 Creation Time 状态
------------------------------------------------------------
1447276326 2015-11-12 05:12:06 +0800 shutoff
| 4)恢复快照
[iyunv@centos ~]# virsh snapshot-revert tpp1 1447276326
5、删除快照
[iyunv@centos ~]# virsh snapshot-delete tpp1 1447276326
Domain snapshot 1447276326 deleted |
五、磁盘扩容
有两种方式,一种是扩充容量,另一种是直接增加磁盘。raw格式和qcow2格式的虚拟磁盘扩容步骤是一样的,下面我们对raw格式的虚拟磁盘进行扩容。 1、扩充容量 1)查看虚拟机信息(大小为10G)
[iyunv@centos ~]# qemu-img info /data/tpp1.qcow2
image: /data/tpp1.img file format: raw virtual size: 10G (10739318784 bytes) disk size: 400K cluster_size: 65536 |
2)增加5G [iyunv@centos ~]# qemu-img resize /data/tpp1.img +5G
[iyunv@centos ~]# qemu-img info /data/tpp1.img
image: /data/tpp1.img file format: raw virtual size: 15G (16108027904 bytes) disk size: 400K cluster_size: 65536 |
3)使其生效
[iyunv@centos ~]# virsh destroy tpp1
[iyunv@centos ~]# virsh start tpp1
[iyunv@centos ~]# virsh console tpp1 //进入子机
[iyunv@localhost ~]# fdisk -l //查看磁盘分区已经增加到 16 GB
Disk /dev/vda: 16.1 GB, 16108027904 bytes
16 heads, 63 sectors/track, 31211 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: 0x0003aadd
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 20808 9973760 8e Linux LVM
Partition 2 does not end on cylinder boundary.
.......
|
[iyunv@localhost ~]# df -h //但磁盘挂载的空间并没有增加
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root 8.5G 766M 7.3G 10% /
tmpfs 246M 0 246M 0% /dev/shm
/dev/vda1 485M 33M 427M 8% /boot
4)新增加的空间还没有划分使用,故要继续分区 [iyunv@localhost ~]# 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: 16.1 GB, 16108027904 bytes
16 heads, 63 sectors/track, 31211 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: 0x0003aadd
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 20808 9973760 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-31211, default 1): 20809
Last cylinder, +cylinders or +size{K,M,G} (20809-31211, default 31211): 31211
Command (m for help): p
Disk /dev/vda: 16.1 GB, 16108027904 bytes
16 heads, 63 sectors/track, 31211 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: 0x0003aadd
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 20808 9973760 8e Linux LVM
Partition 2 does not end on cylinder boundary.
/dev/vda3 20809 31211 5243112 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.
|
[iyunv@localhost ~]# reboot //重启生效
5)把/dev/vda3 加入到lvm里面去 [iyunv@localhost ~]# pvcreate /dev/vda3 //创建物理卷 Physical volume "/dev/vda3" successfully created |
[iyunv@localhost ~]# pvs //列出物理卷
PV VG Fmt Attr PSize PFree
/dev/vda2 VolGroup lvm2 a-- 9.51g 0
/dev/vda3 lvm2 a-- 5.00g 5.00g
|
[iyunv@localhost ~]# vgextend VolGroup /dev/vda3 //物理卷加入卷组
Volume group "VolGroup" successfully extended |
[iyunv@localhost ~]# vgs //查看卷组,可看到5G空余
VG #PV #LV #SN Attr VSize VFree
VolGroup 2 2 0 wz--n- 14.50g 5.00g
|
[iyunv@localhost ~]# lvs //查看逻辑卷
LV VG Attr LSize Pool Origin Data% Move Log Cpy%Sync Convert
lv_root VolGroup -wi-ao---- 8.54g
lv_swap VolGroup -wi-ao---- 992.00m
|
[iyunv@localhost ~]# lvextend -l +100%FREE /dev/VolGroup/lv_root //卷组空余加入到lv_root
Extending logical volume lv_root to 13.54 GiB
Logical volume lv_root successfully resized
|
[iyunv@localhost ~]# 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 3548160 (4k) blocks.
The filesystem on /dev/VolGroup/lv_root is now 3548160 blocks long.
|
[iyunv@localhost ~]# df -h //可查看到增加到了15G
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root 14G 769M 12G 6% /
tmpfs 246M 0 246M 0% /dev/shm
/dev/vda1 485M 33M 427M 8% /boot
| 以上步骤成功则表示扩充容量成功,下面我们进行第二种方式
2、增加磁盘
六、调整cpu和内存查看子机配置
七、不重启虚拟机在线增加网卡
八、虚拟机迁移
1)查看子机状态
[iyunv@centos ~]# virsh list --all
Id 名称 状态 ---------------------------------------------------- 3 tpp3 running |
2)关闭子机 [iyunv@centos ~]# virsh shutdown tpp3 2)导出文件
[iyunv@centos ~]# cd /etc/libvirt/qemu/ [iyunv@centos qemu]# virsh dumpxml tpp3.xml > tpp1.xml
|