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

項(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
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 228,983評(píng)論 6 537
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 98,772評(píng)論 3 422
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事?!?“怎么了?”我有些...
    開封第一講書人閱讀 176,947評(píng)論 0 381
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)。 經(jīng)常有香客問我,道長(zhǎng),這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,201評(píng)論 1 315
  • 正文 為了忘掉前任,我火速辦了婚禮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 71,960評(píng)論 6 410
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 55,350評(píng)論 1 324
  • 那天,我揣著相機(jī)與錄音,去河邊找鬼。 笑死,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,406評(píng)論 3 444
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 42,549評(píng)論 0 289
  • 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 49,104評(píng)論 1 335
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 40,914評(píng)論 3 356
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 43,089評(píng)論 1 371
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,647評(píng)論 5 362
  • 正文 年R本政府宣布,位于F島的核電站,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 44,340評(píng)論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,753評(píng)論 0 28
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 36,007評(píng)論 1 289
  • 我被黑心中介騙來泰國(guó)打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 51,834評(píng)論 3 395
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 48,106評(píng)論 2 375

推薦閱讀更多精彩內(nèi)容