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:  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:  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)

你可能感兴趣的:(Learning KVM - part5 克隆并初始化VM)