分类: 系统运维
2015-01-08 22:20:07
原文地址:linux系统下KVM虚拟机使用 作者:永恒不变的传说
安装KVM
1. [root@ser1 Desktop]# yum -y groupinstall "Virtualization" "Virtualization Client" "Virtualization Tools" "Virtualization Platform"
[root@ser1 Desktop]# service libvirtd restart
[root@ser1 Desktop]# chkconfig libvirtd on
[root@ser1 Desktop]# virt-manager
[root@ser1 Desktop]# cd /var/lib/libvirt/images/ //硬盘缺损目录
[root@ser1 images]# qemu-img create -f qcow2 rhel6base.img 80G //创建了一个80G的文件
[root@ser1 images]# ll -h rhel6base.img //查看文件大小
[root@ser1 images]# qemu-img info rhel6base.img //查看文件的详细信息
image: rhel6base.img
file format: qcow2
virtual size: 80G (85899345920 bytes)
disk size: 136K
cluster_size: 65536
[root@ser1 images]# qemu-img create -b rhel6base.img -f qcow2 rhel6basenode1.img
用过命令实现克隆
[root@ser1 images]# qemu-img create -b rhel6base.img -f qcow2 rhel6basenode1.img
[root@ser1 images]# virsh list --all
[root@ser1 images]# virsh dumpxml rhel6base > /tmp/myvm.xml
[root@ser1 images]# vim /tmp/myvm.xml
修改四个地方
1.名字
2.uuid
[root@ser1 images]# uuidgen
0585dfa9-3cb4-4b25-8f3b-580f4031ca24
3.磁盘文件名
4.Mac地址
[root@ser1 images]# virsh define /tmp/myvm.xml
[root@ser1 images]# virsh start rhel6basenode1
编写脚本快速克隆虚拟机
#!/bin/bash
BASEVM=rhel6base
IMG_DIR=/var/lib/libvirt/images
read -p "input a number: " VM_NVM
NEWVM=rhel6basenode$VM_NVM
#read -p "input a number: " VM_NVM
qemu-img create -b $IMG_DIR/${BASEVM}.img -f qcow2 $IMG_DIR/${NEWVM}.img
virsh dumpxml $BASEVM > /tmp/myvm.xml
sed -i "/name>r/s/$BASEVM/$NEWVM/" /tmp/myvm.xml
sed -i "/uuid/s/uuid>.\{36\}/uuid>$(uuidgen)/" /tmp/myvm.xml
sed -i "/${BASEVM}.img/s/${BASEVM}.img/${NEWVM}.img/" /tmp/myvm.xml
sed -i "/mac addr/s/00'\/>/0${VM_NVM}'\/>/" /tmp/myvm.xml
virsh define /tmp/myvm.xml
virsh start $NEWVM