KVM学习笔记(二)--虚拟机克隆

来源:互联网 发布:什么软件是学做甜品 编辑:程序博客网 时间:2024/05/18 18:53

一、KVM创建虚拟机


创建磁盘文件及虚拟机镜像


[root@ckcest04 images]# ll /var/lib/libvirt/images/    #kvm存放虚拟机的路径[root@kvmaster2 images]# qemu-img create ckcest07.img 30G Formatting 'ckcest07.img', fmt=raw size=32212254729[root@kvmaster2 images]# virt-install --name=ckcest07 --ram 2048 --vcpus=2 --disk path=/var/lib/libvirt/images/ckcest07.img,size=150 \--cdrom /opt/Centos-6.4-x86_64-bin-DVD1.iso --network bridge=br0 --force --autostart#virt-install 安装虚拟机命令,更多请--help#--name 虚拟机名字#--ram 内存#--vcpus cpu核数#--disk path 虚拟机路径,大小#--cdrom iso路径#--network bridge 桥接到网桥的名字,教程一里的网桥名#--autostart 自动开启#然后就是图形界面的安装操作系统步骤,这里不再赘述。


配置操作系统


[root@ckcest07 opt]# sed -i '/^HWADDR/d' /etc/sysconfig/network-scripts/ifcfg-eth0[root@ckcest07 opt]# sed -i '/^UUID/d' /etc/sysconfig/network-scripts/ifcfg-eth0[root@ckcest07 opt]# echo -n > /etc/udev/rules.d/70-persistent-net.rules#上面三条命令是为了用此系统做镜像,再建立虚拟机的时候,可以获得新的mac地址,否则到时候还要重新配置


[root@kvmaster2 images]# virsh list              #虚拟机运行正常 Id    名称                         状态---------------------------------------------------- 1     ckcest07                       running


将raw格式转换成qcow2格式


[root@kvmaster2 images]# qemu-img info ckcest07.imgimage: ckcest07.imgfile format: rawvirtual size: 30G (32212254720 bytes)disk size: 2.9G[root@kvmaster2 images]# qemu-img convert -O qcow2 ckcest07.img ckcest07.img.qcow2[root@kvmaster2 images]# qemu-img info ckcest07.img.qcow2 image: ckcest07.img.qcow2file format: qcow2virtual size: 30G (32212254720 bytes)disk size: 2.3Gcluster_size: 65536#虚拟机各种磁盘格式介绍:http://blog.chinaunix.net/uid-14735472-id-4220089.html

根据创建的"基类"镜像,生成新的虚拟机

[root@kvmaster2 images]# qemu-img create -f qcow2 ckcest08.qcow2 -o backing_file=ckcest07.img 150G \Formatting 'ckcest08.qcow2', fmt=qcow2size=161061273600 backing_file='ckcest07.img' encryption=off cluster_size=65536#基类镜像是30G大小、派生出来的镜像可以额外使用150G空间。[root@kvmaster2 images]# qemu-img info ckcest08.qcow2 image: ckcest08.qcow2file format: qcow2virtual size: 150G (161061273600 bytes)disk size: 196Kcluster_size: 65536backing file: ckcest07.img


挂载额外的磁盘空间


[root@ckcest08 ~]# df -h                                  #根目录只有20G大小文件系统      容量  已用  可用 已用%% 挂载点/dev/mapper/vg_ckcest07-LogVol03                       20G  2.0G   17G  11% /tmpfs                 939M     0  939M   0% /dev/shm/dev/vda1              97M   33M   60M  35% /boot/dev/mapper/vg_ckcest07-LogVol00                      9.9G  151M  9.2G   2% /home/dev/mapper/vg_ckcest07-LogVol02                       97M  5.6M   87M   7% /tmp[root@ckcest08 ~]# fdisk -l                       #额外的磁盘空间,后面将他挂载到根目录下Disk /dev/vda: 161.1 GB, 161061273600 bytes16 heads, 63 sectors/track, 312076 cylindersUnits = cylinders of 1008 * 512 = 516096 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk identifier: 0x000e1aeb   Device Boot      Start         End      Blocks   Id  System/dev/vda1   *           3         206      102400   83  LinuxPartition 1 does not end on cylinder boundary./dev/vda2             206       62416    31353856   8e  Linux LVMPartition 2 does not end on cylinder boundary.#因为磁盘是用了LVM做了逻辑卷,所以先把磁盘放到VG,不了解lvm的朋友可以看下http://www.cnblogs.com/gaojun/archive/2012/08/22/2650229.html[root@ckcest08 ~]# fdisk /dev/vda                      #fdisk来创建分区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: 161.1 GB, 161061273600 bytes16 heads, 63 sectors/track, 312076 cylindersUnits = cylinders of 1008 * 512 = 516096 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk identifier: 0x000e1aeb   Device Boot      Start         End      Blocks   Id  System/dev/vda1   *           3         206      102400   83  LinuxPartition 1 does not end on cylinder boundary./dev/vda2             206       62416    31353856   8e  Linux LVMPartition 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                    #之前有了2个,所以这里是3,回车。First cylinder (1-312076, default 1): 62417Last cylinder, +cylinders or +size{K,M,G} (62417-312076, default 312076): Using default value 312076Command (m for help): pDisk /dev/vda: 161.1 GB, 161061273600 bytes16 heads, 63 sectors/track, 312076 cylindersUnits = cylinders of 1008 * 512 = 516096 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk identifier: 0x000e1aeb   Device Boot      Start         End      Blocks   Id  System/dev/vda1   *           3         206      102400   83  LinuxPartition 1 does not end on cylinder boundary./dev/vda2             206       62416    31353856   8e  Linux LVMPartition 2 does not end on cylinder boundary./dev/vda3           62417      312076   125828640   83  Linux       #新加了个分区,所有剩余空间都搞这了 Command (m for help): t                                           #修改分区类型Partition number (1-4): 3                                         #选择修改的分区号Hex code (type L to list codes): 8e                               #8e为lvm的分区类型Changed system type of partition 3 to 8e (Linux LVM)              Command (m for help): pDisk /dev/vda: 161.1 GB, 161061273600 bytes16 heads, 63 sectors/track, 312076 cylindersUnits = cylinders of 1008 * 512 = 516096 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk identifier: 0x000e1aeb   Device Boot      Start         End      Blocks   Id  System/dev/vda1   *           3         206      102400   83  LinuxPartition 1 does not end on cylinder boundary./dev/vda2             206       62416    31353856   8e  Linux LVMPartition 2 does not end on cylinder boundary./dev/vda3           62417      312076   125828640   8e  Linux LVM        #修改成功Command (m for help): w                                                                    #w为写入分区并保存。The partition table has been altered!Calling ioctl() to re-read partition table.WARNING: Re-reading the partition table failed with error 16: 设备或资源忙.The kernel still uses the old table. The new table will be used atthe next reboot or after you run partprobe(8) or kpartx(8)Syncing disks.[root@ckcest08 ~]# partprobe                     #让内核加载新的分区表[root@ckcest08 ~]# reboot                        #最好重启,若是新分区标加载成功可以不重启。[root@ckcest08 ~]# pvscan                        #目前pv只有一个vda2在  PV /dev/vda2   VG vg_ckcest07   lvm2 [29.90 GiB / 0    free]  Total: 1 [29.90 GiB] / in use: 1 [29.90 GiB] / in no VG: 0 [0   ][root@ckcest08 ~]# pvcreate /dev/vda3            #将vda3也创建成pv, Physical volume "/dev/vda3" successfully created[root@ckcest08 ~]# pvscan  PV /dev/vda2   VG vg_ckcest07     lvm2 [29.90 GiB / 0    free]  PV /dev/vda3                      lvm2 [120.00 GiB]   #vda3成功加入  Total: 2 [149.90 GiB] / in use: 1 [29.90 GiB] / in no VG: 1 [120.00 GiB]   [root@ckcest08 ~]# vgdisplay                              #vg目前只有30G  --- Volume group ---  VG Name               vg_ckcest07  System ID               Format                lvm2  Metadata Areas        1  Metadata Sequence No  5  VG Access             read/write  VG Status             resizable  MAX LV                0  Cur LV                4  Open LV               4  Max PV                0  Cur PV                1  Act PV                1  VG Size               29.90 GiB  PE Size               4.00 MiB  Total PE              7654  Alloc PE / Size       7654 / 29.90 GiB  Free  PE / Size       0 / 0     VG UUID               rjJTHk-dOjL-mWQ8-I4FK-ukqW-OQvr-58Ro6Y[root@ckcest08 ~]# vgextend vg_ckcest07 /dev/vda3             #将vda3分区的容量加入vg  Volume group "vg_ckcest07" successfully extended[root@ckcest08 ~]# vgdisplay                                  #变成150G了。  --- Volume group ---  VG Name               vg_ckcest07  System ID               Format                lvm2  Metadata Areas        2  Metadata Sequence No  6  VG Access             read/write  VG Status             resizable  MAX LV                0  Cur LV                4  Open LV               4  Max PV                0  Cur PV                2  Act PV                2  VG Size               149.89 GiB  PE Size               4.00 MiB  Total PE              38373  Alloc PE / Size       7654 / 29.90 GiB  Free  PE / Size       30719 / 120.00 GiB  VG UUID               rjJTHk-dOjL-mWQ8-I4FK-ukqW-OQvr-58Ro6Y [root@ckcest08 ~]# lvdisplay               #因为做基类镜像时就都做了lvm所以有这么多,这里只需要将vg的剩余容量挂载到根目录下即可  --- Logical volume ---  LV Path                /dev/vg_ckcest07/LogVol01  LV Name                LogVol01  VG Name                vg_ckcest07  LV UUID                QUPJNF-mUkd-9DNx-H02e-UvvN-pz4X-oga4Tt  LV Write Access        read/write  LV Creation host, time ckcest07, 2016-08-04 17:11:32 +0800  LV Status              available  # open                 1  LV Size                100.00 MiB  Current LE             25  Segments               1  Allocation             inherit  Read ahead sectors     auto  - currently set to     256  Block device           253:0     --- Logical volume ---  LV Path                /dev/vg_ckcest07/LogVol00     LV Name                LogVol00                  #这个lv是挂载到/home下  VG Name                vg_ckcest07  LV UUID                PGkJ9t-BdF3-GG0r-P2FM-TSbX-J2ND-K3T8iB  LV Write Access        read/write  LV Creation host, time ckcest07, 2016-08-04 17:11:32 +0800  LV Status              available  # open                 1  LV Size                10.00 GiB  Current LE             2560  Segments               1  Allocation             inherit  Read ahead sectors     auto  - currently set to     256  Block device           253:2     --- Logical volume ---  LV Path                /dev/vg_ckcest07/LogVol03  LV Name                LogVol03   VG Name                vg_ckcest07  LV UUID                fOFqir-sIhU-0tBP-9a4C-tbiz-fufG-rQzrgF  LV Write Access        read/write  LV Creation host, time ckcest07, 2016-08-04 17:11:35 +0800  LV Status              available  # open                 1  LV Size                19.70 GiB               #这个lv挂载到根目录下  Current LE             5044  Segments               1  Allocation             inherit  Read ahead sectors     auto  - currently set to     256  Block device           253:1     --- Logical volume ---  LV Path                /dev/vg_ckcest07/LogVol02  LV Name                LogVol02                   #这个lv挂载到/tmp下  VG Name                vg_ckcest07  LV UUID                TDEt0k-HW0p-mhZZ-UQew-t3hn-ysLs-oRykxd  LV Write Access        read/write  LV Creation host, time ckcest07, 2016-08-04 17:11:38 +0800  LV Status              available  # open                 1  LV Size                100.00 MiB  Current LE             25  Segments               1  Allocation             inherit  Read ahead sectors     auto  - currently set to     256  Block device           253:3   [root@ckcest08 ~]# lvresize -l +100%FREE /dev/vg_ckcest07/LogVol03      #将vg剩余空间挂载到根目录下  Extending logical volume LogVol03 to 139.70 GiB  Logical volume LogVol03 successfully resized[root@ckcest08 ~]# resize2fs /dev/vg_ckcest07/LogVol03                 #重新加载分区大小..等待他加载完成。resize2fs 1.41.12 (17-May-2010)Filesystem at /dev/vg_ckcest07/LogVol03 is mounted on /; on-line resizing requiredold desc_blocks = 2, new_desc_blocks = 9Performing an on-line resize of /dev/vg_ckcest07/LogVol03 to 36621312 (4k) blocks.The filesystem on /dev/vg_ckcest07/LogVol03 is now 36621312 blocks long.[root@ckcest08 ~]# df -h文件系统          容量  已用  可用 已用%% 挂载点/dev/mapper/vg_ckcest07-LogVol03                      138G  2.0G  129G   2% /               #磁盘空间都挂载来了,donetmpfs                 939M     0  939M   0% /dev/shm/dev/vda1              97M   33M   60M  35% /boot/dev/mapper/vg_ckcest07-LogVol00                      9.9G  151M  9.2G   2% /home/dev/mapper/vg_ckcest07-LogVol02                       97M  5.6M   87M   7% /tmp



三、KVM克隆虚拟机


0 0
原创粉丝点击