镜像就是安装好的操作系统的一个磁盘文件。在创建新虚拟机时,如果有模板镜像,可以省去安装操作系统的时间,且便于分发。
制作并测试REHL7镜像
测试,在vm01虚拟机的中制作FTP镜像文件,既用户使用FTP镜像文件,就可以直接使用FTP服务了,匿名用户可以从pub目录下载,也可以上传文件至upload目录。
在vm01虚拟机中安装FTP服务器软件vsftpd软件包。
首先,在vm01上进行配置,确保防火墙,SElinux关闭。
配置好软件仓库。
安装FTP服务,启动FTP服务(此处之前已经安装和启动好了)
设置FTP的上传目录upload,保证FTP的匿名用户能够上传文件到此目录。
设置匿名用户上传权限。设置匿名用户上传,创建目录和其他写的权限。
在宿主机中测试vsfptd服务器,是否能够上传和下载。
在宿主机中安装ftp软件包,才能够使用ftp命令。
[root@localhost ~]# ftp 192.168.100.239
bash: ftp: command not found...
[root@localhost ~]# yum install -y ftp
Loaded plugins: langpacks, product-id, search-disabled-repos, subscription-
: manager
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
RHEL7 | 4.1 kB 00:00
Resolving Dependencies
--> Running transaction check
---> Package ftp.x86_64 0:0.17-67.el7 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
================================================================================
Package Arch Version Repository Size
================================================================================
Installing:
ftp x86_64 0.17-67.el7 RHEL7 61 k
Transaction Summary
================================================================================
Install 1 Package
Total download size: 61 k
Installed size: 96 k
Downloading packages:
ftp-0.17-67.el7.x86_64.rpm | 61 kB 00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : ftp-0.17-67.el7.x86_64 1/1
Verifying : ftp-0.17-67.el7.x86_64 1/1
Installed:
ftp.x86_64 0:0.17-67.el7
Complete!
[root@localhost ~]# ftp 192.168.100.239
Connected to 192.168.100.239 (192.168.100.239).
220 (vsFTPd 3.0.2)
Name (192.168.100.239:root): ftp
331 Please specify the password.
Password:
230 Login successful.
Remote system type is UNIX.
Using binary mode to transfer files.
ftp> cd upload
250 Directory successfully changed.
ftp> put anaconda-ks.cfg
local: anaconda-ks.cfg remote: anaconda-ks.cfg
227 Entering Passive Mode (192,168,100,239,228,119).
150 Ok to send data.
226 Transfer complete.
2189 bytes sent in 0.00361 secs (607.04 Kbytes/sec)
ftp> bye
221 Goodbye.
修改虚拟机vm01的IP地址获取方法(DHCP),保证作为镜像后,启动系统后IP地址不会重复。
在宿主机上安装工具virt-sysprep所提供的软件包libguestsfs-tools。
virt-sysprep可以初始化操作系统,让其镜像会有新的MAC地址,SSH密钥等。对REHL7宿主机进行封装,为制作REHL7镜像做好准备。
[root@localhost ~]# yum install libguestfs-tools -y
Loaded plugins: langpacks, product-id, search-disabled-repos, subscription-
: manager
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
Resolving Dependencies
--> Running transaction check
---> Package libguestfs-tools.noarch 1:1.32.7-3.el7 will be installed
--> Processing Dependency: libguestfs = 1:1.32.7-3.el7 for package: 1:libguestfs-tools-1.32.7-3.el7.noarch
--> Processing Dependency: libguestfs-tools-c = 1:1.32.7-3.el7 for package: 1:libguestfs-tools-1.32.7-3.el7.noarch
--> Processing Dependency: perl(Win::Hivex) >= 1.2.7 for package: 1:libguestfs-tools-1.32.7-3.el7.noarch
--> Processing Dependency: perl(Locale::TextDomain) for package: 1:libguestfs-tools-1.32.7-3.el7.noarch
--> Processing Dependency: perl(Sys::Guestfs) for package: 1:libguestfs-tools-1.32.7-3.el7.noarch
--> Processing Dependency: perl(Sys::Virt) for package: 1:libguestfs-tools-1.32.7-3.el7.noarch
--> Processing Dependency: perl(Win::Hivex) for package: 1:libguestfs-tools-1.32.7-3.el7.noarch
--> Processing Dependency: perl(Win::Hivex::Regedit) for package: 1:libguestfs-tools-1.32.7-3.el7.noarch
--> Running transaction check
---> Package libguestfs.x86_64 1:1.32.7-3.el7 will be installed
--> Processing Dependency: supermin5 >= 5.1.16-2 for package: 1:libguestfs-1.32.7-3.el7.x86_64
--> Processing Dependency: hivex for package: 1:libguestfs-1.32.7-3.el7.x86_64
--> Processing Dependency: scrub for package: 1:libguestfs-1.32.7-3.el7.x86_64
--> Processing Dependency: syslinux for package: 1:libguestfs-1.32.7-3.el7.x86_64
--> Processing Dependency: syslinux-extlinux for package: 1:libguestfs-1.32.7-3.el7.x86_64
---> Package libguestfs-tools-c.x86_64 1:1.32.7-3.el7 will be installed
--> Processing Dependency: /usr/bin/hexedit for package: 1:libguestfs-tools-c-1.32.7-3.el7.x86_64
---> Package perl-Sys-Guestfs.x86_64 1:1.32.7-3.el7 will be installed
---> Package perl-Sys-Virt.x86_64 0:2.0.0-1.el7 will be installed
---> Package perl-hivex.x86_64 0:1.3.10-5.7.el7 will be installed
---> Package perl-libintl.x86_64 0:1.20-12.el7 will be installed
--> Running transaction check
---> Package hexedit.x86_64 0:1.2.13-5.el7 will be installed
---> Package hivex.x86_64 0:1.3.10-5.7.el7 will be installed
---> Package scrub.x86_64 0:2.5.2-5.el7 will be installed
---> Package supermin5.x86_64 0:5.1.16-4.el7 will be installed
---> Package syslinux.x86_64 0:4.05-13.el7 will be installed
---> Package syslinux-extlinux.x86_64 0:4.05-13.el7 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
================================================================================
Package Arch Version Repository Size
================================================================================
Installing:
libguestfs-tools noarch 1:1.32.7-3.el7 RHEL7 40 k
Installing for dependencies:
hexedit x86_64 1.2.13-5.el7 RHEL7 39 k
hivex x86_64 1.3.10-5.7.el7 RHEL7 100 k
libguestfs x86_64 1:1.32.7-3.el7 RHEL7 1.9 M
libguestfs-tools-c x86_64 1:1.32.7-3.el7 RHEL7 2.8 M
perl-Sys-Guestfs x86_64 1:1.32.7-3.el7 RHEL7 320 k
perl-Sys-Virt x86_64 2.0.0-1.el7 RHEL7 272 k
perl-hivex x86_64 1.3.10-5.7.el7 RHEL7 41 k
perl-libintl x86_64 1.20-12.el7 RHEL7 875 k
scrub x86_64 2.5.2-5.el7 RHEL7 40 k
supermin5 x86_64 5.1.16-4.el7 RHEL7 550 k
syslinux x86_64 4.05-13.el7 RHEL7 990 k
syslinux-extlinux x86_64 4.05-13.el7 RHEL7 363 k
Transaction Summary
================================================================================
Install 1 Package (+12 Dependent packages)
Total download size: 8.2 M
Installed size: 30 M
Downloading packages:
(1/13): hivex-1.3.10-5.7.el7.x86_64.rpm | 100 kB 00:00
(2/13): hexedit-1.2.13-5.el7.x86_64.rpm | 39 kB 00:00
(3/13): libguestfs-tools-1.32.7-3.el7.noarch.rpm | 40 kB 00:00
(4/13): libguestfs-1.32.7-3.el7.x86_64.rpm | 1.9 MB 00:00
(5/13): libguestfs-tools-c-1.32.7-3.el7.x86_64.rpm | 2.8 MB 00:00
(6/13): perl-Sys-Guestfs-1.32.7-3.el7.x86_64.rpm | 320 kB 00:00
(7/13): perl-Sys-Virt-2.0.0-1.el7.x86_64.rpm | 272 kB 00:00
(8/13): perl-hivex-1.3.10-5.7.el7.x86_64.rpm | 41 kB 00:00
(9/13): scrub-2.5.2-5.el7.x86_64.rpm | 40 kB 00:00
(10/13): supermin5-5.1.16-4.el7.x86_64.rpm | 550 kB 00:00
(11/13): perl-libintl-1.20-12.el7.x86_64.rpm | 875 kB 00:00
(12/13): syslinux-extlinux-4.05-13.el7.x86_64.rpm | 363 kB 00:00
(13/13): syslinux-4.05-13.el7.x86_64.rpm | 990 kB 00:00
--------------------------------------------------------------------------------
Total 27 MB/s | 8.2 MB 00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : hivex-1.3.10-5.7.el7.x86_64 1/13
Installing : perl-hivex-1.3.10-5.7.el7.x86_64 2/13
Installing : syslinux-4.05-13.el7.x86_64 3/13
Installing : syslinux-extlinux-4.05-13.el7.x86_64 4/13
Installing : supermin5-5.1.16-4.el7.x86_64 5/13
Installing : scrub-2.5.2-5.el7.x86_64 6/13
Installing : 1:libguestfs-1.32.7-3.el7.x86_64 7/13
Installing : 1:perl-Sys-Guestfs-1.32.7-3.el7.x86_64 8/13
Installing : perl-Sys-Virt-2.0.0-1.el7.x86_64 9/13
Installing : perl-libintl-1.20-12.el7.x86_64 10/13
Installing : hexedit-1.2.13-5.el7.x86_64 11/13
Installing : 1:libguestfs-tools-c-1.32.7-3.el7.x86_64 12/13
Installing : 1:libguestfs-tools-1.32.7-3.el7.noarch 13/13
Verifying : 1:libguestfs-1.32.7-3.el7.x86_64 1/13
Verifying : syslinux-extlinux-4.05-13.el7.x86_64 2/13
Verifying : perl-hivex-1.3.10-5.7.el7.x86_64 3/13
Verifying : hexedit-1.2.13-5.el7.x86_64 4/13
Verifying : syslinux-4.05-13.el7.x86_64 5/13
Verifying : perl-libintl-1.20-12.el7.x86_64 6/13
Verifying : perl-Sys-Virt-2.0.0-1.el7.x86_64 7/13
Verifying : scrub-2.5.2-5.el7.x86_64 8/13
Verifying : 1:libguestfs-tools-1.32.7-3.el7.noarch 9/13
Verifying : 1:perl-Sys-Guestfs-1.32.7-3.el7.x86_64 10/13
Verifying : supermin5-5.1.16-4.el7.x86_64 11/13
Verifying : 1:libguestfs-tools-c-1.32.7-3.el7.x86_64 12/13
Verifying : hivex-1.3.10-5.7.el7.x86_64 13/13
Installed:
libguestfs-tools.noarch 1:1.32.7-3.el7
Dependency Installed:
hexedit.x86_64 0:1.2.13-5.el7
hivex.x86_64 0:1.3.10-5.7.el7
libguestfs.x86_64 1:1.32.7-3.el7
libguestfs-tools-c.x86_64 1:1.32.7-3.el7
perl-Sys-Guestfs.x86_64 1:1.32.7-3.el7
perl-Sys-Virt.x86_64 0:2.0.0-1.el7
perl-hivex.x86_64 0:1.3.10-5.7.el7
perl-libintl.x86_64 0:1.20-12.el7
scrub.x86_64 0:2.5.2-5.el7
supermin5.x86_64 0:5.1.16-4.el7
syslinux.x86_64 0:4.05-13.el7
syslinux-extlinux.x86_64 0:4.05-13.el7
Complete!
**将宿主机中的虚拟机vm01进行关闭。**不能在线封装,需要关闭虚拟机。使用命令virt-sysprep -d vm01进行封装。
[root@localhost ~]# virsh shutdown vm01
Domain vm01 is being shutdown
[root@localhost ~]# virt-sysprep -d vm01
[ 0.0] Examining the guest ...
[ 48.9] Performing "abrt-data" ...
[ 48.9] Performing "bash-history" ...
[ 48.9] Performing "blkid-tab" ...
[ 48.9] Performing "crash-data" ...
[ 48.9] Performing "cron-spool" ...
[ 48.9] Performing "dhcp-client-state" ...
[ 48.9] Performing "dhcp-server-state" ...
[ 48.9] Performing "dovecot-data" ...
[ 48.9] Performing "logfiles" ...
[ 49.0] Performing "machine-id" ...
[ 49.0] Performing "mail-spool" ...
[ 49.0] Performing "net-hostname" ...
[ 49.0] Performing "net-hwaddr" ...
[ 49.0] Performing "pacct-log" ...
[ 49.0] Performing "package-manager-cache" ...
[ 49.0] Performing "pam-data" ...
[ 49.0] Performing "puppet-data-log" ...
[ 49.0] Performing "rh-subscription-manager" ...
[ 49.0] Performing "rhn-systemid" ...
[ 49.0] Performing "rpm-db" ...
[ 49.0] Performing "samba-db-log" ...
[ 49.0] Performing "script" ...
[ 49.0] Performing "smolt-uuid" ...
[ 49.0] Performing "ssh-hostkeys" ...
[ 49.0] Performing "ssh-userdir" ...
[ 49.0] Performing "sssd-db-log" ...
[ 49.0] Performing "tmp-files" ...
[ 49.0] Performing "udev-persistent-net" ...
[ 49.0] Performing "utmp" ...
[ 49.0] Performing "yum-uuid" ...
[ 49.0] Performing "customize" ...
[ 49.0] Setting a random seed
[ 49.1] Performing "lvm-uuids" ...
封装完成后,查看封装的虚拟机镜像文件。
[root@localhost ~]# cd /var/lib/libvirt/images/
[root@localhost images]# qemu-img info vm01.qcow2
image: vm01.qcow2
file format: qcow2
virtual size: 9.0G (9663676416 bytes)
disk size: 1.3G
cluster_size: 65536
Format specific information:
compat: 1.1
lazy refcounts: true
使用virt-sparsify命令对镜像文件进行压缩。
[root@localhost images]# virt-sparsify --tmp /tmp --compress --convert qcow2 vm01.qcow2 /root/rhel7-ftp.qcow2
[ 0.1] Create overlay file in /tmp to protect source disk
[ 0.2] Examine source disk
[ 2.4] Fill free space in /dev/rhel/root with zero
100% ⟦▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒⟧ 00:00
[ 37.4] Clearing Linux swap on /dev/rhel/swap
[ 39.4] Fill free space in /dev/sda1 with zero
[ 41.3] Copy to destination and make sparse
[ 120.6] Sparsify operation completed with no errors.
virt-sparsify: Before deleting the old disk, carefully check that the
target disk boots and works correctly.
压缩后,查看磁盘镜像文件。并把镜像复制到/var/lib/libvirt/images/目录下。
[root@localhost images]# qemu-img info /root/rhel7-ftp.qcow2
image: /root/rhel7-ftp.qcow2
file format: qcow2
virtual size: 9.0G (9663676416 bytes)
disk size: 457M
cluster_size: 65536
Format specific information:
compat: 1.1
lazy refcounts: false
[root@localhost images]# cp /root/rhel7-ftp.qcow2 /var/lib/libvirt/images/
测试REHL7-FTP镜像
在虚拟机管理器下,新建虚拟机。
点击finish后直接启动新的虚拟机。
到宿主机上测试ftp服务是否可以使用。
[root@localhost images]# ftp 192.168.122.236
Connected to 192.168.122.236 (192.168.122.236).
220 (vsFTPd 3.0.2)
Name (192.168.122.236:root): ftp
331 Please specify the password.
Password:
230 Login successful.
Remote system type is UNIX.
Using binary mode to transfer files.
ftp> ls
227 Entering Passive Mode (192,168,122,236,250,215).
150 Here comes the directory listing.
drwxr-xr-x 2 0 0 6 Jun 23 2016 pub
drwxr-xr-x 2 14 0 29 Apr 02 07:27 upload
226 Directory send OK.
ftp> bye
221 Goodbye.
ftp服务可以使用,由镜像文件新建的虚拟机工作正常。