虚拟化技术之kvm镜像模板制作工具virt-sysprep

  • A+
所属分类:linux技术
摘要

  virt-sysprep这个工具来自libguest-tools这个工具包,它能够把kvm虚拟机对应的磁盘文件做成一个模板,后续我们启动虚拟机就可以基于这个镜像模板启动;什么是镜像模板呢?所谓模板就是去除了一个虚拟机自身的私有信息的一个镜像磁盘文件;我们知道把一个镜像文件启动为虚拟机以后,它会生成许多私有信息,比如网卡的mac地址,ip地址,磁盘的uuid,ssh密钥等等;对于模板我们是不需要把这些私有信息的,而virt-sysprep这个工具就能很好的帮助我们去制作一个模板文件,它能够让我们在模板中打入自己想要新装的软件包,想要加入到模板里的文件等等操作;简单点virt-sysprep这个工具能够让我们自定义在模板镜像中定义有哪些东西,后续我们基于这个模板直接启动为虚拟机;也就是在我们启动虚拟机之前把我们需要的东西直接通过virt-sysprep这个工具打入到磁盘镜像文件中去;

  virt-sysprep这个工具来自libguest-tools这个工具包,它能够把kvm虚拟机对应的磁盘文件做成一个模板,后续我们启动虚拟机就可以基于这个镜像模板启动;什么是镜像模板呢?所谓模板就是去除了一个虚拟机自身的私有信息的一个镜像磁盘文件;我们知道把一个镜像文件启动为虚拟机以后,它会生成许多私有信息,比如网卡的mac地址,ip地址,磁盘的uuid,ssh密钥等等;对于模板我们是不需要把这些私有信息的,而virt-sysprep这个工具就能很好的帮助我们去制作一个模板文件,它能够让我们在模板中打入自己想要新装的软件包,想要加入到模板里的文件等等操作;简单点virt-sysprep这个工具能够让我们自定义在模板镜像中定义有哪些东西,后续我们基于这个模板直接启动为虚拟机;也就是在我们启动虚拟机之前把我们需要的东西直接通过virt-sysprep这个工具打入到磁盘镜像文件中去;

  要想使用virt-sysprep这个工具,我们首先要安装libguest-tools这个工具包;

  1、安装libguest-tools

[root@node1 ~]# yum install libguestfs-tools 已加载插件:fastestmirror, langpacks base                                                                                            | 3.6 kB  00:00:00      docker-ce-stable                                                                                | 3.5 kB  00:00:00      epel                                                                                            | 4.7 kB  00:00:00      extras                                                                                          | 2.9 kB  00:00:00      updates                                                                                         | 2.9 kB  00:00:00      (1/3): epel/x86_64/updateinfo                                                                   | 1.0 MB  00:00:00      (2/3): epel/x86_64/group_gz                                                                     |  95 kB  00:00:00      (3/3): epel/x86_64/primary_db                                                                   | 6.9 MB  00:00:00      Loading mirror speeds from cached hostfile  * base: mirrors.aliyun.com  * extras: mirrors.aliyun.com  * updates: mirrors.aliyun.com 正在解决依赖关系 --> 正在检查事务 ---> 软件包 libguestfs-tools.noarch.1.1.40.2-9.el7_8.1 将被 安装 --> 正在处理依赖关系 libguestfs-tools-c = 1:1.40.2-9.el7_8.1,它被软件包 1:libguestfs-tools-1.40.2-9.el7_8.1.noarch 需要 --> 正在处理依赖关系 libguestfs = 1:1.40.2-9.el7_8.1,它被软件包 1:libguestfs-tools-1.40.2-9.el7_8.1.noarch 需要 --> 正在处理依赖关系 perl(Win::Hivex) >= 1.2.7,它被软件包 1:libguestfs-tools-1.40.2-9.el7_8.1.noarch 需要 --> 正在处理依赖关系 perl(Win::Hivex::Regedit),它被软件包 1:libguestfs-tools-1.40.2-9.el7_8.1.noarch 需要 --> 正在处理依赖关系 perl(Win::Hivex),它被软件包 1:libguestfs-tools-1.40.2-9.el7_8.1.noarch 需要 --> 正在处理依赖关系 perl(Sys::Virt),它被软件包 1:libguestfs-tools-1.40.2-9.el7_8.1.noarch 需要 --> 正在处理依赖关系 perl(Sys::Guestfs),它被软件包 1:libguestfs-tools-1.40.2-9.el7_8.1.noarch 需要 --> 正在处理依赖关系 perl(Locale::TextDomain),它被软件包 1:libguestfs-tools-1.40.2-9.el7_8.1.noarch 需要 --> 正在检查事务 ……省略部分内容…… 已安装:   libguestfs-tools.noarch 1:1.40.2-9.el7_8.1                                                                             作为依赖被安装:   hexedit.x86_64 0:1.2.13-5.el7                           hivex.x86_64 0:1.3.10-6.9.el7                                   libguestfs.x86_64 1:1.40.2-9.el7_8.1                    libguestfs-tools-c.x86_64 1:1.40.2-9.el7_8.1                    perl-Sys-Guestfs.x86_64 1:1.40.2-9.el7_8.1              perl-Sys-Virt.x86_64 0:4.5.0-2.el7                              perl-hivex.x86_64 0:1.3.10-6.9.el7                      perl-libintl.x86_64 0:1.20-12.el7                               scrub.x86_64 0:2.5.2-7.el7                              squashfs-tools.x86_64 0:4.3-0.21.gitaae0aff4.el7                supermin5.x86_64 0:5.1.19-1.el7                         syslinux.x86_64 0:4.05-15.el7                                   syslinux-extlinux.x86_64 0:4.05-15.el7                   完毕! [root@node1 ~]#  

  2、查看virt-sysprep的帮助信息

[root@node1 ~]# virt-sysprep --help virt-sysprep: reset or unconfigure a virtual machine so clones can be made   virt-sysprep [--options] -d domname   virt-sysprep [--options] -a disk.img [-a disk.img ...]  A short summary of the options is given below.  For detailed help please read the man page virt-sysprep(1).  Options:   -a, --add <file>                    Add disk image file   --append-line <FILE:LINE>           Append line(s) to the file   -c, --connect <uri>                 Set libvirt URI   --chmod <PERMISSIONS:FILE>          Change the permissions of a file   --color, --colors, --colour, --colours                                       Use ANSI colour sequences even if not tty   --commands-from-file <FILENAME>     Read customize commands from file   --copy <SOURCE:DEST>                Copy files in disk image   --copy-in <LOCALPATH:REMOTEDIR>     Copy local files or directories into image   -d, --domain <domain>               Set libvirt guest name   --delete <PATH>                     Delete a file or directory   -n, --dryrun, --dry-run             Perform a dry run   --echo-keys                         Don’t turn off echo for passphrases   --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)   --help                              Display brief help   --hostname <HOSTNAME>               Set the hostname   --install <PKG,PKG..>               Add package(s) to install   --keep-user-accounts <users>        Users to keep   --key <SELECTOR>                    Specify a LUKS key   --keys-from-stdin                   Read passphrases from stdin   --link <TARGET:LINK[:LINK..]>       Create symbolic links   --list-operations                   List supported operations   --mkdir <DIR>                       Create a directory   --mount-options <opts>              Set mount options (eg /:noatime;/var:rw,noatime)   --move <SOURCE:DEST>                Move files in disk image   --network                           Enable appliance network   --no-logfile                        Scrub build log file   --no-network                        Disable appliance network (default)   --no-selinux-relabel                Compatibility option, does nothing   --operation, --operations <operations>                                       Enable/disable specific operations   --password <USER:SELECTOR>          Set user password   --password-crypto <md5|sha256|sha512>                                       Set password crypto   -q, --quiet                         Don’t print progress 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   --sm-attach <SELECTOR>              Attach to a subscription-manager pool   --sm-credentials <SELECTOR>         Credentials for subscription-manager   --sm-register                       Register using subscription-manager   --sm-remove                         Remove all the subscriptions   --sm-unregister                     Unregister using subscription-manager   --ssh-inject <USER[:SELECTOR]>      Inject a public key into the guest   --timezone <TIMEZONE>               Set the default timezone   --touch <FILE>                      Run touch on a file   --truncate <FILE>                   Truncate a file to zero size   --truncate-recursive <PATH>         Recursively truncate all files in directory   --uninstall <PKG,PKG..>             Uninstall package(s)   --update                            Update packages   --upload <FILE:DEST>                Upload local file to destination   -V, --version                       Display version and exit   -v, --verbose                       Enable libguestfs debugging messages   --write <FILE:CONTENT>              Write file   -x                                  Enable tracing of libguestfs calls [root@node1 ~]#  

  提示:从上面的帮助信息可以了解到,virt-sysprep这个工具的命令行语法格式是virt-sysprep+一些选项,然后后面用-d来指定需要操作的虚拟机名称;而对于virt-sysprep这个工具默认不指定要移除哪些私有文件,它默认要移除的私有信息文件可以使用--list-operations;

  查看virt-sysprep支持移除的私有信息列表

[root@node1 ~]# virt-sysprep --list-operations abrt-data * Remove the crash data generated by ABRT backup-files * Remove editor backup files from the guest 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 and DHCP_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 passwd-backups * Remove /etc/passwd- and similar backup files 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 [root@node1 ~]#  

  提示:以上列出来的东西都表示在不指定要移除那些私有信息的前提下,它默认要移除的私有信息列表;如果我们要手动指定移除哪些操作,可以使用--enable选项来指定,或者用--operation 来指定上述列出的信息即可;

  示例:只移除ssh-userdir和bash-history

  移除前

[root@node1 ~]# virsh list --all  Id    名称                         状态 ----------------------------------------------------  2     centos7                        running  [root@node1 ~]# virsh console centos7  连接到域 centos7 换码符为 ^]   CentOS Linux 7 (Core) Kernel 3.10.0-693.el7.x86_64 on an x86_64  localhost login: root Password:  Last login: Sat Aug 22 08:56:19 on ttyS0 [root@localhost ~]# ls -a .   anaconda-ks.cfg  .bash_logout   .bashrc  .ssh ..  .bash_history    .bash_profile  .cshrc   .tcshrc [root@localhost ~]#  [root@node1 ~]# 

  提示:移除前,虚拟机里存在的.bash_history和.ssh目录都存在;现在我们把虚拟机关机,然后使用sysprep 移除指定的ssh-userdir和bash-history这两项;

[root@node1 ~]# virsh shutdown centos7  域 centos7 被关闭  [root@node1 ~]# virsh list --all  Id    名称                         状态 ----------------------------------------------------  -     centos7                        关闭  [root@node1 ~]# virt-sysprep --enable bash-history,ssh-userdir -d centos7 [   0.0] Examining the guest ... [   3.0] Performing "bash-history" ... [   3.0] Performing "ssh-userdir" ... [root@node1 ~]#  

  提示:剥离指定虚拟机里的指定私有信息,必须要将虚拟机关机才能操作;

  验证:启动虚拟机,查看对应的.bash_history和.ssh目录是否移除?

[root@node1 ~]# virsh start centos7  域 centos7 已开始  [root@node1 ~]# virsh list --all  Id    名称                         状态 ----------------------------------------------------  6     centos7                        running  [root@node1 ~]# virsh console centos7  连接到域 centos7 换码符为 ^]   CentOS Linux 7 (Core) Kernel 3.10.0-693.el7.x86_64 on an x86_64  localhost login: root Password:  Last login: Sat Aug 22 09:06:31 on ttyS0 [root@localhost ~]# ll -a total 24 dr-xr-x---.  2 root root  114 Aug 22 09:07 . dr-xr-xr-x. 17 root root  244 Aug 19 08:34 .. -rw-------.  1 root root 1259 Aug 19 08:30 anaconda-ks.cfg -rw-r--r--.  1 root root   18 Dec 28  2013 .bash_logout -rw-r--r--.  1 root root  176 Dec 28  2013 .bash_profile -rw-r--r--.  1 root root  176 Dec 28  2013 .bashrc -rw-r--r--.  1 root root  100 Dec 28  2013 .cshrc -rw-r--r--.  1 root root  129 Dec 28  2013 .tcshrc [root@localhost ~]#  

  提示:可以看到新启动的虚拟机,对应文件.bash-history文件和.ssh目录被移除了;

  --copy <SOURCE:DEST>:把指定虚拟机上的文件拷入到指定虚拟机目录下

[root@node1 ~]# virsh console centos7  连接到域 centos7 换码符为 ^]   CentOS Linux 7 (Core) Kernel 3.10.0-693.el7.x86_64 on an x86_64  localhost login: root Password:  Last login: Sat Aug 22 09:26:45 on ttyS0 [root@localhost ~]# ls [root@localhost ~]# echo "this is test file on /root/" >test.txt [root@localhost ~]# cat test.txt  this is test file on /root/ [root@localhost ~]#  [root@node1 ~]#  

  提示:我们在在虚拟机/root/目录下创建一个test.txt文件,文件内容为“this is test file on /root”;

  拷贝虚拟机里/root/test.txt到/tmp/目录下

[root@node1 ~]# virsh shutdown centos7  域 centos7 被关闭  [root@node1 ~]# virsh list --all  Id    名称                         状态 ----------------------------------------------------  -     centos7                        关闭  [root@node1 ~]# virt-sysprep --copy /root/test.txt:/tmp/ -d centos7 [   0.0] Examining the guest ... [   2.7] Performing "abrt-data" ... [   2.7] Performing "backup-files" ... [   3.4] Performing "bash-history" ... [   3.4] Performing "blkid-tab" ... [   3.4] Performing "crash-data" ... [   3.4] Performing "cron-spool" ... [   3.5] Performing "dhcp-client-state" ... [   3.5] Performing "dhcp-server-state" ... [   3.5] Performing "dovecot-data" ... [   3.5] Performing "logfiles" ... [   3.5] Performing "machine-id" ... [   3.5] Performing "mail-spool" ... [   3.5] Performing "net-hostname" ... [   3.5] Performing "net-hwaddr" ... [   3.5] Performing "pacct-log" ... [   3.5] Performing "package-manager-cache" ... [   3.5] Performing "pam-data" ... [   3.5] Performing "passwd-backups" ... [   3.6] Performing "puppet-data-log" ... [   3.6] Performing "rh-subscription-manager" ... [   3.6] Performing "rhn-systemid" ... [   3.6] Performing "rpm-db" ... [   3.6] Performing "samba-db-log" ... [   3.6] Performing "script" ... [   3.6] Performing "smolt-uuid" ... [   3.6] Performing "ssh-hostkeys" ... [   3.6] Performing "ssh-userdir" ... [   3.6] Performing "sssd-db-log" ... [   3.6] Performing "tmp-files" ... [   3.6] Performing "udev-persistent-net" ... [   3.6] Performing "utmp" ... [   3.6] Performing "yum-uuid" ... [   3.6] Performing "customize" ... [   3.6] Setting a random seed [   3.6] Setting the machine ID in /etc/machine-id [   3.6] Copying (in image): /root/test.txt to /tmp/ [   4.1] Performing "lvm-uuids" ... [root@node1 ~]#  

  验证:开启虚拟机,到/tmp/目录下,看看是否有test.txt文件存在?内容是否是我们写入的内容?

[root@node1 ~]# virsh start centos7  域 centos7 已开始  [root@node1 ~]# virsh list   Id    名称                         状态 ----------------------------------------------------  13    centos7                        running  [root@node1 ~]# virsh console centos7  连接到域 centos7 换码符为 ^]   CentOS Linux 7 (Core) Kernel 3.10.0-693.el7.x86_64 on an x86_64  localhost login: root Password:  [root@localhost ~]# ls  test.txt [root@localhost ~]# ls /tmp/ systemd-private-7a99ef0b00024b7485b8d7c8b14aff2f-chronyd.service-2IwMge systemd-private-7a99ef0b00024b7485b8d7c8b14aff2f-systemd-hostnamed.service-8R1P0n test.txt [root@localhost ~]# cat /tmp/test.txt  this is test file on /root/ [root@localhost ~]#  [root@node1 ~]#  

  提示:可以看到在虚拟机内部/tmp/目录下有一个test.txt文件,并且内容也是我们刚才写入的内容;

  --copy-in <LOCALPATH:REMOTEDIR>:拷本宿主机上的文件或目录到虚拟机指定的目录下

[root@node1 ~]# ls [root@node1 ~]# echo "this is test file on 192.168.0.41" > a.txt [root@node1 ~]# cat a.txt this is test file on 192.168.0.41 [root@node1 ~]# virsh shutdown centos7  域 centos7 被关闭  [root@node1 ~]# virsh list --all  Id    名称                         状态 ----------------------------------------------------  -     centos7                        关闭  [root@node1 ~]# virt-sysprep --copy-in /root/a.txt:/root/ -d centos7 [   0.0] Examining the guest ... [   2.7] Performing "abrt-data" ... [   2.7] Performing "backup-files" ... [   3.4] Performing "bash-history" ... [   3.4] Performing "blkid-tab" ... [   3.4] Performing "crash-data" ... [   3.4] Performing "cron-spool" ... [   3.4] Performing "dhcp-client-state" ... [   3.4] Performing "dhcp-server-state" ... [   3.4] Performing "dovecot-data" ... [   3.4] Performing "logfiles" ... [   3.5] Performing "machine-id" ... [   3.5] Performing "mail-spool" ... [   3.5] Performing "net-hostname" ... [   3.5] Performing "net-hwaddr" ... [   3.5] Performing "pacct-log" ... [   3.5] Performing "package-manager-cache" ... [   3.5] Performing "pam-data" ... [   3.5] Performing "passwd-backups" ... [   3.5] Performing "puppet-data-log" ... [   3.5] Performing "rh-subscription-manager" ... [   3.5] Performing "rhn-systemid" ... [   3.5] Performing "rpm-db" ... [   3.5] Performing "samba-db-log" ... [   3.5] Performing "script" ... [   3.5] Performing "smolt-uuid" ... [   3.5] Performing "ssh-hostkeys" ... [   3.5] Performing "ssh-userdir" ... [   3.5] Performing "sssd-db-log" ... [   3.5] Performing "tmp-files" ... [   3.5] Performing "udev-persistent-net" ... [   3.5] Performing "utmp" ... [   3.6] Performing "yum-uuid" ... [   3.6] Performing "customize" ... [   3.6] Setting a random seed [   3.6] Setting the machine ID in /etc/machine-id [   3.6] Copying: /root/a.txt to /root/ [   3.6] Performing "lvm-uuids" ... [root@node1 ~]#  

  提示:我们在本地宿主机上的/root/目录下创建了一个a.txt文件,内容为“this is test file on 192.168.0.41”;然后把这个文件通过virt-sysprep工具拷贝到指定虚拟机的/root/目录下;

  验证:启动虚拟机,查看/root/目录下是否有我们刚才拷贝到a.txt文件?内容是否是我们刚才写入的内容?

[root@node1 ~]# virsh start centos7  域 centos7 已开始  [root@node1 ~]# virsh list   Id    名称                         状态 ----------------------------------------------------  15    centos7                        running  [root@node1 ~]# virsh console centos7  连接到域 centos7 换码符为 ^]   CentOS Linux 7 (Core) Kernel 3.10.0-693.el7.x86_64 on an x86_64  localhost login: root Password:  [root@localhost ~]# ls a.txt  test.txt [root@localhost ~]# cat a.txt  this is test file on 192.168.0.41 [root@localhost ~]#  [root@node1 ~]#  

  提示:拷贝目录的方式同拷贝文件的方式一样,我这里就不过多演示;

  --chmod <PERMISSIONS:FILE>:改变指定虚拟机里的文件按属性

[root@localhost ~]# ll total 8 -rw-r--r-- 1 root root 34 Aug 22 09:33 a.txt -rw-r--r-- 1 root root 28 Aug 22 09:27 test.txt [root@localhost ~]#  [root@node1 ~]# virsh shutdown centos7  域 centos7 被关闭  [root@node1 ~]# virt-sysprep --chmod 0600:/root/a.txt -d centos7 [   0.0] Examining the guest ... [   2.9] Performing "abrt-data" ... [   2.9] Performing "backup-files" ... [   3.6] Performing "bash-history" ... [   3.6] Performing "blkid-tab" ... [   3.7] Performing "crash-data" ... [   3.7] Performing "cron-spool" ... [   3.7] Performing "dhcp-client-state" ... [   3.7] Performing "dhcp-server-state" ... [   3.7] Performing "dovecot-data" ... [   3.7] Performing "logfiles" ... [   3.7] Performing "machine-id" ... [   3.7] Performing "mail-spool" ... [   3.7] Performing "net-hostname" ... [   3.7] Performing "net-hwaddr" ... [   3.7] Performing "pacct-log" ... [   3.7] Performing "package-manager-cache" ... [   3.8] Performing "pam-data" ... [   3.8] Performing "passwd-backups" ... [   3.8] Performing "puppet-data-log" ... [   3.8] Performing "rh-subscription-manager" ... [   3.8] Performing "rhn-systemid" ... [   3.8] Performing "rpm-db" ... [   3.8] Performing "samba-db-log" ... [   3.8] Performing "script" ... [   3.8] Performing "smolt-uuid" ... [   3.8] Performing "ssh-hostkeys" ... [   3.8] Performing "ssh-userdir" ... [   3.8] Performing "sssd-db-log" ... [   3.8] Performing "tmp-files" ... [   3.8] Performing "udev-persistent-net" ... [   3.8] Performing "utmp" ... [   3.8] Performing "yum-uuid" ... [   3.8] Performing "customize" ... [   3.8] Setting a random seed [   3.8] Setting the machine ID in /etc/machine-id [   3.8] Changing permissions of /root/a.txt to 0600 [   3.9] Performing "lvm-uuids" ... [root@node1 ~]#  

  提示:权限的表示必须是4位数字表示,不能是3位;有关Linux系统文件的权限相关说明可以参考我的博客https://www.cnblogs.com/qiuhom-1874/tag/%E6%9D%83%E9%99%90/

  验证:启动虚拟机链接到虚拟机控制台查看对应文件的权限,是否是我们指定的权限?

[root@node1 ~]# virsh start centos7  域 centos7 已开始  [root@node1 ~]# virsh console centos7  连接到域 centos7 换码符为 ^]   CentOS Linux 7 (Core) Kernel 3.10.0-693.el7.x86_64 on an x86_64  localhost login: root Password:  [root@localhost ~]# ll  total 8 -rw------- 1 root root 34 Aug 22 09:33 a.txt -rw-r--r-- 1 root root 28 Aug 22 09:27 test.txt [root@localhost ~]#  [root@node1 ~]#  

  --append-line <FILE:LINE>:给指定虚拟机上的文件追加一行数据,类似在虚拟机内部执行echo“context” >> /path/to/file操作

[root@node1 ~]# virsh  [root@node1 ~]# virsh shutdown centos7  域 centos7 被关闭  [root@node1 ~]# virt-sysprep --append-line /root/a.txt:"this is append new data" -d centos7 [   0.0] Examining the guest ... [   3.0] Performing "abrt-data" ... [   3.0] Performing "backup-files" ... [   3.8] Performing "bash-history" ... [   3.8] Performing "blkid-tab" ... [   3.8] Performing "crash-data" ... [   3.8] Performing "cron-spool" ... [   3.8] Performing "dhcp-client-state" ... [   3.8] Performing "dhcp-server-state" ... [   3.8] Performing "dovecot-data" ... [   3.8] Performing "logfiles" ... [   3.8] Performing "machine-id" ... [   3.8] Performing "mail-spool" ... [   3.9] Performing "net-hostname" ... [   3.9] Performing "net-hwaddr" ... [   3.9] Performing "pacct-log" ... [   3.9] Performing "package-manager-cache" ... [   3.9] Performing "pam-data" ... [   3.9] Performing "passwd-backups" ... [   3.9] Performing "puppet-data-log" ... [   3.9] Performing "rh-subscription-manager" ... [   3.9] Performing "rhn-systemid" ... [   3.9] Performing "rpm-db" ... [   3.9] Performing "samba-db-log" ... [   3.9] Performing "script" ... [   3.9] Performing "smolt-uuid" ... [   3.9] Performing "ssh-hostkeys" ... [   3.9] Performing "ssh-userdir" ... [   3.9] Performing "sssd-db-log" ... [   3.9] Performing "tmp-files" ... [   4.0] Performing "udev-persistent-net" ... [   4.0] Performing "utmp" ... [   4.0] Performing "yum-uuid" ... [   4.0] Performing "customize" ... [   4.0] Setting a random seed [   4.0] Setting the machine ID in /etc/machine-id [   4.0] Appending line to /root/a.txt [   4.1] Performing "lvm-uuids" ... [root@node1 ~]#  

  验证:开启虚拟机链接到控制台查看对应文件内容是否有新内容追加进去?

[root@node1 ~]# virsh start centos7  域 centos7 已开始  [root@node1 ~]# virsh console centos7  连接到域 centos7 换码符为 ^]   CentOS Linux 7 (Core) Kernel 3.10.0-693.el7.x86_64 on an x86_64  localhost login: root Password:  [root@localhost ~]# ll total 8 -rw------- 1 root root 58 Aug 22 09:50 a.txt -rw-r--r-- 1 root root 28 Aug 22 09:27 test.txt [root@localhost ~]# cat a.txt  this is test file on 192.168.0.41 this is append new data [root@localhost ~]#  [root@node1 ~]#  

  --delete <PATH>:删除虚拟机上的指定文件或目录

[root@node1 ~]# virsh console centos7  连接到域 centos7 换码符为 ^]   [root@localhost ~]# ls a.txt  test.txt [root@localhost ~]#  [root@node1 ~]# virsh shutdown centos7  域 centos7 被关闭  [root@node1 ~]# virt-sysprep --delete /root/test.txt -d centos7 [   0.0] Examining the guest ... [   2.9] Performing "abrt-data" ... [   2.9] Performing "backup-files" ... [   3.5] Performing "bash-history" ... [   3.5] Performing "blkid-tab" ... [   3.6] Performing "crash-data" ... [   3.6] Performing "cron-spool" ... [   3.6] Performing "dhcp-client-state" ... [   3.6] Performing "dhcp-server-state" ... [   3.6] Performing "dovecot-data" ... [   3.6] Performing "logfiles" ... [   3.6] Performing "machine-id" ... [   3.6] Performing "mail-spool" ... [   3.6] Performing "net-hostname" ... [   3.6] Performing "net-hwaddr" ... [   3.6] Performing "pacct-log" ... [   3.6] Performing "package-manager-cache" ... [   3.6] Performing "pam-data" ... [   3.6] Performing "passwd-backups" ... [   3.6] Performing "puppet-data-log" ... [   3.6] Performing "rh-subscription-manager" ... [   3.6] Performing "rhn-systemid" ... [   3.7] Performing "rpm-db" ... [   3.7] Performing "samba-db-log" ... [   3.7] Performing "script" ... [   3.7] Performing "smolt-uuid" ... [   3.7] Performing "ssh-hostkeys" ... [   3.7] Performing "ssh-userdir" ... [   3.7] Performing "sssd-db-log" ... [   3.7] Performing "tmp-files" ... [   3.7] Performing "udev-persistent-net" ... [   3.7] Performing "utmp" ... [   3.7] Performing "yum-uuid" ... [   3.7] Performing "customize" ... [   3.7] Setting a random seed [   3.7] Setting the machine ID in /etc/machine-id [   3.7] Deleting: /root/test.txt [   3.8] Performing "lvm-uuids" ... [root@node1 ~]#  

  验证:链接到虚拟机控制台,查看对应文件是否被删除?

[root@node1 ~]# virsh start centos7  域 centos7 已开始  [root@node1 ~]# virsh console centos7  连接到域 centos7 换码符为 ^]   CentOS Linux 7 (Core) Kernel 3.10.0-693.el7.x86_64 on an x86_64  localhost login: root Password:  [root@localhost ~]# ll total 4 -rw------- 1 root root 58 Aug 22 09:50 a.txt [root@localhost ~]#  [root@node1 ~]#  

  --hostname <HOSTNAME>:设置指定虚拟机的主机名

[root@node1 ~]# virsh console centos7  连接到域 centos7 换码符为 ^]   [root@localhost ~]# hostname localhost.localdomain [root@localhost ~]# init 0 Powering off. [  107.277110] Power down.  [root@node1 ~]# virt-sysprep --hostname centos7.test.com -d centos7 [   0.0] Examining the guest ... [   2.7] Performing "abrt-data" ... [   2.8] Performing "backup-files" ... [   3.5] Performing "bash-history" ... [   3.5] Performing "blkid-tab" ... [   3.5] Performing "crash-data" ... [   3.5] Performing "cron-spool" ... [   3.5] Performing "dhcp-client-state" ... [   3.5] Performing "dhcp-server-state" ... [   3.5] Performing "dovecot-data" ... [   3.5] Performing "logfiles" ... [   3.6] Performing "machine-id" ... [   3.6] Performing "mail-spool" ... [   3.6] Performing "net-hostname" ... [   3.6] Performing "net-hwaddr" ... [   3.6] Performing "pacct-log" ... [   3.6] Performing "package-manager-cache" ... [   3.6] Performing "pam-data" ... [   3.6] Performing "passwd-backups" ... [   3.6] Performing "puppet-data-log" ... [   3.6] Performing "rh-subscription-manager" ... [   3.6] Performing "rhn-systemid" ... [   3.6] Performing "rpm-db" ... [   3.6] Performing "samba-db-log" ... [   3.6] Performing "script" ... [   3.6] Performing "smolt-uuid" ... [   3.6] Performing "ssh-hostkeys" ... [   3.6] Performing "ssh-userdir" ... [   3.6] Performing "sssd-db-log" ... [   3.7] Performing "tmp-files" ... [   3.7] Performing "udev-persistent-net" ... [   3.7] Performing "utmp" ... [   3.7] Performing "yum-uuid" ... [   3.7] Performing "customize" ... [   3.7] Setting a random seed [   3.7] Setting the machine ID in /etc/machine-id [   3.7] Setting the hostname: centos7.test.com [   3.8] Performing "lvm-uuids" ... [root@node1 ~]#  

  验证:链接到虚拟机控制台,查看对应虚拟机是否更改了主机名?

[root@node1 ~]# virsh start centos7  域 centos7 已开始  [root@node1 ~]# virsh console centos7 连接到域 centos7 换码符为 ^]   CentOS Linux 7 (Core) Kernel 3.10.0-693.el7.x86_64 on an x86_64  centos7 login: root Password:  [root@centos7 ~]# hostname centos7.test.com [root@centos7 ~]#  [root@node1 ~]#  

  --install <PKG,PKG..>:在指定虚拟机上安装指定包

[root@node1 ~]# virsh console centos7  连接到域 centos7 换码符为 ^]   [root@centos7 ~]# rpm -q httpd package httpd is not installed [root@centos7 ~]# rpm -q net-tools package net-tools is not installed [root@centos7 ~]# [root@node1 ~]#  virsh shutdown centos7  域 centos7 被关闭  [root@node1 ~]# virt-sysprep --install httpd,net-tools -d centos7 [   0.0] Examining the guest ... [   2.7] Performing "abrt-data" ... [   2.7] Performing "backup-files" ... [   3.4] Performing "bash-history" ... [   3.4] Performing "blkid-tab" ... [   3.4] Performing "crash-data" ... [   3.4] Performing "cron-spool" ... [   3.4] Performing "dhcp-client-state" ... [   3.4] Performing "dhcp-server-state" ... [   3.4] Performing "dovecot-data" ... [   3.4] Performing "logfiles" ... [   3.5] Performing "machine-id" ... [   3.5] Performing "mail-spool" ... [   3.5] Performing "net-hostname" ... [   3.5] Performing "net-hwaddr" ... [   3.5] Performing "pacct-log" ... [   3.5] Performing "package-manager-cache" ... [   3.5] Performing "pam-data" ... [   3.5] Performing "passwd-backups" ... [   3.5] Performing "puppet-data-log" ... [   3.5] Performing "rh-subscription-manager" ... [   3.5] Performing "rhn-systemid" ... [   3.5] Performing "rpm-db" ... [   3.5] Performing "samba-db-log" ... [   3.5] Performing "script" ... [   3.5] Performing "smolt-uuid" ... [   3.6] Performing "ssh-hostkeys" ... [   3.6] Performing "ssh-userdir" ... [   3.6] Performing "sssd-db-log" ... [   3.6] Performing "tmp-files" ... [   3.6] Performing "udev-persistent-net" ... [   3.6] Performing "utmp" ... [   3.6] Performing "yum-uuid" ... [   3.6] Performing "customize" ... [   3.6] Setting a random seed [   3.6] Setting the machine ID in /etc/machine-id [   3.6] Installing packages: httpd net-tools Loaded plugins: fastestmirror    One of the configured repositories failed (Unknown),  and yum doesn't have enough cached data to continue. At this point the only  safe thing yum can do is fail. There are a few ways to work "fix" this:       1. Contact the upstream for the repository and get them to fix the problem.       2. Reconfigure the baseurl/etc. for the repository, to point to a working         upstream. This is most often useful if you are using a newer         distribution release than is supported by the repository (and the         packages for the previous distribution release still work).       3. Run the command with the repository temporarily disabled             yum --disablerepo=<repoid> ...       4. Disable the repository permanently, so yum won't use it by default. Yum         will then just ignore the repository until you permanently enable it         again or use --enablerepo for temporary usage:              yum-config-manager --disable <repoid>         or             subscription-manager repos --disable=<repoid>       5. Configure the failing repository to be skipped, if it is unavailable.         Note that yum will try to contact the repo. when it runs most commands,         so will have to try and fail each time (and thus. yum will be be much         slower). If it is a very temporary problem though, this is often a nice         compromise:              yum-config-manager --save --setopt=<repoid>.skip_if_unavailable=true  Cannot find a valid baseurl for repo: base/7/x86_64 Could not retrieve mirrorlist http://mirrorlist.centos.org/?release=7&arch=x86_64&repo=os&infra=stock error was 14: curl#6 - "Could not resolve host: mirrorlist.centos.org; Unknown error"  virt-sysprep: warning: the command may have failed because the network is  disabled.  Try either removing ‘--no-network’ or adding ‘--network’  on the command line.  virt-sysprep: error: yum -y install 'httpd' 'net-tools': command exited  with an error  If reporting bugs, run virt-sysprep with debugging enabled and include the  complete output:    virt-sysprep -v -x [...] [root@node1 ~]#  

  提示:出现以上错误的原因是虚拟机不能够正常的连接到指定yum仓库下载包;解决办法上面已经提示我们了,要在移除私有信息时加上--no-network或者直接在安装包的时候加上--network选项即可;

[root@node1 ~]# virt-sysprep --network --install httpd,net-tools -d centos7            [   0.0] Examining the guest ... [   5.2] Performing "abrt-data" ... [   5.2] Performing "backup-files" ... [   5.8] Performing "bash-history" ... [   5.8] Performing "blkid-tab" ... [   5.9] Performing "crash-data" ... [   5.9] Performing "cron-spool" ... [   5.9] Performing "dhcp-client-state" ... [   5.9] Performing "dhcp-server-state" ... [   5.9] Performing "dovecot-data" ... [   5.9] Performing "logfiles" ... [   5.9] Performing "machine-id" ... [   5.9] Performing "mail-spool" ... [   5.9] Performing "net-hostname" ... [   6.0] Performing "net-hwaddr" ... [   6.0] Performing "pacct-log" ... [   6.0] Performing "package-manager-cache" ... [   6.0] Performing "pam-data" ... [   6.0] Performing "passwd-backups" ... [   6.0] Performing "puppet-data-log" ... [   6.0] Performing "rh-subscription-manager" ... [   6.0] Performing "rhn-systemid" ... [   6.0] Performing "rpm-db" ... [   6.0] Performing "samba-db-log" ... [   6.0] Performing "script" ... [   6.0] Performing "smolt-uuid" ... [   6.0] Performing "ssh-hostkeys" ... [   6.0] Performing "ssh-userdir" ... [   6.0] Performing "sssd-db-log" ... [   6.0] Performing "tmp-files" ... [   6.0] Performing "udev-persistent-net" ... [   6.1] Performing "utmp" ... [   6.1] Performing "yum-uuid" ... [   6.1] Performing "customize" ... [   6.1] Setting a random seed [   6.1] Setting the machine ID in /etc/machine-id [   6.1] Installing packages: httpd net-tools [  29.1] Performing "lvm-uuids" ... [root@node1 ~]#  

  验证:链接到虚拟机控制台查看对应的包是否都安装上了?

[root@node1 ~]# virsh start centos7  域 centos7 已开始  [root@node1 ~]# virsh console centos7 连接到域 centos7 换码符为 ^]   CentOS Linux 7 (Core) Kernel 3.10.0-693.el7.x86_64 on an x86_64  centos7 login: root Password:  [root@centos7 ~]# rpm -q httpd httpd-2.4.6-93.el7.centos.x86_64 [root@centos7 ~]# rpm -q net-tools net-tools-2.0-0.25.20131004git.el7.x86_64 [root@centos7 ~]#  [root@node1 ~]#  

  --mkdir <DIR>:在虚拟机上创建目录,类似在虚拟机上执行mkdir -p /path/to/dir

[root@node1 ~]# virsh shutdown centos7  域 centos7 被关闭  [root@node1 ~]# virt-sysprep --mkdir /mnt/test/ -d centos7 [   0.0] Examining the guest ... [   2.8] Performing "abrt-data" ... [   2.8] Performing "backup-files" ... [   3.5] Performing "bash-history" ... [   3.5] Performing "blkid-tab" ... [   3.5] Performing "crash-data" ... [   3.5] Performing "cron-spool" ... [   3.6] Performing "dhcp-client-state" ... [   3.6] Performing "dhcp-server-state" ... [   3.6] Performing "dovecot-data" ... [   3.6] Performing "logfiles" ... [   3.6] Performing "machine-id" ... [   3.6] Performing "mail-spool" ... [   3.6] Performing "net-hostname" ... [   3.6] Performing "net-hwaddr" ... [   3.6] Performing "pacct-log" ... [   3.6] Performing "package-manager-cache" ... [   3.7] Performing "pam-data" ... [   3.7] Performing "passwd-backups" ... [   3.7] Performing "puppet-data-log" ... [   3.7] Performing "rh-subscription-manager" ... [   3.7] Performing "rhn-systemid" ... [   3.7] Performing "rpm-db" ... [   3.7] Performing "samba-db-log" ... [   3.7] Performing "script" ... [   3.7] Performing "smolt-uuid" ... [   3.7] Performing "ssh-hostkeys" ... [   3.7] Performing "ssh-userdir" ... [   3.7] Performing "sssd-db-log" ... [   3.7] Performing "tmp-files" ... [   3.7] Performing "udev-persistent-net" ... [   3.7] Performing "utmp" ... [   3.7] Performing "yum-uuid" ... [   3.7] Performing "customize" ... [   3.7] Setting a random seed [   3.7] Setting the machine ID in /etc/machine-id [   3.7] Making directory: /mnt/test/ [   3.8] Performing "lvm-uuids" ... [root@node1 ~]#  

  验证:开启虚拟机,查看对应目录下是否有test目录创建?

[root@node1 ~]# virsh start  centos7  域 centos7 已开始  [root@node1 ~]# virsh console centos7 连接到域 centos7 换码符为 ^]   centos7 login: root Password:  [root@centos7 ~]# ll /mnt/ total 0 drwxr-xr-x 2 root root 6 Aug 22 10:43 test [root@centos7 ~]#  [root@node1 ~]#  

  --move <SOURCE:DEST>:把虚拟机上的指定文件移动到虚拟机指定位置

[root@node1 ~]# virsh shutdown centos7  域 centos7 被关闭  [root@node1 ~]# virt-sysprep --move /root/a.txt:/mnt/test/ -d centos7 [   0.0] Examining the guest ... [   2.9] Performing "abrt-data" ... [   3.0] Performing "backup-files" ... [   3.6] Performing "bash-history" ... [   3.6] Performing "blkid-tab" ... [   3.6] Performing "crash-data" ... [   3.7] Performing "cron-spool" ... [   3.7] Performing "dhcp-client-state" ... [   3.7] Performing "dhcp-server-state" ... [   3.7] Performing "dovecot-data" ... [   3.7] Performing "logfiles" ... [   3.7] Performing "machine-id" ... [   3.7] Performing "mail-spool" ... [   3.7] Performing "net-hostname" ... [   3.7] Performing "net-hwaddr" ... [   3.7] Performing "pacct-log" ... [   3.7] Performing "package-manager-cache" ... [   3.8] Performing "pam-data" ... [   3.8] Performing "passwd-backups" ... [   3.8] Performing "puppet-data-log" ... [   3.8] Performing "rh-subscription-manager" ... [   3.8] Performing "rhn-systemid" ... [   3.8] Performing "rpm-db" ... [   3.8] Performing "samba-db-log" ... [   3.8] Performing "script" ... [   3.8] Performing "smolt-uuid" ... [   3.8] Performing "ssh-hostkeys" ... [   3.8] Performing "ssh-userdir" ... [   3.8] Performing "sssd-db-log" ... [   3.8] Performing "tmp-files" ... [   3.8] Performing "udev-persistent-net" ... [   3.8] Performing "utmp" ... [   3.8] Performing "yum-uuid" ... [   3.8] Performing "customize" ... [   3.8] Setting a random seed [   3.8] Setting the machine ID in /etc/machine-id [   3.8] Moving: /root/a.txt -> /mnt/test/ [   3.9] Performing "lvm-uuids" ... [root@node1 ~]#  

  验证:查看虚拟机/mnt/test/目录下是否有a.txt文件?

[root@node1 ~]# virsh start centos7  域 centos7 已开始  [root@node1 ~]# virsh console centos7  连接到域 centos7 换码符为 ^]   centos7 login: root Password:  [root@centos7 ~]# ll /mnt/test/ total 4 -rw-r--r-- 1 root root 58 Aug 22 10:42 a.txt [root@centos7 ~]# cat /mnt/test/a.txt  this is test file on 192.168.0.41 this is append new data [root@centos7 ~]#  

  --run <SCRIPT>:在虚拟机上运行宿主机上指定目录下的脚本

[root@node1 ~]# cat >> test.sh << EOF > #!/bin/bash > echo "this is test file" > aa.html > EOF [root@node1 ~]# ll 总用量 8 -rw-r--r-- 1 root root 34 8月  22 21:33 a.txt -rw-r--r-- 1 root root 47 8月  22 22:55 test.sh [root@node1 ~]# cat test.sh  #!/bin/bash echo "this is test file" > aa.html [root@node1 ~]# virsh shutdown centos7  域 centos7 被关闭  [root@node1 ~]# virt-sysprep --run /root/test.sh -d centos7 [   0.0] Examining the guest ... [   2.9] Performing "abrt-data" ... [   2.9] Performing "backup-files" ... [   3.6] Performing "bash-history" ... [   3.6] Performing "blkid-tab" ... [   3.6] Performing "crash-data" ... [   3.6] Performing "cron-spool" ... [   3.6] Performing "dhcp-client-state" ... [   3.6] Performing "dhcp-server-state" ... [   3.6] Performing "dovecot-data" ... [   3.6] Performing "logfiles" ... [   3.7] Performing "machine-id" ... [   3.7] Performing "mail-spool" ... [   3.7] Performing "net-hostname" ... [   3.7] Performing "net-hwaddr" ... [   3.7] Performing "pacct-log" ... [   3.7] Performing "package-manager-cache" ... [   3.7] Performing "pam-data" ... [   3.7] Performing "passwd-backups" ... [   3.7] Performing "puppet-data-log" ... [   3.7] Performing "rh-subscription-manager" ... [   3.7] Performing "rhn-systemid" ... [   3.7] Performing "rpm-db" ... [   3.7] Performing "samba-db-log" ... [   3.7] Performing "script" ... [   3.7] Performing "smolt-uuid" ... [   3.7] Performing "ssh-hostkeys" ... [   3.7] Performing "ssh-userdir" ... [   3.7] Performing "sssd-db-log" ... [   3.7] Performing "tmp-files" ... [   3.8] Performing "udev-persistent-net" ... [   3.8] Performing "utmp" ... [   3.8] Performing "yum-uuid" ... [   3.8] Performing "customize" ... [   3.8] Setting a random seed [   3.8] Setting the machine ID in /etc/machine-id [   3.8] Running: /root/test.sh [   3.9] Performing "lvm-uuids" ... [root@node1 ~]#  

  提示:以上宿主机上的脚本就只是干了一件事,把“this is test file”这个字符串追加到/root/aa.html这个文件中,如果脚本在虚拟机上执行成功,它会在虚拟机生成aa.html文件,以上脚本没有指定aa.html文件的位置,默认它会在放在根目录下

  验证:链接虚拟机,查看根目录下是否有aa.html文件生成?

[root@node1 ~]# virsh start centos7  域 centos7 已开始  [root@node1 ~]# virsh console centos7 连接到域 centos7 换码符为 ^]   centos7 login:  centos7 login: root Password:  [root@centos7 ~]# ll total 0 [root@centos7 ~]# find / -name "aa.html" /aa.html [root@centos7 ~]# cat /aa.html  this is test file [root@centos7 ~]#  [root@node1 ~]#  

  --run-command <'CMD+ARGS'>:在指定虚拟机上运行命令

[root@node1 ~]# virsh shutdown centos7  域 centos7 被关闭  [root@node1 ~]# virt-sysprep --run-command 'mkdir /root/xx' -d centos7 [   0.0] Examining the guest ... [   2.8] Performing "abrt-data" ... [   2.8] Performing "backup-files" ... [   3.5] Performing "bash-history" ... [   3.5] Performing "blkid-tab" ... [   3.5] Performing "crash-data" ... [   3.5] Performing "cron-spool" ... [   3.6] Performing "dhcp-client-state" ... [   3.6] Performing "dhcp-server-state" ... [   3.6] Performing "dovecot-data" ... [   3.6] Performing "logfiles" ... [   3.6] Performing "machine-id" ... [   3.6] Performing "mail-spool" ... [   3.6] Performing "net-hostname" ... [   3.6] Performing "net-hwaddr" ... [   3.6] Performing "pacct-log" ... [   3.6] Performing "package-manager-cache" ... [   3.7] Performing "pam-data" ... [   3.7] Performing "passwd-backups" ... [   3.7] Performing "puppet-data-log" ... [   3.7] Performing "rh-subscription-manager" ... [   3.7] Performing "rhn-systemid" ... [   3.7] Performing "rpm-db" ... [   3.7] Performing "samba-db-log" ... [   3.7] Performing "script" ... [   3.7] Performing "smolt-uuid" ... [   3.7] Performing "ssh-hostkeys" ... [   3.7] Performing "ssh-userdir" ... [   3.7] Performing "sssd-db-log" ... [   3.7] Performing "tmp-files" ... [   3.7] Performing "udev-persistent-net" ... [   3.7] Performing "utmp" ... [   3.7] Performing "yum-uuid" ... [   3.7] Performing "customize" ... [   3.7] Setting a random seed [   3.8] Setting the machine ID in /etc/machine-id [   3.8] Running: mkdir /root/xx [   3.9] Performing "lvm-uuids" ... [root@node1 ~]#  

  验证:链接虚拟机控制台,查看在虚拟机/root/目录下是否有xx目录被创建?

[root@node1 ~]# virsh start centos7  域 centos7 已开始  [root@node1 ~]# virsh console centos7 连接到域 centos7 换码符为 ^]   centos7 login: root Password:  [root@centos7 ~]# ll total 0 drwxr-xr-x 2 root root 6 Aug 22 11:03 xx [root@centos7 ~]#  

  --touch <FILE>:在指定虚拟机上创建空文件或更新时间戳,类似在虚拟机上执行touch 命令

[root@node1 ~]# virsh shutdown centos7  域 centos7 被关闭  [root@node1 ~]# virt-sysprep --touch /root/xx/a.txt -d centos7 [   0.0] Examining the guest ... [   2.8] Performing "abrt-data" ... [   2.8] Performing "backup-files" ... [   3.5] Performing "bash-history" ... [   3.5] Performing "blkid-tab" ... [   3.5] Performing "crash-data" ... [   3.5] Performing "cron-spool" ... [   3.6] Performing "dhcp-client-state" ... [   3.6] Performing "dhcp-server-state" ... [   3.6] Performing "dovecot-data" ... [   3.6] Performing "logfiles" ... [   3.6] Performing "machine-id" ... [   3.6] Performing "mail-spool" ... [   3.6] Performing "net-hostname" ... [   3.6] Performing "net-hwaddr" ... [   3.6] Performing "pacct-log" ... [   3.6] Performing "package-manager-cache" ... [   3.6] Performing "pam-data" ... [   3.7] Performing "passwd-backups" ... [   3.7] Performing "puppet-data-log" ... [   3.7] Performing "rh-subscription-manager" ... [   3.7] Performing "rhn-systemid" ... [   3.7] Performing "rpm-db" ... [   3.7] Performing "samba-db-log" ... [   3.7] Performing "script" ... [   3.7] Performing "smolt-uuid" ... [   3.7] Performing "ssh-hostkeys" ... [   3.7] Performing "ssh-userdir" ... [   3.7] Performing "sssd-db-log" ... [   3.7] Performing "tmp-files" ... [   3.7] Performing "udev-persistent-net" ... [   3.7] Performing "utmp" ... [   3.7] Performing "yum-uuid" ... [   3.7] Performing "customize" ... [   3.7] Setting a random seed [   3.7] Setting the machine ID in /etc/machine-id [   3.7] Running touch: /root/xx/a.txt [   3.8] Performing "lvm-uuids" ... [root@node1 ~]#  

  验证:链接指定虚拟机,查看对应文件是否有创建?

[root@node1 ~]# virsh start centos7  域 centos7 已开始  [root@node1 ~]# virsh console centos7  连接到域 centos7 换码符为 ^]   centos7 login: root Password:  [root@centos7 ~]# ll  total 0 drwxr-xr-x 2 root root 19 Aug 22 11:09 xx [root@centos7 ~]# ll xx/ total 0 -rw-r--r-- 1 root root 0 Aug 22 11:09 a.txt [root@centos7 ~]#  [root@node1 ~]#  

  更新/root/xx/a.txt的时间戳

[root@node1 ~]# virsh shutdown centos7  域 centos7 被关闭  [root@node1 ~]# virt-sysprep --touch /root/xx/a.txt -d centos7 [   0.0] Examining the guest ... [   2.7] Performing "abrt-data" ... [   2.7] Performing "backup-files" ... [   3.5] Performing "bash-history" ... [   3.5] Performing "blkid-tab" ... [   3.5] Performing "crash-data" ... [   3.5] Performing "cron-spool" ... [   3.5] Performing "dhcp-client-state" ... [   3.5] Performing "dhcp-server-state" ... [   3.5] Performing "dovecot-data" ... [   3.5] Performing "logfiles" ... [   3.5] Performing "machine-id" ... [   3.5] Performing "mail-spool" ... [   3.5] Performing "net-hostname" ... [   3.6] Performing "net-hwaddr" ... [   3.6] Performing "pacct-log" ... [   3.6] Performing "package-manager-cache" ... [   3.6] Performing "pam-data" ... [   3.6] Performing "passwd-backups" ... [   3.6] Performing "puppet-data-log" ... [   3.6] Performing "rh-subscription-manager" ... [   3.6] Performing "rhn-systemid" ... [   3.6] Performing "rpm-db" ... [   3.6] Performing "samba-db-log" ... [   3.6] Performing "script" ... [   3.6] Performing "smolt-uuid" ... [   3.6] Performing "ssh-hostkeys" ... [   3.6] Performing "ssh-userdir" ... [   3.6] Performing "sssd-db-log" ... [   3.6] Performing "tmp-files" ... [   3.7] Performing "udev-persistent-net" ... [   3.7] Performing "utmp" ... [   3.7] Performing "yum-uuid" ... [   3.7] Performing "customize" ... [   3.7] Setting a random seed [   3.7] Setting the machine ID in /etc/machine-id [   3.7] Running touch: /root/xx/a.txt [   3.8] Performing "lvm-uuids" ... [root@node1 ~]# virsh start centos7  域 centos7 已开始  [root@node1 ~]# virsh console centos7  连接到域 centos7 换码符为 ^]   centos7 login: root Password:  [root@centos7 ~]# ll /root/xx/ total 0 -rw-r--r-- 1 root root 0 Aug 22 11:12 a.txt [root@centos7 ~]# 

  --truncate <FILE>:清空虚拟机上的指定文件

[root@node1 ~]# virsh console centos7  连接到域 centos7 换码符为 ^]   [root@centos7 ~]# cat /mnt/test/a.txt  this is test file on 192.168.0.41 this is append new data [root@centos7 ~]#  [root@node1 ~]# virsh shutdown centos7  域 centos7 被关闭  [root@node1 ~]# virt-sysprep --truncate /mnt/test/a.txt -d centos7 [   0.0] Examining the guest ... [   2.7] Performing "abrt-data" ... [   2.7] Performing "backup-files" ... [   3.3] Performing "bash-history" ... [   3.4] Performing "blkid-tab" ... [   3.4] Performing "crash-data" ... [   3.4] Performing "cron-spool" ... [   3.4] Performing "dhcp-client-state" ... [   3.4] Performing "dhcp-server-state" ... [   3.4] Performing "dovecot-data" ... [   3.4] Performing "logfiles" ... [   3.4] Performing "machine-id" ... [   3.4] Performing "mail-spool" ... [   3.4] Performing "net-hostname" ... [   3.4] Performing "net-hwaddr" ... [   3.5] Performing "pacct-log" ... [   3.5] Performing "package-manager-cache" ... [   3.5] Performing "pam-data" ... [   3.5] Performing "passwd-backups" ... [   3.5] Performing "puppet-data-log" ... [   3.5] Performing "rh-subscription-manager" ... [   3.5] Performing "rhn-systemid" ... [   3.5] Performing "rpm-db" ... [   3.5] Performing "samba-db-log" ... [   3.5] Performing "script" ... [   3.5] Performing "smolt-uuid" ... [   3.5] Performing "ssh-hostkeys" ... [   3.5] Performing "ssh-userdir" ... [   3.5] Performing "sssd-db-log" ... [   3.5] Performing "tmp-files" ... [   3.5] Performing "udev-persistent-net" ... [   3.5] Performing "utmp" ... [   3.6] Performing "yum-uuid" ... [   3.6] Performing "customize" ... [   3.6] Setting a random seed [   3.6] Setting the machine ID in /etc/machine-id [   3.6] Truncating: /mnt/test/a.txt [   3.7] Performing "lvm-uuids" ... [root@node1 ~]#  

  验证:连接虚拟机控制台,查看/mnt/test/a.txt文件是否被清空?

[root@node1 ~]# virsh start centos7  域 centos7 已开始  [root@node1 ~]# virsh console centos7  连接到域 centos7 换码符为 ^]   centos7 login: root Password:  [root@centos7 ~]# ll /mnt/test/a.txt  -rw-r--r-- 1 root root 0 Aug 22 11:14 /mnt/test/a.txt [root@centos7 ~]# cat /mnt/test/a.txt [root@centos7 ~]#  [root@node1 ~]#  

  --truncate-recursive <PATH>:在虚拟机上递归清空指定目录下的所有文件内容

[root@node1 ~]# virsh console centos7  连接到域 centos7 换码符为 ^]   [root@centos7 ~]# ls xx [root@centos7 ~]# echo "aaa" > a1 [root@centos7 ~]# echo "bb" > b1    [root@centos7 ~]# echo "cc" > c1   [root@centos7 ~]# ls a1  b1  c1  xx [root@centos7 ~]# cat a1 b1 c1 aaa bb cc [root@centos7 ~]#  [root@node1 ~]# virsh shutdown centos7  域 centos7 被关闭  [root@node1 ~]# virt-sysprep --truncate-recursive /root/ -d centos7 [   0.0] Examining the guest ... [   2.7] Performing "abrt-data" ... [   2.7] Performing "backup-files" ... [   3.4] Performing "bash-history" ... [   3.4] Performing "blkid-tab" ... [   3.4] Performing "crash-data" ... [   3.4] Performing "cron-spool" ... [   3.4] Performing "dhcp-client-state" ... [   3.4] Performing "dhcp-server-state" ... [   3.4] Performing "dovecot-data" ... [   3.4] Performing "logfiles" ... [   3.5] Performing "machine-id" ... [   3.5] Performing "mail-spool" ... [   3.5] Performing "net-hostname" ... [   3.5] Performing "net-hwaddr" ... [   3.5] Performing "pacct-log" ... [   3.5] Performing "package-manager-cache" ... [   3.5] Performing "pam-data" ... [   3.5] Performing "passwd-backups" ... [   3.5] Performing "puppet-data-log" ... [   3.5] Performing "rh-subscription-manager" ... [   3.6] Performing "rhn-systemid" ... [   3.6] Performing "rpm-db" ... [   3.6] Performing "samba-db-log" ... [   3.6] Performing "script" ... [   3.6] Performing "smolt-uuid" ... [   3.6] Performing "ssh-hostkeys" ... [   3.6] Performing "ssh-userdir" ... [   3.6] Performing "sssd-db-log" ... [   3.6] Performing "tmp-files" ... [   3.6] Performing "udev-persistent-net" ... [   3.6] Performing "utmp" ... [   3.6] Performing "yum-uuid" ... [   3.6] Performing "customize" ... [   3.6] Setting a random seed [   3.6] Setting the machine ID in /etc/machine-id [   3.6] Recursively truncating: /root/ [   3.7] Performing "lvm-uuids" ... [root@node1 ~]#  

  验证:链接虚拟机控制台,查看root下的文件是否都被清空了?

[root@node1 ~]# virsh start centos7  域 centos7 已开始  [root@node1 ~]# virsh console centos7  连接到域 centos7 换码符为 ^]   centos7 login: root Password:  -bash-4.2# ll total 0 -rw-r--r-- 1 root root  0 Aug 22 11:18 a1 -rw-r--r-- 1 root root  0 Aug 22 11:18 b1 -rw-r--r-- 1 root root  0 Aug 22 11:18 c1 drwxr-xr-x 2 root root 19 Aug 22 11:09 xx -bash-4.2# cat a1 -bash-4.2# cat b1 -bash-4.2# cat c1 -bash-4.2#  [root@node1 ~]#  

  提示:从链接虚拟机控制台,登录虚拟机的shell提示符就可以知道,这个命令它会连同隐藏文件一并清空;

  --uninstall <PKG,PKG..>:卸载虚拟机上的指定包

[root@node1 ~]# virsh shutdown centos7  域 centos7 被关闭  [root@node1 ~]# virt-sysprep --uninstall httpd,net-tools -d centos7 [   0.0] Examining the guest ... [   2.7] Performing "abrt-data" ... [   2.7] Performing "backup-files" ... [   3.4] Performing "bash-history" ... [   3.4] Performing "blkid-tab" ... [   3.4] Performing "crash-data" ... [   3.4] Performing "cron-spool" ... [   3.4] Performing "dhcp-client-state" ... [   3.4] Performing "dhcp-server-state" ... [   3.4] Performing "dovecot-data" ... [   3.4] Performing "logfiles" ... [   3.4] Performing "machine-id" ... [   3.4] Performing "mail-spool" ... [   3.4] Performing "net-hostname" ... [   3.4] Performing "net-hwaddr" ... [   3.5] Performing "pacct-log" ... [   3.5] Performing "package-manager-cache" ... [   3.5] Performing "pam-data" ... [   3.5] Performing "passwd-backups" ... [   3.5] Performing "puppet-data-log" ... [   3.5] Performing "rh-subscription-manager" ... [   3.5] Performing "rhn-systemid" ... [   3.5] Performing "rpm-db" ... [   3.5] Performing "samba-db-log" ... [   3.5] Performing "script" ... [   3.5] Performing "smolt-uuid" ... [   3.5] Performing "ssh-hostkeys" ... [   3.5] Performing "ssh-userdir" ... [   3.5] Performing "sssd-db-log" ... [   3.5] Performing "tmp-files" ... [   3.5] Performing "udev-persistent-net" ... [   3.5] Performing "utmp" ... [   3.5] Performing "yum-uuid" ... [   3.6] Performing "customize" ... [   3.6] Setting a random seed [   3.6] Setting the machine ID in /etc/machine-id [   3.6] Uninstalling packages: httpd net-tools [   4.5] Performing "lvm-uuids" ... [root@node1 ~]#  

  验证:链接虚拟机控制台,看看对应包是否卸载?

[root@node1 ~]# virsh start centos7  域 centos7 已开始  [root@node1 ~]# virsh console centos7 连接到域 centos7 换码符为 ^]   centos7 login:  centos7 login: root Password:  -bash-4.2# rpm -q httpd net-tools package httpd is not installed package net-tools is not installed -bash-4.2#  [root@node1 ~]#  

   --upload <FILE:DEST>:把宿主机上的指定文件上传到虚拟机指定目录下;

[root@node1 ~]# virsh console centos7  连接到域 centos7 换码符为 ^]   -bash-4.2# ls a1  b1  c1  xx -bash-4.2#  [root@node1 ~]# ls a.txt  test.sh [root@node1 ~]# cat test.sh  #!/bin/bash echo "this is test file" > aa.html [root@node1 ~]# virsh shutdown centos7  域 centos7 被关闭  [root@node1 ~]# virt-sysprep --upload /root/test.sh:/root/ -d centos7 [   0.0] Examining the guest ... [   2.8] Performing "abrt-data" ... [   2.8] Performing "backup-files" ... [   3.4] Performing "bash-history" ... [   3.4] Performing "blkid-tab" ... [   3.5] Performing "crash-data" ... [   3.5] Performing "cron-spool" ... [   3.5] Performing "dhcp-client-state" ... [   3.5] Performing "dhcp-server-state" ... [   3.5] Performing "dovecot-data" ... [   3.5] Performing "logfiles" ... [   3.5] Performing "machine-id" ... [   3.5] Performing "mail-spool" ... [   3.5] Performing "net-hostname" ... [   3.5] Performing "net-hwaddr" ... [   3.5] Performing "pacct-log" ... [   3.6] Performing "package-manager-cache" ... [   3.6] Performing "pam-data" ... [   3.6] Performing "passwd-backups" ... [   3.6] Performing "puppet-data-log" ... [   3.6] Performing "rh-subscription-manager" ... [   3.6] Performing "rhn-systemid" ... [   3.6] Performing "rpm-db" ... [   3.6] Performing "samba-db-log" ... [   3.6] Performing "script" ... [   3.6] Performing "smolt-uuid" ... [   3.6] Performing "ssh-hostkeys" ... [   3.6] Performing "ssh-userdir" ... [   3.6] Performing "sssd-db-log" ... [   3.6] Performing "tmp-files" ... [   3.6] Performing "udev-persistent-net" ... [   3.6] Performing "utmp" ... [   3.6] Performing "yum-uuid" ... [   3.6] Performing "customize" ... [   3.7] Setting a random seed [   3.7] Setting the machine ID in /etc/machine-id [   3.7] Uploading: /root/test.sh to /root/ [   3.7] Performing "lvm-uuids" ... [root@node1 ~]#  

  验证:链接虚拟机控制台,查看root目录下是否有test.sh文件,文件内容是不是和宿主机上的test.sh一样?

[root@node1 ~]# virsh start centos7  域 centos7 已开始  [root@node1 ~]# virsh console centos7  连接到域 centos7 换码符为 ^]   centos7 login: root Password:  -bash-4.2# ls  a1  b1  c1  test.sh  xx -bash-4.2# cat test.sh  #!/bin/bash echo "this is test file" > aa.html -bash-4.2#  [root@node1 ~]#  

  以上就是virt-sysprep这个工具的常用选项用法,演示了这么多,它其实就是在执行指定选项时,它会临时启动一个虚拟机,然后通过特定选项来调用特定的api接口和宿主机通信,从而达到在虚拟机上执行命令,把宿主机上的文件上传到虚拟机等等操作,指定任务执行完成后,它会把临时虚拟机做清理,然后保存到指定虚拟机的磁盘文件上;通过这一系列的操作,最后我们就把指定文件打入到指定虚拟机的磁盘镜像文件中去了;后续我们就可以直接基于这个模板文件来创建或启动虚拟机,启动起来的虚拟机就会有我们事先打入到模板中的文件,程序包等等;