djbon2112

vminstall

Apr 23rd, 2014
313
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 9.09 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # vminstall: Installs and configures a virtual machine
  4.  
  5. # vminstall will install and configure a Debian virtual machine using deootstrap, onto a series of logical volumes
  6. # as defined in a static configuration. A number of template files are used to provide kickstarting of the config-
  7. # uration of the VMs.
  8.  
  9. V=0.10  # Initial version, provides a basic installation onto a fixed set of logical volumes using debootstrap with
  10.     # a minimal Debian configuration; 64-bit is default.
  11. V=0.11  # Added random temporary root password generation.
  12.  
  13. #
  14. # BEGIN HEADER
  15. #
  16.  
  17. # Define static configuration in /etc/default/vminstall
  18. #   VOLUME_GROUP: full path to the VM volume group
  19. #   TEMPLATE_DIR: full path to the tempates directory
  20. #   PUBLIC_BRIDGE: bridge interface (e.g. vmbr0) for the VM public interfaces
  21. #   LOCAL_BRIDGE: bridge interface (e.g. vmbr1) for the VM local interfaces
  22. #   MASTER_SERVER: FQDN to the master server for configuration management
  23. #   MASTER_AUTH: Auth credentials for HTTP access to certificates on master server
  24. source /etc/default/vminstall
  25.  
  26. # Define the echoe function, which echoes to STDERR, e.g. 'echoe "Info" "Creating logical volume..."'
  27. function echoe {
  28.     echo -n "vminstall: " 1>&2
  29.     echo -n $1 1>&2
  30.     echo -n ": " 1>&2
  31.     echo -n $2 1>&2
  32.     echo 1>&2
  33. }
  34.  
  35. # Define the fail function, which echoes to STDERR and quits, e.g. 'fail "Something is wrong."'
  36. function fail {
  37.     echo -n "vminstall: " 1>&2
  38.     echo -n "Fatal Error" 1>&2
  39.     echo -n ": " 1>&2
  40.     echo -n $1 1>&2
  41.     echo 1>&2
  42.     exit 1
  43. }
  44.  
  45. #
  46. # END HEADER
  47. #
  48.  
  49. #
  50. # BEGIN BODY
  51. #
  52.  
  53. # Get the FQDN of the VM from the command line
  54. if [ $1 ]; then
  55.     VM_FQDN=$1
  56.  
  57.     # Get the domain name from the FQDN on the command line
  58.     VM_NAME=$( echo $VM_FQDN | awk -F. '{ print $1 }' )
  59.     VM_DOMAIN=$( echo $VM_FQDN | awk -F. '{$1="";OFS="." ; print $0}' | sed 's/^.//' )
  60.  
  61.     echoe "Info" "Creating new VM $VM_FQDN."
  62. else
  63.     fail "A fully-qualified VM name must be specified, e.g. vminstall ns1.bonilan.net"
  64. fi
  65.  
  66. # Get the public and local IP addresses for the VM from DNS
  67. VM_PUBLICIP=$( host $VM_FQDN | grep -v 'IPv6\|NXDOMAIN' | awk '{ print $4 }' )
  68. if [ $VM_PUBLICIP ]; then
  69.     echoe "Info" "$VM_FQDN has IP address $VM_PUBLICIP"
  70. else
  71.     fail "$VM_FQDN not found in DNS."
  72. fi
  73. VM_LOCALIP=$( host $VM_NAME.local | grep -v 'IPv6\|NXDOMAIN' | awk '{ print $4 }' )
  74. if [ $VM_LOCALIP ]; then
  75.     echoe "Info" "$VM_NAME.local has IP address $VM_LOCALIP"
  76. else
  77.     echoe "Warning" "$VM_NAME.local not found in DNS; VM will only have public interface"
  78. fi
  79.  
  80. # Get the public and local netmasks and public gateway for the VM from the HV host
  81. #
  82. #  HUGE HACK!
  83. #  I'm making an assumption that, at install, the VM will have the same IPs on the bridge
  84. #  that the hypervisor itself has. I.E., if the HV has 10.1.2.3/24 on vmbr0, then the VM
  85. #  will probably have a 10.1.2.X/24 address on vmbr0. This is how I obtain the netmasks and
  86. #  gateway. While an alternative might be better, this should work for 95% of use cases.
  87. #
  88. HV_NAME=$( echo `hostname` | awk -F. '{ print $1 }' )
  89. HV_DOMAIN=$( echo `hostname` | awk -F. '{$1="";OFS="." ; print $0}' | sed 's/^.//' )
  90. HV_PUBLICGATEWAY=$( ip route | grep default | awk '{ print $3 }' )
  91. HV_PUBLICIP=$( host ${HV_NAME}.${HV_DOMAIN} | grep -v 'IPv6\|NXDOMAIN' | awk '{ print $4 }' )
  92. HV_PUBLICNETMASK=$( \
  93.     ifconfig | \
  94.     grep -A8 $PUBLIC_BRIDGE | \
  95.     grep $HV_PUBLICIP | \
  96.     awk '{ print $4 }' | cut -d':' -f2 )
  97. HV_LOCALIP=$( host ${HV_NAME}.local | grep -v 'IPv6\|NXDOMAIN' | awk '{ print $4 }' )
  98. HV_LOCALNETMASK=$( \
  99.     ifconfig | \
  100.     grep -A8 $LOCAL_BRIDGE | \
  101.     grep $HV_LOCALIP | \
  102.     awk '{ print $4 }' | cut -d':' -f2 )
  103. # Now assign the HV values to the VM
  104. VM_PUBLICGATEWAY="$HV_PUBLICGATEWAY"
  105. VM_PUBLICNETMASK="$HV_PUBLICNETMASK"
  106. VM_LOCALNETMASK="$HV_LOCALNETMASK"
  107.  
  108. # Make sure we have debootstrap
  109. test -x /usr/sbin/debootstrap \
  110.     && echoe "Info" "Debootstrap found, continuing." \
  111.     || fail "Debootstrap is required for vminstall."
  112.  
  113. # Make sure the volume group exists
  114. test -d /dev/vmvg \
  115.     && echoe "Info" "Volume group $VOLUME_GROUP exists..." \
  116.     || fail "Volume group $VOLUME_GROUP does not exist."
  117.  
  118. # Get the size and free space of the volume group in GB; warn if less than 50 GB
  119. VG_FREE="$(vgdisplay --short --units G $VOLUME_GROUP | awk '{ print $8 }' | cut -d'.' -f1)"
  120. echoe "Info" "Volume group $VOLUME_GROUP has $VG_FREE GB free."
  121. if [ $VG_FREE -lt 50 ]; then
  122.     echoe "Warning" "Volume group $VOLUME_GROUP has less than 50 GB free; VM installation may fail."
  123.     echo -n "Continue? (y/n) "
  124.     read C
  125.     if ! [ $C = "y" ]; then
  126.         fail "Aborted"
  127.     fi
  128. fi
  129.  
  130. # Create the new logical volumes
  131. echoe "Info" "Creating logical volumes for $VM_FQDN..."
  132. lvcreate -L 2G -n ${VM_NAME}_root $VOLUME_GROUP \
  133.     && lvcreate -L 4G -n ${VM_NAME}_usr $VOLUME_GROUP \
  134.     && lvcreate -L 4G -n ${VM_NAME}_var $VOLUME_GROUP \
  135.     && lvcreate -L 4G -n ${VM_NAME}_log $VOLUME_GROUP \
  136.     && lvcreate -L 4G -n ${VM_NAME}_swap $VOLUME_GROUP \
  137.     && echoe "Info" "Done." \
  138.     || fail "Failed to create one or more logical volumes."
  139.  
  140. # Make ext4 filesystems on the new logical volumes
  141. echoe "Info" "Creating filesystems on new logical volumes..."
  142. VM_FSROOT="$VOLUME_GROUP/${VM_NAME}_root"
  143. VM_FSUSR="$VOLUME_GROUP/${VM_NAME}_usr"
  144. VM_FSVAR="$VOLUME_GROUP/${VM_NAME}_var"
  145. VM_FSLOG="$VOLUME_GROUP/${VM_NAME}_log"
  146. VM_FSSWAP="$VOLUME_GROUP/${VM_NAME}_swap"
  147. mkfs.ext4 $VM_FSROOT &>/dev/null \
  148.     && mkfs.ext4 $VM_FSUSR &>/dev/null \
  149.     && mkfs.ext4 $VM_FSVAR &>/dev/null \
  150.     && mkfs.ext4 $VM_FSLOG &>/dev/null \
  151.     && mkswap $VM_FSSWAP &>/dev/null \
  152.     && echoe "Info" "Done." \
  153.     || fail "Failed to create one or more filesystems."
  154.  
  155. # Make a temporary directory to mount the filesystems
  156. VM_TMPDIR=$(mktemp -d)
  157.  
  158. # Mount the LVs to the tmpdir, creating subdirs as needed
  159. echoe "Info" "Mounting new logical volumes under $VM_TMPDIR."
  160. mount $VM_FSROOT $VM_TMPDIR \
  161.     || fail "Failed to mount $VM_FSROOT on $VM_TMPDIR."
  162. mkdir $VM_TMPDIR/usr \
  163.     && mount $VM_FSUSR $VM_TMPDIR/usr \
  164.     || fail "Failed to mount $VM_FSUSR on $VM_TMPDIR/usr."
  165. mkdir $VM_TMPDIR/var \
  166.     && mount $VM_FSVAR $VM_TMPDIR/var \
  167.     || fail "Failed to mount $VM_FSVAR on $VM_TMPDIR/var."
  168. mkdir $VM_TMPDIR/var/log \
  169.     && mount $VM_FSLOG $VM_TMPDIR/var/log \
  170.     || fail "Failed to mount $VM_FSLOG on $VM_TMPDIR/var/log."
  171.  
  172. # Debootstrap a Debian Wheezy installation to the TMPDIR
  173. echoe "Info" "Performing debootstrap installation..."
  174. debootstrap \
  175.     --include=less,vim,sudo,openssh-server,bcfg2,curl,linux-image-amd64 \
  176.     wheezy $VM_TMPDIR http://debian.mirror.rafal.ca/debian/ \
  177.     && echoe "Info" "Done." \
  178.     || fail "Failed to install base system under $VM_TMPDIR."
  179.  
  180. # Set up the template files
  181. echoe "Info" "Configuring template files..."
  182. #
  183. # Network interfaces
  184. echoe "Info" "/etc/network/interfaces"
  185. cp $TEMPLATE_DIR/interfaces $VM_TMPDIR/etc/network/interfaces
  186. sed --in-place "s/X.X.X.X/${VM_PUBLICIP}/g"         $VM_TMPDIR/etc/network/interfaces
  187. sed --in-place "s/X.X.X.N/${VM_PUBLICNETMASK}/g"    $VM_TMPDIR/etc/network/interfaces
  188. sed --in-place "s/X.X.X.G/${VM_PUBLICGATEWAY}/g"    $VM_TMPDIR/etc/network/interfaces
  189. sed --in-place "s/Y.Y.Y.Y/${VM_LOCALIP}/g"      $VM_TMPDIR/etc/network/interfaces
  190. sed --in-place "s/Y.Y.Y.N/${VM_LOCALNETMASK}/g"     $VM_TMPDIR/etc/network/interfaces
  191. #
  192. # fstab
  193. echoe "Info" "/etc/fstab"
  194. cp $TEMPLATE_DIR/fstab $VM_TMPDIR/etc/fstab
  195. #
  196. # hostname
  197. echoe "Info" "/etc/hostname"
  198. echo $VM_FQDN > $VM_TMPDIR/etc/hostname
  199. #
  200. # Bcfg2 config
  201. echoe "Info" "/etc/bcfg2.conf"
  202. cp $TEMPLATE_DIR/bcfg2.conf $VM_TMPDIR/etc/bcfg2.conf
  203. sed --in-place "s/FQDN/${VM_FQDN}/g"            $VM_TMPDIR/etc/bcfg2.conf
  204. #
  205. # pygrub config
  206. echoe "Info" "/boot/grub/menu.lst"
  207. mkdir --parents $VM_TMPDIR/boot/grub
  208. cp $TEMPLATE_DIR/menu.lst $VM_TMPDIR/boot/grub/
  209. echoe "Info" "Done."
  210.  
  211. # Pull the certificate from the master server
  212. echoe "Info" "Installing certificates..."
  213. mkdir --parents $VM_TMPDIR/etc/ssl
  214. curl --fail --output $VM_TMPDIR/etc/ssl/${VM_FQDN}.tgz \
  215.     http://${MASTER_AUTH}@${MASTER_SERVER}/certs/${VM_FQDN}.tgz \
  216.     && echoe "Info" "Downloaded SSL certificates from master server." \
  217.     || echoe "Error" "Failed to download SSL certificates from master server."
  218. tar --extract --gzip --file $VM_TMPDIR/etc/ssl/${VM_FQDN}.tgz --directory $VM_TMPDIR/etc/ssl/ \
  219.     && echoe "Info" "Extracted SSL certificates." \
  220.     || echoe "Warning" "Failed to extract SSL certificates."
  221. echoe "Info" "Done."
  222.  
  223. # Set up the root password
  224. echoe "Info" "Setting root password."
  225. VM_TMPROOTPW=$(pwgen -s 8 1)
  226. echo "root:${VM_TMPROOOTPW}" | chpasswd --root $VM_TMPDIR
  227.  
  228. # Clean up the mounted directories
  229. umount $VM_TMPDIR/var/log
  230. umount $VM_TMPDIR/var
  231. umount $VM_TMPDIR/usr
  232. umount $VM_TMPDIR
  233. echoe "Info" "Unmounted temporary directories."
  234.  
  235. # Prepare the VM configuration file
  236. echoe "Info" "Preparing VM configuration file at /etc/xen/domains/$VM_NAME with autostart."
  237. cp $TEMPLATE_DIR/vm.template /etc/xen/domains/$VM_NAME
  238. sed --in-place "s/NAME/${VM_NAME}/g" /etc/xen/domains/$VM_NAME
  239. ln -s /etc/xen/domains/$VM_NAME /etc/xen/auto/$VM_NAME
  240. echoe "Info" "Default VM configuration installed; edit the configuration to specify more RAM or vCPUs."
  241.  
  242. # Done
  243. echoe "Info" "VM installation for $VM_FQDN finished. Root password for login is ${VM_TMPROOTPW}."
  244.  
  245. #
  246. # END BODY
  247. #
Advertisement
Add Comment
Please, Sign In to add comment