template-config/vm-firstrun.sh

78 lines
2.5 KiB
Bash

#!/bin/bash
# /usr/local/sbin/vm-firstrun.sh
# This script depends on having these packages installed:
# - cloud-utils-growpart
# - parted
# This should be a simple bash file that sets the CFG_* variables
[ -f /etc/sysconfig/vm-firstrun ] && . /etc/sysconfig/vm-firstrun
## Set default values for anything that wasn't already set
# This will be run at firstboot to generate the hostname
CFG_HOSTNAME_SCRIPT="${CFG_HOSTNAME_SCRIPT:=uuidgen}"
# These can be set in config, there's just no reason for a default besides (blank)
# CFG_HOSTNAME_PREFIX="${CFG_HOSTNAME_PREFIX:=}"
# CFG_HOSTNAME_POSTFIX="${CFG_HOSTNAME_POSTFIX:=}"
CFG_DOMAIN="${CFG_DOMAIN:=localdomain}"
CFG_ROOTFS_DISK="${CFG_ROOTFS_DISK:=/dev/sda}"
CFG_ROOTFS_PARTITION="${CFG_ROOTFS_PARTITION:=3}"
CFG_ROOTFS_TYPE="${CFG_ROOTFS_TYPE:=xfs}"
CFG_SERVICE_NAME="${CFG_SERVICE_NAME:=vm-firstrun}"
## Helper Functions
function log() {
echo "$@" | logger --stderr -t vm-firstrun
}
## Do the things
old_hostname="$(hostname -f)"
new_hostname="${CFG_HOSTNAME_PREFIX}$(${CFG_HOSTNAME_SCRIPT})${CFG_HOSTNAME_POSTFIX}"
if ! [ -z "${CFG_DOMAIN}" ]; then
new_hostname="${new_hostname}.${CFG_DOMAIN}"
fi
log "Setting hostname from '${old_hostname}' to '${new_hostname}'"
hostnamectl set-hostname "${new_hostname}"
if [ -b "${CFG_ROOTFS_DISK}" ]; then
if [ -b "${CFG_ROOTFS_DISK}${CFG_ROOTFS_PARTITION}" ]; then
# expand the partition
log "Expanding rootfs partition"
growpart "${CFG_ROOTFS_DISK}" "${CFG_ROOTFS_PARTITION}"
# reload partition table
log "Reloading partition table"
partprobe "${CFG_ROOTFS_DISK}"
# grow the rootfs
case "${CFG_ROOTFS_TYPE}" in
xfs)
log "Growing rootfs of type: ${CFG_ROOTFS_TYPE}"
xfs_growfs /
;;
ext*)
log "Growing ${CFG_ROOTFS_TYPE} rootfs on device: ${CFG_ROOTFS_DISK}${CFG_ROOTFS_PARTITION}"
resize2fs "${CFG_ROOTFS_DISK}${CFG_ROOTFS_PARTITION}"
;;
*)
log "Unsupported rootfs type: ${CFG_ROOTFS_TYPE}"
log "Doing nothing, please grow the fs manually"
;;
esac
else
log "rootfs partition not found, unable to expand: ${CFG_ROOTFS_DISK}${CFG_ROOTFS_PARTITION}"
fi
else
log "rootfs disk not found, unable to expand: ${CFG_ROOTFS_DISK}"
fi
#Run update to get the latest packages
log "Updating latest packages"
yum update -y
log "updating done"
log "initial config done, disabling myself"
systemctl disable "${CFG_SERVICE_NAME}"