顯示具有 KVM 標籤的文章。 顯示所有文章
顯示具有 KVM 標籤的文章。 顯示所有文章

2012年11月15日 星期四

KVM with LVM snapshot



一般來說, 如果使用 KVM來進行VM的管理,多半採用
virsh snapshot-create (VM name)
virsh snapshot-create-as (VM name) (snapshot name)
virsh snapshot-list (VM name)   #查到snapshot name
virsh snapshot-revert (VM name) (snapshot name)  
但這種方式,只適用在qcow2的vm image上

若VM的disk 使用raw type, 那便沒辦法了
於是, LVM的idea一般人都會想到, 利用 LVM的logical volumn當成VM raw disk
再利用snapshot 搭配 rollback(lvconvert --merge),可以進行類似KVM snapshot功能

step 1)先在Host的VG建立一個LV
lvcreate -L 100G -n lv_test /dev/VolGroup

step 2)建立一個VM
virt-install -n vm_test -r 1024 --vcpus=1 --disk path=/dev/VolGroup/lv_test .....(略)

安裝完成後,用root登入,然後將VM關掉

step 3)測試VM raw disk snapshot
  take snapshots:

    time line       t0  ----->    t1          -----> t2           -------> t3            ------>  t4

    take snapshot                lv_test_ss1                               lv_test_ss2
    
    [action in VM]  init 0       boot                touch 1.txt           boot                  rm *
                                                     rm A.txt                                    touch 2.txt
                                                     init 0                                      init 0
    
    [files at ~/.]  A.txt                            B.txt                                       2.txt
                    B.txt                            1.txt                                       
                                                                                                 
                                                                                                 


  rollback:

    time line       t5  ----->    t6          -----> t7           -------> t8   

    rollback        lv_test_ss1                      lv_test_ss2           
    
    [action in VM]  boot         init 0              boot                       
                                    
                                                 
    
    [files at ~/.]  2.txt        A.txt               A.txt                 B.txt
                                 B.txt               B.txt                 1.txt
                               (回到t0)                                   (回到t2)


take snapshot lv_test_ss1)
    lvcreate -L 50G -s -n lv_test_ss1 /dev/VolGroup/lv_test   
take snapshot lv_test_ss2)
    lvcreate -L 50G -s -n lv_test_ss2 /dev/VolGroup/lv_test   
rollback to snapshot lv_test_ss1)
    lvconvert --merge /dev/VolGroup/lv_test_ss1   
rollback to snapshot lv_test_ss2)
    lvconvert --merge /dev/VolGroup/lv_test_ss2
     
    
註:1)對於snapshot本身不能再對它取snapshot,也就是說,下面是無效指令
    lvcreate -L 50G -s -n lv_test_xx /dev/VolGroup/lv_test_ss1    
   2)上例來看,rollback 至 lv_test_ss1時, 檔案回復至當時最原始的A.txt以及B.txt
              rollback 至 lv_test_ss2時, 檔案回復至剛做lv_test_ss2前的B.txt以及1.txt
   3)看來在進行snapshot rollback時, 對於中間的snapshot的內容,也會一一進行追蹤
     不會導致檔案的遺失. 例如上例中, 在rollback至lv_test_ss1時,早已沒有了1.txt
     但在進行lv_test_ss2 rollback時,卻還是正確的檔案數量(也沒受t4 rm *的影響)
   4)lvconvert --merge似乎是RHEL/CentOS 6之後才有提供
   5)如果執行 lvconvert --merge出現warning "Can't merge over open origin volume"時,那你可能會需要對LV做 deactivate and activate的動作
     lvchange -an /dev/VolGroup/lv_test     ##deactivate
     lvchange -ay /dev/VolGroup/lv_test     ##activate
   6)一般如果你直接對snapshot做lvremove的動作, 那所有的異動都會進到原本的LV
   
     
     

2012年11月9日 星期五

KVM -- create a virtual machine console displayed on vnc



prepare yum server in ftp://172.16.43.248/pub62/
and kickstart file @172.16.43.248:/var/www/html/pub62/ks.cfg



  172.16.43.248               172.16.43.145 (qemu:///system)    
+------------------+     +-----------------------+      
|Yum Server        |     | Virtualization        |     
|ftp /pub62        |     | Virtualization Client |     
|http /pub62/ks.cfg|     |                       |      
|                  |     | VNCSERVERS "50:root"  |      
|                  |     |                       |      
|------------------|     |       172.16.43.149   |     
|                  |     |       +------------+  |   vnc: 172.16.43.145:0  
|                  |     |       | cent6      +--= br0 (BRIDGE eth0)    
|                  |     |       +------------+  |     
|                  |     |                       |      
|                  |     |           dhcp        |     
|                  |     |       +------------+  |   vnc: 172.16.43.145:1  
|                  |     |       |centNAT     +--= virbr0 (NAT)    
|                  |     |       +------------+  |     
+-------+----------+     +------+----------------+     
        |                       | eth0  vnc: 172.16.43.145:50         
--------+-----------------------+--------------------------+ vnc client accessable

[root@localhost ~]# yum install tigervnc-server
[root@localhost ~]# vi /etc/sysconfig/vncservers
   :
   :
# VNCSERVERS="2:myusername"
# VNCSERVERARGS[2]="-geometry 800x600 -nolisten tcp -localhost"

VNCSERVERS="50:root"
[root@localhost ~]# vncpasswd
[root@localhost ~]# vncserver
New 'localhost.localdomain:50 (root)' desktop is localhost.localdomain:50

Starting applications specified in /root/.vnc/xstartup
Log file is /root/.vnc/localhost.localdomain:50.log

[root@localhost ~]# vncserver -kill :50
[root@localhost ~]# service vncserver start
[root@localhost ~]# chkconfig vncserver on



[root@localhost ~]# yum groupinstall 'Virtualization' 'Virtualization Client'
[root@localhost ~]# lsmod | grep kvm
kvm_intel              50380  6 
kvm                   305081  1 kvm_intel

[root@localhost ~]# service NetworkManager stop
[root@localhost ~]# chkconfig NetworkManager off


<<<<<<<<<< BRIDGE >>>>>>>>>>


[root@localhost ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE="eth0"
HWADDR="68:05:CA:08:46:F0"
#NM_CONTROLLED="yes"
ONBOOT="yes"
BOOTPROTO=static
BRIDGE=br0
#IPADDR=172.16.43.145
#NETMAS=255.255.255.0
#GATEWAY=172.16.43.1
[root@localhost ~]# cat /etc/sysconfig/network-scripts/ifcfg-br0
DEVICE=br0
ONBOOT=yes
BOOTPROTO=static
IPADDR=172.16.43.145
NETMASK=255.255.255.0
GATEWAY=172.16.43.1
TYPE=Bridge
USERCTL=yes
NAME="Bridge eth0"

[root@localhost ~]# lv_create -L 100G -n lv_test VolGroup

[root@localhost ~]# cat rhel-install4.sh
#!/bin/bash
virsh destroy cent6 &> /dev/null

##vnc的port如果不指定,可避免同時安裝多台VM的listen port衝突,port便由系統配發
##vnc的配置也可由--vnc(預設便是listen 127.0.0.1) 取代--graphics vnc,...
/usr/sbin/virt-install -n cent6 -r 1024 --vcpus=1 --accelerate --disk path=/dev/VolGroup/lv_test --network bridge:br0 -x "ks=http://172.16.43.248/pub62/ks.cfg ksdevice=eth0 ip=172.16.43.149 netmask=255.255.255.0 dns=168.95.1.1 gateway=172.16.43.1" -l ftp://172.16.43.248/pub62/ --graphics vnc[,port=5900],listen=0.0.0.0 &

sleep 5 
virsh autostart cent6



<<<<<<<<<< NAT >>>>>>>>>>


[root@localhost ~]# cat note-NAT 
[root@localhost ~]# lvcreate -L 100G -n lv_test2 VolGroup

[root@localhost ~]# vi /etc/sysctl.conf
# Kernel sysctl configuration file for Red Hat Linux
#
# For binary values, 0 is disabled, 1 is enabled.  See sysctl(8) and
# sysctl.conf(5) for more details.

# Controls IP packet forwarding
net.ipv4.ip_forward = 1

[root@localhost ~]# sysctl -p
net.ipv4.ip_forward = 1
  :


[root@localhost ~]# service iptables status
iptables: Firewall is not running.
[root@localhost ~]# iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination       

[root@localhost ~]# service libvirtd restart
Stopping libvirtd daemon:                                  [  OK  ]
Starting libvirtd daemon:                                  [  OK  ]
[root@localhost ~]# iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
ACCEPT     udp  --  anywhere             anywhere            udp dpt:domain 
ACCEPT     tcp  --  anywhere             anywhere            tcp dpt:domain 
ACCEPT     udp  --  anywhere             anywhere            udp dpt:bootps 
ACCEPT     tcp  --  anywhere             anywhere            tcp dpt:bootps 

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         
ACCEPT     all  --  anywhere             192.168.122.0/24    state RELATED,ESTABLISHED 
ACCEPT     all  --  192.168.122.0/24     anywhere            
ACCEPT     all  --  anywhere             anywhere            
REJECT     all  --  anywhere             anywhere            reject-with icmp-port-unreachable 
REJECT     all  --  anywhere             anywhere            reject-with icmp-port-unreachable 

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination         


[root@localhost ~]# cat ./rhel-install5.sh
#!/bin/bash
virsh destroy centNAT &> /dev/null

/usr/sbin/virt-install -n centNAT -r 1024 --vcpus=1 --accelerate --disk path=/dev/VolGroup/lv_test2 --network network:default -x "ks=http://172.16.43.248/pub62/ks.cfg  ip=dhcp dns=168.95.1.1 " -l ftp://172.16.43.248/pub62/ --graphics vnc,port=5901,listen=0.0.0.0 &

sleep 5 
virsh autostart centNAT


If you found out that ksmd was run with high CPU% please do this:


service ksm stop
service ksmtuned stop

refer to : https://bugzilla.redhat.com/show_bug.cgi?id=541230

文章分類