Learning KVM - part5 克隆并初始化VM

  • 如果您打算创建多台相同配置的虚拟机(VM),使用克隆功能是一个快捷便利的方式。
  • virt-clone命令可以复制一个已经存在的虚拟机,这个名利只能在vm停机状态使用,它将克隆已存在VM的所有信息,包括UUID和MAC地址。
  • 我们可以使用virt-sysprep工具来配置新克隆的VM。

克隆VM

下面我们来演示一下如何来克隆一台已经存在的虚拟机实例:

# 1.列出已创建的虚拟机kvm1;
[root@kvm-node1 images]# virsh list 
 Id    Name                           State
----------------------------------------------------
 25    kvm1                           running

[root@kvm-node1 images]# virsh console kvm1
Connected to domain kvm1
Escape character is ^]

login as 'cirros' user. default password: 'cubswin:)'. use 'sudo' for root.
cirros login: cirros 
Password: 
$ 
$ 
$ ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue 
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000
    link/ether 52:54:00:37:15:d9 brd ff:ff:ff:ff:ff:ff
    inet 192.168.57.35/24 brd 192.168.57.255 scope global eth0
    inet6 fe80::5054:ff:fe37:15d9/64 scope link 
       valid_lft forever preferred_lft forever
# 2. 暂停kvm1, virt-clone必须在待克隆的VM停机时才能执行。
[root@kvm-node1 images]# virsh suspend kvm1
Domain kvm1 suspended

[root@kvm-node1 images]# virsh list
 Id    Name                           State
----------------------------------------------------
 25    kvm1                           paused
# 3.执行virt-clone命令来克隆kvm1
[root@kvm-node1 images]# virt-clone --connect qemu:///system --original kvm1 --name kvm-clone1 \
--file /var/lib/libvirt/images/kvm-clone1.qcow2
WARNING  Setting the graphics device port to autoport, in order to avoid conflicting.
Allocating 'kvm-clone1.qcow2'                                       |  39 MB  00:00:00     

Clone 'kvm-clone1' created successfully.
# 4.克隆完成后,恢复被暂停的vm(kvm1)
[root@kvm-node1 images]# virsh list
 Id    Name                           State
----------------------------------------------------
 25    kvm1                           paused

[root@kvm-node1 images]# virsh resume kvm1
Domain kvm1 resumed

[root@kvm-node1 images]# virsh list --all
 Id    Name                           State
----------------------------------------------------
 25    kvm1                           running
 -     kvm-clone1                     shut off
 -     test                           shut off

初始化VM

  • 当虚拟机实例克隆完成以后我们需要使用工具virt-sysperp来初始化虚拟机实例。virt-sysperp会将虚假机初始化到系统刚安装的状态,它会删除掉虚拟机中的ssh key文件、重置网络MAC地址、主机名以及系统用户。

[root@kvm-node1 images]# virt-sysprep -d kvm-clone1
[ 0.0] Examining the guest ...
[ 44.9] Performing "abrt-data" ...
[ 44.9] Performing "bash-history" ...
[ 44.9] Performing "blkid-tab" ...
[ 44.9] Performing "crash-data" ...
[ 44.9] Performing "cron-spool" ...
[ 44.9] Performing "dhcp-client-state" ...
[ 44.9] Performing "dhcp-server-state" ...
[ 44.9] Performing "dovecot-data" ...
[ 44.9] Performing "logfiles" ...
[ 44.9] Performing "machine-id" ...
[ 44.9] Performing "mail-spool" ...
[ 44.9] Performing "net-hostname" ...
[ 44.9] Performing "net-hwaddr" ...
[ 44.9] Performing "pacct-log" ...
[ 44.9] Performing "package-manager-cache" ...
[ 44.9] Performing "pam-data" ...
[ 44.9] Performing "puppet-data-log" ...
[ 44.9] Performing "rh-subscription-manager" ...
[ 44.9] Performing "rhn-systemid" ...
[ 44.9] Performing "rpm-db" ...
[ 44.9] Performing "samba-db-log" ...
[ 44.9] Performing "script" ...
[ 44.9] Performing "smolt-uuid" ...
[ 44.9] Performing "ssh-hostkeys" ...
[ 44.9] Performing "ssh-userdir" ...
[ 44.9] Performing "sssd-db-log" ...
[ 44.9] Performing "tmp-files" ...
[ 45.0] Performing "udev-persistent-net" ...
[ 45.0] Performing "utmp" ...
[ 45.0] Performing "yum-uuid" ...
[ 45.0] Performing "customize" ...
[ 45.0] Setting a random seed
[ 45.0] Performing "lvm-uuids" ...

- 下面我们来看下上面步骤的含义:
```sh
[root@kvm-node1 images]# virt-sysprep --list-operations
abrt-data * Remove the crash data generated by ABRT
bash-history * Remove the bash history in the guest
blkid-tab * Remove blkid tab in the guest
ca-certificates   Remove CA certificates in the guest
crash-data * Remove the crash data generated by kexec-tools
cron-spool * Remove user at-jobs and cron-jobs
customize * Customize the guest
dhcp-client-state * Remove DHCP client leases
dhcp-server-state * Remove DHCP server leases
dovecot-data * Remove Dovecot (mail server) data
firewall-rules   Remove the firewall rules
flag-reconfiguration   Flag the system for reconfiguration
fs-uuids   Change filesystem UUIDs
kerberos-data   Remove Kerberos data in the guest
logfiles * Remove many log files from the guest
lvm-uuids * Change LVM2 PV and VG UUIDs
machine-id * Remove the local machine ID
mail-spool * Remove email from the local mail spool directory
net-hostname * Remove HOSTNAME in network interface configuration
net-hwaddr * Remove HWADDR (hard-coded MAC address) configuration
pacct-log * Remove the process accounting log files
package-manager-cache * Remove package manager cache
pam-data * Remove the PAM data in the guest
puppet-data-log * Remove the data and log files of puppet
rh-subscription-manager * Remove the RH subscription manager files
rhn-systemid * Remove the RHN system ID
rpm-db * Remove host-specific RPM database files
samba-db-log * Remove the database and log files of Samba
script * Run arbitrary scripts against the guest
smolt-uuid * Remove the Smolt hardware UUID
ssh-hostkeys * Remove the SSH host keys in the guest
ssh-userdir * Remove ".ssh" directories in the guest
sssd-db-log * Remove the database and log files of sssd
tmp-files * Remove temporary files
udev-persistent-net * Remove udev persistent net rules
user-account   Remove the user accounts in the guest
utmp * Remove the utmp file
yum-uuid * Remove the yum UUID
  • virt-sysprep支持的配置项,通过这些选项我们可以很方便的定制操作系统:

-a file Add disk image file
--add file Add disk image file
-c uri Set libvirt URI
--chmod PERMISSIONS:FILE Change the permissions of a file
--connect uri Set libvirt URI
-d domain Set libvirt guest name
--debug-gc Debug GC and memory allocations (internal)
--delete PATH Delete a file or directory
--domain domain Set libvirt guest name
--dry-run Perform a dry run
--dryrun Perform a dry run
--dump-pod Dump POD (internal)
--dump-pod-options Dump POD for options (internal)
--edit FILE:EXPR Edit file using Perl expression
--enable operations Enable specific operations
--firstboot SCRIPT Run script at first guest boot
--firstboot-command 'CMD+ARGS' Run command at first guest boot
--firstboot-install PKG,PKG.. Add package(s) to install at first boot
--format format Set format (default: auto)
--hostname HOSTNAME Set the hostname
--install PKG,PKG.. Add package(s) to install
--keep-user-accounts users Users to keep
--link TARGET:LINK[:LINK..] Create symbolic links
--list-operations List supported operations
--long-options List long options
--mkdir DIR Create a directory
--mount-options opts Set mount options (eg /:noatime;/var:rw,noatime)
-n Perform a dry run
--no-logfile Scrub build log file
--no-selinux-relabel Compatibility option, does nothing
--operation Enable/disable specific operations
--operations Enable/disable specific operations
--password USER:SELECTOR Set user password
--password-crypto md5|sha256|sha512 Set password crypto
-q Don't print log messages
--quiet Don't print log messages
--remove-user-accounts users Users to remove
--root-password SELECTOR Set root password
--run SCRIPT Run script in disk image
--run-command 'CMD+ARGS' Run command in disk image
--script script Script or program to run on guest
--scriptdir dir Mount point on host
--scrub FILE Scrub a file
--selinux-relabel Relabel files with correct SELinux labels
--timezone TIMEZONE Set the default timezone
--update Update core packages
--upload FILE:DEST Upload local file to destination
-v Enable debugging messages
-V Display version and exit
--verbose Enable debugging messages
--version Display version and exit
--write FILE:CONTENT Write file
-x Enable tracing of libguestfs calls
-help Display this list of options
--help Display this list of options

- 下面我们再来尝试一下使用`virt-sysprep`命令来配置root用户密码和主机名:

[root@kvm-node1 images]# virt-sysprep -d test-clone1 --hostname test-clone1 --root-password password:123456
[ 0.0] Examining the guest ...
[ 17.2] Performing "abrt-data" ...
[ 17.2] Performing "bash-history" ...
[ 17.2] Performing "blkid-tab" ...
[ 17.2] Performing "crash-data" ...
[ 17.2] Performing "cron-spool" ...
[ 17.3] Performing "dhcp-client-state" ...
[ 17.3] Performing "dhcp-server-state" ...
[ 17.3] Performing "dovecot-data" ...
[ 17.3] Performing "logfiles" ...
[ 17.5] Performing "machine-id" ...
[ 17.5] Performing "mail-spool" ...
[ 17.5] Performing "net-hostname" ...
[ 17.5] Performing "net-hwaddr" ...
[ 17.5] Performing "pacct-log" ...
[ 17.5] Performing "package-manager-cache" ...
[ 17.5] Performing "pam-data" ...
[ 17.5] Performing "puppet-data-log" ...
[ 17.5] Performing "rh-subscription-manager" ...
[ 17.5] Performing "rhn-systemid" ...
[ 17.5] Performing "rpm-db" ...
[ 17.5] Performing "samba-db-log" ...
[ 17.5] Performing "script" ...
[ 17.5] Performing "smolt-uuid" ...
[ 17.6] Performing "ssh-hostkeys" ...
[ 17.6] Performing "ssh-userdir" ...
[ 17.6] Performing "sssd-db-log" ...
[ 17.6] Performing "tmp-files" ...
[ 17.6] Performing "udev-persistent-net" ...
[ 17.6] Performing "utmp" ...
[ 17.7] Performing "yum-uuid" ...
[ 17.7] Performing "customize" ...
[ 17.7] Setting a random seed
[ 17.7] Setting the hostname: test-clone1
[ 17.7] Setting passwords
[ 19.8] Performing "lvm-uuids" ...

我们来检查一下配置是否生效:(Looks good ^_^)

[root@kvm-node1 images]# virsh list --all
Id Name State


25 kvm1 running

  • kvm-clone1                     shut off
    
  • test                           shut off
    
  • test-clone1                    shut off
    

[root@kvm-node1 images]# virsh start test-clone1
Domain test-clone1 started

[root@kvm-node1 images]# virsh list --all
Id Name State


25 kvm1 running
30 test-clone1 running

  • kvm-clone1                     shut off
    
  • test                           shut off
    

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

推荐阅读更多精彩内容

  • PLEASE READ THE FOLLOWING APPLE DEVELOPER PROGRAM LICENSE...
    念念不忘的阅读 13,296评论 5 6
  • NAME dnsmasq - A lightweight DHCP and caching DNS server....
    ximitc阅读 2,647评论 0 0
  • 学理论先学一个标准,明白了,再学另外一个标准,明白了,再考虑两者之间相互融合、改进。
    灼灼2015阅读 1,118评论 0 0
  • 2013年冬天,摩羯月,买了「大小說家」送给了你,像每个普通少女一样,要送给喜欢的男生的礼物,包装成很隆重的样子,...
    moltigem阅读 289评论 0 0
  • 胖的人往往有趣而且善良,这是一种趋利避害羞的本能,只有这样才能被温柔的对待。因为人们偏爱美的东西。
    春日的风阅读 159评论 0 0