-
Notifications
You must be signed in to change notification settings - Fork 1
/
perl-ova2box.pl
48 lines (39 loc) · 1.08 KB
/
perl-ova2box.pl
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
#!/usr/bin/perl
use strict;
use warnings;
use Archive::Tar;
my $mode = '0644';
my $owner = 'root:root';
my $file;
sub trim_ovf {
my $ovf = shift;
$ovf =~ s#<ExtraData>.*?</ExtraData>\s*##s;
$ovf =~ s#<DVDImages>.*?</DVDImages>\s*##s;
$ovf =~ s#<RemoteDisplay>.*?</RemoteDisplay>\s*##s;
$ovf =~ s#<GuestProperties>.*?</GuestProperties>\s*##s;
$ovf;
}
my $arg = $ARGV[0];
$arg =~ s/\.(\w+)?$//;
my $arg_ova = "$arg.ova";
my $arg_box = "$arg.box";
$arg_box = $ARGV[1] if (defined $ARGV[1]);
my $tar = Archive::Tar->new($arg_ova) or die;
foreach $file ($tar->list_files()) {
print STDERR "$file...\n";
if ($file =~ /\.ovf$/) {
my $ovf = $tar->get_content($file);
$tar->remove($file);
$tar->add_data($file = 'box.ovf', trim_ovf($ovf));
}
$tar->chmod($file, $mode);
$tar->chown($file, $owner);
}
$tar->add_data($file = 'metadata.json', '{"provider": "virtualbox"}');
$tar->chmod($file, $mode);
$tar->chown($file, $owner);
$tar->add_data($file = 'Vagrantfile', '');
$tar->chmod($file, $mode);
$tar->chown($file, $owner);
$tar->write($arg_box, COMPRESS_GZIP);
1;