File Coverage

lib/Rex/Virtualization/VBox/start.pm
Criterion Covered Total %
statement 23 45 51.1
branch 0 14 0.0
condition 0 9 0.0
subroutine 8 9 88.8
pod 0 1 0.0
total 31 78 39.7


line stmt bran cond sub pod time code
1             #
2             # (c) Jan Gehring
3             #
4              
5             package Rex::Virtualization::VBox::start;
6              
7 1     1   18 use v5.12.5;
  1         5  
8 1     1   5 use warnings;
  1         5  
  1         43  
9              
10             our $VERSION = '1.14.3'; # VERSION
11              
12 1     1   21 use Rex::Logger;
  1         2  
  1         5  
13 1     1   33 use Rex::Helper::Run;
  1         6  
  1         77  
14 1     1   15 use Rex::Commands::File;
  1         2  
  1         6  
15 1     1   8 use Rex::Commands;
  1         2  
  1         6  
16 1     1   8 use Rex::Helper::Path;
  1         2  
  1         61  
17 1     1   12 use Cwd 'getcwd';
  1         6  
  1         452  
18              
19             sub execute {
20 0     0 0   my ( $class, $arg1, %opt ) = @_;
21              
22 0 0         unless ($arg1) {
23 0           die("You have to define the vm name!");
24             }
25              
26 0           my $dom = $arg1;
27 0           Rex::Logger::debug("starting domain: $dom");
28              
29 0 0         unless ($dom) {
30 0           die("VM $dom not found.");
31             }
32              
33 0           my $virt_settings = Rex::Config->get("virtualization");
34 0           my $headless = 0;
35 0 0         if ( ref($virt_settings) ) {
36 0 0 0       if ( exists $virt_settings->{headless} && $virt_settings->{headless} ) {
37 0           $headless = 1;
38             }
39             }
40              
41 0 0 0       if ( $headless && $^O =~ m/^MSWin/ && !Rex::is_ssh() ) {
      0        
42 0           Rex::Logger::info(
43             "Right now it is not possible to run VBoxHeadless under Windows.");
44 0           $headless = 0;
45             }
46              
47 0 0         if ($headless) {
48 0           my $filename = get_tmp_file;
49              
50 0           file( "$filename", content => <
51             use POSIX();
52              
53             my \$pid = fork();
54             if (defined \$pid && \$pid == 0 ) {
55             # child
56             chdir "/";
57             umask 0;
58             POSIX::setsid();
59             local \$SIG{'HUP'} = 'IGNORE';
60             my \$spid = fork();
61             if (defined \$spid && \$spid == 0 ) {
62              
63             open( STDIN, "
64             open( STDOUT, "+>/dev/null" );
65             open( STDERR, "+>/dev/null" );
66              
67             # 2nd child
68             unlink "$filename";
69             exec("VBoxHeadless --startvm \\\"$dom\\\"");
70             exit;
71              
72              
73             }
74              
75             exit; # end first child (2nd parent)
76             }
77             else {
78             waitpid( \$pid, 0 );
79             }
80              
81             exit;
82              
83             EOF
84              
85 0           i_run "perl $filename", fail_ok => 1;
86             }
87             else {
88 0           i_run "VBoxManage startvm \"$dom\"", fail_ok => 1;
89             }
90              
91 0 0         if ( $? != 0 ) {
92 0           die("Error starting vm $dom");
93             }
94              
95             }
96              
97             1;