~ubuntu-virt/ubuntu/maverick/eucalyptus/2.0

444.1.69 by root
added license header to source files (not clc/)
1
#!/usr/bin/perl
2
#Copyright (c) 2009  Eucalyptus Systems, Inc.	
3
#
4
#This program is free software: you can redistribute it and/or modify
5
#it under the terms of the GNU General Public License as published by 
6
#the Free Software Foundation, only version 3 of the License.  
7
# 
8
#This file is distributed in the hope that it will be useful, but WITHOUT
9
#ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10
#FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
11
#for more details.  
12
#
13
#You should have received a copy of the GNU General Public License along
14
#with this program.  If not, see <http://www.gnu.org/licenses/>.
15
# 
16
#Please contact Eucalyptus Systems, Inc., 130 Castilian
17
#Dr., Goleta, CA 93101 USA or visit <http://www.eucalyptus.com/licenses/> 
18
#if you need additional information or have any questions.
19
#
20
#This file may incorporate work covered under the following copyright and
21
#permission notice:
22
#
23
#  Software License Agreement (BSD License)
24
#
25
#  Copyright (c) 2008, Regents of the University of California
26
#  
27
#
28
#  Redistribution and use of this software in source and binary forms, with
29
#  or without modification, are permitted provided that the following
30
#  conditions are met:
31
#
32
#    Redistributions of source code must retain the above copyright notice,
33
#    this list of conditions and the following disclaimer.
34
#
35
#    Redistributions in binary form must reproduce the above copyright
36
#    notice, this list of conditions and the following disclaimer in the
37
#    documentation and/or other materials provided with the distribution.
38
#
39
#  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
40
#  IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
41
#  TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
42
#  PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
43
#  OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
44
#  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
45
#  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
46
#  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
47
#  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
48
#  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
49
#  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. USERS OF
50
#  THIS SOFTWARE ACKNOWLEDGE THE POSSIBLE PRESENCE OF OTHER OPEN SOURCE
51
#  LICENSED MATERIAL, COPYRIGHTED MATERIAL OR PATENTED MATERIAL IN THIS
52
#  SOFTWARE, AND IF ANY SUCH MATERIAL IS DISCOVERED THE PARTY DISCOVERING
53
#  IT MAY INFORM DR. RICH WOLSKI AT THE UNIVERSITY OF CALIFORNIA, SANTA
54
#  BARBARA WHO WILL THEN ASCERTAIN THE MOST APPROPRIATE REMEDY, WHICH IN
55
#  THE REGENTS’ DISCRETION MAY INCLUDE, WITHOUT LIMITATION, REPLACEMENT
56
#  OF THE CODE SO IDENTIFIED, LICENSING OF THE CODE SO IDENTIFIED, OR
57
#  WITHDRAWAL OF THE CODE CAPABILITY TO THE EXTENT NEEDED TO COMPLY WITH
58
#  ANY SUCH LICENSES OR RIGHTS.
59
#
60
25.2.1 by root
added KVM support (driver)
61
#
62
# this generates an XML template for libvirt domain specification,
63
# which is tailored to an instance by the node controller
64
65
use diagnostics;
66
use warnings; 
67
use sigtrap;
68
use strict;
69
use English; # for descriptive predefined var names
70
use Data::Dumper; # for debugging: print Dumper($var)
71
use Getopt::Long;
72
use FindBin;
73
$OUTPUT_AUTOFLUSH = 1; # no output buffering
74
75
our $use_ramdisk = 0;
444.67.12 by root
applied gdiff from 1.6.2-g
76
our $local_kvm="";
444.76.1 by root
First cut of virtio implementation.
77
our $use_virtio_net = 0;
78
our $use_virtio_root = 0;
1108.1.3 by Dave Walker (Daviey)
* New upstream bug fix snapshot, -r1241
79
our $base_path = "";
80
our $multi_boot_floppy = 0;
444.67.12 by root
applied gdiff from 1.6.2-g
81
82
if ( -x "/usr/bin/kvm" ) {
83
    $local_kvm="/usr/bin/kvm";
84
} elsif ( -x "/usr/bin/qemu-kvm" ) {
85
    $local_kvm="/usr/bin/qemu-kvm";
86
} else {
87
    $local_kvm="kvm";
88
}
25.2.1 by root
added KVM support (driver)
89
90
# deal with command-line parameters
91
GetOptions('ramdisk'        => sub { $use_ramdisk = 1; },
35.1.9 by Dmitrii Zagorodnov
added support for KVM swap and ephemeral disk to handlers_* and storage.c
92
           'ephemeral'      => sub { }, # option ignored 
444.76.1 by root
First cut of virtio implementation.
93
           'virtionet'      => sub { $use_virtio_net = 1; },
94
           'virtioroot'      => sub { $use_virtio_root = 1; },
1108.1.3 by Dave Walker (Daviey)
* New upstream bug fix snapshot, -r1241
95
           'basepath=s'      => \$base_path,
25.2.1 by root
added KVM support (driver)
96
		   ) or exit (1);
97
1108.1.3 by Dave Walker (Daviey)
* New upstream bug fix snapshot, -r1241
98
if ($base_path && &is_multiboot_img($base_path . "/kernel")) {
99
   $multi_boot_floppy = 1;
100
}
101
25.2.1 by root
added KVM support (driver)
102
print <<EOF;
103
<domain type='kvm'>
104
    <name>NAME</name>
105
    <os>
106
    <type>hvm</type>
107
EOF
108
1108.1.3 by Dave Walker (Daviey)
* New upstream bug fix snapshot, -r1241
109
if ( $multi_boot_floppy ) {
110
    my $mb_load_cmd = "/usr/share/eucalyptus/mk-mb-loader " .
111
        "'" . $base_path . "/kernel' '" .  $base_path .  "/loader' 2>&1";
112
    my $mb_out = `$mb_load_cmd`;
113
    if ( $? != 0 ) { print("failed to make loader\n${mb_out}\n"); exit(1); }
114
    print "        <boot dev='fd'/>\n";
115
    print "        <boot dev='hd'/>\n";
444.76.1 by root
First cut of virtio implementation.
116
} else {
1108.1.3 by Dave Walker (Daviey)
* New upstream bug fix snapshot, -r1241
117
    print "        <kernel>BASEPATH/kernel</kernel>\n";
118
    if ( $use_ramdisk ) {
119
        print "        <initrd>BASEPATH/ramdisk</initrd>\n";
120
    }
121
122
    if ( $use_virtio_root ) {
123
        print "        <cmdline>root=/dev/vda1 console=ttyS0</cmdline>\n"
124
    } else {
125
        print "        <cmdline>root=/dev/sda1 console=ttyS0</cmdline>\n"
126
    }
444.76.1 by root
First cut of virtio implementation.
127
}
128
25.2.1 by root
added KVM support (driver)
129
print <<EOF;
130
    </os>
25.1.35 by root
neilnull
131
    <features>
132
        <acpi/>
133
    </features>
25.2.1 by root
added KVM support (driver)
134
    <memory>MEMORY</memory>
135
    <vcpu>VCPUS</vcpu>
136
    <devices>
1050.2.1 by Dave Walker (Daviey)
Ubuntu patches unapplied
137
        <emulator>$local_kvm</emulator>
444.76.1 by root
First cut of virtio implementation.
138
EOF
139
1108.1.3 by Dave Walker (Daviey)
* New upstream bug fix snapshot, -r1241
140
if ( $multi_boot_floppy ) {
141
   print <<EOF
142
        <disk type='file' device='floppy'>
143
            <source file='BASEPATH/loader'/>
144
            <target dev='fda' bus='fdc'/>
145
        </disk>
146
EOF
147
}
148
444.76.1 by root
First cut of virtio implementation.
149
if ( $use_virtio_root ) {
150
    print <<EOF;
151
        <disk type='file' device='disk'>
152
            <source file='BASEPATH/disk'/>
153
            <target dev='vda' bus='virtio'/>
154
        </disk>
155
EOF
156
} else {
157
    print <<EOF;
25.2.1 by root
added KVM support (driver)
158
        <disk type='file'>
25.4.1 by Dmitrii Zagorodnov
moved partition-to-disk invocation into caching code, updated the XML
159
            <source file='BASEPATH/disk'/>
25.2.1 by root
added KVM support (driver)
160
            <target dev='sda'/>
161
        </disk>
444.76.1 by root
First cut of virtio implementation.
162
EOF
163
}
164
165
if ( $use_virtio_net ) {
166
    print <<EOF;
167
        <interface type='bridge'>
168
            <source bridge='BRIDGEDEV'/>
169
            <mac address='PRIVMACADDR'/>
170
            <model type='virtio'/>
171
        </interface>
172
EOF
173
} else {
174
    print <<EOF;
25.2.1 by root
added KVM support (driver)
175
        <interface type='bridge'>
176
            <source bridge='BRIDGEDEV'/>
177
            <mac address='PRIVMACADDR'/>
426 by root
switched default kvm ethernet interface to e1000, added preliminary support for post iptables rules
178
            <model type='e1000'/>
25.2.1 by root
added KVM support (driver)
179
        </interface>
444.76.1 by root
First cut of virtio implementation.
180
EOF
181
}
182
183
print <<EOF;
25.2.1 by root
added KVM support (driver)
184
        <serial type="file">
185
            <source path='BASEPATH/console.log'/>
186
            <target port='1'/>
187
        </serial>
188
    </devices>
189
</domain>
190
EOF
1108.1.3 by Dave Walker (Daviey)
* New upstream bug fix snapshot, -r1241
191
192
# This checks if a binary looks like a multiboot image
193
# The check is similar to grub 0.97's mbchk and to kvm's multiboot
194
# check in hw/multiboot.c:load_multiboot
195
sub is_multiboot_img
196
{
197
    my $kpath=$_[0];
198
    my $buf;
199
    my $found = 0;
200
    my $header_magic = 0x1BADB002;
201
    my $header_size = 32;     # there are 8 uint32 values
202
    my $search_bytes = 8192;  # copied from kvm and grub's mbchk
203
    my $magic = 0;
204
    my $flags = 0;
205
    my $checksum = 0;
206
    my $count = 0;
207
208
    if (! (-s $kpath)) { return(0); }
209
    open INF, $kpath or return(0);
210
    binmode INF;
211
    if (!($count = read (INF, $buf, $search_bytes))) {
212
        close INF;
213
        return(0);
214
    }
215
    close INF;
216
    for(my $i=0;$i < ($count - $header_size);$i=$i+4) {
217
        if (unpack("V",substr($buf,$i,4)) == $header_magic ) {
218
            $found=$i; last;
219
        }
220
    }
221
222
    if ($found == 0) { return(0); }
223
    ($magic,$flags,$checksum) = unpack("VVV",substr($buf,$found,4*3));
224
    # magic + flags + checksum == (uint32) 0 == 2**32
225
    if(($magic+$flags+$checksum) % 2**32 == 0 ) {
226
        return(1);
227
    }
228
    return(0);
229
}