]>
git.proxmox.com Git - qemu-server.git/blob - qmupdate
8 # script to upgrade V0.9.1 to V0.9.2 format
10 my $confvars_0_9_1 = {
14 cpulimit
=> 'natural',
15 cpuunits
=> 'natural',
31 sub load_config_0_9_1
{
32 my ($vmid, $filename) = @_;
34 my $fh = new IO
::File
($filename, "r") ||
39 while (my $line = <$fh>) {
41 next if $line =~ m/^\#/;
43 next if $line =~ m/^\s*$/;
45 if ($line =~ m/^([a-z]+):\s*(\S+)\s*$/) {
48 if (my $type = $confvars_0_9_1->{$key}) {
49 $res->{$key} = $value;
51 return undef; # unknown setting
59 sub parse_network_0_9_1
{
65 foreach my $rec (split (/\s*,\s*/, $data)) {
66 if ($rec =~ m/^(tap|user)$/) {
68 } elsif ($rec =~ m/^model\s*=\s*(ne2k_pci|e1000|rtl8139|pcnet|virtio|ne2k_isa|i82551|i82557b|i82559er)$/) {
70 } elsif ($rec =~ m/macaddr\s*=\s*([0-9a-f:]+)/i) {
80 sub random_ether_addr
{
82 my $rand = Digest
::SHA
::sha1_hex
(rand(), time());
85 for (my $i = 0; $i < 6; $i++) {
86 my $ss = hex (substr ($rand, $i*2, 2));
88 $ss &= 0xfe; # clear multicast
89 $ss |= 2; # set local id
91 $ss = sprintf ("%02X", $ss);
103 sub convert_0_9_1_to_0_9_2
{
104 my ($vmid, $cfile, $conf) = @_;
106 print "Upgrading VM $vmid to new format\n";
108 die "undefined vm id" if !$vmid || $vmid !~ m/^\d+$/;
117 my $tmpdir = "/var/lib/vz/images/$vmid.upgrade";
118 my $tmpconf = "$cfile.upgrade";
123 mkdir $tmpdir || die "unable to create dir '$tmpdir'\n";
125 my $fh = new IO
::File
($cfile, "r") ||
126 die "unable to read config for VM $vmid\n";
127 my $newfh = new IO
::File
($tmpconf, "w") ||
128 die "unable to create file '$tmpconf'\n";
130 while (my $line = <$fh>) {
132 next if $line =~ m/^\#/;
134 next if $line =~ m/^\s*$/;
136 if ($line =~ m/^([a-z]+):\s*(\S+)\s*$/) {
139 if (my $type = $confvars_0_9_1->{$key}) {
140 if ($key eq 'network') {
141 my $onw = parse_network_0_9_1
($value);
142 if ($onw && ($onw->{type
} eq 'tap')) {
143 if (!$onw->{macaddr
}) {
144 $onw->{macaddr
} = random_ether_addr
();
146 print $newfh "vlan0: $onw->{model}=$onw->{macaddr}\n";
147 } elsif ($onw && ($onw->{type
} eq 'user')) {
148 if (!$onw->{macaddr
}) {
149 $onw->{macaddr
} = random_ether_addr
();
151 print $newfh "vlanu: $onw->{model}=$onw->{macaddr}\n";
153 die "unable to convert network specification\n";
155 } elsif ($key eq 'cdrom') {
157 print $newfh "ide2: $value,media=cdrom\n";
158 } elsif (defined ($dmap->{$key})) {
159 if ($value =~ m
|^/var/lib
/vz/images
/([^/]+)$|) {
161 } elsif ($value !~ m
|/|) {
164 die "wrong image path";
167 link "/var/lib/vz/images/$value", "$tmpdir/$value";
169 (-f
"$tmpdir/$value") ||
170 die "unable to create image link\n";
172 push @$images, $value;
174 print $newfh "$dmap->{$key}: $value\n";
176 print $newfh "$key: $value\n";
179 die "unknown setting '$key'\n";
185 print $newfh "bootdisk: ide0\n";
186 } elsif ($conf->{hdb
}) {
187 print $newfh "bootdisk: ide1\n";
188 } elsif ($conf->{sda
}) {
189 print $newfh "bootdisk: scsi0\n";
190 } elsif ($conf->{sdb
}) {
191 print $newfh "bootdisk: scsi1\n";
198 system ("rm -rf $tmpdir $tmpconf");
201 if (!rename $tmpdir, "/var/lib/vz/images/$vmid") {
202 system ("rm -rf $tmpdir $tmpconf");
203 die "commiting '/var/lib/vz/images/$vmid' failed - $!\n";
205 if (!rename $tmpconf, $cfile) {
206 system ("rm -rf /var/lib/vz/images/$vmid $tmpconf");
207 die "commiting new configuration '$cfile' failed - $!\n";
210 foreach my $img (@$images) {
211 unlink "/var/lib/vz/images/$img";
217 foreach my $vmconf (</etc/qemu-server
/*.conf
>) {
218 next if $vmconf !~ m
|/etc/qemu-server
/(\d
+)\
.conf
|;
220 next if -d
"/var/lib/vz/images/$vmid"; # already new format
223 my $res = load_config_0_9_1
($vmid, $vmconf);
225 if ($res && ($res->{network
} || $res->{hda
} || $res->{hdb
} ||
226 $res->{sda
} || $res->{sda
} || $res->{cdrom
})) {
227 convert_0_9_1_to_0_9_2
($vmid, $vmconf, $res);