1272234Sgjb#!/bin/sh
2272234Sgjb#-
3278505Sgjb# Copyright (c) 2014, 2015 The FreeBSD Foundation
4272234Sgjb# All rights reserved.
5272234Sgjb#
6272234Sgjb# This software was developed by Glen Barber under sponsorship
7272234Sgjb# from the FreeBSD Foundation.
8272234Sgjb#
9272234Sgjb# Redistribution and use in source and binary forms, with or without
10272234Sgjb# modification, are permitted provided that the following conditions
11272234Sgjb# are met:
12272234Sgjb# 1. Redistributions of source code must retain the above copyright
13272234Sgjb#    notice, this list of conditions and the following disclaimer.
14272234Sgjb# 2. Redistributions in binary form must reproduce the above copyright
15272234Sgjb#    notice, this list of conditions and the following disclaimer in the
16272234Sgjb#    documentation and/or other materials provided with the distribution.
17272234Sgjb#
18272234Sgjb# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19272234Sgjb# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20272234Sgjb# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21272234Sgjb# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22272234Sgjb# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23272234Sgjb# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24272234Sgjb# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25272234Sgjb# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26272234Sgjb# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27272234Sgjb# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28272234Sgjb# SUCH DAMAGE.
29272234Sgjb#
30272234Sgjb# mk-vmimage.sh: Create virtual machine disk images in various formats.
31272234Sgjb#
32272234Sgjb# $FreeBSD$
33272234Sgjb#
34272234Sgjb
35277458Sgjbusage() {
36277458Sgjb	echo "${0} usage:"
37277458Sgjb	echo "${@}"
38277458Sgjb	return 1
39277458Sgjb}
40277458Sgjb
41274134Sgjbmain() {
42274134Sgjb	local arg
43278502Sgjb	VMCONFIG="/dev/null"
44274134Sgjb	while getopts "C:c:d:f:i:o:s:S:" arg; do
45274134Sgjb		case "${arg}" in
46274134Sgjb			C)
47274134Sgjb				VMBUILDCONF="${OPTARG}"
48274134Sgjb				;;
49274134Sgjb			c)
50274134Sgjb				VMCONFIG="${OPTARG}"
51274134Sgjb				;;
52274134Sgjb			d)
53274134Sgjb				DESTDIR="${OPTARG}"
54274134Sgjb				;;
55274134Sgjb			f)
56274134Sgjb				VMFORMAT="${OPTARG}"
57274134Sgjb				;;
58274134Sgjb			i)
59277458Sgjb				VMBASE="${OPTARG}"
60274134Sgjb				;;
61274134Sgjb			o)
62274134Sgjb				VMIMAGE="${OPTARG}"
63274134Sgjb				;;
64274134Sgjb			s)
65274134Sgjb				VMSIZE="${OPTARG}"
66274134Sgjb				;;
67274134Sgjb			S)
68274134Sgjb				WORLDDIR="${OPTARG}"
69274134Sgjb				;;
70274134Sgjb			*)
71274134Sgjb				;;
72274134Sgjb		esac
73272234Sgjb	done
74274134Sgjb	shift $(( ${OPTIND} - 1))
75272234Sgjb
76274134Sgjb	if [ -z "${VMBASE}" -o \
77274134Sgjb		-z "${WORLDDIR}" -o \
78274134Sgjb		-z "${DESTDIR}" -o \
79274134Sgjb		-z "${VMSIZE}" -o \
80278502Sgjb		-z "${VMIMAGE}" ];
81274134Sgjb	then
82278502Sgjb		usage || exit 0
83272380Sgjb	fi
84272380Sgjb
85274134Sgjb	if [ -z "${VMBUILDCONF}" ] || [ ! -e "${VMBUILDCONF}" ]; then
86274134Sgjb		echo "Must provide the path to vmimage.subr."
87274134Sgjb		return 1
88272234Sgjb	fi
89272234Sgjb
90274134Sgjb	. "${VMBUILDCONF}"
91274134Sgjb
92278502Sgjb	if [ ! -z "${VMCONFIG}" ] && [ ! -c "${VMCONFIG}" ]; then
93274134Sgjb		. "${VMCONFIG}"
94272234Sgjb	fi
95272234Sgjb
96281876Sgjb	case ${TARGET}:${TARGET_ARCH} in
97281876Sgjb		arm64:aarch64)
98281876Sgjb			ROOTLABEL="ufs"
99281876Sgjb			NOSWAP=1
100281876Sgjb			;;
101281876Sgjb		*)
102281876Sgjb			ROOTLABEL="gpt"
103281876Sgjb			;;
104281876Sgjb	esac
105281876Sgjb
106277458Sgjb	vm_create_base
107274134Sgjb	vm_install_base
108274134Sgjb	vm_extra_install_base
109274134Sgjb	vm_extra_install_packages
110274134Sgjb	vm_extra_install_ports
111274134Sgjb	vm_extra_enable_services
112274134Sgjb	vm_extra_pre_umount
113277458Sgjb	vm_extra_pkg_rmcache
114277458Sgjb	cleanup
115280299Scperciva	vm_copy_base
116277458Sgjb	vm_create_disk || return 0
117274134Sgjb	vm_extra_create_disk
118272234Sgjb
119272234Sgjb	return 0
120272234Sgjb}
121272234Sgjb
122272234Sgjbmain "$@"
123