#!/bin/bash
#set -x

# PURPOSE: Create an automated minimal Fedora 21 guest with 40GB disk, 8GB
# memory and 8 vCPUs


# [1] Create the qcow2 disk image with preallocation and 'fallocate'(which
# pre-allocates all the blocks to a file) it for max. performance

diskimage=/var/lib/libvirt/images/f21vm.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 secret
firewall --enabled --ssh
selinux --enforcing
timezone --utc America/New_York
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=f21-vm \
    --disk path=$diskimage,format=qcow2,cache=none \
    --ram 8000 \
    --vcpus=8 \
    --check-cpu \
    --accelerate \
    --os-type linux \
    --os-variant fedora20 \
    --cpuset auto \
    --hvm \
    --location=http://dl.fedoraproject.org/pub/fedora/linux/development/21/x86_64/os/ \
    --nographics 
