|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +set -e |
| 4 | +set -x |
| 5 | + |
| 6 | +setDefaults() { |
| 7 | + NETWORK=192.168.237.0 |
| 8 | + NETMASK=255.255.255.0 |
| 9 | + DHCPIP=192.168.237.1 |
| 10 | + LOWERIP=192.168.237.10 |
| 11 | + UPPERIP=192.168.237.250 |
| 12 | + VMNAME=network-boot |
| 13 | + OSTYPE=Linux |
| 14 | +} |
| 15 | + |
| 16 | +loadPrefs() { |
| 17 | + CONFIG=${_CONFIG:-~/.backupxe-vm} |
| 18 | + [ -f $CONFIG ] || return 0 |
| 19 | + . $CONFIG |
| 20 | +} |
| 21 | + |
| 22 | +savePrefs() { |
| 23 | + cat > $CONFIG << EOF |
| 24 | +VBOXNET=$VBOXNET |
| 25 | +NETWORK=$NETWORK |
| 26 | +LOWERIP=$LOWERIP |
| 27 | +UPPERIP=$UPPERIP |
| 28 | +VMNAME=$VMNAME |
| 29 | +OSTYPE=$OSTYPE |
| 30 | +EOF |
| 31 | +} |
| 32 | + |
| 33 | +parseCommandLine() { |
| 34 | + if ! options=$(getopt -o h,c:,n:,m:,i:,l:,u:,N:,t: -l help,config:,network,netmask,dhcp-server-ip:,dhcp-lower-ip:,dhcp-upper-ip,vm-name:,os-type -- "$@") |
| 35 | + then |
| 36 | + # something went wrong, getopt will put out an error message for us |
| 37 | + exit 1 |
| 38 | + fi |
| 39 | + |
| 40 | + eval set -- "$options" |
| 41 | + |
| 42 | + while [ $# -gt 0 ] ; do |
| 43 | + case $1 in |
| 44 | + -h|--help) usage ;; |
| 45 | + -c|--config) _CONFIG=$2 ; [ -f "$_CONFIG" ] || exit ; shift ;; |
| 46 | + -n|--dhcp-network) _NETWORK=$2 ; shift ;; |
| 47 | + -m|--dhcp-netmask) _NETMASK=$2 ; shift ;; |
| 48 | + -i|--dhcp-server-ip) _DHCPIP=$2 ; shift ;; |
| 49 | + -l|--dhcp-lower-ip) _LOWERIP=$2 ; shift ;; |
| 50 | + -u|--dhcp-upper-ip) _UPPERIP=$2 ; shift ;; |
| 51 | + -N|--name) _VMNAME=$2 ; shift ;; |
| 52 | + -t|--os-type) _OSTYPE=$2 ; shift ;; |
| 53 | + (--) shift; break;; |
| 54 | + (-*) echo "$(basename $0): error - unrecognized option $1" 1>&2; exit 1;; |
| 55 | + (*) break;; |
| 56 | + esac |
| 57 | + shift |
| 58 | + done |
| 59 | + |
| 60 | + setDefaults |
| 61 | + loadPrefs |
| 62 | + NETWORK=${_NETWORK:-$NETWORK} |
| 63 | + NETMASK=${_NETMASK:-$NETMASK} |
| 64 | + DHCPIP=${_DHCPIP:-$DHCPIP} |
| 65 | + LOWERIP=${_LOWERIP:-$LOWERIP} |
| 66 | + UPPERIP=${_UPPERIP:-$UPPERIP} |
| 67 | + VMNAME=${_VMNAME:-$VMNAME} |
| 68 | + OSTYPE=${_OSTYPE:-$OSTYPE} |
| 69 | +} |
| 70 | + |
| 71 | +createOrUpdateNetworkConfig() { |
| 72 | + VBoxManage list hostonlyifs | grep -q \ $VBOXNET$ || unset VBOXNET |
| 73 | + if [ -z "$VBOXNET" ] ; then |
| 74 | + VBOXNET=$(VBoxManage hostonlyif create | sed -r -n -e 's/.*(vboxnet[0-9]+).*/\1/p') |
| 75 | + [ -z "$VBOXNET" ] && exit 1 |
| 76 | + savePrefs |
| 77 | + VBoxManage hostonlyif ipconfig $VBOXNET --ip $NETWORK |
| 78 | + VBoxManage dhcpserver add --ifname $VBOXNET --ip $DHCPIP --netmask $NETMASK --lowerip $LOWERIP --upperip $UPPERIP --enable |
| 79 | + else |
| 80 | + VBoxManage hostonlyif ipconfig $VBOXNET --ip $NETWORK |
| 81 | + VBoxManage dhcpserver modify --ifname $VBOXNET --ip $DHCPIP --netmask $NETMASK --lowerip $LOWERIP --upperip $UPPERIP --enable |
| 82 | + fi |
| 83 | +} |
| 84 | + |
| 85 | +createOrUpdateVM() { |
| 86 | + VBoxManage showvminfo $VMNAME > /dev/null 2>&1 || VBoxManage createvm --name $VMNAME --ostype $OSTYPE --register |
| 87 | + VBoxManage modifyvm $VMNAME --hostonlyadapter1 $VBOXNET |
| 88 | + VBoxManage modifyvm $VMNAME --nic1 hostonly |
| 89 | + VBoxManage modifyvm $VMNAME --boot1 net |
| 90 | +} |
| 91 | + |
| 92 | +startVM() { |
| 93 | + VBoxManage startvm network-boot |
| 94 | +} |
| 95 | + |
| 96 | +main() { |
| 97 | + parseCommandLine "$@" |
| 98 | + createOrUpdateNetworkConfig |
| 99 | + createOrUpdateVM |
| 100 | + startVM |
| 101 | +} |
| 102 | + |
| 103 | +main "$@" |
0 commit comments