~ubuntu-branches/ubuntu/utopic/xen/utopic

« back to all changes in this revision

Viewing changes to tools/xm-test/ramdisk/bin/create_disk_image

  • Committer: Bazaar Package Importer
  • Author(s): Bastian Blank
  • Date: 2010-05-06 15:47:38 UTC
  • mto: (1.3.1) (15.1.1 sid) (4.1.1 experimental)
  • mto: This revision was merged to the branch mainline in revision 3.
  • Revision ID: james.westby@ubuntu.com-20100506154738-agoz0rlafrh1fnq7
Tags: upstream-4.0.0
ImportĀ upstreamĀ versionĀ 4.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
}
 
38
 
 
39
function die()
 
40
{
 
41
        cleanup
 
42
        echo "$@"
 
43
        exit 1
 
44
}
 
45
 
 
46
function usage()
 
47
{
 
48
        cat << EOU
 
49
Command creates a hvm guest disk image for xm-test. 
 
50
 
 
51
Usage: $0 [OPTIONS]
 
52
 
 
53
OPTIONS:
 
54
    -d|--dvrdir <name>       Directory where to find 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
    -n|--netdrv <name>       Network driver name to use for disk image.
 
59
    -r|--rootfs <image>      Rootfs image to use for disk image.
 
60
 
 
61
This script defaults to using the 8139too.ko driver for network tests. 
 
62
If a dvrdir isn't added on the command-line, it will look in 
 
63
/lib/modules/ directory relating to the supplied kernel. If the
 
64
network driver is built into the kernel, you can specify the key word
 
65
"builtin" with the -d option and the script will continue.
 
66
 
 
67
Note: Many network drivers rely upon mii.ko. This script will look
 
68
for that module in the same location as the network driver, either
 
69
for the kernel or the location used with the -d option.
 
70
 
 
71
EOU
 
72
}
 
73
 
 
74
function check_dependencies()
 
75
{
 
76
        which lilo > /dev/null 2>&1
 
77
        if [ $? -ne 0 ]; then
 
78
                die "$PROGNAME requires lilo version 22.7+ to be installed."
 
79
        fi
 
80
        local pass="$( lilo -V | cut -f3 -d " " | awk -F "." '
 
81
                {
 
82
                        if ($1 >= 22 && $2 >= 7)
 
83
                                print "true"
 
84
                        else
 
85
                                print "false"
 
86
                }')"
 
87
        if [ $pass = "false" ]; then
 
88
                die "$PROGNAME requires lilo version 22.7+ to be installed."
 
89
        fi
 
90
}
 
91
 
 
92
function initialize_globals()
 
93
{
 
94
        PROGNAME="create_disk_image"
 
95
        IMAGE="disk.img"
 
96
        KERNEL=""
 
97
        DRVDIR=""
 
98
        NETDRV="8139too.ko"
 
99
        LCONF="lilo.conf"
 
100
        LOOPD=""    # Loop device for entire disk image
 
101
        LOOPP=""    # Loop device for ext partition
 
102
        ROOTFS=""
 
103
        MNT="/tmp/$PROGNAME-mnt"
 
104
        SIZE=8192
 
105
        SECTORS=32
 
106
        HEADS=8
 
107
        CYLINDERS=$(($SIZE/$SECTORS/$HEADS*2))
 
108
        BSIZE=$(($SIZE-$SECTORS))
 
109
        OFFSET=$(($SECTORS*512))
 
110
}
 
111
 
 
112
function get_options()
 
113
{
 
114
        while [ $# -gt 0 ]; do
 
115
                case $1 in
 
116
                        -d|--drvdir)
 
117
                                shift
 
118
                                DRVDIR=${1}
 
119
                                shift
 
120
                                ;;
 
121
                        -i|--image)
 
122
                                shift
 
123
                                IMAGE=${1}
 
124
                                shift
 
125
                                ;;
 
126
                        -k|--kernel)
 
127
                                shift
 
128
                                KERNEL=${1}
 
129
                                shift
 
130
                                ;;
 
131
                        -n|--netdrv)
 
132
                                shift
 
133
                                NETDRV=${1}
 
134
                                shift
 
135
                                ;;
 
136
                        -r|--rootfs)
 
137
                                shift
 
138
                                ROOTFS=${1}
 
139
                                shift
 
140
                                ;;
 
141
                        *)
 
142
                                usage
 
143
                                exit 1
 
144
                                ;;
 
145
                esac
 
146
        done
 
147
}
 
148
 
 
149
function get_loopd()
 
150
{
 
151
        local loop
 
152
 
 
153
        for i in `seq 0 7`; do
 
154
                losetup /dev/loop$i > /dev/null 2>&1
 
155
                if [ $? -ne 0 ]; then
 
156
                        # found one
 
157
                        echo $i
 
158
                        return 0
 
159
                fi
 
160
        done
 
161
        die "No free loopback devices."
 
162
}
 
163
 
 
164
function losetup_image()
 
165
{
 
166
        local loop=$1
 
167
        shift
 
168
 
 
169
        # If offset, then associate with it
 
170
        if [ $# -eq 1 ]; then
 
171
                losetup -o $1 $loop $IMAGE
 
172
        else
 
173
                losetup $loop $IMAGE
 
174
        fi
 
175
 
 
176
        if [ $? -ne 0 ]; then
 
177
                die "Failed to losetup $IMAGE to $loop."
 
178
        fi
 
179
 
 
180
        echo "Associated $IMAGE with $loop"
 
181
}
 
182
 
 
183
function create_disk_image()
 
184
{
 
185
        dd bs=1024 count=$SIZE of=$IMAGE if=/dev/zero
 
186
 
 
187
        fdisk -b 512 -C $CYLINDERS -H $HEADS -S $SECTORS "$IMAGE" > /dev/null 2>&1 << EOF
 
188
n
 
189
p
 
190
1
 
191
1
 
192
 
 
193
a
 
194
1
 
195
w
 
196
EOF
 
197
}
 
198
 
 
199
function makefs_image()
 
200
{
 
201
        mke2fs -N 24 -b 1024 $LOOPP $BSIZE
 
202
 
 
203
        if [ $? -ne 0 ]; then
 
204
                die "mke2fs $LOOPP failed."
 
205
        fi
 
206
}
 
207
 
 
208
function dd_rootfs_to_image()
 
209
{
 
210
        if [ ! "$ROOTFS" ]; then
 
211
                die "Must specify rootfs image to use."
 
212
        fi
 
213
 
 
214
        dd if="$ROOTFS" of="$LOOPP" > /dev/null 2>&1
 
215
        if [ $? -ne 0 ]; then
 
216
                die "Failed to dd $ROOTFS to $LOOPP."
 
217
        fi
 
218
 
 
219
        # Resize fs to use full partition
 
220
        e2fsck -f $LOOPP 
 
221
        resize2fs $LOOPP
 
222
        if [ $? -ne 0 ]; then
 
223
                die "Failed to resize rootfs on $LOOPP."
 
224
        fi
 
225
}
 
226
 
 
227
function get_kernel()
 
228
{
 
229
        # look in /boot for an existing kernel
 
230
        local -a kernels=( `ls /boot | grep vmlinuz` )
 
231
        local k
 
232
 
 
233
        for k in ${kernels[@]}; do
 
234
                case "$k" in
 
235
                        *xen*)
 
236
                                continue
 
237
                                ;;
 
238
                        *)
 
239
                                KERNEL="/boot/$k"
 
240
                                echo "Using kernel $KERNEL"
 
241
                                break
 
242
                                ;;
 
243
                esac
 
244
        done
 
245
}
 
246
 
 
247
function copy_kernel_to_image()
 
248
{
 
249
        if [ ! "$KERNEL" ]; then
 
250
                get_kernel || die "Couldn't find a kernel to use."
 
251
        fi
 
252
 
 
253
        mkdir "$MNT/boot"
 
254
 
 
255
        cp "$KERNEL" "$MNT/boot"
 
256
}
 
257
 
 
258
function copy_netdriver_to_image()
 
259
{
 
260
        local kernel=`basename $KERNEL`
 
261
        local kversion=$( echo $kernel | sed 's/^vmlinuz-//' )
 
262
        local fdir="/lib/modules/$kversion/kernel/drivers/net"
 
263
                                                                                
 
264
        mkdir "$MNT/lib/modules"
 
265
        if [ -e "$DRVDIR" ]; then
 
266
                if [ -e "$DRVDIR/$NETDRV" ]; then
 
267
                        cp $DRVDIR/mii.ko $MNT/lib/modules
 
268
                        cp $DRVDIR/$NETDRV $MNT/lib/modules
 
269
                else
 
270
                        die "Failed to find $NETDRV at $DRVDIR."
 
271
                fi
 
272
        elif [ -e "$fdir/$NETDRV" ]; then
 
273
                cp $fdir/mii.ko $MNT/lib/modules
 
274
                cp $fdir/$NETDRV $MNT/lib/modules
 
275
        else
 
276
                die "Xm-test requires at minimum the 8139too.ko driver to run."
 
277
        fi
 
278
 
 
279
        # Make sure that modules will be installed
 
280
        if [ -e "$MNT/etc/init.d/rcS" ]; then
 
281
                echo "insmod /lib/modules/mii.ko" >> $MNT/etc/init.d/rcS
 
282
                echo "insmod /lib/modules/$NETDRV" >> $MNT/etc/init.d/rcS
 
283
        else
 
284
                die "Failed to add insmod command to rcS file on image."
 
285
        fi
 
286
}
 
287
 
 
288
function lilo_image()
 
289
{
 
290
        local kernel=`basename $KERNEL`
 
291
 
 
292
        (
 
293
        cat <<EOC
 
294
boot=$LOOPD
 
295
delay=10
 
296
geometric
 
297
map=$MNT/boot/map
 
298
disk=$LOOPD
 
299
        bios=0x80
 
300
        sectors=$SECTORS
 
301
        heads=$HEADS
 
302
        cylinders=$CYLINDERS
 
303
        partition=$LOOPP
 
304
                start=$SECTORS
 
305
image=$MNT/boot/$kernel
 
306
        append="root=0301 console=tty0 console=ttyS0"
 
307
#       append="root=0301"
 
308
        label=Linux
 
309
        read-only
 
310
EOC
 
311
        ) > "/$MNT/boot/$LCONF"
 
312
}
 
313
 
 
314
function install_lilo()
 
315
{
 
316
        lilo -C "$MNT/boot/$LCONF"
 
317
        if [ $? -ne 0 ]; then
 
318
                die "Failed to install $MNT/boot/$LCONF."
 
319
        fi
 
320
}
 
321
 
 
322
function add_getty_to_inittab()
 
323
{
 
324
        local itab=$MNT/etc/inittab
 
325
 
 
326
        if [ -e "$itab" ]; then
 
327
                echo "# Start getty on serial line" >> $itab
 
328
                echo "S0:12345:respawn:/sbin/getty ttyS0" >> $itab
 
329
        fi
 
330
}
 
331
 
 
332
 
 
333
# Main starts here
 
334
initialize_globals
 
335
check_dependencies
 
336
 
 
337
get_options "$@"
 
338
 
 
339
create_disk_image
 
340
 
 
341
# Get the first free loop device
 
342
ldev=$(get_loopd)
 
343
LOOPD="/dev/loop$ldev"
 
344
losetup_image $LOOPD
 
345
 
 
346
# Now associate where the partition will go
 
347
ldev=$(get_loopd)
 
348
LOOPP="/dev/loop$ldev"
 
349
losetup_image $LOOPP $OFFSET
 
350
 
 
351
makefs_image
 
352
 
 
353
dd_rootfs_to_image
 
354
 
 
355
if [ -e "$MNT" ]; then
 
356
        rm -Rf "$MNT"
 
357
fi
 
358
 
 
359
mkdir "$MNT";
 
360
if [ $? -ne 0 ]; then
 
361
        die "Failed to create temporary mount point $MNT."
 
362
fi
 
363
 
 
364
mount "$LOOPP" "$MNT";
 
365
if [ $? -ne 0 ]; then
 
366
        die "Failed to mount $LOOPP on $MNT."
 
367
fi
 
368
 
 
369
copy_kernel_to_image
 
370
if [ ! "$DRVDIR" = "builtin" ]; then
 
371
        copy_netdriver_to_image
 
372
fi
 
373
#add_getty_to_inittab
 
374
 
 
375
lilo_image
 
376
install_lilo
 
377
 
 
378
umount "$MNT"
 
379
rm -Rf "$MNT";
 
380
 
 
381
losetup -d $LOOPD
 
382
losetup -d $LOOPP
 
383
 
 
384
exit 0