garnet: Split network power in ruby.stats
[gem5.git] / util / mkblankimage.sh
1 #!/bin/bash
2 #
3 # makeblankimage.sh
4 # Make a blank M5 disk image
5 #
6
7 while getopts "m" OPT
8 do
9 case "$OPT" in
10 m) MOUNT_IT=1
11 esac
12 done
13
14 DEBUG=0
15
16 if [ $DEBUG -ne 0 ]; then
17 set -x -e
18 OUTPUT=""
19 else
20 OUTPUT="> /dev/null 2>&1"
21 fi
22
23 abort() {
24 echo $@
25 exec /bin/false
26 }
27
28 find_prog() {
29 PROG_PATH=`which $1`
30 if [ $? -ne 0 ]; then
31 abort "Unable to find program $1, check your PATH variable"
32 fi
33 echo $PROG_PATH
34 }
35
36 run_priv() {
37 if [ "$HAVE_SUDO" = "y" ]; then
38 eval $SUDO $@ $OUTPUT
39 else
40 eval $@ $OUTPUT
41 fi
42
43 if [ $? -ne 0 ]; then
44 abort "Failed to run $@ as root"
45 fi
46 }
47
48 usage() {
49 abort "Usage: $0 [root-path to copy] [extra ownership commands ...]"
50 }
51
52 # Setup PATH to look in the sbins
53 export PATH=$PATH:/sbin:/usr/sbin
54
55 # Get all of the programs needed, or exit
56 DD=`find_prog dd`
57 SFDISK=`find_prog sfdisk`
58 LOSETUP=`find_prog losetup`
59 SUDO=`find_prog sudo`
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`
65 CP=`find_prog cp`
66 CHOWN=`find_prog chown`
67
68 # Prompt for the root password, if needed
69 CUR_USER=`$WHOAMI`
70
71 if [ $# -ge 1 ]; then
72 if [ ! $MOUNT_IT ]; then
73 ROOT_PATH=$1
74
75 if [ ! -d $ROOT_PATH ]; then
76 usage
77 fi
78 else
79 ROOT_PATH=""
80 fi
81 else
82 ROOT_PATH=""
83 fi
84
85 if [ ! "$CUR_USER" = "root" ]; then
86 echo -n "Do you have sudo access? [y/n] "
87 read HAVE_SUDO
88
89 if [ ! "$HAVE_SUDO" = "y" ]; then
90 abort "You must have sudo access or run this script as root"
91 fi
92 fi
93
94 echo -n "How large do you want this disk image (in MB): "
95 read USER_SIZE_MB
96
97 # size in bytes = SIZE_MB * 1024 * 1024
98 # size in blocks = SIZE_BYTE / 512
99 let BLK_SIZE=$USER_SIZE_MB*1024*2
100
101 let MAX_LBA=16383*16*63
102
103 if [ $BLK_SIZE -ge $MAX_LBA ]; then
104 CYLS=16383
105 HEADS=16
106 SECTORS=63
107 else
108 # Set Sectors
109 if [ $BLK_SIZE -ge 63 ]; then
110 SECTORS=63
111 else
112 SECTORS=$BLK_SIZE
113 fi
114
115 # Set Heads
116 let HEAD_SIZE=$BLK_SIZE/$SECTORS
117
118 if [ $HEAD_SIZE -ge 16 ]; then
119 HEADS=16
120 else
121 HEADS=$BLK_SIZE
122 fi
123
124 # Set Cylinders
125 let SEC_HEAD=$SECTORS*$HEADS
126 let CYLS=$BLK_SIZE/$SEC_HEAD
127 fi
128
129 # Recalculate number of sectors
130 let BLK_SIZE=$CYLS*$HEADS*$SECTORS
131
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? "
134 read IMAGE_DIR
135
136 if [ ! -d $IMAGE_DIR ]; then
137 abort "The directory $IMAGE_DIR does not exist"
138 fi
139
140 echo -n "What would you like to name the image? "
141 read IMAGE_NAME
142
143 IMAGE_FILE=$IMAGE_DIR/$IMAGE_NAME
144
145 # DD the blank image
146 echo
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"
151 fi
152
153 # losetup the image with no offset to do the fdisk
154 echo
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') "
159 fi
160
161 # fdisk the image
162 run_priv $SFDISK --no-reread -D -C $CYLS -H $HEADS -S $SECTORS /dev/loop0 <<EOF
163 0,
164 ;
165 ;
166 ;
167 EOF
168
169 # Un-losetup the image
170 run_priv $LOSETUP -d /dev/loop0
171
172 # Mount the image with an offset and make the filesystem
173 echo
174 echo "Remounting image and formatting..."
175 let BASE_OFFSET=63*512
176
177 run_priv $LOSETUP -o $BASE_OFFSET /dev/loop0 $IMAGE_FILE
178
179 run_priv $MKE2FS /dev/loop0
180
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"
184
185 run_priv $MKDIR -p /tmp/mnt
186
187 run_priv $MOUNT /dev/loop0 /tmp/mnt
188
189 run_priv $CP -a $ROOT_PATH/* /tmp/mnt
190
191 run_priv $CHOWN -R root.root /tmp/mnt
192
193 # run extra permissions while disk is mounted
194 TOPDIR=`pwd`
195 cd /tmp/mnt
196 i=2
197 while [ $i -le $# ]; do
198 run_priv ${!i}
199 let i=i+1
200 done
201 cd $TOPDIR
202
203 run_priv $UMOUNT /tmp/mnt
204 fi
205
206 run_priv $LOSETUP -d /dev/loop0
207
208
209 if [ $MOUNT_IT -eq 1 ]; then
210 run_priv mount -o loop,offset=$BASE_OFFSET $IMAGE_FILE /tmp/mnt
211 else
212 echo
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"
216 echo
217 echo "And to unmount the image, run:"
218 echo "# $UMOUNT /mount/point"
219 fi;