[BACK]Return to ldom-load CVS log [TXT][DIR] Up to [local] / openbsd / ldom

File: [local] / openbsd / ldom / ldom-load (download)

Revision 1.3, Fri Jun 5 22:56:49 2020 UTC (3 years, 10 months ago) by afresh1
Branch: MAIN
CVS Tags: HEAD
Changes since 1.2: +8 -1 lines

Validate factory-default config is current before backing up

#!/bin/sh
# $AFresh1: ldom-load,v 1.3 2020/06/05 22:56:49 afresh1 Exp $
set -e
conf=$1

factory_default_tgz=/var/backups/ldom-factory-default.tar.gz

# Copyright (c) 2020 Andrew Hewus Fresh <andrew@afresh1.com>
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

if [ -z "$conf" ]; then
	echo "Usage: $0 ldom.conf" >&2
	exit 1
fi

if [ ! -e "$conf" ]; then
	echo "Config '$conf' not found!" >&2
	exit 2
fi

if [ "${conf%.conf}" = "$conf" ]; then
	echo "Config file '$conf' should end in '.conf'" >&2
	exit 3
fi

conf=$( readlink -f $conf )
domain=$( basename "$conf" .conf )
[ "$domain" = ldom ] && domain=openbsd

ldomctl init-system -n "$conf"

OLDPWD="$PWD"
TMPDIR=''
cleanup() {
	cd "$OLDPWD"
	[ "$TMPDIR" -a -e "$TMPDIR" ] && rm -rf "$TMPDIR"
}
TMPDIR=$( mktemp -t -d ldom-load-XXXXXXXXX )
trap cleanup EXIT INT QUIT TERM

cd $TMPDIR

if [ ! -e "$factory_default_tgz" ]; then
	current=$( ldomctl list | sed -ne 's/  *\[current\].*$//p' )
	if [ "$current" != factory-default ]; then
		echo "Current config is not factory-default!" >&2
		echo "  Please create $factory_default_tgz" >&2
		exit 4
	fi

	mkdir factory-default

	cd factory-default
	ldomctl dump
	cd ..

	tar czf "$factory_default_tgz" factory-default

	echo "Backed up ldom dump to $factory_default_tgz"
fi

tar -s "/^factory-default/$domain/" -xzpf "$factory_default_tgz"

cd "$domain"
ldomctl init-system "$conf"
cd ..

ldomctl list | grep -Fwq "$domain" && ldomctl delete "$domain"
ldomctl download "$domain"
ldomctl select   "$domain"
# ldomctl list

echo "Loaded $domain, halt and reset the hardware to boot configuration"