1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
|
#!/bin/bash
# defaults
dir=/home/wwoods/test/qemu/dracut
dracutdir=/home/wwoods/src/dracut
anacondadir=/home/wwoods/src/anaconda
ram=768
disk=$dir/disk.img
disk2=$dir/disk2.img
#cdrom=$dir/iso/Fedora-17-x86_64-DVD.iso
cdrom=$dir/iso/fedora-17-x86_64-netinst.iso
#graphics="spice"
graphics="vnc"
dracut_opts="--add anaconda --omit plymouth"
usecd="true"
ARGS=$(getopt --options "dcstm:" \
--longoptions "fromcd,nocd,virtconsole,serialconsole,text,ram:" \
--name testboot.sh \
-- "$@") || exit 1
eval set -- "$ARGS"
while :; do case "$1" in
-d|--fromcd) fromcd="true"; shift;;
--nocd) usecd=""; shift;;
-c|--virtconsole) console="--console pty"; shift;;
-s|--serialconsole) console="--serial pty"; shift;;
-t|--text) graphics="none"; shift;;
-m|--ram) ram="$2"; shift 2;;
--) shift; break;;
*) echo "getopt parse error"; exit 1 ;;
esac; done
if [ ! -f $cdrom ]; then
echo "Can't find cdrom!"
exit 1
fi
[ $(id -u) = 0 ] || { echo "be root."; exit 1; }
chcon -t virt_content_t $cdrom
initrd_unpack() {
local cat=cat img="$1" outdir="$2"
case `file $1` in
*:\ gzip*) cat=zcat ;;
*:\ xz*|*:\ XZ*) cat=xzcat ;;
esac
rm -rf $outdir $outdir.tmp; mkdir -p $outdir.tmp
( cd $outdir.tmp ; $cat $img | cpio -iumd 2>/dev/null )
mv $outdir.tmp $outdir
}
get_modules() {
echo -n "finding kernel version..."
moddir=$(find $1 -name "modules.dep")
moddir=${moddir%/modules.dep}
kv=${moddir##*/}
echo "$kv" | tee $dir/vmlinuz.kv
if [ ! -d /lib/modules/$kv ]; then
echo -n "getting modules..."
cp -a $moddir /lib/modules/$kv
echo "done"
fi
}
apply_updates() {
(
cd $dir/updates
find . -depth -type d | while read d; do
[ ! -d "../initramfs/$d" ] && mkdir -p "../initramfs/$d"
done
find . -depth \! -type d | while read f; do
cp -a "$f" "../initramfs/$f"
echo " ${f#.}"
done
)
}
initrd_cpio() {
( cd $dir/initramfs; find . | cpio -co ) 2>/dev/null
}
# TODO: check for $dracutdir/dracut.sh and git pull otherwise
setup_dracut() {
if ! [ -f $dracutdir/modules.d/80anaconda/parse-kickstart ]; then
if [ ! -d $anacondadir/dracut ]; then
# TODO: git pull
echo "ERROR: can't find anaconda dracut module :/"
exit 1
else
ln -s $anacondadir/dracut $dracutdir/modules.d/80anaconda
fi
fi
}
grab_kernel_and_initrd() {
local cdrom="$1" kernel="$2" initrd="$3" mnt="$dir/.mnt"
umount $mnt &>/dev/null
mkdir -p $mnt
mount -o ro,loop $cdrom $mnt || { echo "failed to mount cdrom!"; exit 1; }
cp -f $mnt/isolinux/vmlinuz $kernel
cp -f $mnt/isolinux/initrd.img $initrd
umount $mnt
rmdir $mnt
}
if [ "$fromcd" != "true" ]; then
kernel_args="$@"
# check RAM
[ "$ram" -lt 768 ] && kernel_args="$* inst.memcheck=0"
# do we need to pull out the kernel/modules?
vmlinuz="$dir/vmlinuz"
[ -e $vmlinuz.kv ] && kv=$(< $vmlinuz.kv)
if [ $cdrom -nt $vmlinuz -o -z "$kv" -o ! -d /lib/modules/$kv ]; then
echo "getting kernel and modules"
grab_kernel_and_initrd $cdrom $vmlinuz $dir/initrd.img.orig
initrd_unpack $dir/initrd.img.orig $dir/initramfs.orig
get_modules $dir/initramfs.orig # sets $kv
# force rebuild of initrd.img
rm -rf $dir/initramfs
fi
# do we need to create the initramfs dir?
initrd="$dir/initrd.img"
if [ ! -d $dir/initramfs ]; then
echo "rebuilding initramfs"
setup_dracut
(
cd $dracutdir
./dracut.sh --local --no-compress --nomdadmconf --nolvmconf \
--prefix /run/initramfs $dracut_opts \
--force $dir/.initrd.cpio $kv
) || exit 1
initrd_unpack $dir/.initrd.cpio $dir/initramfs
cp $dir/initramfs.orig/.buildstamp $dir/initramfs/.buildstamp
# force rebuild of initrd.img
rm -rf $initrd
fi
# do we need to build initrd.img from initramfs?
if [ ! -e $initrd ] || \
find initramfs updates -newer $initrd | grep -q .; then
if [ -d updates ]; then
echo "applying updates to initramfs"
apply_updates
fi
echo "making initrd.img"
initrd_cpio | xz -1 --check=crc32 > $dir/.initrd.tmp || exit 1
mv -f $dir/.initrd.tmp $initrd
fi
bootarg="kernel=$vmlinuz,initrd=$initrd,kernel_args=$kernel_args"
fi
echo "Creating 'testboot' guest..."
virsh destroy testboot &>/dev/null
virsh undefine testboot &>/dev/null
virt-install --name testboot --ram $ram --graphics $graphics \
--os-type linux --os-variant fedora16 $console \
--disk $disk,size=10 --disk $disk2,size=10 \
--network network=default,model=virtio,mac=52:54:00:03:31:38 \
--network network=default,model=virtio,mac=52:54:00:03:31:39 \
${usecd:+--disk $cdrom,device=cdrom} \
${bootarg:+--boot "$bootarg"}
virsh destroy testboot &>/dev/null
# historical interest: this is how it'd work in qemu
do_qemu() {
echo "starting qemu"
echo "C-a x to exit, C-a c for console monitor"
qemu-kvm -m $ram -kernel $kernel -initrd $initrd \
-cdrom $cdrom -nographic -append $kernel_args
}
|