Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # vminstall: Installs and configures a virtual machine
- # vminstall will install and configure a Debian virtual machine using deootstrap, onto a series of logical volumes
- # as defined in a static configuration. A number of template files are used to provide kickstarting of the config-
- # uration of the VMs.
- V=0.10 # Initial version, provides a basic installation onto a fixed set of logical volumes using debootstrap with
- # a minimal Debian configuration; 64-bit is default.
- V=0.11 # Added random temporary root password generation.
- #
- # BEGIN HEADER
- #
- # Define static configuration in /etc/default/vminstall
- # VOLUME_GROUP: full path to the VM volume group
- # TEMPLATE_DIR: full path to the tempates directory
- # PUBLIC_BRIDGE: bridge interface (e.g. vmbr0) for the VM public interfaces
- # LOCAL_BRIDGE: bridge interface (e.g. vmbr1) for the VM local interfaces
- # MASTER_SERVER: FQDN to the master server for configuration management
- # MASTER_AUTH: Auth credentials for HTTP access to certificates on master server
- source /etc/default/vminstall
- # Define the echoe function, which echoes to STDERR, e.g. 'echoe "Info" "Creating logical volume..."'
- function echoe {
- echo -n "vminstall: " 1>&2
- echo -n $1 1>&2
- echo -n ": " 1>&2
- echo -n $2 1>&2
- echo 1>&2
- }
- # Define the fail function, which echoes to STDERR and quits, e.g. 'fail "Something is wrong."'
- function fail {
- echo -n "vminstall: " 1>&2
- echo -n "Fatal Error" 1>&2
- echo -n ": " 1>&2
- echo -n $1 1>&2
- echo 1>&2
- exit 1
- }
- #
- # END HEADER
- #
- #
- # BEGIN BODY
- #
- # Get the FQDN of the VM from the command line
- if [ $1 ]; then
- VM_FQDN=$1
- # Get the domain name from the FQDN on the command line
- VM_NAME=$( echo $VM_FQDN | awk -F. '{ print $1 }' )
- VM_DOMAIN=$( echo $VM_FQDN | awk -F. '{$1="";OFS="." ; print $0}' | sed 's/^.//' )
- echoe "Info" "Creating new VM $VM_FQDN."
- else
- fail "A fully-qualified VM name must be specified, e.g. vminstall ns1.bonilan.net"
- fi
- # Get the public and local IP addresses for the VM from DNS
- VM_PUBLICIP=$( host $VM_FQDN | grep -v 'IPv6\|NXDOMAIN' | awk '{ print $4 }' )
- if [ $VM_PUBLICIP ]; then
- echoe "Info" "$VM_FQDN has IP address $VM_PUBLICIP"
- else
- fail "$VM_FQDN not found in DNS."
- fi
- VM_LOCALIP=$( host $VM_NAME.local | grep -v 'IPv6\|NXDOMAIN' | awk '{ print $4 }' )
- if [ $VM_LOCALIP ]; then
- echoe "Info" "$VM_NAME.local has IP address $VM_LOCALIP"
- else
- echoe "Warning" "$VM_NAME.local not found in DNS; VM will only have public interface"
- fi
- # Get the public and local netmasks and public gateway for the VM from the HV host
- #
- # HUGE HACK!
- # I'm making an assumption that, at install, the VM will have the same IPs on the bridge
- # that the hypervisor itself has. I.E., if the HV has 10.1.2.3/24 on vmbr0, then the VM
- # will probably have a 10.1.2.X/24 address on vmbr0. This is how I obtain the netmasks and
- # gateway. While an alternative might be better, this should work for 95% of use cases.
- #
- HV_NAME=$( echo `hostname` | awk -F. '{ print $1 }' )
- HV_DOMAIN=$( echo `hostname` | awk -F. '{$1="";OFS="." ; print $0}' | sed 's/^.//' )
- HV_PUBLICGATEWAY=$( ip route | grep default | awk '{ print $3 }' )
- HV_PUBLICIP=$( host ${HV_NAME}.${HV_DOMAIN} | grep -v 'IPv6\|NXDOMAIN' | awk '{ print $4 }' )
- HV_PUBLICNETMASK=$( \
- ifconfig | \
- grep -A8 $PUBLIC_BRIDGE | \
- grep $HV_PUBLICIP | \
- awk '{ print $4 }' | cut -d':' -f2 )
- HV_LOCALIP=$( host ${HV_NAME}.local | grep -v 'IPv6\|NXDOMAIN' | awk '{ print $4 }' )
- HV_LOCALNETMASK=$( \
- ifconfig | \
- grep -A8 $LOCAL_BRIDGE | \
- grep $HV_LOCALIP | \
- awk '{ print $4 }' | cut -d':' -f2 )
- # Now assign the HV values to the VM
- VM_PUBLICGATEWAY="$HV_PUBLICGATEWAY"
- VM_PUBLICNETMASK="$HV_PUBLICNETMASK"
- VM_LOCALNETMASK="$HV_LOCALNETMASK"
- # Make sure we have debootstrap
- test -x /usr/sbin/debootstrap \
- && echoe "Info" "Debootstrap found, continuing." \
- || fail "Debootstrap is required for vminstall."
- # Make sure the volume group exists
- test -d /dev/vmvg \
- && echoe "Info" "Volume group $VOLUME_GROUP exists..." \
- || fail "Volume group $VOLUME_GROUP does not exist."
- # Get the size and free space of the volume group in GB; warn if less than 50 GB
- VG_FREE="$(vgdisplay --short --units G $VOLUME_GROUP | awk '{ print $8 }' | cut -d'.' -f1)"
- echoe "Info" "Volume group $VOLUME_GROUP has $VG_FREE GB free."
- if [ $VG_FREE -lt 50 ]; then
- echoe "Warning" "Volume group $VOLUME_GROUP has less than 50 GB free; VM installation may fail."
- echo -n "Continue? (y/n) "
- read C
- if ! [ $C = "y" ]; then
- fail "Aborted"
- fi
- fi
- # Create the new logical volumes
- echoe "Info" "Creating logical volumes for $VM_FQDN..."
- lvcreate -L 2G -n ${VM_NAME}_root $VOLUME_GROUP \
- && lvcreate -L 4G -n ${VM_NAME}_usr $VOLUME_GROUP \
- && lvcreate -L 4G -n ${VM_NAME}_var $VOLUME_GROUP \
- && lvcreate -L 4G -n ${VM_NAME}_log $VOLUME_GROUP \
- && lvcreate -L 4G -n ${VM_NAME}_swap $VOLUME_GROUP \
- && echoe "Info" "Done." \
- || fail "Failed to create one or more logical volumes."
- # Make ext4 filesystems on the new logical volumes
- echoe "Info" "Creating filesystems on new logical volumes..."
- VM_FSROOT="$VOLUME_GROUP/${VM_NAME}_root"
- VM_FSUSR="$VOLUME_GROUP/${VM_NAME}_usr"
- VM_FSVAR="$VOLUME_GROUP/${VM_NAME}_var"
- VM_FSLOG="$VOLUME_GROUP/${VM_NAME}_log"
- VM_FSSWAP="$VOLUME_GROUP/${VM_NAME}_swap"
- mkfs.ext4 $VM_FSROOT &>/dev/null \
- && mkfs.ext4 $VM_FSUSR &>/dev/null \
- && mkfs.ext4 $VM_FSVAR &>/dev/null \
- && mkfs.ext4 $VM_FSLOG &>/dev/null \
- && mkswap $VM_FSSWAP &>/dev/null \
- && echoe "Info" "Done." \
- || fail "Failed to create one or more filesystems."
- # Make a temporary directory to mount the filesystems
- VM_TMPDIR=$(mktemp -d)
- # Mount the LVs to the tmpdir, creating subdirs as needed
- echoe "Info" "Mounting new logical volumes under $VM_TMPDIR."
- mount $VM_FSROOT $VM_TMPDIR \
- || fail "Failed to mount $VM_FSROOT on $VM_TMPDIR."
- mkdir $VM_TMPDIR/usr \
- && mount $VM_FSUSR $VM_TMPDIR/usr \
- || fail "Failed to mount $VM_FSUSR on $VM_TMPDIR/usr."
- mkdir $VM_TMPDIR/var \
- && mount $VM_FSVAR $VM_TMPDIR/var \
- || fail "Failed to mount $VM_FSVAR on $VM_TMPDIR/var."
- mkdir $VM_TMPDIR/var/log \
- && mount $VM_FSLOG $VM_TMPDIR/var/log \
- || fail "Failed to mount $VM_FSLOG on $VM_TMPDIR/var/log."
- # Debootstrap a Debian Wheezy installation to the TMPDIR
- echoe "Info" "Performing debootstrap installation..."
- debootstrap \
- --include=less,vim,sudo,openssh-server,bcfg2,curl,linux-image-amd64 \
- wheezy $VM_TMPDIR http://debian.mirror.rafal.ca/debian/ \
- && echoe "Info" "Done." \
- || fail "Failed to install base system under $VM_TMPDIR."
- # Set up the template files
- echoe "Info" "Configuring template files..."
- #
- # Network interfaces
- echoe "Info" "/etc/network/interfaces"
- cp $TEMPLATE_DIR/interfaces $VM_TMPDIR/etc/network/interfaces
- sed --in-place "s/X.X.X.X/${VM_PUBLICIP}/g" $VM_TMPDIR/etc/network/interfaces
- sed --in-place "s/X.X.X.N/${VM_PUBLICNETMASK}/g" $VM_TMPDIR/etc/network/interfaces
- sed --in-place "s/X.X.X.G/${VM_PUBLICGATEWAY}/g" $VM_TMPDIR/etc/network/interfaces
- sed --in-place "s/Y.Y.Y.Y/${VM_LOCALIP}/g" $VM_TMPDIR/etc/network/interfaces
- sed --in-place "s/Y.Y.Y.N/${VM_LOCALNETMASK}/g" $VM_TMPDIR/etc/network/interfaces
- #
- # fstab
- echoe "Info" "/etc/fstab"
- cp $TEMPLATE_DIR/fstab $VM_TMPDIR/etc/fstab
- #
- # hostname
- echoe "Info" "/etc/hostname"
- echo $VM_FQDN > $VM_TMPDIR/etc/hostname
- #
- # Bcfg2 config
- echoe "Info" "/etc/bcfg2.conf"
- cp $TEMPLATE_DIR/bcfg2.conf $VM_TMPDIR/etc/bcfg2.conf
- sed --in-place "s/FQDN/${VM_FQDN}/g" $VM_TMPDIR/etc/bcfg2.conf
- #
- # pygrub config
- echoe "Info" "/boot/grub/menu.lst"
- mkdir --parents $VM_TMPDIR/boot/grub
- cp $TEMPLATE_DIR/menu.lst $VM_TMPDIR/boot/grub/
- echoe "Info" "Done."
- # Pull the certificate from the master server
- echoe "Info" "Installing certificates..."
- mkdir --parents $VM_TMPDIR/etc/ssl
- curl --fail --output $VM_TMPDIR/etc/ssl/${VM_FQDN}.tgz \
- http://${MASTER_AUTH}@${MASTER_SERVER}/certs/${VM_FQDN}.tgz \
- && echoe "Info" "Downloaded SSL certificates from master server." \
- || echoe "Error" "Failed to download SSL certificates from master server."
- tar --extract --gzip --file $VM_TMPDIR/etc/ssl/${VM_FQDN}.tgz --directory $VM_TMPDIR/etc/ssl/ \
- && echoe "Info" "Extracted SSL certificates." \
- || echoe "Warning" "Failed to extract SSL certificates."
- echoe "Info" "Done."
- # Set up the root password
- echoe "Info" "Setting root password."
- VM_TMPROOTPW=$(pwgen -s 8 1)
- echo "root:${VM_TMPROOOTPW}" | chpasswd --root $VM_TMPDIR
- # Clean up the mounted directories
- umount $VM_TMPDIR/var/log
- umount $VM_TMPDIR/var
- umount $VM_TMPDIR/usr
- umount $VM_TMPDIR
- echoe "Info" "Unmounted temporary directories."
- # Prepare the VM configuration file
- echoe "Info" "Preparing VM configuration file at /etc/xen/domains/$VM_NAME with autostart."
- cp $TEMPLATE_DIR/vm.template /etc/xen/domains/$VM_NAME
- sed --in-place "s/NAME/${VM_NAME}/g" /etc/xen/domains/$VM_NAME
- ln -s /etc/xen/domains/$VM_NAME /etc/xen/auto/$VM_NAME
- echoe "Info" "Default VM configuration installed; edit the configuration to specify more RAM or vCPUs."
- # Done
- echoe "Info" "VM installation for $VM_FQDN finished. Root password for login is ${VM_TMPROOTPW}."
- #
- # END BODY
- #
Advertisement
Add Comment
Please, Sign In to add comment