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/Linux%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接口和宿主機通信,從而達到在虛擬機上執行命令,把宿主機上的文件上傳到虛擬機等等操作,指定任務執行完成后,它會把臨時虛擬機做清理,然后保存到指定虛擬機的磁盤文件上;通過這一系列的操作,最后我們就把指定文件打入到指定虛擬機的磁盤鏡像文件中去了;后續我們就可以直接基於這個模板文件來創建或啟動虛擬機,啟動起來的虛擬機就會有我們事先打入到模板中的文件,程序包等等;