Resize qcow2 image

Cloud/KVM 2013. 9. 28. 23:07
[Info]
  • e.g.) Default 8GB(orig.img) -> 20GB(test.img) any..
  • Both Single-Image and BackingType-Image 
  • Test format: qcow2
  • Tools: parted, qemu-img, qemu-nbd
  • Test Platform: Ubuntu 13.04 x86_64


[Start]


- Check Image

# qemu-img info orig.img 

image: orig.img

file format: qcow2

virtual size: 8.0G (8589934592 bytes)

disk size: 883M

cluster_size: 65536

# qemu-img create -b /home/orig.img -f qcow2 test.img

# qemu-img info test.img

image: test.img

file format: qcow2

virtual size: 8.0G (8589934592 bytes)

disk size: 196K

cluster_size: 65536

backing file: /home/orig.img


- Connect NBD & Check partitions

# modprobe nbd

# qemu-nbd -c /dev/nbd0 /home/test.img

# ls -al /dev/nbd0*

brw-rw---- 1 root disk 43, 0 Sep 28 23:18 /dev/nbd0

brw-rw---- 1 root disk 43, 1 Sep 28 23:18 /dev/nbd0p1

# parted /dev/nbd0 --script print free

Model: Unknown (unknown)

Disk /dev/nbd0: 8590MB

Sector size (logical/physical): 512B/512B

Partition Table: msdos


Number  Start   End     Size    Type     File system  Flags

        32.3kB  1049kB  1016kB           Free Space

 1      1049kB  8589MB  8588MB  primary  ext4         boot

        8589MB  8590MB  1049kB           Free Space

- Disconnect NBD & Resize image

# qemu-nbd -d /dev/nbd0

/dev/nbd0 disconnected

# qemu-img resize test.img 20G

Image resized.


- Check image

# qemu-img info test.img

image: test.img

file format: qcow2

virtual size: 20G (21474836480 bytes)

disk size: 200K

cluster_size: 65536

backing file: /home/orig.img


Connect NBD & Check partitions

qemu-nbd -c /dev/nbd0 /home/test.img

parted /dev/nbd0 --script print free

Model: Unknown (unknown)

Disk /dev/nbd0: 21.5GB

Sector size (logical/physical): 512B/512B

Partition Table: msdos


Number  Start   End     Size    Type     File system  Flags

        32.3kB  1049kB  1016kB           Free Space

 1      1049kB  8589MB  8588MB  primary  ext4         boot

        8589MB  21.5GB  12.9GB           Free Space


- Re-Partitioning (Remove Partition and Create Partition) ("1" is partition-number)

# parted /dev/nbd0 --script rm 1

# parted /dev/nbd0 --script print free

Model: Unknown (unknown)

Disk /dev/nbd0: 21.5GB

Sector size (logical/physical): 512B/512B

Partition Table: msdos


Number  Start   End     Size    Type  File system  Flags

        32.3kB  21.5GB  21.5GB        Free Space


# parted -a optimal /dev/nbd0 --script mkpart primary ext4 0% 100%

# parted /dev/nbd0 --script print free

Model: Unknown (unknown)

Disk /dev/nbd0: 21.5GB

Sector size (logical/physical): 512B/512B

Partition Table: msdos


Number  Start   End     Size    Type     File system  Flags

        32.3kB  1049kB  1016kB           Free Space

 1      1049kB  21.5GB  21.5GB  primary  ext4


- Mount image's Partition (to edit boot-script :: rc.local)

# kpartx -a /dev/nbd0

# mount /dev/mapper/nbd0p1 /mnt/


- Apply Parition to VM (by resize2fs)

# echo 'resize2fs /dev/vda1; sed -i \"/resize2fs/d\" /etc/rc.local' >> /mnt/etc/rc.local


[End]

Booting VM with new image




[Appendix]

- Disconnect kpartx & NBD

# umount /mnt

# kpartx -d /dev/nbd0

# qemu-nbd -d /dev/nbd0


Posted by 사랑줍는거지
,

Qemu & kvm guide #1 (intro & basic)

View more documents from JungIn Jung


Posted by 사랑줍는거지
,





Migration

Introduction

KVM currently supports savevm/loadvm and offline or live migration Migration commands are given when in qemu-monitor (Alt-Ctrl-2). Upon successful completion, the migrated VM continues to run on the destination host.

Note

You can migrate a guest between an AMD host to an Intel host and back. Naturally, a 64-bit guest can only be migrated to a 64-bit host, but a 32-bit guest can be migrated at will.

There are some older Intel processors which don't support NX (or XD), which may cause problems in a cluster which includes NX-supporting hosts. To disable NX for a given guest, start it with such a parameter: -cpu qemu64,-nx

Requirements

  • The VM image is accessible on both source and destination hosts (located on a shared storage, e.g. using nfs).
  • It is recommended an images-directory would be found on the same path on both hosts (for migrations of a copy-on-write image -- an image created on top of a base-image using "qemu-image create -b ...")
  • The src and dst hosts must be on the same subnet (keeping guest's network when tap is used).
  • Do not use -snapshot qemu command line option.
  • For tcp: migration protocol
  • the guest on the destination must be started the same way it was started on the source.

highlights / merits

  • Almost unnoticeable guest down time
  • Guest is not involved (unique to KVM Live Migration [#1 1])
  • Capability to tunnel VM state through an external program (unique to KVM Live Migration [#1 1])
  • ssh/gzip/bzip2/gpg/your own
  • Upon success guest continues to run on destination host, upon failure guest continues to run on source host (with one exception)
  • Short and Simple
  • Easy to enhance
  • Hardware independence (almost).
  • Support for migration of stopped (paused) VMs.
  • Open

Anchor(1) 1 These features are unique to KVM Live Migration as far as I know. If you know of other hypervisor that support any of them please update this page or let me (Uri) know.

User Interface

The user interface is through the qemu monitor (alt-ctrl-2 on the SDL window)

Management

migrate [-d] <URI>
migrate_cancel    

The '-d' will let you query the status of the migration.

With no '-d' the monitor prompt returns when the migration completes. URI can be one of 'exec:<command>' or tcp:<ip:port>

Status

info migrate 

Migration Parameters

migrate_set_speed <speed>   set bandwidth control parameters (max transfer rate per second)

Example / HOWTO

A is the source host, B is the destination host:

TCP example:

1. Start the VM on B with the exact same parameters as the VM on A, in migration-listen mode:

B: <qemu-command-line> -incoming tcp:0:4444 (or other PORT))

2. Start the migration (always on the source host):

A: migrate -d tcp:B:4444 (or other PORT)

3. Check the status (on A only):

A: (qemu) info migrate                   

Offline example:

1. unlimit bandwidth used for migration:

A: migrate_set_speed 1g                

2. stop the guest:

A: stop                                

3. continue with either TCP or exec migration as described above.

Problems / Todo

  • TSC offset on the new host must be set in such a way that the guest sees a monotonically increasing TSC, otherwise the guest may hang indefinitely after migration.
  • usbdevice tablet complains after migration.
  • handle migration completion better (especially when network problems occur).
  • More informative status.
  • Migration does not work while CPU real-mode/protected mode are still changing.

savevm/loadvm to an external state file (using pseudo-migration)

  • To be supported directly by Migration Protocols, but until then...
  • Save VM state into a compressed file
    • Save
stop                                                               
migrate_set_speed 4095m                                            
migrate "exec:gzip -c > STATEFILE.gz"                              
    • Load
gzip -c -d STATEFILE.gz | <qemu-command-line> -incoming "exec: cat"   or
<qemu-command-line> -incoming "exec: gzip -c -d STATEFILE.gz"
  • Save VM State into an encrypted file (assumes KEY has already been generated)
    • Save
stop                                                                          
migrate_set_speed 4095m
migrate "exec:gpg -q -e -r KEY -o STATFILE.gpg"
  • Load VM state from an encrypted file
gpg -q -d -r KEY STATEFILE.gpg | <qemu-command-line> -incoming "exec:cat"

more exec: options

  • Send encrypted VM state from host A to host B (Note: ssh is probably better, this is just for show)
    • on host A
migrate "exec:gpg -q -e -r KEY | nc B 4444"
    • on host B
nc -l 4444 | gpg -q -d -r KEY | <qemu-command-line> -incoming "exec:cat"

Algorithm (the short version)

1. Setup

  • Start guest on destination, connect, enable dirty page logging and more

2. Transfer Memory

  • Guest continues to run
  • Bandwidth limitation (controlled by the user)
  • First transfer the whole memory
  • Iteratively transfer all dirty pages (pages that were written to by the guest).

3. Stop the guest

  • And sync VM image(s) (guest's hard drives).

4. Transfer State

  • As fast as possible (no bandwidth limitation)
  • All VM devices' state and dirty pages yet to be transferred

5. Continue the guest

  • On destination upon success
    • Broadcast "I'm over here" Ethernet packet to announce new location of NIC(s).
  • On source upon failure (with one exception).


Instructions for kvm-13 and below: MigrationQemu0.8.2.


Posted by 사랑줍는거지
,

qcow2 이미지 내부 파티션을 LVM으로 했을경우를 가정했으며, LVM이 아닌 일반적인 파티션을 적용했을 경우에는 "kpart -a /dev/nbd0" 처리후, /dev/mapper/ 에 생성되 각 파티션을 바로 마운트 하면 된다. 한마디로 더 간단해진다.



[pre-install]

# yum install qemu-common

# modprobe nbd



[mount]

# qemu-nbd -c /dev/nbd0 image.qcow2


[하기 (1)안, (2)안 중, 택일]

(1)안

# kpart -a /dev/nbd0

# pvscan

# vgscan

# vgchange -a y VolGroup00


(2)안

# tune2fs -c 0 -i 0 /dev/nbd0
 


# mount /dev/VolGroup00/LogVol00 /mnt/lvm/



[umount]

# vgchange -a n VolGroup00

# karpt -d /dev/nbd0

# qemu-nbd -d /dev/nbd0

'Cloud > KVM' 카테고리의 다른 글

Resize qcow2 image  (0) 2013.09.28
KVM/QEMU Guide #1 (Intro & Basic)  (0) 2012.06.22
KVM/QEMU - How to Migration(include LIve)  (0) 2012.05.13
[IaaS] - CloudStack CE 2.1.7 설치  (1) 2011.02.22
Posted by 사랑줍는거지
,
그간 잊어 먹을까 노심초사..... 메모해둬야지....했던걸 이제서야 정리 한다. 지극히 주관적인 눈높이에, 메모 수준이지만, 안해두는 것보단 나을듯......
(Cloud.com에 공개되어 있는 PDF 설치 문서 내용에 다 있는 내용이다. 해당 문서를 먼저 참조 하길.......)

CloudStack CE......... 꽤 괜찮은 물건이다. WEB-UI는 물론이고 IaaS의 기본적인 환경(물론 제약사항은 있다)을 아주 쉽고 빠르게 구축해준다. 작동 메커니즘에 대한 이해도를 어느정도 쌓는다면, CE(Community Edition)만으로도 중소규모의 IaaS는 거뜬히 소화해낼 수 있을 듯하다. Opennebula의 경우 사용자가 원하는대로 구축해 나갈수 있는 요소들이 많은 반면, CloudStack은 가져다 쓰면 그만이다. 즉, 여러가지 고민하기는 싫고, 웬만한 IaaS 환경은 다 제공되었으면~~ 할 때 쓰기 딱! 좋다. (물론 단점도 있다... 이건 직접 찾아보시길.........)

보다 자세한 CloudStack의 소개나 아키텍쳐등의 정보를 원하면 아래 사이트를 참조하시고(문서들이 PDF로 아주 잘 작성되어 있음), 본 글에서는 CentOS기반(설치 문서도 사이트에 있음)에서 CloudStack CE 설치 과정 요약해둠.

설치는 크게 두 부분으로 나뉘는데, [관리노드], [컴퓨팅노드] 로 구분지음.
(CloudStack CE는 Hypervisior로 KVM만 지원함.) 

사이트 : http://cloud.com


[환경]

- OS
- 관리노드/컴퓨팅노드 : CentOS 5.5 x86_64 (최소 설치)

-  KVM (Kernel based Virtual Machine) 운영이 가능한 H/W
- yum -y groupinstall KVM

- JDK

- MySQL
- innodb 설정 조정함.

- Hostname
- 반드시 FQDN이어야 함. ex) test.foo.com

- DNS를 운영하거나, /etc/hosts 파일을 이용 권장




[설치]

<관리노드>

- 클라우드 노드 관리
/etc/hosts 또는 DNS Server(Option) 

- MySQL 설치
yum -y install mysql-server
sed -i '/^\[mysqld\]/a innodb_lock_wait_timeout=600' /etc/my.cnf
sed -i '/^\[mysqld\]/a innodb_rollback_on_timeout=1' /etc/my.cnf
service mysqld start
chkconfig mysqld on
mysqladmin -uroot password '1234'

- CloudStack CE Yum-Repo 다운로드/설치
wget http://download.cloud.com/foss/centos/cloud.repo -O /etc/yum.repos.d/cloud.repo
yum clean all
yum -y install cloud-console-proxy
yum -y install cloud-client

- 관리노드 DB 생성
cloud-setup-databases cloud:1234@localhost kvm --deploy-as=root:1234

- CloudStack CE에 불필요한 libvirt 관련 기본 기능 Off
virsh net-destroy default
rm -f /etc/libvirt/qemu/networks/default.xml
service dnsmasq stop
chkconfig dnsmasq off

- 관리노드 기본 환경 구성
cloud-setup-management

- 관리 콘솔 구성
cloud-setup-console-proxy

- 관리노드 설치 완료 및 재부팅
reboot

- WEB-UI 접속
http://{관리노드 IP}:8080/client
Default Auth : admin / password

<컴퓨팅노드>
- CloudStack CE Yum-Repo 다운로드/설치
wget http://download.cloud.com/foss/centos/cloud.repo -O /etc/yum.repos.d/cloud.repo
yum clean all
yum -y install cloud-agent
cloud-setup-agent
(cloud-setup-agent --no-kvm (not use kvm module))

- CloudStack CE에 불필요한 libvirt 관련 기본 기능 Off
virsh net-destroy default
rm -f /etc/libvirt/qemu/networks/default.xml
service dnsmasq stop
chkconfig dnsmasq off

- Console-Proxy를 위한 iptables 정책. (/etc/rc.local에 추가)
/sbin/iptables -I INPUT -i cloud0 -j ACCEPT  
/sbin/iptables -I FORWARD -i cloud0 -o cloud0 -j ACCEPT  
/sbin/iptables -I FORWARD -i cloudbr0 -o cloudbr0 -j ACCEPT 
/sbin/iptables -I INPUT -m tcp -p tcp --dport 5900:6100 -j ACCEPT 

- 컴퓨팅노드 설치 완료 및 재부팅
- 재부팅 완료후, 정상적으로 cloud-agent 서비스가 수행되면, 관리노드의 WEB-UI의 Host탭에서 컴퓨팅노드가 보여야 함.

<끝>


* 설치는 매우 간단하지만, 운영에 있어서는 반드시 cloud.com의 PDF문서들은 꼭! 필독!
Posted by 사랑줍는거지
,