ia64/xen-unstable

view tools/xm-test/ramdisk/bin/create_disk_image @ 10113:224e9878c6d4

This patch resizes the file system on the xm-test HVM disk.img to use
the entire space available.

Signed-off-by: Daniel Stekloff <dsteklof@us.ibm.com>
author stekloff@elm3b216.beaverton.ibm.com
date Fri May 19 16:29:34 2006 +0100 (2006-05-19)
parents f1b361b05bf3
children 89f7faa0c01a
line source
1 #!/bin/bash
2 ###############################################################################
3 ##
4 ## Copyright (C) International Business Machines Corp., 2005
5 ## Author(s): Daniel Stekloff <dsteklof@us.ibm.com>
6 ##
7 ## This program is free software; you can redistribute it and/or modify
8 ## it under the terms of the GNU General Public License as published by
9 ## the Free Software Foundation; under version 2 of the License.
10 ##
11 ## This program is distributed in the hope that it will be useful,
12 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ## GNU General Public License for more details.
15 ##
16 ## You should have received a copy of the GNU General Public License
17 ## along with this program; if not, write to the Free Software
18 ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 ##
20 ###############################################################################
21 function cleanup()
22 {
23 umount "$MNT"
24 rm -Rf "$MNT";
25 if [ "$LOOPD" ]; then
26 losetup -d $LOOPD
27 fi
28 if [ "$LOOPP" ]; then
29 losetup -d $LOOPP
30 fi
31 if [ -e "$IMAGE" ]; then
32 rm -f "$IMAGE"
33 fi
34 if [ -e "$LCONF" ]; then
35 rm -f "$LCONF"
36 fi
37 }
39 function die()
40 {
41 cleanup
42 echo "$@"
43 exit 1
44 }
46 function usage()
47 {
48 cat << EOU
49 Command creates a hvm guest disk image for xm-test.
51 Usage: $0 [OPTIONS]
53 OPTIONS:
54 -d|--dvrdir <name> Directory where to fine network driver
55 to use for disk image.
56 -i|--image <name> Image name to create.
57 -k|--kernel <name> Kernel name to use for disk image.
58 -r|--rootfs <image> Rootfs image to use for disk image.
60 This script currently only supports the pcnet32 driver for network
61 tests. If a dvrdir isn't added on the command-line, it will look
62 in /lib/modules/ directory relating to the supplied kernel. If the
63 network driver is built into the kernel, you can specify the key word
64 "builtin" with the -d option and the script will continue.
66 Note: The pcnet32 driver relies upon mii.ko. This script will look
67 for that module in the same location as the pcnet32 driver, either
68 for the kernel or the location used with the -d option.
70 EOU
71 }
73 function check_dependencies()
74 {
75 which lilo > /dev/null 2>&1
76 if [ $? -ne 0 ]; then
77 die "$PROGNAME requires lilo version 22.7+ to be installed."
78 fi
79 local pass="$( lilo -V | cut -f3 -d " " | awk -F "." '
80 {
81 if ($1 >= 22 && $2 >= 7)
82 print "true"
83 else
84 print "false"
85 }')"
86 if [ $pass = "false" ]; then
87 die "$PROGNAME requires lilo version 22.7+ to be installed."
88 fi
89 }
91 function initialize_globals()
92 {
93 PROGNAME="create_disk_image"
94 IMAGE="disk.img"
95 KERNEL=""
96 DRVDIR=""
97 LCONF="lilo.conf"
98 LOOPD="" # Loop device for entire disk image
99 LOOPP="" # Loop device for ext partition
100 ROOTFS=""
101 MNT="/tmp/$PROGNAME-mnt"
102 SIZE=8192
103 SECTORS=32
104 HEADS=8
105 CYLINDERS=$(($SIZE/$SECTORS/$HEADS*2))
106 BSIZE=$(($SIZE-$SECTORS))
107 OFFSET=$(($SECTORS*512))
108 }
110 function get_options()
111 {
112 while [ $# -gt 0 ]; do
113 case $1 in
114 -d|--drvdir)
115 shift
116 DRVDIR=${1}
117 shift
118 ;;
119 -i|--image)
120 shift
121 IMAGE=${1}
122 shift
123 ;;
124 -k|--kernel)
125 shift
126 KERNEL=${1}
127 shift
128 ;;
129 -r|--rootfs)
130 shift
131 ROOTFS=${1}
132 shift
133 ;;
134 *)
135 usage
136 exit 1
137 ;;
138 esac
139 done
140 }
142 function get_loopd()
143 {
144 local loop
146 for i in `seq 0 7`; do
147 losetup /dev/loop$i > /dev/null 2>&1
148 if [ $? -ne 0 ]; then
149 # found one
150 echo $i
151 return 0
152 fi
153 done
154 die "No free loopback devices."
155 }
157 function losetup_image()
158 {
159 local loop=$1
160 shift
162 # If offset, then associate with it
163 if [ $# -eq 1 ]; then
164 losetup -o $1 $loop $IMAGE
165 else
166 losetup $loop $IMAGE
167 fi
169 if [ $? -ne 0 ]; then
170 die "Failed to losetup $IMAGE to $loop."
171 fi
173 echo "Associated $IMAGE with $loop"
174 }
176 function create_disk_image()
177 {
178 dd bs=1024 count=$SIZE of=$IMAGE if=/dev/zero
180 fdisk -b 512 -C $CYLINDERS -H $HEADS -S $SECTORS "$IMAGE" > /dev/null 2>&1 << EOF
181 n
182 p
183 1
184 1
186 a
187 1
188 w
189 EOF
190 }
192 function makefs_image()
193 {
194 mke2fs -N 24 -b 1024 $LOOPP $BSIZE
196 if [ $? -ne 0 ]; then
197 die "mke2fs $LOOPP failed."
198 fi
199 }
201 function dd_rootfs_to_image()
202 {
203 if [ ! "$ROOTFS" ]; then
204 die "Must specify rootfs image to use."
205 fi
207 dd if="$ROOTFS" of="$LOOPP" > /dev/null 2>&1
208 if [ $? -ne 0 ]; then
209 die "Failed to dd $ROOTFS to $LOOPP."
210 fi
212 # Resize fs to use full partition
213 e2fsck -f $LOOPP
214 resize2fs $LOOPP
215 if [ $? -ne 0 ]; then
216 die "Failed to resize rootfs on $LOOPP."
217 fi
218 }
220 function get_kernel()
221 {
222 # look in /boot for an existing kernel
223 local -a kernels=( `ls /boot | grep vmlinuz` )
224 local k
226 for k in ${kernels[@]}; do
227 case "$k" in
228 *xen*)
229 continue
230 ;;
231 *)
232 KERNEL="/boot/$k"
233 echo "Using kernel $KERNEL"
234 break
235 ;;
236 esac
237 done
238 }
240 function copy_kernel_to_image()
241 {
242 if [ ! "$KERNEL" ]; then
243 get_kernel || die "Couldn't find a kernel to use."
244 fi
246 mkdir "$MNT/boot"
248 cp "$KERNEL" "$MNT/boot"
249 }
251 function copy_netdriver_to_image()
252 {
253 local kernel=`basename $KERNEL`
254 local kversion=$( echo $kernel | sed 's/^vmlinuz-//' )
255 local fdir="/lib/modules/$kversion/kernel/drivers/net"
257 mkdir "$MNT/lib/modules"
258 if [ -e "$DRVDIR" ]; then
259 if [ -e "$DRVDIR/pcnet32.ko" ]; then
260 cp $DRVDIR/mii.ko $MNT/lib/modules
261 cp $DRVDIR/pcnet32.ko $MNT/lib/modules
262 else
263 die "Failed to find pcnet32.ko at $DRVDIR."
264 fi
265 elif [ -e "$fdir/pcnet32.ko" ]; then
266 cp $fdir/mii.ko $MNT/lib/modules
267 cp $fdir/pcnet32.ko $MNT/lib/modules
268 else
269 die "Xm-test requires the pcnet32 driver to run."
270 fi
272 # Make sure that modules will be installed
273 if [ -e "$MNT/etc/init.d/rcS" ]; then
274 echo "insmod /lib/modules/mii.ko" >> $MNT/etc/init.d/rcS
275 echo "insmod /lib/modules/pcnet32.ko" >> $MNT/etc/init.d/rcS
276 else
277 die "Failed to add insmod command to rcS file on image."
278 fi
279 }
281 function lilo_image()
282 {
283 local kernel=`basename $KERNEL`
285 (
286 cat <<EOC
287 boot=$LOOPD
288 delay=10
289 geometric
290 map=$MNT/boot/map
291 disk=$LOOPD
292 bios=0x80
293 sectors=$SECTORS
294 heads=$HEADS
295 cylinders=$CYLINDERS
296 partition=$LOOPP
297 start=$SECTORS
298 image=$MNT/boot/$kernel
299 append="root=0301 console=tty0 console=ttyS0"
300 # append="root=0301"
301 label=Linux
302 read-only
303 EOC
304 ) > "/$MNT/boot/$LCONF"
305 }
307 function install_lilo()
308 {
309 lilo -C "$MNT/boot/$LCONF"
310 if [ $? -ne 0 ]; then
311 die "Failed to install $MNT/boot/$LCONF."
312 fi
313 }
315 function add_getty_to_inittab()
316 {
317 local itab=$MNT/etc/inittab
319 if [ -e "$itab" ]; then
320 echo "# Start getty on serial line" >> $itab
321 echo "S0:12345:respawn:/sbin/getty ttyS0" >> $itab
322 fi
323 }
326 # Main starts here
327 initialize_globals
328 check_dependencies
330 get_options "$@"
332 create_disk_image
334 # Get the first free loop device
335 ldev=$(get_loopd)
336 LOOPD="/dev/loop$ldev"
337 losetup_image $LOOPD
339 # Now associate where the partition will go
340 ldev=$(get_loopd)
341 LOOPP="/dev/loop$ldev"
342 losetup_image $LOOPP $OFFSET
344 makefs_image
346 dd_rootfs_to_image
348 if [ -e "$MNT" ]; then
349 rm -Rf "$MNT"
350 fi
352 mkdir "$MNT";
353 if [ $? -ne 0 ]; then
354 die "Failed to create temporary mount point $MNT."
355 fi
357 mount "$LOOPP" "$MNT";
358 if [ $? -ne 0 ]; then
359 die "Failed to mount $LOOPP on $MNT."
360 fi
362 copy_kernel_to_image
363 if [ ! "$DRVDIR" = "builtin" ]; then
364 copy_netdriver_to_image
365 fi
366 #add_getty_to_inittab
368 lilo_image
369 install_lilo
371 umount "$MNT"
372 rm -Rf "$MNT";
374 losetup -d $LOOPD
375 losetup -d $LOOPP
377 exit 0