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 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
|
#
# (c) Jan Gehring <jan.gehring@gmail.com>
#
package Rex::Virtualization::VBox::create;
use v5.14.4;
use warnings;
our $VERSION = '1.16.1'; # VERSION
use Rex::Logger;
use Rex::Commands::Gather;
use Rex::Hardware;
use Rex::Commands::Fs;
use Rex::Helper::Run;
use Rex::Commands::File;
use Rex::File::Parser::Data;
use Rex::Template;
use XML::Simple;
use Data::Dumper;
sub execute {
my ( $class, $name, %opt ) = @_;
my $opts = \%opt;
$opts->{name} = $name;
$opts->{type} ||= "Linux26"; # default to Linux 2.6
unless ($opts) {
die("You have to define the create options!");
}
_set_defaults($opts);
i_run "VBoxManage createvm --name \""
. $name
. "\" --ostype \""
. $opts->{type}
. "\" --register";
### add controller
i_run
"VBoxManage storagectl \"$name\" --name \"SATA Controller\" --add sata --controller IntelAhci";
i_run
"VBoxManage storagectl \"$name\" --name \"IDE Controller\" --add ide --controller PIIX4";
### create hds
my $hdd_ports = {
"sata" => 0,
"ide" => 0,
};
for my $hd ( @{ $opts->{storage} } ) {
if ( $hd->{type} eq "file" ) {
my $filename = $hd->{file};
my $size = $hd->{size};
my $format = $hd->{format};
if ( !$filename ) { die("You have to specify 'file'."); }
if ( $hd->{device} eq "disk" ) {
if ( !-f $filename ) {
i_run
"VBoxManage createhd --filename \"$filename\" --size $size --format $format 2>&1";
}
i_run
"VBoxManage storageattach \"$name\" --storagectl \"SATA Controller\" --port "
. $hdd_ports->{sata}
. " --device 0 --type hdd --medium \"$filename\"";
$hdd_ports->{sata}++;
}
if ( $hd->{device} eq "cdrom" ) {
i_run
"VBoxManage storageattach \"$name\" --storagectl \"IDE Controller\" --port "
. $hdd_ports->{ide}
. " --device 0 --type dvddrive --medium \"$filename\"";
$hdd_ports->{sata}++;
}
}
}
# memory
i_run "VBoxManage modifyvm \"$name\" --memory " . $opts->{memory};
# cpus
i_run "VBoxManage modifyvm \"$name\" --cpus " . $opts->{cpus};
# boot
i_run "VBoxManage modifyvm \"$name\" --boot1 " . $opts->{boot};
return;
}
sub _set_defaults {
my ($opts) = @_;
if ( !exists $opts->{"name"} ) {
die("You have to give a name.");
}
if ( !exists $opts->{"storage"} ) {
die("You have to add at least one storage disk.");
}
if ( !exists $opts->{"memory"} ) {
$opts->{"memory"} = 512;
}
else {
# default is mega byte
$opts->{memory} = $opts->{memory};
}
if ( !exists $opts->{"cpus"} ) {
$opts->{"cpus"} = 1;
}
if ( !exists $opts->{"boot"} ) {
$opts->{"boot"} = "disk";
}
# normalize
if ( $opts->{boot} eq "hd" ) {
$opts->{boot} = "disk";
}
_set_storage_defaults($opts);
_set_network_defaults($opts);
}
sub _set_storage_defaults {
my ($opts) = @_;
my $store_letter = "a";
for my $store ( @{ $opts->{"storage"} } ) {
if ( !exists $store->{"type"} ) {
$store->{"type"} = "file";
}
if ( $store->{type} eq "file" && !exists $store->{format} ) {
$store->{format} = "VDI";
}
if ( !exists $store->{"size"} && $store->{"type"} eq "file" ) {
$store->{"size"} = "10G";
}
if ( $store->{type} eq "file" ) {
$store->{size} = _calc_size( $store->{size} );
}
if ( exists $store->{"file"}
&& $store->{"file"} =~ m/\.iso$/
&& !exists $store->{"device"} )
{
$store->{"device"} = "cdrom";
}
if ( !exists $store->{"device"} ) {
$store->{"device"} = "disk";
}
if ( $store->{"device"} eq "cdrom" ) {
$store->{"readonly"} = 1;
}
}
}
sub _set_network_defaults {
my ( $opts, $hyper ) = @_;
if ( !exists $opts->{"network"} ) {
$opts->{"network"} = [
{
type => "bridge",
bridge => "eth0",
},
];
}
for my $netdev ( @{ $opts->{"network"} } ) {
if ( !exists $netdev->{"type"} ) {
$netdev->{"type"} = "bridge";
}
if ( !exists $netdev->{"bridge"} ) {
$netdev->{"bridge"} = "eth0";
}
}
}
sub _calc_size {
my ($size) = @_;
my $ret_size = 0;
if ( $size =~ m/^(\d+)G$/ ) {
$ret_size = $1 * 1024;
}
elsif ( $size =~ m/^(\d+)M$/ ) {
$ret_size = $1;
}
}
1;
|