4 # Make a blank M5 disk image
16 if [ $DEBUG -ne 0 ]; then
20 OUTPUT
="> /dev/null 2>&1"
31 abort
"Unable to find program $1, check your PATH variable"
37 if [ "$HAVE_SUDO" = "y" ]; then
44 abort
"Failed to run $@ as root"
49 abort
"Usage: $0 [root-path to copy] [extra ownership commands ...]"
52 # Setup PATH to look in the sbins
53 export PATH
=$PATH:/sbin
:/usr
/sbin
55 # Get all of the programs needed, or exit
57 SFDISK
=`find_prog sfdisk`
58 LOSETUP
=`find_prog losetup`
60 MKE2FS
=`find_prog mke2fs`
61 MKDIR
=`find_prog mkdir`
62 MOUNT
=`find_prog mount`
63 UMOUNT
=`find_prog umount`
64 WHOAMI
=`find_prog whoami`
66 CHOWN
=`find_prog chown`
68 # Prompt for the root password, if needed
72 if [ ! $MOUNT_IT ]; then
75 if [ ! -d $ROOT_PATH ]; then
85 if [ ! "$CUR_USER" = "root" ]; then
86 echo -n "Do you have sudo access? [y/n] "
89 if [ ! "$HAVE_SUDO" = "y" ]; then
90 abort
"You must have sudo access or run this script as root"
94 echo -n "How large do you want this disk image (in MB): "
97 # size in bytes = SIZE_MB * 1024 * 1024
98 # size in blocks = SIZE_BYTE / 512
99 let BLK_SIZE
=$USER_SIZE_MB*1024*2
101 let MAX_LBA
=16383*16*63
103 if [ $BLK_SIZE -ge $MAX_LBA ]; then
109 if [ $BLK_SIZE -ge 63 ]; then
116 let HEAD_SIZE
=$BLK_SIZE/$SECTORS
118 if [ $HEAD_SIZE -ge 16 ]; then
125 let SEC_HEAD
=$SECTORS*$HEADS
126 let CYLS
=$BLK_SIZE/$SEC_HEAD
129 # Recalculate number of sectors
130 let BLK_SIZE
=$CYLS*$HEADS*$SECTORS
132 # Get the name of the file and directory to build in
133 echo -n "What directory would you like to build the image in? "
136 if [ ! -d $IMAGE_DIR ]; then
137 abort
"The directory $IMAGE_DIR does not exist"
140 echo -n "What would you like to name the image? "
143 IMAGE_FILE
=$IMAGE_DIR/$IMAGE_NAME
147 echo "dd'ing the blank image (this make take a while)..."
148 eval $DD if=/dev
/zero of
=$IMAGE_FILE bs
=512 count
=$BLK_SIZE $OUTPUT
149 if [ $?
-ne 0 ]; then
150 abort
"Unable to create the blank image $IMAGE_NAME in $IMAGE_DIR"
153 # losetup the image with no offset to do the fdisk
155 echo "Binding the image and partitioning..."
156 run_priv
$LOSETUP /dev
/loop0
$IMAGE_FILE
157 if [ $?
-ne 0 ]; then
158 abort
"losetup to /dev/loop0 failed, make sure nothing is setup on loop0 (check by typing 'mount') "
162 run_priv
$SFDISK --no-reread -D -C $CYLS -H $HEADS -S $SECTORS /dev
/loop0
<<EOF
169 # Un-losetup the image
170 run_priv
$LOSETUP -d /dev
/loop0
172 # Mount the image with an offset and make the filesystem
174 echo "Remounting image and formatting..."
175 let BASE_OFFSET
=63*512
177 run_priv
$LOSETUP -o $BASE_OFFSET /dev
/loop0
$IMAGE_FILE
179 run_priv
$MKE2FS /dev
/loop0
181 # If a root path was specified then copy the root path into the image
182 if [ ! -z "$ROOT_PATH" ]; then
183 echo "Copying root from $ROOT_PATH to image file"
185 run_priv
$MKDIR -p /tmp
/mnt
187 run_priv
$MOUNT /dev
/loop0
/tmp
/mnt
189 run_priv
$CP -a $ROOT_PATH/* /tmp
/mnt
191 run_priv
$CHOWN -R root.root
/tmp
/mnt
193 # run extra permissions while disk is mounted
197 while [ $i -le $# ]; do
203 run_priv
$UMOUNT /tmp
/mnt
206 run_priv
$LOSETUP -d /dev
/loop0
209 if [ $MOUNT_IT -eq 1 ]; then
210 run_priv mount
-o loop
,offset
=$BASE_OFFSET $IMAGE_FILE /tmp
/mnt
213 echo "Disk image creation complete."
214 echo "To mount the image, run the following commands:"
215 echo "# $MOUNT -o loop,offset=$BASE_OFFSET $IMAGE_FILE /mount/point"
217 echo "And to unmount the image, run:"
218 echo "# $UMOUNT /mount/point"