xref: /freebsd/release/riscv/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
20scriptdir=$(dirname $(realpath $0))
21. ${scriptdir}/../../tools/boot/install-boot.sh
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 gpt \
67    -p efi:=${espfilename} \
68    -p freebsd-ufs:=${2}.part \
69    -o ${2}
70rm ${espfilename}
71rm ${2}.part
72
73