1278985Sgjb#!/bin/sh
2278985Sgjb#-
3278985Sgjb# Copyright (c) 2014 The FreeBSD Foundation
4278985Sgjb# All rights reserved.
5278985Sgjb#
6278985Sgjb# This software was developed by Glen Barber under sponsorship
7278985Sgjb# from the FreeBSD Foundation.
8278985Sgjb#
9278985Sgjb# Redistribution and use in source and binary forms, with or without
10278985Sgjb# modification, are permitted provided that the following conditions
11278985Sgjb# are met:
12278985Sgjb# 1. Redistributions of source code must retain the above copyright
13278985Sgjb#    notice, this list of conditions and the following disclaimer.
14278985Sgjb# 2. Redistributions in binary form must reproduce the above copyright
15278985Sgjb#    notice, this list of conditions and the following disclaimer in the
16278985Sgjb#    documentation and/or other materials provided with the distribution.
17278985Sgjb#
18278985Sgjb# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19278985Sgjb# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20278985Sgjb# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21278985Sgjb# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22278985Sgjb# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23278985Sgjb# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24278985Sgjb# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25278985Sgjb# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26278985Sgjb# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27278985Sgjb# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28278985Sgjb# SUCH DAMAGE.
29278985Sgjb#
30278985Sgjb# mk-vmimage.sh: Create virtual machine disk images in various formats.
31278985Sgjb#
32278985Sgjb# $FreeBSD$
33278985Sgjb#
34278985Sgjb
35278985Sgjbusage() {
36278985Sgjb	echo "${0} usage:"
37278985Sgjb	echo "${@}"
38278985Sgjb	return 1
39278985Sgjb}
40278985Sgjb
41278985Sgjbmain() {
42278985Sgjb	local arg
43278985Sgjb	VMCONFIG="/dev/null"
44278985Sgjb	while getopts "C:c:d:f:i:o:s:S:" arg; do
45278985Sgjb		case "${arg}" in
46278985Sgjb			C)
47278985Sgjb				VMBUILDCONF="${OPTARG}"
48278985Sgjb				;;
49278985Sgjb			c)
50278985Sgjb				VMCONFIG="${OPTARG}"
51278985Sgjb				;;
52278985Sgjb			d)
53278985Sgjb				DESTDIR="${OPTARG}"
54278985Sgjb				;;
55278985Sgjb			f)
56278985Sgjb				VMFORMAT="${OPTARG}"
57278985Sgjb				;;
58278985Sgjb			i)
59278985Sgjb				VMBASE="${OPTARG}"
60278985Sgjb				;;
61278985Sgjb			o)
62278985Sgjb				VMIMAGE="${OPTARG}"
63278985Sgjb				;;
64278985Sgjb			s)
65278985Sgjb				VMSIZE="${OPTARG}"
66278985Sgjb				;;
67278985Sgjb			S)
68278985Sgjb				WORLDDIR="${OPTARG}"
69278985Sgjb				;;
70278985Sgjb			*)
71278985Sgjb				;;
72278985Sgjb		esac
73278985Sgjb	done
74278985Sgjb	shift $(( ${OPTIND} - 1))
75278985Sgjb
76278985Sgjb	if [ -z "${VMBASE}" -o \
77278985Sgjb		-z "${WORLDDIR}" -o \
78278985Sgjb		-z "${DESTDIR}" -o \
79278985Sgjb		-z "${VMSIZE}" -o \
80278985Sgjb		-z "${VMIMAGE}" ];
81278985Sgjb	then
82278985Sgjb		usage || exit 0
83278985Sgjb	fi
84278985Sgjb
85278985Sgjb	if [ -z "${VMBUILDCONF}" ] || [ ! -e "${VMBUILDCONF}" ]; then
86278985Sgjb		echo "Must provide the path to vmimage.subr."
87278985Sgjb		return 1
88278985Sgjb	fi
89278985Sgjb
90278985Sgjb	. "${VMBUILDCONF}"
91278985Sgjb
92278985Sgjb	if [ ! -z "${VMCONFIG}" ] && [ ! -c "${VMCONFIG}" ]; then
93278985Sgjb		. "${VMCONFIG}"
94278985Sgjb	fi
95278985Sgjb
96278985Sgjb	vm_create_base
97278985Sgjb	vm_install_base
98278985Sgjb	vm_extra_install_base
99278985Sgjb	vm_extra_install_packages
100278985Sgjb	vm_extra_install_ports
101278985Sgjb	vm_extra_enable_services
102278985Sgjb	vm_extra_pre_umount
103278985Sgjb	vm_extra_pkg_rmcache
104278985Sgjb	cleanup
105282111Sgjb	vm_copy_base
106278985Sgjb	vm_create_disk || return 0
107278985Sgjb	vm_extra_create_disk
108278985Sgjb
109278985Sgjb	return 0
110278985Sgjb}
111278985Sgjb
112278985Sgjbmain "$@"
113