在centos 7系统中制作kvm镜像

项目的需求,需要制作kvm虚拟机镜像。在网上查找了一些资料并经过实际的配置操作。下面简单说明一下步骤,以供参考。

准备iso镜像

目前制作镜像考虑不考虑从无到有,只是在已有镜像的基础上加上一些业务功能和配置,做成自己的镜像文件,所以,必须基于已有的iso镜像。

以ubuntu为例。可以从官网上获取iso镜像,可以选择mini镜像(大小只有不到50M)或者正常的镜像文件。需要主要的是mini镜像会在安装的过程中下载大量文件,整体来说在这里并没有节省大小。所以如果网络状况不好的话还是下载完整的镜像更合适。

系统准备工作

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 “libvirtd”. 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为例:

创建镜像

# 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

安装镜像

创建镜像之后就会启动virt-manager GUI来安装ubuntu镜像,这个过程和其他安装ubuntu虚机的过程一样,此处不再一一说明,主要包含如下步骤,根据自己的需要进行配置就可以。

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

镜像安装完成之后重启就可以看到登录界面了。我在这碰到过一个问题,就是重启之后会发现卡在如下位置:

/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", 然后执行:

sudo update-grub

配置镜像

现在可以登录到vm之后根据自己的需要在vm中安装业务需要的软件和功能了。
安装完毕之后登出。

清理工作

因为镜像在本地安装之后会配置上mac地址之类的,影响后续的分发,可以通过如下命令来清理:

# 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的大小:

# qemu-img resize ubuntu.qcow2 +1G  
Image resized.  

注意:qcow2镜像只能增加大小,不能减少,raw的格式支持增加和减少

从上面可以看出镜像的实际占据的空间有2.3G,很大,不便于分发。为了便于分发,可以将镜像进行压缩,命令如下:

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

注意 如果在安装的时候发现这样的错误:

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作为后端镜像,创建新的镜像文件,这样在运行新的镜像的时候,后端镜像文件不会被修改,除非使用qemu-img commit来提交修改到后端镜像。这样使用一个base镜像就可以很快创建不同的service 镜像,便于镜像的管理和分发。

使用backing_file创建一个新的镜像的命令如下:

# 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
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,117评论 4 360
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 66,963评论 1 290
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 107,897评论 0 240
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,805评论 0 203
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,208评论 3 286
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,535评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,797评论 2 311
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,493评论 0 197
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,215评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,477评论 2 244
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 31,988评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,325评论 2 252
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,971评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,055评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,807评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,544评论 2 271
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,455评论 2 266

推荐阅读更多精彩内容