xref: /freebsd/release/i386/make-memstick.sh (revision 911f0260390e18cf85f3dbf2c719b593efdc1e3c)
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# $FreeBSD$
11#
12
13set -e
14
15if [ "$(uname -s)" = "FreeBSD" ]; then
16	PATH=/bin:/usr/bin:/sbin:/usr/sbin
17	export PATH
18fi
19
20if [ $# -ne 2 ]; then
21	echo "make-memstick.sh /path/to/directory/or/manifest /path/to/image/file"
22	exit 1
23fi
24
25MAKEFSARG=${1}
26
27if [ -f ${MAKEFSARG} ]; then
28	BASEBITSDIR=`dirname ${MAKEFSARG}`
29	METALOG=${MAKEFSARG}
30elif [ -d ${MAKEFSARG} ]; then
31	BASEBITSDIR=${MAKEFSARG}
32	METALOG=
33else
34	echo "${MAKEFSARG} must exist"
35	exit 1
36fi
37
38if [ -e ${2} ]; then
39	echo "won't overwrite ${2}"
40	exit 1
41fi
42
43echo '/dev/ufs/FreeBSD_Install / ufs ro,noatime 1 1' > ${BASEBITSDIR}/etc/fstab
44echo 'root_rw_mount="NO"' > ${BASEBITSDIR}/etc/rc.conf.local
45if [ -n "${METALOG}" ]; then
46	metalogfilename=$(mktemp /tmp/metalog.XXXXXX)
47	cat ${METALOG} > ${metalogfilename}
48	echo "./etc/fstab type=file uname=root gname=wheel mode=0644" >> ${metalogfilename}
49	echo "./etc/rc.conf.local type=file uname=root gname=wheel mode=0644" >> ${metalogfilename}
50	MAKEFSARG=${metalogfilename}
51fi
52makefs -D -N ${BASEBITSDIR}/etc -B little -o label=FreeBSD_Install -o version=2 ${2}.part ${MAKEFSARG}
53rm ${BASEBITSDIR}/etc/fstab
54rm ${BASEBITSDIR}/etc/rc.conf.local
55if [ -n "${METALOG}" ]; then
56	rm ${metalogfilename}
57fi
58
59mkimg -s mbr \
60    -b ${BASEBITSDIR}/boot/mbr \
61    -p freebsd:-"mkimg -s bsd -b ${BASEBITSDIR}/boot/boot -p freebsd-ufs:=${2}.part" \
62    -o ${2}
63rm ${2}.part
64
65