correct the script name in instructions
[ovirt-node-image.git] / create-ovirt-iso-nodes
blobd74dc4dd64521c59faff1a7a465eddbfd3564a71
1 #!/bin/bash
3 # Create fake oVirt Nodes for testing CDROM boot
4 # Copyright 2008 Red Hat, Inc.
5 # Written by Perry Myers <pmyers@redhat.com>
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; version 2 of the License.
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 Library General Public License for more details.
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.
20 PATH=$PATH:/sbin:/usr/sbin
22 ME=$(basename "$0")
23 warn() { printf '%s: %s\n' "$ME" "$*" >&2; }
24 try_h() { printf "Try \`$ME -h' for more information.\n" >&2; }
25 die() { warn "$@"; try_h; exit 1; }
27 BRIDGENAME=ovirtbr0
28 IMGDIR_DEFAULT=/var/lib/libvirt/images
29 imgdir=$IMGDIR_DEFAULT
30 NODEIMG_DEFAULT=/usr/share/ovirt-node-image/ovirt-node-image.iso
31 nodeimg=$NODEIMG_DEFAULT
32 NUM_DISKS_DEFAULT=1
33 RANGE_DEFAULT="6-9"
34 RAM_DEFAULT=512
35 VCPUS_DEFAULT=1
37 NODE_DISK_FMT=qcow2
38 NODE_DISK_SIZE=6144M
40 gen_fake_managed_node() {
41 local num=$1
42 local src_nodeimg=$2
43 local last_mac=$(( 54 + $num ))
45 local os_variant=fedora10
46 if [ "$no_virtio" = 1 ]; then
47 os_variant=fedora8
50 echo "Creating fake node$num using $nodeimg..."
51 local dest_nodeimg="$imgdir/node${num}-$(basename $src_nodeimg)"
52 echo "$src_nodeimg -> $dest_nodeimg"
53 rsync -av $src_nodeimg $dest_nodeimg
55 virsh destroy node$num > /dev/null 2>&1
56 virsh undefine node$num > /dev/null 2>&1
58 local disks=
59 for ((i=0;i<$num_disks;i+=1)); do
60 qemu-img create -f $NODE_DISK_FMT \
61 $imgdir/node${num}-${i}.$NODE_DISK_FMT $NODE_DISK_SIZE
62 disks="$disks --disk path=$imgdir/node${num}-${i}.$NODE_DISK_FMT"
63 done
65 # FIXME: virt-install should be changed to have a --nostart parameter
66 # that just defines the VM w/o starting it.
67 virt-install --name=node$num --ram=$ram --vcpus=$vcpus $disks \
68 --cdrom=$dest_nodeimg --livecd \
69 --network=bridge:$BRIDGENAME --mac=00:16:3e:12:34:$last_mac \
70 --vnc --accelerate --hvm --noautoconsole \
71 --os-type=linux --os-variant=$os_variant \
72 --force --noreboot
73 virsh destroy node$num > /dev/null 2>&1
74 echo "node$num created"
77 usage() {
78 case $# in 1) warn "$1"; try_h; exit 1;; esac
79 cat <<EOF
80 Usage: $ME [-d image_dir] [-n node.iso] [-c num_disks] [-s start-stop]
81 [-v vcpus] [-r ram] [-x]
82 -n: node.iso to boot (default: $NODEIMG_DEFAULT)
83 -d: directory to place virtual disk (default: $IMGDIR_DEFAULT)
84 -c: number of disks per fake node (default: $NUM_DISKS_DEFAULT)
85 -s: node range (default: $RANGE_DEFAULT)
86 -v: vcpus per node (default: $VCPUS_DEFAULT)
87 -r: ram in MB per node (default: $RAM_DEFAULT)
88 -x: toggle virtio devices off
89 -h: display this help and exit
90 EOF
93 err=0 help=0
94 no_virtio=0
95 num_disks=$NUM_DISKS_DEFAULT
96 range=$RANGE_DEFAULT
97 ram=$RAM_DEFAULT
98 vcpus=$VCPUS_DEFAULT
99 while getopts :d:n:s:c:v:r:xh c; do
100 case $c in
101 n) nodeimg=$OPTARG;;
102 d) imgdir=$OPTARG;;
103 c) num_disks=$OPTARG;;
104 s) range=$OPTARG;;
105 v) vcpus=$OPTARG;;
106 r) ram=$OPTARG;;
107 x) no_virtio=1;;
108 h) help=1;;
109 '?') err=1; warn "invalid option: \`-$OPTARG'";;
110 :) err=1; warn "missing argument to \`-$OPTARG' option";;
111 *) err=1; warn "internal error: \`-$OPTARG' not handled";;
112 esac
113 done
114 test $err = 1 && { try_h; exit 1; }
115 test $help = 1 && { usage; exit 0; }
117 # first, check to see we are root
118 if [ $( id -u ) -ne 0 ]; then
119 die "Must run as root"
122 mkdir -p $imgdir
124 test -f $nodeimg || die "could not find $nodeimg"
126 # define the fake managed nodes we will use.
127 range_start=$(echo $range | cut -d '-' -f 1)
128 range_stop=$(echo $range | cut -d '-' -f 2)
130 for i in `seq $range_start $range_stop` ; do
131 gen_fake_managed_node $i $nodeimg
132 done