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> <image filename>
9#
10# $FreeBSD$
11#
12
13PATH=/bin:/usr/bin:/sbin:/usr/sbin
14export PATH
15
16BLOCKSIZE=10240
17
18if [ $# -ne 2 ]; then
19  echo "make-memstick.sh /path/to/directory /path/to/image/file"
20  exit 1
21fi
22
23tempfile="${2}.$$"
24
25if [ ! -d ${1} ]; then
26  echo "${1} must be a directory"
27  exit 1
28fi
29
30if [ -e ${2} ]; then
31  echo "won't overwrite ${2}"
32  exit 1
33fi
34
35echo '/dev/da0s3 / ufs ro,noatime 1 1' > ${1}/etc/fstab
36rm -f ${tempfile}
37makefs -B big ${tempfile} ${1}
38if [ $? -ne 0 ]; then
39  echo "makefs failed"
40  exit 1
41fi
42rm ${1}/etc/fstab
43
44#
45# Use $BLOCKSIZE for transfers to improve efficiency.  When calculating
46# how many blocks to transfer "+ 2" is to account for truncation in the
47# division and to provide space for the label.
48#
49
50filesize=`stat -f "%z" ${tempfile}`
51blocks=$(($filesize / ${BLOCKSIZE} + 1728))
52dd if=/dev/zero of=${2} bs=${BLOCKSIZE} count=${blocks}
53if [ $? -ne 0 ]; then
54  echo "creation of image file failed"
55  exit 1
56fi
57
58unit=`mdconfig -a -t vnode -f ${2}`
59if [ $? -ne 0 ]; then
60  echo "mdconfig failed"
61  exit 1
62fi
63
64gpart create -s APM ${unit}
65gpart add -t freebsd-boot -s 800K ${unit}
66gpart bootcode -p ${1}/boot/boot1.hfs -i 1 ${unit}
67gpart add -t freebsd-ufs -l FreeBSD_Install ${unit}
68
69dd if=${tempfile} of=/dev/${unit}s3 bs=$BLOCKSIZE conv=sync
70if [ $? -ne 0 ]; then
71  echo "copying filesystem into image file failed"
72  exit 1
73fi
74
75mdconfig -d -u ${unit}
76
77rm -f ${tempfile}
78
79