From 00c78aa3242fd9d48c6495b4d9cd1e5473463405 Mon Sep 17 00:00:00 2001 From: Tamas Gerczei Date: Fri, 16 Oct 2015 13:12:27 +0200 Subject: [PATCH] initial commit --- README.md | 3 +- smartos.ipxe.tpl | 5 ++++ update_smartos_platform.sh | 59 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 66 insertions(+), 1 deletion(-) create mode 100644 smartos.ipxe.tpl create mode 100644 update_smartos_platform.sh diff --git a/README.md b/README.md index 5b9461f..6255242 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,3 @@ # smartos-netboot-updater -Automatically update the SmartOS platform served for netbooting +I run this script as a nightly cron job on my netboot server so that I always have a current SmartOS platform version to boot. +Inspired by Alain O'Dea's article @ http://blog.alainodea.com/en/ipxe-smartos diff --git a/smartos.ipxe.tpl b/smartos.ipxe.tpl new file mode 100644 index 0000000..198e15d --- /dev/null +++ b/smartos.ipxe.tpl @@ -0,0 +1,5 @@ +#!ipxe +# /var/lib/tftpboot/smartos.ipxe.tpl +kernel /smartos/$release/platform/i86pc/kernel/amd64/unix -B smartos=true +initrd /smartos/$release/platform/i86pc/amd64/boot_archive +boot diff --git a/update_smartos_platform.sh b/update_smartos_platform.sh new file mode 100644 index 0000000..966064a --- /dev/null +++ b/update_smartos_platform.sh @@ -0,0 +1,59 @@ +#!/usr/bin/env bash +# SmartOS platform image updater for netboot environments | tamas@gerczei.eu +ME=$(basename ${0%.sh}) +DESTDIR="/var/tmp/" +LOCATION="us-east" +FILENAME="platform-latest.tgz" +TFTPROOT="/var/lib/tftpboot" +URL="https://${LOCATION}.manta.joyent.com/Joyent_Dev/public/SmartOS/${FILENAME}" +REPLY=$(curl ${URL} -z ${DESTDIR:-/var/tmp}/${FILENAME} -o ${DESTDIR:-/var/tmp}/${FILENAME} -s -L -w %{http_code} -C - 2>/dev/null) + +case $REPLY in + 200) + # OK, new image downloaded + # extract it + tar xf ${DESTDIR:-/var/tmp}/${FILENAME} -C ${TFTPROOT}/smartos --transform 's!^platform-!!' 2>/dev/null + + if [ $? -ne 0 ] + then + # failed to extract, force the process to repeat next time and bail out + rm ${DESTDIR:-/var/tmp}/${FILENAME} + exit 1 + fi + + # determine which one it is + shopt -s extglob + LASTDIR="$(ls -dt ${TFTPROOT}/smartos/+([0-9])T+([0-9])Z | head -1)" + + # re-organize it slightly + mkdir ${LASTDIR}/platform + mv ${LASTDIR}/i86pc ${LASTDIR}/platform + + # set ownership + chown -R tftp:tftp ${LASTDIR} + + # generate iPXE configuration + TODAY="$(date '+%d%m%y')" + if [ -f ${TFTPROOT}/smartos.ipxe ] + then + # secure a copy of the previous configuration file + cp ${TFTPROOT}/smartos.ipxe ${TFTPROOT}/smartos.ipxe.${TODAY} + fi + sed -e "s/\$release/$(basename ${LASTDIR})/g" < ${TFTPROOT}/smartos.ipxe.tpl > ${TFTPROOT}/smartos.ipxe + + # housekeeping and logging + logger -t $ME -p user.debug created ${LASTDIR} and ${TFTPROOT}/smartos.ipxe + logger -t $ME -p user.debug removed $(find ${TFTPROOT}/smartos -maxdepth 1 -type d -mtime +${KEEPDAYS:-28} -printf '%f ' -exec rm -r {} \;) + ;; + + 304) + # NOT MODIFIED, no update + ;; + *) + # WTF + echo "server returned $REPLY, I cannot handle that" + exit 1 + ;; +esac + +exit 0