如何完全自动化无人值守的virt-install?

时间:2016-10-04 20:57:36

标签: virtualization qemu kvm

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

我已经用Google搜索过,很难找到这样做的例子。我发现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"

这是preseed.cfg文件(我在Web和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

毕竟,当我执行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.

它只是挂起。如果我^Z进入后台并启动virsh,我会看到vm处于运行状态。

我认为我很接近,但需要修复它以便:

  1. 安装显示完整且virt-install返回shell。
  2. 新虚拟机已关闭,我已经准备就绪了。
  3. 我认为#2可以在preseed.cfg文件中使用某种清理指令完成(仍在研究这个),但是非常感谢修复#1的任何帮助。

1 个答案:

答案 0 :(得分:0)

要拥有virt-install use a Kickstart file to initialize an operating system,您需要通过ks=参数指定--extra-args参数到内核:

--initrd-inject preseed.cfg \
--extra-args="ks=file:/preseed.cfg console=tty0 console=ttyS0,115200"

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

您还可以通过HTTP指定ks

--extra-args="ks=http://192.168.1.1/preseed.cfg"

或FTP:

--extra-args="ks=ftp://192.168.1.1/preseed.cfg"

或NFS:

--extra-args="ks=nfs:192.168.1.1:/preseed.cfg"