1. 程式人生 > >改變kvm鏡像大小和使用已經存在的鏡像

改變kvm鏡像大小和使用已經存在的鏡像

kvm image

在這個知識點我們將會用到以下工具:
  • qemu-img
  • losetup
  • tune2fs
  • e2fsck
  • kpartx
  • fdisk
  • resizefs

下面步驟演示我們如何添加空間到先前創建好的raw格式的鏡像,擴展root分區和擴展系統鏡像。我們的目標是從10G擴展到20G。

(1)首先獲取鏡像目前大小

root@kvm:# qemu-img info debian.img
image: debian.img
file format: raw
virtual size: 10G (10737418240 bytes)
disk size: 848M

(2)添加額外的10G空間到鏡像中

root@kvm:~# qemu-img resize -f raw debian.img +10GB

Image resized.
註意:並不是所有的鏡像格式都支持resize的,在resize前應該轉換成raw格式的鏡像。

(3)再次獲取鏡像的大小,看是否有變化

root@kvm:~# qemu-img info debian.img
image: debian.img
file format: raw
virtual size: 20G (21474836480 bytes)
disk size: 848M

二. 使用預先存在的鏡像

有很多LINUX發行商他們已經構建好針對QEMU/KVM格式的系統鏡像。我們直接來用即可。

(1)去獲取 Debian的鏡像:

root@kvm:~tmp# wget https://people.debian.org/~aurel32/qemu/amd64/debian_wheezy_amd64_standard.qcow2

--2017-03-09 22:07:20-- 2 Resolving people.debian.org (people.debian.org)... 2001:41c8:1000:21::21:30, 5.153.231.30 Connecting to people.debian.org (people.debian.org)|2001:41c8:1000:21::21:30|:443... connected. HTTP request sent, awaiting response... 200 OK Length: 267064832 (255M) Saving to: ‘debian_wheezy_amd64_standard.qcow2’ debian_wheezy_amd64_standard.qcow2 100% . [===================================>] 254.69M 35.8MB/s in 8.3s 2017-03-09 22:07:29 (30.9 MB/s) - ‘debian_wheezy_amd64_standard.qcow2’ saved [267064832/267064832]

(2)檢查鏡象的類型

root@kvm:~tmp# wget https://people.debian.org/~aurel32/qemu/amd64/debian_wheezy_amd64_standard.qcow2
--2017-03-09 22:07:20-- 2 Resolving people.debian.org (people.debian.org)... 2001:41c8:1000:21::21:30, 5.153.231.30 Connecting to people.debian.org (people.debian.org)|2001:41c8:1000:21::21:30|:443... connected. HTTP request sent, awaiting response... 200 OK Length: 267064832 (255M) Saving to: ‘debian_wheezy_amd64_standard.qcow2’ debian_wheezy_amd64_standard.qcow2 100% . [===================================>] 254.69M 35.8MB/s in 8.3s 2017-03-09 22:07:29 (30.9 MB/s) - ‘debian_wheezy_amd64_standard.qcow2’ saved [267064832/267064832]

(3)下載centos的鏡像

root@kvm:/tmp# wget https://cloud.centos.org/centos/7/images/CentOS-7-x86_64-GenericCloud.qcow2 --2017-03-09 22:11:34-- https://cloud.centos.org/centos/7/images/CentOS-7-x86_64-GenericCloud.qcow2 Resolving cloud.centos.org (cloud.centos.org)... 2604:4500::2a8a, 136.243.75.209 Connecting to cloud.centos.org (cloud.centos.org)|2604:4500::2a8a|:443... connected. HTTP request sent, awaiting response... 200 OK Length: 1361182720 (1.3G) Saving to: ‘CentOS-7-x86_64-GenericCloud.qcow2’
CentOS-7-x86_64-GenericCloud.qcow2 100%[=========================================>] 1.27G 22.3MB/s in 54s
2017-03-09 22:12:29 (24.0 MB/s) - ‘CentOS-7-x86_64-GenericCloud.qcow2’ saved [1361182720/1361182720]
FINISHED --2017-03-09 22:12:29-- Total wall clock time: 54s Downloaded: 1 files, 1.3G in 54s (24.0 MB/s)

(4)檢查鏡像的類型

root@kvm:~# qemu-img info CentOS-7-x86_64-GenericCloud.qcow2
image: CentOS-7-x86_64-GenericCloud.qcow2
file format: qcow2
virtual size: 8.0G (8589934592 bytes)
disk size: 1.3G
cluster_size: 65536
Format specific information:
compat: 0.10
refcount bits: 16
root@kvm:~#

下面是一些其它發行商LINUX的下地鏈接:請參考
Official Ubuntu images: https://uec-images.ubuntu.com/releases/
Official CentOS images: https://cloud.centos.org/centos/
Official Debian images: http://cdimage.debian.org/cdimage/openstack/
Official Fedora images: https://alt.fedoraproject.org/cloud/
Official openSUSE : http://download.opensuse.org/repositories/Cloud:/Images:/

改變kvm鏡像大小和使用已經存在的鏡像