Skip to content

Instantly share code, notes, and snippets.

@anuaimi
Created November 18, 2013 14:29
Show Gist options
  • Save anuaimi/7528685 to your computer and use it in GitHub Desktop.
Save anuaimi/7528685 to your computer and use it in GitHub Desktop.
simple kvm/libvirt script
#!/bin/bash
# install kvm & create ubuntu guest
check_root() {
if [ "$(id -u)" != "0" ]; then
echo "This script must be run as root" 1>&2
return 1
fi
return 0
}
#
# make sure hw is sufficent
#
check_hw() {
# make sure have 64 bit OS
arch=`uname -m`
if [ "$arch" != 'x86_64' ]; then
echo "error: OS is not 64 bits"
return 1
fi
# make sure hw supports virtualization
info=`egrep '(vmx|svm)' --color=always /proc/cpuinfo`
if [ ${#info} == 0 ]; then
echo "error: CPU does not support virtualization"
return 1
fi
# this server meets requirements
return 0
}
install_kvm() {
# install lib-virt
sudo apt-get install -y ubuntu-virt-server ubuntu-vm-builder
sudo apt-get install -y virtinst
# add current ueser to libvirtd group
sudo adduser `id -un` libvirtd
sudo adduser `id -un` kvm
# need to relogin for changes to take affect
return 0
}
setup_bridge() {
# install bridge networking
sudo apt-get install -y bridge-utils
# set external interface to accept packets for other IPs
sudo ip link set eth0 promisc on
# create bridge
sudo brctl addbr br100
auto br100
iface br100 inet static
address 192.168.100.1
netmask 255.255.255.0
bridge_stp off
bridge_fd 0
# restart networking so bridge is running
sudo /etc/init.d/networking restart
return 0
}
#
# start here
#
if [ check_root ]; then
exit 1
fi
if [ check_hw == 1 ]; then
exit 1
fi
## CHECK IF WE HAVE ALREADY RUN STATE 1
# make sure we have the latest details of ubuntu repos
sudo apt-get update
if [ install_kvm ]; then
exit 1
fi
# mark state 1 done
echo "1" > ~/state.txt
#
# setup kvm & libvirt
#
# test that working
virsh -c qemu:///system list
#
# setup network
#
# need to turn on promisouc on eth0
sudo echo << EOF
auto br0
iface br0 inet static
address 192.168.0.10
network 192.168.0.0
netmask 255.255.255.0
broadcast 192.168.0.255
gateway 192.168.0.1
bridge_ports eth0
bridge_stp off
bridge_fd 0
bridge_maxwait 0
EOF
sudo /etc/init.d/newtorking stop
sudo /etc/init.d/networking start
#
# get guest running
#
# download cloud image
# from http://cloud-images.ubuntu.com/precise/current/
wget http://cloud-images.ubuntu.com/precise/current/precise-server-cloudimg-amd64.tar.gz
tarball=precise-server-cloudimg-amd64.tar.gz
contents=${tarball}.contents
tar -Sxvzf ${tarball} | tee "${contents}"
base=$(sed -n 's/.img$//p' "${contents}")
kernel=$(echo ${base}-vmlinuz-*)
floppy=${base}-floppy
img=${base}.img
qemu-img create -f qcow2 -b ${img} disk.img
kvm -fda ${floppy} -drive if=virtio,file=disk.img -boot a -vga std -k en-us -vnc :1
sudo ubuntu-vm-builder kvm precise \
--domain newvm \
--dest newvm \
--arch i386 \
--hostname vm1 \
--mem 1024 \
--user anuaimi \
--pass m0veF@wrd \
--ip 192.168.0.12 \
--mask 255.255.255.0 \
--net 192.168.0.0 \
--bcast 192.168.0.255 \
--gw 192.168.0.1 \
--dns 192.168.0.1 \
--components main,universe \
--addpkg acpid \
--addpkg vim \
--addpkg openssh-server \
--addpkg avahi-daemon \
--libvirt qemu:///system ;
vmbuilder kvm ubuntu \
--dest=/home/htkh/VirtualMachines \
--overwrite \
--mem=1024 \
--cpus=1 \
--rootsize=7168 \
--swapsize=1024 \
--ip=192.168.1.201 \
--mask=255.255.255.0 \
--bcast=192.168.1.255 \
--gw=192.168.1.254 \
--addpkg=openssh-server \
--addpkg=vim \
--addpkg=cron \
--addpkg=acpid \
--arch=amd64 \
--suite=lucid \
--flavour virtual \
--components main,universe,restricted \
--hostname MediaServer \
--user htkh \
--pass mypassword \
--libvirt qemu:///system \
--bridge=br0 ;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment