-
Set variables
base="$(readlink -f "$(dirname "$0")")"
vps="$1"
ip="$2"
root="$3"
size="$4"
memory="$5"
vcpu="$6"
max_cpu=8
max_memory=16384000
id=$(echo ${vps}|sed s#"^\(qs\|windows\|linux\|vps\)\([0-9]*\)$"#"\2"#g)
Verify $id is numeric and non-empty before continuing.
-
Generate MAC address
prefix="00:16:3E"
s="$(printf "%06s" $(echo "obase=16; $id"|bc)|sed s#" "#"0"#g)"
mac="${prefix}:${s:0:2}:${s:2:2}:${s:4:2}"
Verify $mac matches ^([0-9A-F]{2}:){5}[0-9A-F]{2}$ before continuing.
-
Detect storage pool type
pool="$(virsh pool-dumpxml vz 2>/dev/null | grep '<type>' | sed 's/.*<type>\(.*\)<\/type>.*/\1/')"
If virsh pool-dumpxml vz fails, run ./create_libvirt_storage_pools.sh first.
-
Create storage volume
LVM (pool = logical):
${base}/vps_kvm_lvmcreate.sh ${vps} ${size}
device="/dev/vz/${vps}"
ZFS (pool = zfs):
zfs create vz/${vps}
device="/vz/${vps}/os.qcow2"
qemu-img create -f qcow2 -o preallocation=metadata $device ${size}M
Verify [ -e "$device" ] before continuing.
-
Build libvirt XML from template
cp ${base}/windows.xml /tmp/${vps}.xml
sed -i s#"windows"#"${vps}"#g /tmp/${vps}.xml
sed -i s#"<mac address='.*'"#"<mac address='$mac'"#g /tmp/${vps}.xml
sed -i s#"<parameter name='IP' value.*/>"#"<parameter name='IP' value='$ip'/>"#g /tmp/${vps}.xml
sed -i s#"<source dev='/dev/vz/windows'"#"<source dev='$device'"#g /tmp/${vps}.xml
sed -i s#"<\(vcpu.*\)>.*</vcpu>"#"<vcpu placement='static' current='$vcpu'>$max_cpu</vcpu>"#g /tmp/${vps}.xml
sed -i s#"<memory.*memory>"#"<memory unit='KiB'>$memory</memory>"#g /tmp/${vps}.xml
sed -i s#"<currentMemory.*currentMemory>"#"<currentMemory unit='KiB'>$memory</currentMemory>"#g /tmp/${vps}.xml
Verify virsh define /tmp/${vps}.xml exits 0 before continuing.
-
Register VM and set resource limits
virsh define /tmp/${vps}.xml
virsh autostart ${vps}
virsh schedinfo ${vps} --set cpu_shares=$((vcpu * 1024))
-
Update DHCP
DHCPVPS="/etc/dhcp/dhcpd.vps"
[ ! -f "$DHCPVPS" ] && DHCPVPS="/etc/dhcpd.vps"
cp $DHCPVPS ${DHCPVPS}.backup
grep -v -e "host ${vps} " -e "fixed-address $ip;" ${DHCPVPS}.backup > $DHCPVPS
echo "host ${vps} { hardware ethernet $mac; fixed-address $ip; }" >> $DHCPVPS
Verify the new entry exists: grep -q "host ${vps}" $DHCPVPS
-
Rebuild ebtables firewall
${base}/run_buildebtables.sh
Check /tmp/buildeb.err is empty; if not, inspect and re-run.
-
Set up VNC
vnc="$(grep "^$id:" ${base}/vps.vncmap | cut -d: -f2)"
${base}/provirted.phar vnc setup $vps $vnc
Verify virsh vncdisplay $vps returns a display number.
-
Start VM
virsh start ${vps}
virsh dominfo ${vps}