File Coverage

lib/Rex/Virtualization/VBox/create.pm
Criterion Covered Total %
statement 35 114 30.7
branch 0 48 0.0
condition 0 14 0.0
subroutine 12 17 70.5
pod 0 1 0.0
total 47 194 24.2


line stmt bran cond sub pod time code
1             #
2             # (c) Jan Gehring
3             #
4              
5             package Rex::Virtualization::VBox::create;
6              
7 1     1   14 use v5.12.5;
  1         4  
8 1     1   5 use warnings;
  1         3  
  1         124  
9              
10             our $VERSION = '1.14.3'; # VERSION
11              
12 1     1   8 use Rex::Logger;
  1         9  
  1         8  
13 1     1   34 use Rex::Commands::Gather;
  1         2  
  1         16  
14 1     1   9 use Rex::Hardware;
  1         2  
  1         19  
15 1     1   39 use Rex::Commands::Fs;
  1         3  
  1         9  
16 1     1   7 use Rex::Helper::Run;
  1         3  
  1         77  
17 1     1   7 use Rex::Commands::File;
  1         3  
  1         14  
18 1     1   8 use Rex::File::Parser::Data;
  1         5  
  1         17  
19 1     1   37 use Rex::Template;
  1         3  
  1         10  
20              
21 1     1   36 use XML::Simple;
  1         3  
  1         11  
22              
23 1     1   81 use Data::Dumper;
  1         3  
  1         1108  
24              
25             sub execute {
26 0     0 0   my ( $class, $name, %opt ) = @_;
27              
28 0           my $opts = \%opt;
29 0           $opts->{name} = $name;
30 0   0       $opts->{type} ||= "Linux26"; # default to Linux 2.6
31              
32 0 0         unless ($opts) {
33 0           die("You have to define the create options!");
34             }
35              
36 0           _set_defaults($opts);
37              
38             i_run "VBoxManage createvm --name \""
39             . $name
40             . "\" --ostype \""
41             . $opts->{type}
42 0           . "\" --register";
43              
44             ### add controller
45 0           i_run
46             "VBoxManage storagectl \"$name\" --name \"SATA Controller\" --add sata --controller IntelAhci";
47 0           i_run
48             "VBoxManage storagectl \"$name\" --name \"IDE Controller\" --add ide --controller PIIX4";
49              
50             ### create hds
51 0           my $hdd_ports = {
52             "sata" => 0,
53             "ide" => 0,
54             };
55              
56 0           for my $hd ( @{ $opts->{storage} } ) {
  0            
57 0 0         if ( $hd->{type} eq "file" ) {
58 0           my $filename = $hd->{file};
59 0           my $size = $hd->{size};
60 0           my $format = $hd->{format};
61 0 0         if ( !$filename ) { die("You have to specify 'file'."); }
  0            
62              
63 0 0         if ( $hd->{device} eq "disk" ) {
64              
65 0 0         if ( !-f $filename ) {
66 0           i_run
67             "VBoxManage createhd --filename \"$filename\" --size $size --format $format 2>&1";
68             }
69              
70             i_run
71             "VBoxManage storageattach \"$name\" --storagectl \"SATA Controller\" --port "
72             . $hdd_ports->{sata}
73 0           . " --device 0 --type hdd --medium \"$filename\"";
74 0           $hdd_ports->{sata}++;
75             }
76              
77 0 0         if ( $hd->{device} eq "cdrom" ) {
78             i_run
79             "VBoxManage storageattach \"$name\" --storagectl \"IDE Controller\" --port "
80             . $hdd_ports->{ide}
81 0           . " --device 0 --type dvddrive --medium \"$filename\"";
82 0           $hdd_ports->{sata}++;
83             }
84              
85             }
86             }
87              
88             # memory
89 0           i_run "VBoxManage modifyvm \"$name\" --memory " . $opts->{memory};
90              
91             # cpus
92 0           i_run "VBoxManage modifyvm \"$name\" --cpus " . $opts->{cpus};
93              
94             # boot
95 0           i_run "VBoxManage modifyvm \"$name\" --boot1 " . $opts->{boot};
96              
97 0           return;
98             }
99              
100             sub _set_defaults {
101 0     0     my ($opts) = @_;
102              
103 0 0         if ( !exists $opts->{"name"} ) {
104 0           die("You have to give a name.");
105             }
106              
107 0 0         if ( !exists $opts->{"storage"} ) {
108 0           die("You have to add at least one storage disk.");
109             }
110              
111 0 0         if ( !exists $opts->{"memory"} ) {
112 0           $opts->{"memory"} = 512;
113             }
114             else {
115             # default is mega byte
116 0           $opts->{memory} = $opts->{memory};
117             }
118              
119 0 0         if ( !exists $opts->{"cpus"} ) {
120 0           $opts->{"cpus"} = 1;
121             }
122              
123 0 0         if ( !exists $opts->{"boot"} ) {
124 0           $opts->{"boot"} = "disk";
125             }
126              
127             # normalize
128 0 0         if ( $opts->{boot} eq "hd" ) {
129 0           $opts->{boot} = "disk";
130             }
131              
132 0           _set_storage_defaults($opts);
133              
134 0           _set_network_defaults($opts);
135              
136             }
137              
138             sub _set_storage_defaults {
139 0     0     my ($opts) = @_;
140              
141 0           my $store_letter = "a";
142 0           for my $store ( @{ $opts->{"storage"} } ) {
  0            
143              
144 0 0         if ( !exists $store->{"type"} ) {
145 0           $store->{"type"} = "file";
146             }
147              
148 0 0 0       if ( $store->{type} eq "file" && !exists $store->{format} ) {
149 0           $store->{format} = "VDI";
150             }
151              
152 0 0 0       if ( !exists $store->{"size"} && $store->{"type"} eq "file" ) {
153 0           $store->{"size"} = "10G";
154             }
155              
156 0 0         if ( $store->{type} eq "file" ) {
157 0           $store->{size} = _calc_size( $store->{size} );
158             }
159              
160 0 0 0       if ( exists $store->{"file"}
      0        
161             && $store->{"file"} =~ m/\.iso$/
162             && !exists $store->{"device"} )
163             {
164 0           $store->{"device"} = "cdrom";
165             }
166              
167 0 0         if ( !exists $store->{"device"} ) {
168 0           $store->{"device"} = "disk";
169             }
170              
171 0 0         if ( $store->{"device"} eq "cdrom" ) {
172 0           $store->{"readonly"} = 1;
173             }
174              
175             }
176              
177             }
178              
179             sub _set_network_defaults {
180 0     0     my ( $opts, $hyper ) = @_;
181              
182 0 0         if ( !exists $opts->{"network"} ) {
183 0           $opts->{"network"} = [
184             {
185             type => "bridge",
186             bridge => "eth0",
187             },
188             ];
189             }
190              
191 0           for my $netdev ( @{ $opts->{"network"} } ) {
  0            
192              
193 0 0         if ( !exists $netdev->{"type"} ) {
194 0           $netdev->{"type"} = "bridge";
195             }
196              
197 0 0         if ( !exists $netdev->{"bridge"} ) {
198 0           $netdev->{"bridge"} = "eth0";
199             }
200              
201             }
202             }
203              
204             sub _calc_size {
205 0     0     my ($size) = @_;
206              
207 0           my $ret_size = 0;
208 0 0         if ( $size =~ m/^(\d+)G$/ ) {
    0          
209 0           $ret_size = $1 * 1024;
210             }
211              
212             elsif ( $size =~ m/^(\d+)M$/ ) {
213 0           $ret_size = $1;
214             }
215             }
216              
217             1;