摘要:本文将带你了解Vmware之KVM虚拟化的基本管理,希望本文对大家学Vmware有所帮助。
本文将带你了解Vmware之KVM虚拟化的基本管理,希望本文对大家学Vmware有所帮助。
这篇文章主要介绍KVM虚拟化的基本管理:
一、virsh 命令常用操作
二、克隆虚拟机
三、磁盘镜像格式转换
四、快照管理
五、磁盘扩容
六、调整cpu和内存查看子机配置
七、不重启虚拟机在线增加网卡
八、虚拟机迁移
一、virsh 命令常用操作
1、开启并登入子机
[root@centos ~]# virsh start tpp1
[root@centos ~]# ssh 192.168.0.10
也可以在开启的同时进入子机:
[root@centos ~]# virsh start tpp1 --console
2、关闭子机
[root@centos ~]# virsh shutdown tpp1
//关闭
注意:默认我们没有办法在宿主机直接shutdown自己,我们需要借助于子机上的acpid服务,这个服务是让宿主机可以去调用子机的电源关闭的接口。所以,子机上需要安装并启动acpid服务,若子机没开启acpid服务,虽然显示关闭成功,但查看仍为running状态。
[root@centos ~]# virsh console tpp1
[root@localhost ~]# yum install -y acpid
[root@localhost ~]# /etc/init.d/acpid start
然后Ctrl+] 退出子机
[root@centos ~]# virsh shutdown tpp1
[root@centos ~]# virsh destroy tpp1
//另一种方法是直接从列表中删除
3、子机随宿主机开机自启
[root@centos ~]# virsh autostart tpp1
//自动启动
[root@centos ~]# virsh autostart --disable tpp1
//解除自动启动
4、列出子机
[root@centos ~]# virsh list
//只能列出启动的子机
[root@centos ~]# virsh list --all
//把所有子机都列出来
5、删除子机
[root@centos ~]# virsh destroy tpp1
[root@centos ~]# virsh undefine tpp1
[root@centos ~]# rm -f /data/tpp1.img
//若为qcow2格式的,删除tpp2.qcow2
6、挂起和恢复子机
[root@centos ~]# virsh suspend tpp1
//挂起
[root@centos ~]# virsh resume tpp1
//恢复
7、退出子机
按ctrl+] 可以返回到宿主机。
二、克隆虚拟机
[root@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)
[root@centos ~]# virsh list --all
//可以看到克隆的子机
三、磁盘镜像格式转换
1)查看子机磁盘镜像格式
[root@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格式
[root@centos ~]# qemu-img convert -f raw -O
qcow2 /data/tpp1.img /data/tpp1.qcow2
说明:其实就是相当于复制了一份,会生成一个qcow2格式的文件(tpp1.qcow2)
3)再次查看子机磁盘镜像格式
[root@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)编辑子机配置文件
[root@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、创建快照
[root@centos ~]# virsh snapshot-create tpp1
//提示以下信息则成功
Domain snapshot 1447276326 created
注意:若磁盘镜像格式为raw格式则会报下面错误:
unsupported configuration: internal snapshot for
disk vda unsupported for storage type raw
2、列出快照
[root@centos ~]# virsh snapshot-list tpp1
名称
Creation Time
状态
------------------------------------------------------------
1447276326 2015-11-12 05:12:06
+0800 shutoff
3、查看当前子机的快照版本
[root@centos ~]# virsh snapshot-current
tpp1
说明:tpp1子机的快照文件在
/var/lib/libvirt/qemu/snapshot/tpp1/ 目录下
[root@centos ~]# ls
/var/lib/libvirt/qemu/snapshot/tpp1/
1447276326.xml
4、恢复快照
1)首先需要关闭子机
[root@centos ~]# virsh destroy tpp1
2)确认子机是否关闭
[root@centos ~]# virsh domstate tpp1
关闭
3) 列出快照
[root@centos ~]# virsh snapshot-list tpp1
名称
Creation Time
状态
------------------------------------------------------------
1447276326 2015-11-12 05:12:06 +0800
shutoff
4)恢复快照
[root@centos ~]# virsh snapshot-revert tpp1
1447276326
5、删除快照
[root@centos ~]# virsh snapshot-delete tpp1
1447276326
Domain snapshot 1447276326 deleted
五、磁盘扩容
有两种方式,一种是扩充容量,另一种是直接增加磁盘。raw格式和qcow2格式的虚拟磁盘扩容步骤是一样的,下面我们对raw格式的虚拟磁盘进行扩容。
1、扩充容量
1)查看虚拟机信息(大小为10G)
[root@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
[root@centos ~]# qemu-img resize /data/tpp1.img +5G
Image resized.
[root@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)使其生效
[root@centos ~]# virsh destroy tpp1
[root@centos ~]# virsh start tpp1
[root@centos ~]# virsh console tpp1
//进入子机
[root@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.
.......
[root@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)分区(新增加的空间还没有划分使用,故要继续分区)
[root@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.
[root@localhost ~]# reboot //重启生效
5)把/dev/vda3 加入到lvm里面去
[root@localhost ~]# pvcreate /dev/vda3
//创建物理卷
Physical volume "/dev/vda3"
successfully created
[root@localhost ~]# pvs
//列出物理卷
PV
VG Fmt Attr
PSize PFree
/dev/vda2 VolGroup lvm2 a-- 9.51g
0
/dev/vda3
lvm2 a-- 5.00g 5.00g
[root@localhost ~]# vgextend VolGroup /dev/vda3 //物理卷加入卷组
Volume group "VolGroup"
successfully extended
[root@localhost ~]# vgs
//查看卷组,可看到5G空余
VG
#PV #LV #SN Attr
VSize VFree
VolGroup 2 2
0 wz--n- 14.50g 5.00g
[root@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
[root@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
[root@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.
[root@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)查看子机状态
[root@centos ~]# virsh list --all
Id 名称
状态
----------------------------------------------------
3 tpp3
running
2)关闭子机
[root@centos ~]# virsh shutdown tpp3
2)导出文件
[root@centos ~]# cd /etc/libvirt/qemu/
[root@centos qemu]# virsh dumpxml tpp3.xml > tpp1.xml
本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标系统运维之Vmware频道!
您输入的评论内容中包含违禁敏感词
我知道了
请输入正确的手机号码
请输入正确的验证码
您今天的短信下发次数太多了,明天再试试吧!
我们会在第一时间安排职业规划师联系您!
您也可以联系我们的职业规划师咨询:
版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
沪公网安备 31011502005948号