項(xiàng)目的需求,需要制作kvm虛擬機(jī)鏡像。在網(wǎng)上查找了一些資料并經(jīng)過實(shí)際的配置操作。下面簡(jiǎn)單說明一下步驟,以供參考。
準(zhǔn)備iso鏡像
目前制作鏡像考慮不考慮從無到有,只是在已有鏡像的基礎(chǔ)上加上一些業(yè)務(wù)功能和配置,做成自己的鏡像文件,所以,必須基于已有的iso鏡像。
以u(píng)buntu為例。可以從官網(wǎng)上獲取iso鏡像,可以選擇mini鏡像(大小只有不到50M)或者正常的鏡像文件。需要主要的是mini鏡像會(huì)在安裝的過程中下載大量文件,整體來說在這里并沒有節(jié)省大小。所以如果網(wǎng)絡(luò)狀況不好的話還是下載完整的鏡像更合適。
系統(tǒng)準(zhǔn)備工作
Make sure that your system has the hardware virtualization extensions: For Intel-based hosts, verify the CPU virtualization extension [vmx] are available using following command.
[root@server ~]# grep -e 'vmx' /proc/cpuinfo
flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc aperfmperf eagerfpu pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 fma cx16 xtpr pdcm pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm ida arat epb pln pts dtherm tpr_shadow vnmi flexpriority ept vpid fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid xsaveopt
flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc aperfmperf eagerfpu pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 fma cx16 xtpr pdcm pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm ida arat epb pln pts dtherm tpr_shadow vnmi flexpriority ept vpid fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid xsaveopt
flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc aperfmperf eagerfpu pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 fma cx16 xtpr pdcm pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm ida arat epb pln pts dtherm tpr_shadow vnmi flexpriority ept vpid fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid xsaveopt
flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc aperfmperf eagerfpu pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 fma cx16 xtpr pdcm pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm ida arat epb pln pts dtherm tpr_shadow vnmi flexpriority ept vpid fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid xsaveopt
For AMD-based hosts, verify the CPU virtualization extension [svm] are available.
[root@server ~]# grep -e 'svm' /proc/cpuinfo
If there is no output make sure that virtualization extensions is enabled in BIOS. Verify that KVM modules are loaded in the kernel “it should be loaded by default”.
[root@server ~]# lsmod | grep kvm
kvm_intel 200704 0
kvm 593920 1 kvm_intel
irqbypass 16384 1 kvm
The output should contains kvm_intel for intel-based hosts or kvm_amd for amd-based hosts.
Make sure that Selinux be in Permissive mode.
[root@server ~]# setenforce 0
KVM安裝和部署
主要是用來制作和啟動(dòng)kvm鏡像的tools,這里用virt-manager和virt-install, 制作鏡像的機(jī)器是centos 7, 安裝kvm tools的方法如下:
install qemu-kvm and qemu-img
[root@server ~]# yum install qemu-kvm qemu-img
qemu-kvm和qemu-img主要提供了用戶級(jí)別的KVM鏡像和disk鏡像的管理。
install some usefull tools
- virt-manager provides a GUI tool to administrate your virtual machines.
- libvirt-client provides a CL tool to administrate your virtual environment this tool called virsh.
- virt-install provides the command “virt-install” to create your virtual machines from CLI.
- libvirt provides the server and host side libraries for interacting with hypervisors and host systems.
[root@server ~]# yum install virt-manager libvirt libvirt-python libvirt-client
install additional package groups
For RHEL/CentOS7 users, also still having additional package groups such as: Virtualization Client, Virtualization Platform and Virtualization Tools to install.
[root@server ~]#yum groupinstall virtualization-client virtualization-platform virtualization-tools
restart libvirtd
The virtualization daemon which manage all of the platform is “l(fā)ibvirtd”. lets restart it.
[root@server ~]#systemctl restart libvirtd
After restarting the daemon, then check its status by running following command.
[root@server ~]#systemctl status libvirtd
libvirtd.service - Virtualization daemon
Loaded: loaded (/usr/lib/systemd/system/libvirtd.service; enabled; vendor preset: enabled)
Active: active (running) since 四 2017-12-07 18:50:18 CST; 19h ago
Docs: man:libvirtd(8)
http://libvirt.org
Main PID: 1577 (libvirtd)
使用kvm制作鏡像
這個(gè)過程和其他通過iso文件安裝自己的鏡像非常類似,可以通過virt-manager和virt-install兩種方式安裝。virt-manager安裝基本上是通過GUI的方式安裝,這里簡(jiǎn)單說一下virt-install的安裝方式。以u(píng)buntu的iso為例:
創(chuàng)建鏡像
# qemu-img create -f qcow2 /tmp/ubuntu.qcow2 10G
# virt-install --virt-type kvm --name ubuntu --ram 1024 \
--cdrom=/data/isos/trusty-64-mini.iso \
--disk /tmp/trusty.qcow2,format=qcow2 \
--network network=default \
--graphics vnc,listen=0.0.0.0 --noautoconsole \
--os-type=linux --os-variant=ubuntutrusty
安裝鏡像
創(chuàng)建鏡像之后就會(huì)啟動(dòng)virt-manager GUI來安裝ubuntu鏡像,這個(gè)過程和其他安裝ubuntu虛機(jī)的過程一樣,此處不再一一說明,主要包含如下步驟,根據(jù)自己的需要進(jìn)行配置就可以。
- Configure Hostname
- Select a mirror
- Partition the disks
- Automatic updates
- Software selection: 選擇(OpenSSH server)
- Install GRUB boot loader
- Detach the CD-ROM and reboot
鏡像安裝完成之后重啟就可以看到登錄界面了。我在這碰到過一個(gè)問題,就是重啟之后會(huì)發(fā)現(xiàn)卡在如下位置:
/dev/sda1: clean, 291272/4292608 files, 480345/18174432 blocks
google了一下,原因應(yīng)該是GUI的問題,沒有安裝xdesktop, 和nvida的顯卡驅(qū)動(dòng)沖突了。這里因?yàn)槲也恍枰獂desktop, 解決辦法是啟動(dòng)的時(shí)候esc通過root進(jìn)入recovery mode,修改/etc/default/grub.
sudo vi /etc/default/grub
將GRUB_CMDLINE_LINUX_DEFAULT="quiet splash"
修改為GRUB_CMDLINE_LINUX_DEFAULT="nomodeset"
, 然后執(zhí)行:
sudo update-grub
配置鏡像
現(xiàn)在可以登錄到vm之后根據(jù)自己的需要在vm中安裝業(yè)務(wù)需要的軟件和功能了。
安裝完畢之后登出。
清理工作
因?yàn)殓R像在本地安裝之后會(huì)配置上mac地址之類的,影響后續(xù)的分發(fā),可以通過如下命令來清理:
# virt-sysprep -d trusty
如果virt-sysprep沒有安裝的話,可以通過如下命令安裝:
# yum whatprovides */virt-sysprep
# yum install -y libguestfs-tools-c-1.22.6-22.el7.x86_64
做完以上工作,/tmp/ubuntu.qcow2就是一個(gè)可用的鏡像了。
其他補(bǔ)充
鏡像格式的問題
qemu使用的鏡像主要有qcow2和raw兩種格式,這兩種格式的比較詳細(xì)的介紹文檔可以參考如下鏈接:
https://www.ibm.com/developerworks/cn/linux/1409_qiaoly_qemuimgages/
qcow2和raw的格式可以通過如下命令互相轉(zhuǎn)換:
# qemu-img convert -O raw /tmp/ubuntu.qcow2 /tmp/ubuntu.raw
# qemu-img convert -O qcow2 /tmp/ubuntu.raw /tmp/ubuntu.qcow2
鏡像size
制作好的鏡像可以通過qemu-img查看大小和一些基本內(nèi)容:
[root@forti-zhbo vm]# qemu-img info /tmp/ubuntu.qcow2
image: /tmp/ubuntu.qcow2
file format: qcow2
virtual size: 10G (10737418240 bytes)
disk size: 2.3G
cluster_size: 65536
Format specific information:
compat: 1.1
lazy refcounts: false
可以通過resize命令增加和減少鏡像virtual size的大?。?/p>
# qemu-img resize ubuntu.qcow2 +1G
Image resized.
注意:qcow2鏡像只能增加大小,不能減少,raw的格式支持增加和減少
從上面可以看出鏡像的實(shí)際占據(jù)的空間有2.3G,很大,不便于分發(fā)。為了便于分發(fā),可以將鏡像進(jìn)行壓縮,命令如下:
virt-sparsify --tmp ./tmp2 --compress --convert qcow2 /tmp/ubuntu.qcow2 ./myubuntu.qcow2
可以看到壓縮之后大小只有不到500M了:
# qemu-img info myubuntu.qcow2
image: myubuntu.qcow2
file format: qcow2
virtual size: 10G (10737418240 bytes)
disk size: 459M
cluster_size: 65536
Format specific information:
compat: 1.1
lazy refcounts: false
注意 如果在安裝的時(shí)候發(fā)現(xiàn)這樣的錯(cuò)誤:
virt-sparsify: error: libguestfs error: lvs: lvm lvs --help: Invalid
units specification
If reporting bugs, run virt-sparsify with debugging enabled and include the
complete output:
virt-sparsify -v -x [...]
可能是lvm2的版本太低的原因,在centos 7上可以使用yum install lvm2
重新安裝一下lvm2.
運(yùn)行鏡像
將上述生成的myubuntu.qcow2鏡像拷貝到宿主機(jī)上,通過virt-manager啟動(dòng)一個(gè)GUI,新建一個(gè)虛擬機(jī),然后導(dǎo)入qcow2鏡像就可以運(yùn)行了。
如果是命令行的方式,可以用virt-install導(dǎo)入一個(gè)已經(jīng)存在的qcow2 disk鏡像,導(dǎo)入之后就可以通過virsh start的方式啟動(dòng)該鏡像。
在實(shí)際測(cè)試過程中,可以使用qemu-img的backing_file技術(shù)。將myubuntu作為后端鏡像,創(chuàng)建新的鏡像文件,這樣在運(yùn)行新的鏡像的時(shí)候,后端鏡像文件不會(huì)被修改,除非使用qemu-img commit來提交修改到后端鏡像。這樣使用一個(gè)base鏡像就可以很快創(chuàng)建不同的service 鏡像,便于鏡像的管理和分發(fā)。
使用backing_file創(chuàng)建一個(gè)新的鏡像的命令如下:
# qemu-img create -f qcow2 -o ? myubuntu.qcow2
Supported options:
size Virtual disk size
compat Compatibility level (0.10 or 1.1)
backing_file File name of a base image
backing_fmt Image format of the base image
encryption Encrypt the image
cluster_size qcow2 cluster size
preallocation Preallocation mode (allowed values: off, metadata)
# qemu-img create -f qcow2 -b myubuntu.qcow2 new.img
Formatting ‘new.img, fmt=qcow2 size=8589934592 backing_file=’myubuntu.qcow2 encryption=off cluster_size=65536
對(duì)應(yīng)的commit命令如下:
# qemu-img commit -f qcow2 new.img