1219918Snwhitehorn#!/bin/sh
2219918Snwhitehorn#
3219918Snwhitehorn# This script generates a "memstick image" (image that can be copied to a
4219918Snwhitehorn# USB memory stick) from a directory tree.  Note that the script does not
5219918Snwhitehorn# clean up after itself very well for error conditions on purpose so the
6219918Snwhitehorn# problem can be diagnosed (full filesystem most likely but ...).
7219918Snwhitehorn#
8219918Snwhitehorn# Usage: make-memstick.sh <directory tree> <image filename>
9219918Snwhitehorn#
10219918Snwhitehorn# $FreeBSD$
11219918Snwhitehorn#
12219918Snwhitehorn
13219918SnwhitehornPATH=/bin:/usr/bin:/sbin:/usr/sbin
14219918Snwhitehornexport PATH
15219918Snwhitehorn
16219918SnwhitehornBLOCKSIZE=10240
17219918Snwhitehorn
18219918Snwhitehornif [ $# -ne 2 ]; then
19219918Snwhitehorn  echo "make-memstick.sh /path/to/directory /path/to/image/file"
20219918Snwhitehorn  exit 1
21219918Snwhitehornfi
22219918Snwhitehorn
23219918Snwhitehorntempfile="${2}.$$"
24219918Snwhitehorn
25219918Snwhitehornif [ ! -d ${1} ]; then
26219918Snwhitehorn  echo "${1} must be a directory"
27219918Snwhitehorn  exit 1
28219918Snwhitehornfi
29219918Snwhitehorn
30219918Snwhitehornif [ -e ${2} ]; then
31219918Snwhitehorn  echo "won't overwrite ${2}"
32219918Snwhitehorn  exit 1
33219918Snwhitehornfi
34219918Snwhitehorn
35224504Snwhitehornecho '/dev/da0s3 / ufs ro,noatime 1 1' > ${1}/etc/fstab
36219918Snwhitehornrm -f ${tempfile}
37219918Snwhitehornmakefs -B big ${tempfile} ${1}
38219918Snwhitehornif [ $? -ne 0 ]; then
39219918Snwhitehorn  echo "makefs failed"
40219918Snwhitehorn  exit 1
41219918Snwhitehornfi
42219918Snwhitehornrm ${1}/etc/fstab
43219918Snwhitehorn
44219918Snwhitehorn#
45219918Snwhitehorn# Use $BLOCKSIZE for transfers to improve efficiency.  When calculating
46219918Snwhitehorn# how many blocks to transfer "+ 2" is to account for truncation in the
47219918Snwhitehorn# division and to provide space for the label.
48219918Snwhitehorn#
49219918Snwhitehorn
50219918Snwhitehornfilesize=`stat -f "%z" ${tempfile}`
51219918Snwhitehornblocks=$(($filesize / ${BLOCKSIZE} + 1728))
52219918Snwhitehorndd if=/dev/zero of=${2} bs=${BLOCKSIZE} count=${blocks}
53219918Snwhitehornif [ $? -ne 0 ]; then
54219918Snwhitehorn  echo "creation of image file failed"
55219918Snwhitehorn  exit 1
56219918Snwhitehornfi
57219918Snwhitehorn
58219918Snwhitehornunit=`mdconfig -a -t vnode -f ${2}`
59219918Snwhitehornif [ $? -ne 0 ]; then
60219918Snwhitehorn  echo "mdconfig failed"
61219918Snwhitehorn  exit 1
62219918Snwhitehornfi
63219918Snwhitehorn
64219918Snwhitehorngpart create -s APM ${unit}
65219918Snwhitehorngpart add -t freebsd-boot -s 800K ${unit}
66219918Snwhitehorngpart bootcode -p ${1}/boot/boot1.hfs -i 1 ${unit}
67219918Snwhitehorngpart add -t freebsd-ufs -l FreeBSD_Install ${unit}
68219918Snwhitehorn
69219918Snwhitehorndd if=${tempfile} of=/dev/${unit}s3 bs=$BLOCKSIZE conv=sync
70219918Snwhitehornif [ $? -ne 0 ]; then
71219918Snwhitehorn  echo "copying filesystem into image file failed"
72219918Snwhitehorn  exit 1
73219918Snwhitehornfi
74219918Snwhitehorn
75219918Snwhitehornmdconfig -d -u ${unit}
76219918Snwhitehorn
77219918Snwhitehornrm -f ${tempfile}
78219918Snwhitehorn
79