在centos 7系統(tǒng)中制作kvm鏡像

項目的需求,需要制作kvm虛擬機鏡像。在網(wǎng)上查找了一些資料并經過實際的配置操作。下面簡單說明一下步驟,以供參考。

準備iso鏡像

目前制作鏡像考慮不考慮從無到有,只是在已有鏡像的基礎上加上一些業(yè)務功能和配置,做成自己的鏡像文件,所以,必須基于已有的iso鏡像。

以ubuntu為例。可以從官網(wǎng)上獲取iso鏡像,可以選擇mini鏡像(大小只有不到50M)或者正常的鏡像文件。需要主要的是mini鏡像會在安裝的過程中下載大量文件,整體來說在這里并沒有節(jié)省大小。所以如果網(wǎng)絡狀況不好的話還是下載完整的鏡像更合適。

系統(tǒng)準備工作

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安裝和部署

主要是用來制作和啟動kvm鏡像的tools,這里用virt-manager和virt-install, 制作鏡像的機器是centos 7, 安裝kvm tools的方法如下:

install qemu-kvm and qemu-img

[root@server ~]# yum install qemu-kvm qemu-img

qemu-kvm和qemu-img主要提供了用戶級別的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制作鏡像

這個過程和其他通過iso文件安裝自己的鏡像非常類似,可以通過virt-manager和virt-install兩種方式安裝。virt-manager安裝基本上是通過GUI的方式安裝,這里簡單說一下virt-install的安裝方式。以ubuntu的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)建鏡像之后就會啟動virt-manager GUI來安裝ubuntu鏡像,這個過程和其他安裝ubuntu虛機的過程一樣,此處不再一一說明,主要包含如下步驟,根據(jù)自己的需要進行配置就可以。

  • Configure Hostname
  • Select a mirror
  • Partition the disks
  • Automatic updates
  • Software selection: 選擇(OpenSSH server)
  • Install GRUB boot loader
  • Detach the CD-ROM and reboot

鏡像安裝完成之后重啟就可以看到登錄界面了。我在這碰到過一個問題,就是重啟之后會發(fā)現(xiàn)卡在如下位置:

/dev/sda1: clean, 291272/4292608 files, 480345/18174432 blocks

google了一下,原因應該是GUI的問題,沒有安裝xdesktop, 和nvida的顯卡驅動沖突了。這里因為我不需要xdesktop, 解決辦法是啟動的時候esc通過root進入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è)務需要的軟件和功能了。
安裝完畢之后登出。

清理工作

因為鏡像在本地安裝之后會配置上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就是一個可用的鏡像了。

其他補充

鏡像格式的問題

qemu使用的鏡像主要有qcow2和raw兩種格式,這兩種格式的比較詳細的介紹文檔可以參考如下鏈接:
https://www.ibm.com/developerworks/cn/linux/1409_qiaoly_qemuimgages/

qcow2和raw的格式可以通過如下命令互相轉換:

# 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查看大小和一些基本內容:

[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的格式支持增加和減少

從上面可以看出鏡像的實際占據(jù)的空間有2.3G,很大,不便于分發(fā)。為了便于分發(fā),可以將鏡像進行壓縮,命令如下:

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

注意 如果在安裝的時候發(fā)現(xiàn)這樣的錯誤:

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.

運行鏡像

將上述生成的myubuntu.qcow2鏡像拷貝到宿主機上,通過virt-manager啟動一個GUI,新建一個虛擬機,然后導入qcow2鏡像就可以運行了。

如果是命令行的方式,可以用virt-install導入一個已經存在的qcow2 disk鏡像,導入之后就可以通過virsh start的方式啟動該鏡像。

在實際測試過程中,可以使用qemu-img的backing_file技術。將myubuntu作為后端鏡像,創(chuàng)建新的鏡像文件,這樣在運行新的鏡像的時候,后端鏡像文件不會被修改,除非使用qemu-img commit來提交修改到后端鏡像。這樣使用一個base鏡像就可以很快創(chuàng)建不同的service 鏡像,便于鏡像的管理和分發(fā)。

使用backing_file創(chuàng)建一個新的鏡像的命令如下:

# 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

對應的commit命令如下:

# qemu-img commit -f qcow2 new.img
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容