一、虚拟机的克隆
1.通过命令克隆虚拟机
a.关闭虚拟机
[root@kvm ~]# virsh list Id Name State ---------------------------------------------------- 1 vm1 running [root@kvm ~]# virsh shutdown vm1 Domain vm1 is being shutdown [root@kvm ~]# virsh list --all Id Name State ---------------------------------------------------- - vm1 shut off
b.使用命令克隆虚拟机
[root@kvm ~]# virt-clone -o vm1 -n vm2 -f /data/kvm/hosts/vm2.img Cloning vm1.qcow2 | 1.6 GB 00:33 Clone 'vm2' created successfully. #参数说明 -o 表示要从哪个源进行clone -n 表示新虚拟机的名称 -f 表示新虚拟机使用的disk文件(会自动创建)
二、对磁盘分区进行扩容
#安装工具 [root@kvm ~]# yum -y install libguestfs-tools
1.查看原分区信息
[root@kvm ~]# qemu-img info /data/kvm/hosts/vm2.img image: /data/kvm/hosts/vm2.img file format: qcow2 virtual size: 10G (10737418240 bytes) disk size: 1.6G cluster_size: 65536 [root@kvm ~]# virt-df -h /data/kvm/hosts/vm2.img Filesystem Size Used Available Use% vm2.img:/dev/sda1 484M 32M 428M 7% vm2.img:/dev/VolGroup/lv_root 8.4G 806M 7.2G 10%
2.生成一个5G大小的qcow2磁盘
[root@kvm ~]# qemu-img create -f qcow2 /data/kvm/hosts/vm2_img_add.qcow2 5G Formatting '/data/kvm/hosts/vm2_img_add.qcow2', fmt=qcow2 size=5368709120 encryption=off cluster_size=65536 [root@kvm ~]# ll /data/kvm/hosts/ total 4918548 -rw-r--r-- 1 root root 1737490432 Jul 30 10:00 vm1.qcow2 -rwxr-xr-x 1 qemu qemu 1737687040 Jul 30 10:38 vm2.img -rw-r--r-- 1 root root 197120 Jul 30 10:38 vm2_img_add.qcow2 -rw-r--r-- 1 root root 10737418240 Jul 29 17:52 vm2.raw
3.新增配置文件
[root@kvm ~]# virsh edit vm2 <disk type='file' device='disk'> <driver name='qemu' type='qcow2' cache='none'/> <source file='/data/kvm/hosts/vm2_img_add.qcow2'/> #这里注意盘符,不要有重复 <target dev='vdb' bus='virtio'/> #注意这里的槽位 <address type='pci' domain='0x0000' bus='0x00' slot='0x07' function='0x0'/> </disk>
4.重启vm2进行分区
[root@kvm ~]# virsh list Id Name State ---------------------------------------------------- 2 vm2 running [root@kvm ~]# virsh reboot vm2 #查看磁盘信息 [root@vm2 ~]# fdisk -l Disk /dev/vda: 10.7 GB, 10737418240 bytes 16 heads, 63 sectors/track, 20805 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: 0x00007d4b 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 20806 9972736 8e Linux LVM Partition 2 does not end on cylinder boundary. #这是我们刚添加的磁盘 Disk /dev/vdb: 5368 MB, 5368709120 bytes 16 heads, 63 sectors/track, 10402 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 Disk /dev/mapper/VolGroup-lv_root: 9168 MB, 9168748544 bytes 255 heads, 63 sectors/track, 1114 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 Disk /dev/mapper/VolGroup-lv_swap: 1040 MB, 1040187392 bytes 255 heads, 63 sectors/track, 126 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
5.将磁盘vdb设置成lvm格式
[root@vm2 ~]# fdisk /dev/vdb Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel Building a new DOS disklabel with disk identifier 0x362268c6. Changes will remain in memory only, until you decide to write them. After that, of course, the previous content won't be recoverable. Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite) 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 Partition number (1-4): 1 First cylinder (1-10402, default 1): Using default value 1 Last cylinder, +cylinders or +size{K,M,G} (1-10402, default 10402): Using default value 10402 Command (m for help): t Selected partition 1 Hex code (type L to list codes): 8e Changed system type of partition 1 to 8e (Linux LVM) Command (m for help): w The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks. [root@vm2 ~]# partx call: partx -opts [device] wholedisk
6.创建pv,扩展lvm
[root@vm2 ~]# pvcreate /dev/vdb1 Physical volume "/dev/vdb1" successfully created [root@vm2 ~]# pvs PV VG Fmt Attr PSize PFree /dev/vda2 VolGroup lvm2 a-- 9.51g 0 /dev/vdb1 lvm2 a-- 5.00g 5.00g [root@vm2 ~]# vgs VG #PV #LV #SN Attr VSize VFree VolGroup 1 2 0 wz--n- 9.51g 0 #扩展vg大小 [root@vm2 ~]# vgextend VolGroup /dev/vdb1 Volume group "VolGroup" successfully extended [root@vm2 ~]# vgs VG #PV #LV #SN Attr VSize VFree VolGroup 2 2 0 wz--n- 14.50g 5.00g #扩大了5G空间 #调整大小前分区挂载信息如下 [root@vm2 ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/VolGroup-lv_root 8.5G 806M 7.2G 10% / #只有8.5G tmpfs 246M 0 246M 0% /dev/shm /dev/vda1 485M 32M 428M 7% /boot #调整lvm大小 [root@vm2 ~]# lvextend -l +100%FREE /dev/VolGroup/lv_root Extending logical volume lv_root to 13.54 GiB Logical volume lv_root successfully resized [root@vm2 ~]# 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@vm2 ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/VolGroup-lv_root 14G 808M 12G 7% / #分区大小已经调整到了14G tmpfs 246M 0 246M 0% /dev/shm /dev/vda1 485M 32M 428M 7% /boot #在宿主机上查看vm2的磁盘信息 [root@kvm ~]# virt-df -h /data/kvm/hosts/vm2.img /data/kvm/hosts/vm2_img_add.qcow2 Filesystem Size Used Available Use% vm2.img+:/dev/sda1 484M 32M 428M 7% vm2.img+:/dev/VolGroup/lv_root 13G 808M 12G 6%