summaryrefslogtreecommitdiffstats
path: root/testboot.sh
blob: 1ec7e32222b3f4ef9dd21834dbf01d882c243b56 (plain)
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
#!/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"

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


get_modules() {
    local cat=cat
    case `file $1` in
        *:\ gzip*)       cat=zcat ;;
        *:\ xz*|*:\ XZ*)  cat=xzcat ;;
    esac
    echo -n "finding kernel version..."
    moddir=$($cat $1 | cpio --quiet -t */modules.dep)
    moddir=${moddir%/modules.dep}
    kv=${moddir##*/}
    echo "$kv"
    if [ ! -d /lib/modules/$kv ]; then
        echo -n "getting modules..."
        tmpdir=$dir/.modules.tmp
        mkdir -p $tmpdir
        ( cd $tmpdir; $cat $1 | cpio --quiet -iumd $moddir/* )
        mv $tmpdir/$moddir /lib/modules/$kv
        rm -rf $tmpdir
        echo "done"
    fi
    if [ ! -f $dir/.buildstamp ]; then
        echo -n "getting buildstamp..."
        $cat $1 | cpio --quiet -ium .buildstamp
        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
}

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"
        umount .mnt &>/dev/null
        mkdir -p $dir/.mnt
        if mount -o ro,loop $cdrom $dir/.mnt; then
            cp -f $dir/.mnt/isolinux/vmlinuz $vmlinuz
            cp -f $dir/.mnt/isolinux/initrd.img initrd.img.orig
            get_modules $dir/.mnt/isolinux/initrd.img # sets $kv
            umount $dir/.mnt
            [ -z "$kv" ] && echo "couldn't figure out kernel ver??" && exit 1
            echo "$kv" > $vmlinuz.kv
        fi
        rmdir $dir/.mnt
        rm -rf $dir/initramfs
    fi

    # do we need to rebuild initrd?
    initrd="$dir/initrd.img"
    if [ ! -d $dir/initramfs ] || [ $cdrom -nt $dir/initramfs ]; then
        echo "rebuilding initramfs"
        # TODO: check for $dracutdir/dracut.sh and git pull otherwise
        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
        rm -rf $dir/initramfs $initrd
        (
            cd $dracutdir
            ./dracut.sh --local --no-compress --nomdadmconf --nolvmconf \
                             --prefix /run/initramfs --add anaconda \
                             --omit plymouth \
                             $dir/.initrd.cpio $kv
        ) || exit 1
        rm -rf $dir/.initramfs.tmp; mkdir $dir/.initramfs.tmp
        ( cd $dir/.initramfs.tmp; cpio -iumd ) < $dir/.initrd.cpio
        rm -f $dir/.initrd.cpio
        mv $dir/.initramfs.tmp $dir/initramfs
        cp $dir/.buildstamp $dir/initramfs/.buildstamp
    fi

    if [ ! -e $initrd ] || \
      find initramfs updates -newer $initrd | grep -q .; then
        echo "applying updates to initramfs"
        apply_updates
        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 \
  ${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
}