kickstart+virt-install安装虚拟机
本文提供的方法适用于在远程服务器的kvm+qemu环境中部署CentOS虚拟机。
#!/bin/bash
set -xe
diskimage=/var/lib/libvirt/images/centosvm.qcow2
echo "Creating qcow2 disk image.."
qemu-img create -f qcow2 -o preallocation=metadata $diskimage 40G
#fallocate -l `ls -al $diskimage | awk '{print $5}'` $diskimage
echo `ls -lash $diskimage`
# [2] Create a minimal kickstart file for Fedora
cat << EOF > fed.ks
install
text
reboot
lang en_US.UTF-8
keyboard us
network --bootproto dhcp
rootpw abc123
firewall --enabled --ssh
selinux --enforcing
network --bootproto=static --ip=192.168.122.100 --netmask=255.255.255.0 --gateway=192.168.122.1 --nameserver=223.5.5.5,223.6.6.6 --device=eth0
timezone --utc Asia/Shanghai
bootloader --location=mbr --append="console=tty0 console=ttyS0,115200 rd_NO_PLYMOUTH"
zerombr
clearpart --all --initlabel
autopart
%packages
@core
net-tools
git
emacs
mtr
%end
EOF
# [3] Create the guest
virt-install --connect=qemu:///system \
--network network=default \
--initrd-inject=./fed.ks \
--extra-args="ks=file:/fed.ks console=tty0 console=ttyS0,115200 serial rd_NO_PLYMOUTH" \
--name=test \
--disk path=$diskimage,format=qcow2,cache=none \
--ram 512 \
--vcpus=2 \
--check-cpu \
--accelerate \
--os-type linux \
--os-variant centos7.0 \
--cpuset auto \
--hvm \
--location=CentOS-7-x86_64-Minimal-1804.iso \
--nographics