make-memstick.sh revision 204044
1204044Skensmith#!/bin/sh
2204044Skensmith#
3204044Skensmith# This script generates a "memstick image" (image that can be copied to a
4204044Skensmith# USB memory stick) from a directory tree.  Note that the script does not
5204044Skensmith# clean up after itself very well for error conditions on purpose so the
6204044Skensmith# problem can be diagnosed (full filesystem most likely but ...).
7204044Skensmith#
8204044Skensmith# Usage: make-memstick.sh <directory tree> <image filename>
9204044Skensmith#
10204044Skensmith# $FreeBSD: head/release/scripts/make-memstick.sh 204044 2010-02-18 15:45:43Z kensmith $
11204044Skensmith#
12204044Skensmith
13204044SkensmithPATH=/bin:/usr/bin:/sbin:/usr/sbin
14204044Skensmithexport PATH
15204044Skensmith
16204044SkensmithBLOCKSIZE=10240
17204044Skensmith
18204044Skensmithif [ $# -ne 2 ]; then
19204044Skensmith  echo "make-memstick.sh /path/to/directory /path/to/image/file"
20204044Skensmith  exit 1
21204044Skensmithfi
22204044Skensmith
23204044Skensmithtempfile="${2}.$$"
24204044Skensmith
25204044Skensmithif [ ! -d ${1} ]; then
26204044Skensmith  echo "${1} must be a directory"
27204044Skensmith  exit 1
28204044Skensmithfi
29204044Skensmith
30204044Skensmithif [ -e ${2} ]; then
31204044Skensmith  echo "won't overwrite ${2}"
32204044Skensmith  exit 1
33204044Skensmithfi
34204044Skensmith
35204044Skensmithrm -f ${tempfile}
36204044Skensmithmakefs ${tempfile} ${1}
37204044Skensmithif [ $? -ne 0 ]; then
38204044Skensmith  echo "makefs failed"
39204044Skensmith  exit 1
40204044Skensmithfi
41204044Skensmith
42204044Skensmith#
43204044Skensmith# Use $BLOCKSIZE for transfers to improve efficiency.  When calculating
44204044Skensmith# how many blocks to transfer "+ 2" is to account for truncation in the
45204044Skensmith# division and to provide space for the label.
46204044Skensmith#
47204044Skensmith
48204044Skensmithfilesize=`stat -f "%z" ${tempfile}`
49204044Skensmithblocks=$(($filesize / ${BLOCKSIZE} + 2))
50204044Skensmithdd if=/dev/zero of=${2} bs=${BLOCKSIZE} count=${blocks}
51204044Skensmithif [ $? -ne 0 ]; then
52204044Skensmith  echo "creation of image file failed"
53204044Skensmith  exit 1
54204044Skensmithfi
55204044Skensmith
56204044Skensmithunit=`mdconfig -a -t vnode -f ${2}`
57204044Skensmithif [ $? -ne 0 ]; then
58204044Skensmith  echo "mdconfig failed"
59204044Skensmith  exit 1
60204044Skensmithfi
61204044Skensmith
62204044Skensmithfdisk -BIq /dev/${unit}
63204044Skensmithif [ $? -ne 0 ]; then
64204044Skensmith  echo "fdisk failed"
65204044Skensmith  exit 1
66204044Skensmithfi
67204044Skensmith
68204044Skensmithbsdlabel -B -w /dev/${unit}
69204044Skensmithif [ $? -ne 0 ]; then
70204044Skensmith  echo "bsdlabel failed"
71204044Skensmith  exit 1
72204044Skensmithfi
73204044Skensmith
74204044Skensmithdd if=${tempfile} of=/dev/${unit}a bs=$BLOCKSIZE conv=sync
75204044Skensmithif [ $? -ne 0 ]; then
76204044Skensmith  echo "copying filesystem into image file failed"
77204044Skensmith  exit 1
78204044Skensmithfi
79204044Skensmith
80204044Skensmithmdconfig -d -u ${unit}
81204044Skensmith
82204044Skensmithrm -f ${tempfile}
83204044Skensmith
84