如何完全自动化无人值守的虚拟安装?

Los*_* Al 8 virtualization kvm qemu

首先我要说的是我想做的事情。我想以无人值守的方式完全自动化使用virt-install. 我知道有些人使用 GUI 工具来执行此操作,或者他们编辑预先存在的图像的 XML 描述,但我想从头开始。

我用谷歌搜索了一下,很难找到这样做的例子。我发现这是virt-install要使用的命令,并且它可以与附加的 TTY 控制台交互使用(您在安装过程中手动回答配置问题)。对于完全自动化的解决方案,您可以指定一个kickstart文件(通常是preseed.cfg)来提供您通常手动输入的问题的答案。kickstart 文件还可以指定要安装的附加软件、磁盘和网络配置等)。

我认为我已经完成了大部分工作,只是安装在安装开始后不久就挂起了。我认为这与是否需要(或不需要)将控制台连接到安装有关。这是virt-install我正在使用的命令:

virt-install --connect qemu:///system \
  --name vm --ram 128 \
  --disk path=./vm.qcow2,size=8,format=qcow2 \
  --location 'http://archive.ubuntu.com/ubuntu/dists/trusty/main/installer-amd64/' \
  --network user,model=virtio \
  --initrd-inject preseed.cfg \
  --extra-args="console=tty0 console=ttyS0,115200"
Run Code Online (Sandbox Code Playgroud)

这是这个preseed.cfg文件(我从网上和 Ubuntu 文档中的许多示例中抄袭的):

### Localization
# Locale sets language and country.
d-i debian-installer/locale string en_US
# Keyboard selection.
d-i keyboard-configuration/layoutcode string us
d-i keyboard-configuration/modelcode string pc105
d-i keyboard-configuration/variantcode string

### Network configuration
# netcfg will choose an interface that has link if possible. This makes it
# skip displaying a list if there is more than one interface.
d-i netcfg/choose_interface select auto
# Any hostname and domain names assigned from dhcp take precedence over
# values set here. However, setting the values still prevents the questions
# from being shown, even if values come from dhcp.
d-i netcfg/get_hostname string vm
d-i netcfg/get_domain string foobar.net
# Disable that annoying WEP key dialog.
d-i netcfg/wireless_wep string

### Mirror settings
d-i mirror/country string manual
d-i mirror/http/hostname string us.archive.ubuntu.com
d-i mirror/http/directory string /ubuntu
d-i mirror/http/proxy string

### Partitioning
# Encrypt your home directory?
d-i user-setup/encrypt-home boolean false
# Alternatively, you can specify a disk to partition. The device name
# can be given in either devfs or traditional non-devfs format.
d-i partman-auto/disk string /dev/vda
# In addition, you'll need to specify the method to use.
# The presently available methods are: "regular", "lvm" and "crypto"
d-i partman-auto/method string regular
# You can choose from any of the predefined partitioning recipes.
d-i partman-auto/choose_recipe select atomic
# This makes partman automatically partition without confirmation, provided
# that you told it what to do using one of the methods above.
d-i partman-partitioning/confirm_write_new_label boolean true
d-i partman/choose_partition select finish
d-i partman/confirm boolean true
d-i partman/confirm_nooverwrite boolean true

### Clock and time zone setup
# Controls whether or not the hardware clock is set to UTC.
d-i clock-setup/utc boolean true
# You may set this to any valid setting for $TZ; see the contents of
# /usr/share/zoneinfo/ for valid values.
d-i time/zone string UTC

### Account setup
# Skip creation of a root account (normal user account will be able to
# use sudo).
d-i passwd/root-login boolean false

# To create a normal user account.
d-i passwd/user-fullname string VMuser
d-i passwd/username string vmuser
# Normal user's password, either in clear text
# or encrypted using an MD5 hash.
d-i passwd/user-password-crypted password CRACKMECRACKM

# This is fairly safe to set, it makes grub install automatically to the MBR
# if no other operating system is detected on the machine.
d-i grub-installer/only_debian boolean true

### Package selection
d-i tasksel/first multiselect standard
# Individual additional packages to install
d-i pkgsel/include string openssh-server

### Finishing up the first stage install
# Avoid that last message about the install being complete.
d-i finish-install/reboot_in_progress note
# How do you want to manage upgrades on this system?
d-i pkgsel/update-policy select none
Run Code Online (Sandbox Code Playgroud)

毕竟,当我执行命令时,virt-install我看到:

WARNING  Unable to connect to graphical console: virt-viewer not installed. Please install the 'virt-viewer' package.
WARNING  No console to launch for the guest, defaulting to --wait -1

Starting install...
Retrieving file linux...                                                                                
Retrieving file initrd.gz...                                                                            
Allocating 'virtinst-linux.rCdX0h'                                                                      
Transferring virtinst-linux.rCdX0h                                                                      
Allocating 'virtinst-initrd.gz.BbRBMv'                                                                  
Transferring virtinst-initrd.gz.BbRBMv                                                                  
Creating domain...                                                                                      
Domain installation still in progress. Waiting for installation to complete.
Run Code Online (Sandbox Code Playgroud)

它就挂了。如果我^Z进入后台并启动,virsh我会看到虚拟机处于运行状态。

我想我已经很接近了,但需要修复它,以便:

  1. 安装显示完成并virt-install返回到 shell。
  2. 新的虚拟机已关闭,我剩下的映像文件已准备就绪。

preseed.cfg我认为#2 可以通过某种清理指令在文件中完成(仍在研究这一点),但修复#1 的任何帮助将不胜感激。

Dej*_*ton 6

virt-install 使用 Kickstart 文件来初始化操作系统,您需要ks=通过参数指定将参数传递给内核--extra-args

--initrd-inject preseed.cfg \
--extra-args="ks=file:/preseed.cfg console=tty0 console=ttyS0,115200"
Run Code Online (Sandbox Code Playgroud)

上面的示例将本地 Kickstart 文件注入来宾操作系统,用于自动安装。

您还可以ks通过 HTTP 指定:

--extra-args="ks=http://192.168.1.1/preseed.cfg"
Run Code Online (Sandbox Code Playgroud)

或FTP:

--extra-args="ks=ftp://192.168.1.1/preseed.cfg"
Run Code Online (Sandbox Code Playgroud)

或 NFS:

--extra-args="ks=nfs:192.168.1.1:/preseed.cfg"
Run Code Online (Sandbox Code Playgroud)