KVM

KVM安裝html

1. vmware安裝centos6.6 64位 (略)
其中有幾個注意的地方:
a. 內存給2g
b. 磁盤給50g, 或者再單獨分一個磁盤,用來存儲虛擬機文件
c. 最關鍵的一步,在建立虛擬機時,cpu是須要配置一下虛擬化的
2.  安裝kvm前的準備工做
a. 清除iptables規則
service iptables stop; service iptables save
b. 關閉selinux
sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
setenforce 0
 
 
3. 開始安裝kvm
a. 檢查你的系統是否支持虛擬化
grep -Ei 'vmx|svm' /proc/cpuinfo
若是有輸出內容,則支持,其中intel cpu支持會有vmx,amd cpu支持會有svm
 
b. 經過yum安裝虛擬化的軟件包
yum install -y kvm virt-*  libvirt  bridge-utils qemu-img
說明:
kvm:軟件包中含有KVM內核模塊,它在默認linux內核中提供kvm管理程序
libvirts:安裝虛擬機管理工具,使用virsh等命令來管理和控制虛擬機。
bridge-utils:設置網絡網卡橋接。
virt-*:建立、克隆虛擬機命令,以及圖形化管理工具virt-manager
qemu-img:安裝qemu組件,使用qemu命令來建立磁盤等。
 
 
 
 
c. 檢查kvm模塊是否加載
lsmod |grep kvm
正常應該是:
kvm_intel              55496  3
kvm                   337772  1 kvm_intel
 
若是沒有,須要執行
modprobe kvm-intel
尚未就重啓一下試試
 
d. 配置網卡
cd /etc/sysconfig/network-scripts/
cp ifcfg-eth0 ifcfg-br0
分別編輯eth0和br0
ifcfg-eth0改爲以下:
DEVICE=eth0
HWADDR=00:0C:29:55:A7:0A
TYPE=Ethernet
UUID=2be47d79-2a68-4b65-a9ce-6a2df93759c6
ONBOOT=yes
NM_CONTROLLED=yes
BOOTPROTO=none
BRIDGE=br0
 
 
ifcfg-br0改爲以下:
DEVICE=br0
#HWADDR=00:0C:29:55:A7:0A
TYPE=Bridge
#UUID=2be47d79-2a68-4b65-a9ce-6a2df93759c6
ONBOOT=yes
NM_CONTROLLED=yes
BOOTPROTO=static
IPADDR=192.168.11.17
NETMASK=255.255.255.0
GATEWAY=192.168.11.1
DNS1=202.106.0.20
 
說明: 個人虛擬機是橋接模式,因此設置br0的ip和個人真機一樣的網段,包括網關也是我路由器的ip,你們能夠根據本身的環境去配置,目的是爲了讓虛擬機能夠上網。
 
/etc/init.d/network restart
查看網卡以下:
br0       Link encap:Ethernet  HWaddr 00:0C:29:55:A7:0A
          inet addr:192.168.11.17  Bcast:192.168.11.255  Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:fe55:a70a/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:141326 errors:0 dropped:0 overruns:0 frame:0
          TX packets:90931 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:456024940 (434.8 MiB)  TX bytes:10933593 (10.4 MiB)
 
eth0      Link encap:Ethernet  HWaddr 00:0C:29:55:A7:0A
          inet6 addr: fe80::20c:29ff:fe55:a70a/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:341978 errors:0 dropped:0 overruns:0 frame:0
          TX packets:90946 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:468848861 (447.1 MiB)  TX bytes:10934699 (10.4 MiB)
 
lo        Link encap:Local Loopback
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:0 (0.0 b)  TX bytes:0 (0.0 b)
 
virbr0    Link encap:Ethernet  HWaddr 52:54:00:14:EF:D5
          inet addr:192.168.122.1  Bcast:192.168.122.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:0 (0.0 b)  TX bytes:0 (0.0 b)
 
 
 
e. 啓動或重啓libvirtd服務和messagebus 服務
/etc/init.d/libvirtd start
/etc/init.d/messagebus restart
 
此時能夠查看網絡接口列表
brctl show 結果以下:
bridge name     bridge id               STP enabled     interfaces
br0             8000.000c2955a70a       no              eth0
virbr0          8000.52540014efd5       yes             virbr0-nic
 
 
4. 建立虛擬機
mkdir /data/   //建立一個存儲虛擬機虛擬磁盤的目錄,該目錄所在分區必須足夠大
 
virt-install \
--name  aming1 \
--ram 512 \
--disk path=/data/aming1.img,size=30 \
--vcpus 1 \
--os-type linux \
--os-variant rhel6 \
--network bridge=br0 \
--graphics none \
--console pty,target_type=serial \
--location 'http://mirrors.163.com/centos/6.7/os/i386/' \
--extra-args 'console=ttyS0,115200n8 serial'
 
 
說明:
--name  指定虛擬機的名字
--ram 指定內存分配多少
--disk path 指定虛擬磁盤放到哪裏,size=30 指定磁盤大小爲30G,這樣磁盤文件格式爲raw,raw格式不能作快照,後面有說明,須要轉換爲qcow2格式,若是要使用qcow2格式的虛擬磁盤,須要事先建立qcow2格式的虛擬磁盤。 參考   http://www.361way.com/kvm-qcow2-preallocation-metadata/3354.html   示例:qemu-img create -f qcow2 -o preallocation=metadata  /data/test02.img 7G;  --disk path=/data/test02.img,format=qcow2,size=7,bus=virtio
--vcpus 指定分配cpu幾個
--os-type 指定系統類型爲linux
--os-variant 指定系統版本
--network  指定網絡類型
--graphics 指定安裝經過哪一種類型,能夠是vnc,也能夠沒有圖形,在這裏咱們沒有使用圖形直接使用文本方式
--console 指定控制檯類型
--location 指定安裝介質地址,能夠是網絡地址,也能夠是本地的一個絕對路徑,(--location '/mnt/', 其中/mnt/下就是咱們掛載的光盤鏡像mount /dev/cdrom /mnt)若是是絕對路徑,那麼後面還須要指定一個安裝介質,好比NFS,假如虛擬機設置ip後,不能連外網,那麼就會提示讓咱們選擇安裝途徑:                        
                        ┌───┤ Installation Method ├───┐
                        │                             │
                        │ What type of media contains     │
                        │ the installation image?             │
                        │                                             │
                        │        Local CD/DVD                  │
                        │        Hard drive                      │
                        │        NFS directory                  │
                        │        URL                               │
                        │                                             │
                        │   ┌────┐       ┌──────┐     │
                        │   │ OK    │       │ Back     │     │
                        │   └────┘       └──────┘     │
                        │                                             │
                        │                                             │
                        └─────────────────────┘
 
我在這裏選擇NFS,設置參數以下:
    ┌───────────────────────────┤ NFS Setup ├─────────
     │                                                                                                 │
     │ Please enter the server and NFSv3 path to your CentOS installation   │
     │ image and optionally additional NFS mount options.                        │
     │                                                                                                │
     │       NFS server name:              172.7.15.3______________              │
     │       CentOS directory:             /mnt/images/install.img_                 │
     │       NFS mount options (optional): ro______________________       │
 
 
--extra-args 設定內核參數
 
當按下回車後,稍等幾秒鐘就能夠看到安裝提示了。
開始安裝......
搜索文件 .treeinfo......                             |  720 B     00:00 ...
搜索文件 vmlinuz......                               | 7.7 MB     00:02 ...
搜索文件 initrd.img......                            |  63 MB     00:23 ...
建立存儲文件 centos6.6_1.img                       |  30 GB     00:00
建立域......                                          |    0 B     00:00
鏈接到域 centos6.6_1
Escape character is ^]
 
 
而後就是咱們很是熟悉的OK or  Next 了 ,只不過這個過程是文本模式,若是想使用圖形,只能開啓vnc啦。
 
最後安裝完,reboot就進入剛剛建立的虛擬機了。要想退回到宿主機,ctrl  ] 便可。
virsh list 能夠列出當前的子機列表。
virsh console centos6.6_1  能夠進入指定的子機
-------------------------------------------------------------------------------------------------
克隆虛擬機
virt-clone --original centos6.6_1 --name template --file /data/clone1.img  
若是子機centos6.6_1還未關機,則須要先關機,不然會報錯:
ERROR    必須暫停或者關閉有要克隆設備的域。
關閉子機的方法是:
virsh shutdown centos6.6_1
 
說明: 默認,咱們沒有辦法在宿主機直接shutdown本身,咱們須要藉助於子機上的acpid服務才能夠,這個服務說白了就是讓宿主機能夠去調用子機的電源關閉的接口。因此,子機上須要安裝並啓動acpid服務。
先登陸子機:
virsh console centos6.6_1
登陸後,安裝acpid服務:
yum install -y acpid
啓動:
/etc/init.d/acpid start
按ctrl ] 退出來
此時再執行 virsh shutdown centos6.6_1 就能夠啦。
 
克隆完後,virsh list all 就會發現clone1 這個子機,經過命令
virsh start clone1 能夠開啓該子機。
------------------------------------------------------------------------------
virsh 經常使用操做
a. 開啓子機
virsh start centos6.6_1
也能夠在開啓的同時連上控制檯
virsh start centos6.6_1 --console
 
b. 關閉子機
virsh shutdown centos6.6_1 (這個須要藉助子機上的acpid服務)
另一種方法是
virsh destroy centos6.6_1
 
c. 讓子機隨宿主機開機自動啓動
virsh autostart centos6.6_1
解除自動啓動
virsh autostart --disable centos6.6_1
 
d. 列出子機
virsh list  //只能列出啓動的子機
virsh list --all  //能夠把全部子機都列出來
 
e. 刪除子機
virsh destroy clone1
virsh undefine clone1
rm -f /data/clone1.img
 
f. 掛起子機
virsh suspend centos6.6_1
 
h. 恢復子機
virsh resume centos6.6_1
------------------------------------------------------------------
快照管理
a. 建立快照
virsh snapshot-create centos6.6_1
會報錯:
unsupported configuration: internal snapshot for disk vda unsupported for storage type raw
這是由於raw格式的鏡像不能作快照,因此須要先轉換一下格式
 
b. 磁盤鏡像轉換格式
先查看當前子機磁盤鏡像格式
qemu-img info /data/centos6.6_1.img  
結果是:
image: /data/centos6.6_1.img
file format: raw
virtual size: 30G (32212254720 bytes)
disk size: 1.6G
 
 
把raw格式轉換爲qcow格式(實際上是複製了一份):
qemu-img convert -f raw -O qcow2 /data/centos6.6_1.img /data/centos6.6_1.qcow2
 
qemu-img info /data/centos6.6_1.qcow2   //再次查看格式,結果以下
image: /data/centos6.6_1.qcow2
file format: qcow2
virtual size: 30G (32212254720 bytes)
disk size: 1.1G
cluster_size: 65536
 
 
如今咱們還須要編輯子機配置文件,讓它使用新格式的虛擬磁盤鏡像文件
virsh edit centos6.6_1  //這樣就進入了該子機的配置文件(/etc/libvirt/qemu/centos6.6_1.xml),跟用vim編輯這個文件同樣的用法
須要修改的地方是:
      
      
 
改成:
      
      
 
 
c. 繼續建立快照
virsh snapshot-create centos6.6_1  //此次成功了,提示以下
Domain snapshot 1437248443 created
 
 
列出快照:
virsh snapshot-list centos6.6_1
 
查看當前子機的快照版本:
virsh snapshot-current centos6.6_1
 
centos6.6_1子機的快照文件在  /var/lib/libvirt/qemu/snapshot/centos6.6_1/  目錄下
 
 
d.  恢復快照
首先須要關閉子機
virsh destroy centos6.6_1
 
確認子機是否關閉
virsh domstate centos6.6_1
關閉
 
vish snapshot-list centos6.6_1  //結果是
名稱               Creation Time             狀態
------------------------------------------------------------
1437248443           2015-07-19 03:40:43 +0800 shutoff
1437248847           2015-07-19 03:47:27 +0800 running
 
 
virsh snapshot-revert centos6.6_1  1437248443
 
e. 刪除快照
virsh snapshot-delete centos6.6_1  1437248847
-------------------------------------------------------------
磁盤擴容
a. 對於raw格式的虛擬磁盤擴容
 
qemu-img info /data/kvm/test03.img //自己只有9G
  1. image: /data/kvm/test03.img
  2. file format: raw
  3. virtual size: 9.0G (9663676416 bytes)
  4. disk size: 1.1G
 
qemu-img resize /data/kvm/test03.img +2G
 
qemu-img info /data/kvm/test03.img //如今增長了2G
  1. image: /data/kvm/test03.img
  2. file format: raw
  3. virtual size: 11G (11811160064 bytes)
  4. disk size: 1.1G
 
virsh destroy test03  //關閉test03虛擬機
virsh start test03  //開啓test03虛擬機
virsh console test03  //進入虛擬機
 
fdisk -l   查看已經磁盤分區已經增長
[root@localhost ~]# fdisk -l
 
  1. Disk /dev/vda: 11.8 GB, 11811160064 bytes
  2. 16 heads, 63 sectors/track, 22885 cylinders
  3. Units = cylinders of 1008 * 512 = 516096 bytes
  4. Sector size (logical/physical): 512 bytes / 512 bytes
  5. I/O size (minimum/optimal): 512 bytes / 512 bytes
  6. Disk identifier: 0x000099f3
  7.    Device Boot      Start         End      Blocks   Id  System
  8. /dev/vda1   *           3        1018      512000   83  Linux
  9. Partition 1 does not end on cylinder boundary.
  10. /dev/vda2            1018       16645     7875584   8e  Linux LVM
  11. Partition 2 does not end on cylinder boundary.
  12. Disk /dev/mapper/VolGroup-lv_root: 7205 MB, 7205814272 bytes
  13. 255 heads, 63 sectors/track, 876 cylinders
  14. Units = cylinders of 16065 * 512 = 8225280 bytes
  15. Sector size (logical/physical): 512 bytes / 512 bytes
  16. I/O size (minimum/optimal): 512 bytes / 512 bytes
  17. Disk identifier: 0x00000000
  18. Disk /dev/mapper/VolGroup-lv_swap: 855 MB, 855638016 bytes
  19. 255 heads, 63 sectors/track, 104 cylinders
  20. Units = cylinders of 16065 * 512 = 8225280 bytes
  21. Sector size (logical/physical): 512 bytes / 512 bytes
  22. I/O size (minimum/optimal): 512 bytes / 512 bytes
  23. Disk identifier: 0x00000000
 
可是磁盤掛載的空間並無增長
[root@localhost ~]# df -h
  1. Filesystem            Size  Used Avail Use% Mounted on
  2. /dev/mapper/VolGroup-lv_root
  3.                       6.5G  579M  5.6G  10% /
  4. tmpfs                 250M     0  250M   0% /dev/shm
  5. /dev/vda1             477M   26M  427M   6% /boot
 
由於新增長的空間尚未劃分使用。因此要繼續分區:
[root@localhost ~]# fdisk /dev/vda
 
  1. WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
  2.          switch off the mode (command 'c') and change display units to
  3.          sectors (command 'u').
  4. Command (m for help): p
  5. Disk /dev/vda: 11.8 GB, 11811160064 bytes
  6. 16 heads, 63 sectors/track, 22885 cylinders
  7. Units = cylinders of 1008 * 512 = 516096 bytes
  8. Sector size (logical/physical): 512 bytes / 512 bytes
  9. I/O size (minimum/optimal): 512 bytes / 512 bytes
  10. Disk identifier: 0x000099f3
  11.    Device Boot      Start         End      Blocks   Id  System
  12. /dev/vda1   *           3        1018      512000   83  Linux
  13. Partition 1 does not end on cylinder boundary.
  14. /dev/vda2            1018       <font color="#ff0000">16645</font>     7875584   8e  Linux LVM
  15. Partition 2 does not end on cylinder boundary.
  16. Command (m for help):<font color="#ff0000"> n</font>
  17. Command action
  18.    e   extended
  19.    p   primary partition (1-4)
  20. p
  21. Partition number (1-4): <font color="#ff0000">3</font>
  22. First cylinder (1-22885, default 1): <font color="#ff0000">16646</font>
  23. Last cylinder, +cylinders or +size{K,M,G} (16646-22885, default 22885):
  24. Using default value 22885
  25. Command (m for help): p
  26. Disk /dev/vda: 11.8 GB, 11811160064 bytes
  27. 16 heads, 63 sectors/track, 22885 cylinders
  28. Units = cylinders of 1008 * 512 = 516096 bytes
  29. Sector size (logical/physical): 512 bytes / 512 bytes
  30. I/O size (minimum/optimal): 512 bytes / 512 bytes
  31. Disk identifier: 0x000099f3
  32.    Device Boot      Start         End      Blocks   Id  System
  33. /dev/vda1   *           3        1018      512000   83  Linux
  34. Partition 1 does not end on cylinder boundary.
  35. /dev/vda2            1018       16645     7875584   8e  Linux LVM
  36. Partition 2 does not end on cylinder boundary.
  37. <font color="#ff0000">/dev/vda3           16646       22885     3144960   83  Linux</font>
  38. Command (m for help): w
  39. The partition table has been altered!
  40. Calling ioctl() to re-read partition table.
  41. WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
  42. The kernel still uses the old table. The new table will be used at
  43. the next reboot or after you run partprobe(8) or kpartx(8)
  44. Syncing disks.
 
而後再把這個/dev/vda3 加入到lvm裏面去:
 
ls  /dev/vda3 若是沒有這個分區,須要重啓一下。
 
[root@localhost ~]# pvcreate /dev/vda3
  1.   <span style="line-height: 1.5; ">Physical volume "/dev/vda3" successfully created</span>
 
[root@localhost ~]# pvs
  1. PV         VG       Fmt  Attr PSize PFree
  2.   /dev/vda2 <font color="#ff0000"> VolGroup</font> lvm2 a--  7.51g    0
  3.   /dev/vda3           lvm2 ---  3.00g 3.00g
 
[root@localhost ~]# vgextend VolGroup /dev/vda3
  1. Volume group "VolGroup" successfully extended
 
[root@localhost ~]# vgs
  1. VG       #PV #LV #SN Attr   VSize  <font color="#ff0000">VFree</font>
  2.   VolGroup   2   2   0 wz--n- 10.50g <font color="#ff0000">3.00g</font>
 
[root@localhost ~]# lvs
  1. LV      VG       Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  2.   lv_root VolGroup -wi-ao----   6.71g
  3.   lv_swap VolGroup -wi-ao---- 816.00m
 
[root@localhost ~]# lvextend -l +100%FREE /dev/VolGroup/lv_root
  1. Size of logical volume VolGroup/lv_root changed from 6.71 GiB (1718 extents) to 9.71 GiB (2485 extents).
  2.   Logical volume lv_root successfully resized
 
[root@localhost ~]# df -h
  1. Filesystem            Size  Used Avail Use% Mounted on
  2. /dev/mapper/VolGroup-lv_root
  3.                       6.5G  618M  5.6G  10% /
  4. tmpfs                 250M     0  250M   0% /dev/shm
  5. /dev/vda1             477M   26M  427M   6% /boot
 
[root@localhost ~]# resize2fs /dev/VolGroup/lv_root
  1. resize2fs 1.41.12 (17-May-2010)
  2. Filesystem at /dev/VolGroup/lv_root is mounted on /; on-line resizing required
  3. old desc_blocks = 1, new_desc_blocks = 1
  4. Performing an on-line resize of /dev/VolGroup/lv_root to 2544640 (4k) blocks.
  5. The filesystem on /dev/VolGroup/lv_root is now 2544640 blocks long.
 
[root@localhost ~]# df -h
  1. Filesystem            Size  Used Avail Use% Mounted on
  2. /dev/mapper/VolGroup-lv_root
  3.                      <font color="#ff0000"> 9.5G  </font>618M  8.4G   7% /
  4. tmpfs                 250M     0  250M   0% /dev/shm
  5. /dev/vda1             477M   26M  427M   6% /boot
 
另外,若是是增長磁盤,思路是:
建立磁盤: qemu-img create -f raw  /data/kvm/test03_2.img 5G
關閉虛擬機: virsh destroy test03
編輯配置文件: virsh edit test03  增長以下:
  1. <disk type='file' device='disk'>     
  2.   <driver name='qemu' type='raw' cache='none'/>      
  3.   <source file='/data/kvm/test03_2.img'/>      
  4.   <target dev='vdb' bus='virtio'/>      
  5. </disk>
 
開啓虛擬機:virsh start test03
進入虛擬機:virsh console test03
分區: fdisk /dev/vdb
格式化 (略)
掛載 (略)
固然也能夠按照上面的思路把 /dev/vdb1 加入到 lvm裏面去
 
 
b. qcow2格式
步驟基本上和raw同樣。若是提示 This image format does not support resize, 檢查一下你qemu-img create的時候,是否有加  preallocation=metadata 選項,若是有,就不能resize了。
---------------------------------------------------------------------------------------------------------------------------
調整內存和查看機子配置
virsh dominfo test02
virsh edit  test02
修改:
  524288
  524288
  1
 
爲:
  624288
  624288
  2
 
 
重啓虛擬機:
virsh destroy test02
virsh start test02
--------------------------------------------------------
不重啓虛擬機在線增長網卡
virsh domiflist test02  查看test02子機的網卡列表
virsh attach-interface test02 --type bridge --source br0   //命令行增長一塊網卡
virsh dumpxml test02 > /etc/libvirsh/qemu/test02.xml   //命令行增長的網卡只保存在內存中,重啓就失效,因此須要保存到配置文件中,其中/etc/libvirsh/qemu/test02.xml 爲test02子機的配置文件
virsh console test02 //進入虛擬機後,執行
ifconfig -a
發現多了一個網卡  eth1
--------------------------------------------------
虛擬機遷移
該方式要確保虛擬機是關機狀態。
virsh shutdown test02
virsh dumpxml test02 > /etc/libvirt/qemu/test03.xml  // 若是是遠程機器,須要把該配置文件拷貝到遠程機器上
virsh domblklist test02  //查看test02子機的磁盤所在目錄
Target     Source
------------------------------------------------
vda        /data/add1.qcow2
rsync -av /data/add1.qcow2 /data/test03.qcow2   //若是是遷移到遠程,則須要把該磁盤文件拷貝到遠程機器上
vi /etc/libvirt/qemu/test03.xm  //由於是遷移到本機,配置文件用的是test02子機的配置,不改會有衝突,因此須要修改該文件,若是是遠程機器不用修改
修改domname:
  test03
修改uuid(隨便更改一下數字,位數不要變)
77bb10bd-3ad8-8899-958d-756063002969
修改磁盤路徑:
   
      
      />
      
      
 
   
 
virsh list --all   //會發現新遷移的test03子機
相關文章
相關標籤/搜索