Skip to content

Instantly share code, notes, and snippets.

@jverdeyen
Created August 23, 2012 20:22
Show Gist options
  • Save jverdeyen/3441198 to your computer and use it in GitHub Desktop.
Save jverdeyen/3441198 to your computer and use it in GitHub Desktop.
KVM template
#!/bin/bash
name=beta
disk=4096
swap=1024
mem=4096
suite=precise
ip="192.168.199.199"
cpu=8
echo "root ${disk}" > partitions/$name.conf
echo "swap ${swap}" >> partitions/$name.conf
diskspace=$(( disk+swap ))
lvcreate -L "$diskspace"M -n $name vg0
dd if=/dev/zero of=/dev/vg0/$name.img bs=1M count=512
ubuntu-vm-builder kvm $suite \
-o \
--arch 'amd64' \
--mem=${mem} \
--cpus=${cpu} \
--kernel-flavour 'virtual' \
--hostname "${name}" \
--domain "${name}" \
--mirror 'http://mirror.hetzner.de/ubuntu/packages' \
--security-mirror 'http://mirror.hetzner.de/ubuntu/security' \
--components 'main, universe' \
--addpkg 'openssh-server' \
--addpkg 'vim' \
--addpkg 'acpid' \
--addpkg 'git-core' \
--addpkg 'rsync' \
--addpkg 'htop' \
--name 'Joeri Verdeyen' \
--user 'joeri' \
--pass 'joeri' \
--ip "${ip}" \
--mask '255.255.255.0' \
--net '192.168.199.0' \
--bcast '192.168.199.255' \
--gw '192.168.199.1' \
--libvirt=qemu:///system \
--firstboot=/home/joeri/vm-setups/scripts/firstboot.sh \
--copy=/home/joeri/vm-setups/scripts/copy-files \
--timezone="Europe/Berlin" \
--tmpfs=- \
--raw=/dev/mapper/vg0-$name \
--part=/home/joeri/vm-setups/partitions/$name.conf
xmlpath="/etc/libvirt/qemu/$name.xml"
echo "sleeping for 5 seconds, and setting the virtio driver an no cache"
sleep 5
sudo sed -i "s/bus='ide'/bus='virtio'/g" $xmlpath
sudo sed -i "s/type='raw'/type='raw'\ cache='none'/g" $xmlpath
sudo sed -i "s/<address\ type='drive'\ controller='0'\ bus='0'\ unit='0'\/>/\ /g" $xmlpath
sleep 1
echo "define $xmlpath"
sudo virsh define $xmlpath
exit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment