xref: /freebsd/release/amd64/make-memstick.sh (revision 599273f942b8dc6f957487bb28f36694dab9dad2)
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
14scriptdir=$(dirname $(realpath $0))
15. ${scriptdir}/../../tools/boot/install-boot.sh
16
17if [ "$(uname -s)" = "FreeBSD" ]; then
18	PATH=/bin:/usr/bin:/sbin:/usr/sbin
19	export PATH
20fi
21
22if [ $# -ne 2 ]; then
23	echo "make-memstick.sh /path/to/directory/or/manifest /path/to/image/file"
24	exit 1
25fi
26
27MAKEFSARG=${1}
28
29if [ -f ${MAKEFSARG} ]; then
30	BASEBITSDIR=`dirname ${MAKEFSARG}`
31	METALOG=${MAKEFSARG}
32elif [ -d ${MAKEFSARG} ]; then
33	BASEBITSDIR=${MAKEFSARG}
34	METALOG=
35else
36	echo "${MAKEFSARG} must exist"
37	exit 1
38fi
39
40if [ -e ${2} ]; then
41	echo "won't overwrite ${2}"
42	exit 1
43fi
44
45echo '/dev/ufs/FreeBSD_Install / ufs ro,noatime 1 1' > ${BASEBITSDIR}/etc/fstab
46echo 'root_rw_mount="NO"' > ${BASEBITSDIR}/etc/rc.conf.local
47if [ -n "${METALOG}" ]; then
48	metalogfilename=$(mktemp /tmp/metalog.XXXXXX)
49	cat ${METALOG} > ${metalogfilename}
50	echo "./etc/fstab type=file uname=root gname=wheel mode=0644" >> ${metalogfilename}
51	echo "./etc/rc.conf.local type=file uname=root gname=wheel mode=0644" >> ${metalogfilename}
52	MAKEFSARG=${metalogfilename}
53fi
54makefs -D -N ${BASEBITSDIR}/etc -B little -o label=FreeBSD_Install -o version=2 ${2}.part ${MAKEFSARG}
55rm ${BASEBITSDIR}/etc/fstab
56rm ${BASEBITSDIR}/etc/rc.conf.local
57if [ -n "${METALOG}" ]; then
58	rm ${metalogfilename}
59fi
60
61# Make an ESP in a file.
62espfilename=$(mktemp /tmp/efiboot.XXXXXX)
63if [ -f "${BASEBITSDIR}/boot/loader_ia32.efi" ]; then
64	make_esp_file ${espfilename} ${fat32min} ${BASEBITSDIR}/boot/loader.efi bootx64 \
65	    ${BASEBITSDIR}/boot/loader_ia32.efi bootia32
66else
67	make_esp_file ${espfilename} ${fat32min} ${BASEBITSDIR}/boot/loader.efi
68fi
69
70mkimg -s mbr \
71    -b ${BASEBITSDIR}/boot/mbr \
72    -p efi:=${espfilename} \
73    -p freebsd:-"mkimg -s bsd -b ${BASEBITSDIR}/boot/boot -p freebsd-ufs:=${2}.part" \
74    -a 2 \
75    -o ${2}
76rm ${espfilename}
77rm ${2}.part
78
79