make-memstick.sh revision 219918
1100280Sgordon#!/bin/sh
2100280Sgordon#
3118638Sfjoe# This script generates a "memstick image" (image that can be copied to a
466830Sobrien# USB memory stick) from a directory tree.  Note that the script does not
566830Sobrien# clean up after itself very well for error conditions on purpose so the
666830Sobrien# problem can be diagnosed (full filesystem most likely but ...).
766830Sobrien#
866830Sobrien# Usage: make-memstick.sh <directory tree> <image filename>
966830Sobrien#
1066830Sobrien# $FreeBSD: head/release/powerpc/make-memstick.sh 219918 2011-03-23 16:22:08Z nwhitehorn $
1166830Sobrien#
1266830Sobrien
1366830SobrienPATH=/bin:/usr/bin:/sbin:/usr/sbin
1466830Sobrienexport PATH
1566830Sobrien
1666830SobrienBLOCKSIZE=10240
1766830Sobrien
1866830Sobrienif [ $# -ne 2 ]; then
1966830Sobrien  echo "make-memstick.sh /path/to/directory /path/to/image/file"
2066830Sobrien  exit 1
2166830Sobrienfi
2266830Sobrien
2366830Sobrientempfile="${2}.$$"
2466830Sobrien
2566830Sobrienif [ ! -d ${1} ]; then
2666830Sobrien  echo "${1} must be a directory"
2751231Ssheldonh  exit 1
2851231Ssheldonhfi
29100280Sgordon
30126744Spjdif [ -e ${2} ]; then
31100280Sgordon  echo "won't overwrite ${2}"
32121067Sdougb  exit 1
33108191Sdillonfi
34108191Sdillon
35108191Sdillonecho '/dev/da0s3 / ufs rw,noatime 1 1' > ${1}/etc/fstab
36108191Sdillonrm -f ${tempfile}
37108191Sdillonmakefs -B big ${tempfile} ${1}
38108191Sdillonif [ $? -ne 0 ]; then
39108191Sdillon  echo "makefs failed"
40108191Sdillon  exit 1
41108191Sdillonfi
42108191Sdillonrm ${1}/etc/fstab
43108191Sdillon
44108191Sdillon#
45108191Sdillon# Use $BLOCKSIZE for transfers to improve efficiency.  When calculating
46108191Sdillon# how many blocks to transfer "+ 2" is to account for truncation in the
47108191Sdillon# division and to provide space for the label.
48108191Sdillon#
49108191Sdillon
50108191Sdillonfilesize=`stat -f "%z" ${tempfile}`
51108191Sdillonblocks=$(($filesize / ${BLOCKSIZE} + 1728))
52126787Sphkdd if=/dev/zero of=${2} bs=${BLOCKSIZE} count=${blocks}
53126787Sphkif [ $? -ne 0 ]; then
54126787Sphk  echo "creation of image file failed"
55126787Sphk  exit 1
56126787Sphkfi
57108191Sdillon
58108191Sdillonunit=`mdconfig -a -t vnode -f ${2}`
59108191Sdillonif [ $? -ne 0 ]; then
60108191Sdillon  echo "mdconfig failed"
61108191Sdillon  exit 1
62126787Sphkfi
63126787Sphk
64108191Sdillongpart create -s APM ${unit}
65108191Sdillongpart add -t freebsd-boot -s 800K ${unit}
66108191Sdillongpart bootcode -p ${1}/boot/boot1.hfs -i 1 ${unit}
67121014Skrisgpart add -t freebsd-ufs -l FreeBSD_Install ${unit}
68108191Sdillon
69121014Skrisdd if=${tempfile} of=/dev/${unit}s3 bs=$BLOCKSIZE conv=sync
70108191Sdillonif [ $? -ne 0 ]; then
71108191Sdillon  echo "copying filesystem into image file failed"
72108191Sdillon  exit 1
73108191Sdillonfi
74108191Sdillon
75108191Sdillonmdconfig -d -u ${unit}
76108191Sdillon
77108191Sdillonrm -f ${tempfile}
78108191Sdillon
79108191Sdillon