1. 程式人生 > >KVM學習筆記(二)--虛擬機器克隆

KVM學習筆記(二)--虛擬機器克隆

一、KVM建立虛擬機器

建立磁碟檔案及虛擬機器映象

[[email protected] images]# ll /var/lib/libvirt/images/    #kvm存放虛擬機器的路徑

[[email protected] images]# qemu-img create ckcest07.img 30G 
Formatting 'ckcest07.img', fmt=raw size=32212254729

[[email protected] 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 自動開啟
#然後就是圖形介面的安裝作業系統步驟,這裡不再贅述。

配置作業系統

[[email protected] opt]# sed -i '/^HWADDR/d' /etc/sysconfig/network-scripts/ifcfg-eth0
[[email protected] opt]# sed -i '/^UUID/d' /etc/sysconfig/network-scripts/ifcfg-eth0
[[email protected] opt]# echo -n > /etc/udev/rules.d/70-persistent-net.rules

#上面三條命令是為了用此係統做映象,再建立虛擬機器的時候,可以獲得新的mac地址,否則到時候還要重新配置
[[email protected] images]# virsh list              #虛擬機器執行正常
 Id    名稱                         狀態
----------------------------------------------------
 1     ckcest07                       running

將raw格式轉換成qcow2格式

[[email protected] images]# qemu-img info ckcest07.img
image: ckcest07.img
file format: raw
virtual size: 30G (32212254720 bytes)
disk size: 2.9G

[
[email protected]
images]# qemu-img convert -O qcow2 ckcest07.img ckcest07.img.qcow2 [[email protected] images]# qemu-img info ckcest07.img.qcow2 image: ckcest07.img.qcow2 file format: qcow2 virtual size: 30G (32212254720 bytes) disk size: 2.3G cluster_size: 65536 #虛擬機器各種磁碟格式介紹: http://blog.chinaunix.net/uid-14735472-id-4220089.html

根據建立的"基類"映象,生成新的虛擬機器

[[email protected] 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空間。
[[email protected] images]# qemu-img info ckcest08.qcow2 
image: ckcest08.qcow2
file format: qcow2
virtual size: 150G (161061273600 bytes)
disk size: 196K
cluster_size: 65536
backing file: ckcest07.img


掛載額外的磁碟空間
[[email protected] ~]# 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


[[email protected] ~]# fdisk -l                       #額外的磁碟空間,後面將他掛載到根目錄下

Disk /dev/vda: 161.1 GB, 161061273600 bytes
16 heads, 63 sectors/track, 312076 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: 0x000e1aeb

   Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *           3         206      102400   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/vda2             206       62416    31353856   8e  Linux LVM
Partition 2 does not end on cylinder boundary.

#因為磁碟是用了LVM做了邏輯卷,所以先把磁碟放到VG,不瞭解lvm的朋友可以看下http://www.cnblogs.com/gaojun/archive/2012/08/22/2650229.html

[[email protected] ~]# 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 bytes
16 heads, 63 sectors/track, 312076 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: 0x000e1aeb

   Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *           3         206      102400   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/vda2             206       62416    31353856   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                    #之前有了2個,所以這裡是3,回車。
First cylinder (1-312076, default 1): 62417
Last cylinder, +cylinders or +size{K,M,G} (62417-312076, default 312076): 
Using default value 312076

Command (m for help): p

Disk /dev/vda: 161.1 GB, 161061273600 bytes
16 heads, 63 sectors/track, 312076 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: 0x000e1aeb

   Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *           3         206      102400   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/vda2             206       62416    31353856   8e  Linux LVM
Partition 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): p

Disk /dev/vda: 161.1 GB, 161061273600 bytes
16 heads, 63 sectors/track, 312076 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: 0x000e1aeb

   Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *           3         206      102400   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/vda2             206       62416    31353856   8e  Linux LVM
Partition 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 at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.

[[email protected] ~]# partprobe                     #讓核心載入新的分割槽表

[[email protected] ~]# reboot                        #最好重啟,若是新分割槽標載入成功可以不重啟。

[[email protected] ~]# 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   ]

[[email protected] ~]# pvcreate /dev/vda3            #將vda3也建立成pv,
 Physical volume "/dev/vda3" successfully created

[[email protected] ~]# 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]  
 
[[email protected] ~]# 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

[[email protected] ~]# vgextend vg_ckcest07 /dev/vda3             #將vda3分割槽的容量加入vg
  Volume group "vg_ckcest07" successfully extended

[[email protected] ~]# 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
 
[[email protected] ~]# 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
   
[[email protected] ~]# lvresize -l +100%FREE /dev/vg_ckcest07/LogVol03      #將vg剩餘空間掛載到根目錄下
  Extending logical volume LogVol03 to 139.70 GiB
  Logical volume LogVol03 successfully resized

[[email protected] ~]# resize2fs /dev/vg_ckcest07/LogVol03                 #重新載入分割槽大小..等待他載入完成。
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/vg_ckcest07/LogVol03 is mounted on /; on-line resizing required
old desc_blocks = 2, new_desc_blocks = 9
Performing 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.

[[email protected] ~]# df -h
檔案系統          容量  已用  可用 已用%% 掛載點
/dev/mapper/vg_ckcest07-LogVol03
                      138G  2.0G  129G   2% /               #磁碟空間都掛載來了,done
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


三、KVM克隆虛擬機器


相關推薦

no