make-memstick.sh revision 204044
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: head/release/scripts/make-memstick.sh 204044 2010-02-18 15:45:43Z kensmith $
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
35rm -f ${tempfile}
36makefs ${tempfile} ${1}
37if [ $? -ne 0 ]; then
38  echo "makefs failed"
39  exit 1
40fi
41
42#
43# Use $BLOCKSIZE for transfers to improve efficiency.  When calculating
44# how many blocks to transfer "+ 2" is to account for truncation in the
45# division and to provide space for the label.
46#
47
48filesize=`stat -f "%z" ${tempfile}`
49blocks=$(($filesize / ${BLOCKSIZE} + 2))
50dd if=/dev/zero of=${2} bs=${BLOCKSIZE} count=${blocks}
51if [ $? -ne 0 ]; then
52  echo "creation of image file failed"
53  exit 1
54fi
55
56unit=`mdconfig -a -t vnode -f ${2}`
57if [ $? -ne 0 ]; then
58  echo "mdconfig failed"
59  exit 1
60fi
61
62fdisk -BIq /dev/${unit}
63if [ $? -ne 0 ]; then
64  echo "fdisk failed"
65  exit 1
66fi
67
68bsdlabel -B -w /dev/${unit}
69if [ $? -ne 0 ]; then
70  echo "bsdlabel failed"
71  exit 1
72fi
73
74dd if=${tempfile} of=/dev/${unit}a bs=$BLOCKSIZE conv=sync
75if [ $? -ne 0 ]; then
76  echo "copying filesystem into image file failed"
77  exit 1
78fi
79
80mdconfig -d -u ${unit}
81
82rm -f ${tempfile}
83
84