]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - release/scripts/make-memstick.sh
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.git] / release / scripts / make-memstick.sh
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
13 PATH=/bin:/usr/bin:/sbin:/usr/sbin
14 export PATH
15
16 BLOCKSIZE=10240
17
18 if [ $# -ne 2 ]; then
19   echo "make-memstick.sh /path/to/directory /path/to/image/file"
20   exit 1
21 fi
22
23 tempfile="${2}.$$"
24
25 if [ ! -d ${1} ]; then
26   echo "${1} must be a directory"
27   exit 1
28 fi
29
30 if [ -e ${2} ]; then
31   echo "won't overwrite ${2}"
32   exit 1
33 fi
34
35 rm -f ${tempfile}
36 makefs ${tempfile} ${1}
37 if [ $? -ne 0 ]; then
38   echo "makefs failed"
39   exit 1
40 fi
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
48 filesize=`stat -f "%z" ${tempfile}`
49 blocks=$(($filesize / ${BLOCKSIZE} + 2))
50 dd if=/dev/zero of=${2} bs=${BLOCKSIZE} count=${blocks}
51 if [ $? -ne 0 ]; then
52   echo "creation of image file failed"
53   exit 1
54 fi
55
56 unit=`mdconfig -a -t vnode -f ${2}`
57 if [ $? -ne 0 ]; then
58   echo "mdconfig failed"
59   exit 1
60 fi
61
62 fdisk -BIq /dev/${unit}
63 if [ $? -ne 0 ]; then
64   echo "fdisk failed"
65   exit 1
66 fi
67
68 bsdlabel -B -w /dev/${unit}
69 if [ $? -ne 0 ]; then
70   echo "bsdlabel failed"
71   exit 1
72 fi
73
74 dd if=${tempfile} of=/dev/${unit}a bs=$BLOCKSIZE conv=sync
75 if [ $? -ne 0 ]; then
76   echo "copying filesystem into image file failed"
77   exit 1
78 fi
79
80 mdconfig -d -u ${unit}
81
82 rm -f ${tempfile}
83