假设你已经有了一个基础的kvm虚拟机,虚拟机名称为vm03,磁盘文件为vm03.img,xml配置文件为vm03.xml。需要根据此虚拟机创建其他的虚拟机,如名称为michaelzeng
1.创建xml配置文件,复制vm03.xml为michaelzeng.xml,必须修改如下配置,其他的可按需修改,如cpu,内存等信息
<name>michaelzeng</name>
<uuid>2fab00e6-c13a-4da5-88b3-ff00f0b58262</uuid>
<source file='/data/test/michaelzeng.qcow2'/>
<mac address='cc:0f:d6:7f:0b:b4'/>
uuid可以用命令uuidgen生成;
mac地址可以用命令
python -c "from virtinst.util import randomMAC;print randomMAC(type='qemu')"
echo $RANDOM | md5sum | sed 's/\(..\)/&:/g' | cut -c1-17
#用pyhton的比较好,shell的mac容易mac地址,在定义虚拟机的时候容易报错error: XML error: expected unicast mac address, found multicast '57:1b:0d:57:0c:ff'
2.创建img磁盘文件
我们利用已经安装的虚拟机vm03的镜像,通过它来创建派生镜像,避免每创建一个虚拟机就安一个系统的情况,如果需要经常用此vm03基础镜像来派生镜像,可以将vm03镜像放到内存里加速运行,内存目录为/dev/shm/,基础镜像不可轻易移动位置,否则已经创建的派生镜像将会报错。
qemu-img create -f qcow2 michaelzeng.qcow2 -o backing_file=vm03.qcow2
查看派生镜像信息,可以用如下命令
[root@oss-iaas-kvm-local-001 test]# qemu-img info michaelzeng.qcow2
image: michaelzeng.qcow2
file format: qcow2
virtual size: 60G (64424509440 bytes)
disk size: 267M
cluster_size: 65536
backing file: vm03.qcow2
3.根据xml文件创建一个域(客户机),并启动。如果是virsh define michaelzeng.xml 只是创建域,不启动
virsh create michaelzeng.xml
#等价于先define,后start
virsh define michaelzeng.xml
virsh start michaelzeng
4.利用console 连接到控制台,需要做相关配置才能用console
参考:http://www.linuxidc.com/Linux/2014-10/107891.htm
5.console连进去后,发现网卡未启动,网卡名称eth1,而不是eth0,需要更改/etc/udev/rules.d/70-persistent-net.rules,将eth0那行删除(该行信息应该是基础镜像虚拟机的网卡信息),并将eth1那行NAME改为eth0,重启机器。或者卸载网卡驱动再重新加载。
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="52:54:00:06:ea:ee
2", ATTR{type}=="1", KERNEL=="eth*", NAME="eth0"
6.如何给该机器做些初始化,如hostname,selinux,iptables等
7.给虚拟机磁盘扩容
内存和cpu的扩容,都好办,将michaelzeng.xml配置文件进行更改,然后重新启动便可。
原来的分区如下情况:
[root@vm03 ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/vg_vm03-LogVol00
59G 2.5G 53G 5% /
tmpfs 240M 0 240M 0% /dev/shm
/dev/vda1 194M 31M 153M 17% /boot
可以使用qemu-img工具进行扩容:
qemu-img resize vm03.img 200G
resize后重新启动,发现分区并未自动改变,利用fdisk查看,如下:
[root@vm03 ~]# fdisk -l
Disk /dev/vda: 96.6 GB, 96636764160 bytes
16 heads, 63 sectors/track, 187245 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: 0x0009ba09
Device Boot Start End Blocks Id System
/dev/vda1 * 3 409 204800 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/vda2 409 1425 512000 82 Linux swap / Solaris
Partition 2 does not end on cylinder boundary.
/dev/vda3 1425 124831 62196736 8e Linux LVM
Partition 3 does not end on cylinder boundary.
Disk /dev/mapper/vg_vm03-LogVol00: 63.7 GB, 63686311936 bytes
255 heads, 63 sectors/track, 7742 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
发现磁盘/dev/vda确实扩容为90G,只是未使用,搜了许久,看来只能新建一个分区,然后再将这个分区加入lvm里了,执行fdisk /dev/vda,然后依次输入: n p 12482 187245 t 4 8e w,其中分区的start 和end根据自己情况输入
“`
[root@vm03 ~]# 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): n
Command action
e extended
p primary partition (1-4)
p
Selected partition 4
First cylinder (1-187245, default 1): 124832 (默认是1,需要手输上一分区末尾号)
Last cylinder, +cylinders or +size{K,M,G} (124831-187245, default 187245):
Using default value 187245
Command (m for help): t
Partition number (1-4): 4
Hex code (type L to list codes): 8e
Changed system type of partition 4 to 8e (Linux LVM)
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.
重启机器,识别新分区,然后就是LVM逻辑卷扩容的过程了。
[root@01template200G ~]# pvcreate /dev/vda4
Writing physical volume data to disk “/dev/vda4”
Physical volume “/dev/vda4” successfully created
[root@01template200G ~]# vgextend vg_vm03 /dev/vda4
Volume group “vg_vm03” successfully extended
[root@01template200G ~]# lvextend -L +140G /dev/mapper/vg_vm03-LogVol00
Extending logical volume LogVol00 to 199.31 GiB
Insufficient free space: 35840 extents needed, but only 35839 available
[root@01template200G ~]# lvextend -L +139G /dev/mapper/vg_vm03-LogVol00
Extending logical volume LogVol00 to 198.31 GiB
Logical volume LogVol00 successfully resized
[root@01template200G ~]# resize2fs /dev/mapper/vg_vm03-LogVol00
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/mapper/vg_vm03-LogVol00 is mounted on /; on-line resizing required
old desc_blocks = 4, new_desc_blocks = 13
Performing an on-line resize of /dev/mapper/vg_vm03-LogVol00 to 51986432 (4k) blocks.
The filesystem on /dev/mapper/vg_vm03-LogVol00 is now 51986432 blocks long.
[root@01template200G ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/vg_vm03-LogVol00
196G 2.5G 183G 2% /
tmpfs 3.9G 0 3.9G 0% /dev/shm
/dev/vda1 194M 31M 153M 17% /boot