1#!/bin/sh
2#
3# This script generates a "memstick image" (image that can be copied to a
4# USB memory stick) from a directory tree.  Note that the script does not
5# clean up after itself very well for error conditions on purpose so the
6# problem can be diagnosed (full filesystem most likely but ...).
7#
8# Usage: make-memstick.sh <directory tree or manifest> <image filename>
9#
10#
11
12set -e
13
14if [ "$(uname -s)" = "FreeBSD" ]; then
15	PATH=/bin:/usr/bin:/sbin:/usr/sbin
16	export PATH
17fi
18
19if [ $# -ne 2 ]; then
20	echo "make-memstick.sh /path/to/directory/or/manifest /path/to/image/file"
21	exit 1
22fi
23
24MAKEFSARG=${1}
25
26if [ -f ${MAKEFSARG} ]; then
27	BASEBITSDIR=`dirname ${MAKEFSARG}`
28	METALOG=${MAKEFSARG}
29elif [ -d ${MAKEFSARG} ]; then
30	BASEBITSDIR=${MAKEFSARG}
31	METALOG=
32else
33	echo "${MAKEFSARG} must exist"
34	exit 1
35fi
36
37if [ -e ${2} ]; then
38	echo "won't overwrite ${2}"
39	exit 1
40fi
41
42echo '/dev/ufs/FreeBSD_Install / ufs ro,noatime 1 1' > ${BASEBITSDIR}/etc/fstab
43echo 'root_rw_mount="NO"' > ${BASEBITSDIR}/etc/rc.conf.local
44if [ -n "${METALOG}" ]; then
45	metalogfilename=$(mktemp /tmp/metalog.XXXXXX)
46	cat ${METALOG} > ${metalogfilename}
47	echo "./etc/fstab type=file uname=root gname=wheel mode=0644" >> ${metalogfilename}
48	echo "./etc/rc.conf.local type=file uname=root gname=wheel mode=0644" >> ${metalogfilename}
49	MAKEFSARG=${metalogfilename}
50fi
51makefs -D -N ${BASEBITSDIR}/etc -B little -o label=FreeBSD_Install -o version=2 ${2}.part ${MAKEFSARG}
52rm ${BASEBITSDIR}/etc/fstab
53rm ${BASEBITSDIR}/etc/rc.conf.local
54if [ -n "${METALOG}" ]; then
55	rm ${metalogfilename}
56fi
57
58mkimg -s mbr \
59    -b ${BASEBITSDIR}/boot/mbr \
60    -p freebsd:-"mkimg -s bsd -b ${BASEBITSDIR}/boot/boot -p freebsd-ufs:=${2}.part" \
61    -o ${2}
62rm ${2}.part
63
64