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