Virtualization/linux.pm

filter: Statementstmt 24.5% 14 / 57 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 17 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 30.7% 4 / 13 Podpod 100% 9 / 9 totaltotal 22.3% 27 / 121 CC 15 SCAR 47.0
CC
15
Cov
16%
detect_dmidecode
210.0
__ANON__
6.0
detect_dmesg
2.0
CRAP 110.0
↑ Summary Virtualization.pm »
1 package Sys::Detect::Virtualization::linux;
2 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 3 ▶use base qw( Sys::Detect::Virtualization );
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 =head1 NAME
8  
9 Sys::Detect::Virtualization::linux - Detection of virtualization under a Linux system
10  
11 =head1 DESCRIPTION
12  
13 See L<Sys::Detect::Virtualization> for usage information.
14  
15 =head1 METHODS
16  
17 =head2 Internal Methods
18  
19 =over 4
20  
21 =item new ( )
22  
23 Constructor. You should not invoke this directly. Instead, use L<Sys::Detect::Virtualization>.
24  
25 =cut
26  
27 sub new
28 2 ▶{
Subroutine new: called CC=1 SCAR=0.0
Pod documented
29 2 my ($class) = @_;
30 2 my $self = {};
31 2 bless $self, $class;
32 2 return $self;
33 }
34  
35 =item detect_dmesg ( )
36  
37 Check the output of the 'dmesg' command for telltales.
38  
39 =cut
40  
41 sub detect_dmesg
42 0 ▶{
Subroutine detect_dmesg: not called CC=1 SCAR=6.9
Pod documented
43 0 my ($self) = @_;
44  
45 0 return $self->_check_command_output(
46 $self->_find_bin('dmesg'),
47 [
48 # VMWare
49 qr/vmxnet virtual NIC/i => [ $self->VIRT_VMWARE ],
50 qr/vmware virtual ide cdrom/i => [ $self->VIRT_VMWARE ],
51  
52 # Qemu / KVM
53 qr/qemu virtual cpu/i => [ $self->VIRT_KVM, $self->VIRT_QEMU ],
54  
55 # Microsoft virtual PC
56 qr/Virtual HD, ATA DISK drive/i => [ $self->VIRT_VIRTUALPC ],
57 qr/Virtual CD, ATAPI CD/i => [ $self->VIRT_VIRTUALPC ],
58  
59 # Xen
60 qr/Xen virtual console/ => [ $self->VIRT_XEN ],
61  
62 # Newer kernels are enlightened...
63 qr/booting paravirtualized kernel on kvm/i => [ $self->VIRT_KVM ],
64 qr/booting paravirtualized kernel on lguest/i => [ $self->VIRT_LGUEST ],
65 qr/booting paravirtualized kernel on vmi/i => [ $self->VIRT_VMWARE ],
66 qr/booting paravirtualized kernel on xen/i => [ $self->VIRT_XEN ],
67  
68 # Virtualbox (probably)
69 qr/VBOX (?:CD-ROM|HARDDISK)/ => [ $self->VIRT_VIRTUALBOX ],
70 ],
71 );
72  
73 }
74  
75 =item detect_dmidecode ( )
76  
77 Check the output of the 'dmidecode' command for telltales.
78  
79 =cut
80  
81 sub detect_dmidecode
82 0 ▶{
Subroutine detect_dmidecode: not called CC=14 SCAR=53.5
Pod documented
83 0 my ($self, $args ) = @_;
84  
85  
86 0 eval { require Parse::DMIDecode };
87 0 ▶ if( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
88 0 die "Cannot run dmidecode detection without Parse::DMIDecode: $@";
89 }
90  
91  
92 0 my $dmi_bin = $self->_find_bin( 'dmidecode' );
93 0 ▶ if( ! $dmi_bin ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($dmi_bin) 0 0 0
94 0 die 'dmidecode binary not found';
95 }
96  
97 # Hack! Parse::DMIDecode doesn't handle dmidecode failures very well,
98 # so we first make sure we can run it.
99 0 my $rc = system("$dmi_bin >/dev/null 2>&1");
100 0 ▶ if( $rc != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rc != 0) 0 0 0
101 0 die "Could not run $dmi_bin: Command exited with " . ($rc >> 8);
102 }
103  
104 0 my $decoder;
105 {
106 0 ▶ local $SIG{__WARN__} = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
107 0 ▶ print "$_[0]\n" if $self->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"verbose"} 0 0 0
108 0 };
109 0 $decoder = Parse::DMIDecode->new(
110 dmidecode => $dmi_bin,
111 nowarnings => 1
112 );
113 0 $decoder->probe();
114 }
115  
116 # First, check BIOS vendor
117 # BIOS Information
118 # Vendor: QEMU
119 0 my $vendor = $decoder->keyword('bios-vendor');
120 0 ▶ if( $vendor && $vendor eq 'QEMU' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($vendor and $vendor eq "QEMU") 0 0 0
Condition0%0/3
$vendor and $vendor eq "QEMU"
!ll&&!rl&&r
000
MC/DC0%0/2
$vendor and $vendor eq "QEMU"
$vendor $vendor eq "QEMU"
Truth table0%0/3
A and B
A= $vendorB= $vendor eq "QEMU"
ABresult
0X0
100
111
121 return [
122 0 $self->VIRT_QEMU,
123 $self->VIRT_KVM,
124 ];
125 }
126  
127 # VMWare:
128 # System Information
129 # Manufacturer: VMware, Inc.
130 0 my $mfgr = $decoder->keyword('system-manufacturer');
131 0 ▶ if( $mfgr && $mfgr =~ /VMWare/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mfgr and $mfgr =~ /VMWare/i) 0 0 0
Condition0%0/3
$mfgr and $mfgr =~ /VMWare/i
!ll&&!rl&&r
000
MC/DC0%0/2
$mfgr and $mfgr =~ /VMWare/i
$mfgr $mfgr =~ /VMWare/i
Truth table0%0/3
A and B
A= $mfgrB= $mfgr =~ /VMWare/i
ABresult
0X0
100
111
132 0 return [ $self->VIRT_VMWARE ];
133 }
134  
135 # System Information
136 # Manufacturer: Microsoft Corporation
137 # Product Name: Virtual Machine
138 0 ▶ my $product = $decoder->keyword('system-product-name') || '';
Condition0%0/2
$decoder->keyword('system-product-name') || ''
l!l
00
MC/DC0%0/1
$decoder->keyword('system-product-name') || ''
$decoder->keyword('system-product-name')
Truth table0%0/2
A
A= $decoder->keyword('system-product-name')
Aresult
11
00
139 0 ▶ if( $mfgr && $product && $mfgr =~ /microsoft/i
Branch: 1 branch
BranchTrueFalseTotal
if ($mfgr and $product and $mfgr =~ /microsoft/i and $product =~ /virtual machine/i) 0 0 0
Condition0%0/9
$mfgr and $product and $mfgr =~ /microsoft/i and $product =~ /virtual machine/i
!ll&&!rl&&r
000
$mfgr and $product and $mfgr =~ /microsoft/i
!ll&&!rl&&r
000
$mfgr and $product
!ll&&!rl&&r
000
MC/DC0%0/4
$mfgr and $product and $mfgr =~ /microsoft/i and $product =~ /virtual machine/i
$mfgr $product $mfgr =~ /microsoft/i $product =~ /virtual machine/i
Truth table0%0/5
A and B and C and D
A= $mfgrB= $productC= $mfgr =~ /microsoft/iD= $product =~ /virtual machine/i
ABCDresult
0XXX0
10XX0
110X0
11100
11111
140 && $product =~ /virtual machine/i ) {
141 0 return [ $self->VIRT_VIRTUALPC ];
142 }
143  
144 # VirtualBox
145 0 ▶ if ($product =~ /virtualbox/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($product =~ /virtualbox/i) 0 0 0
146 0 return [ $self->VIRT_VIRTUALBOX ];
147 }
148  
149 0 return [];
150 }
151  
152 =item detect_ide_devices ( )
153  
154 Check /proc/ide/hd*/model for telltale model information.
155  
156 =cut
157  
158 sub detect_ide_devices
159 0 ▶{
Subroutine detect_ide_devices: not called CC=1 SCAR=6.9
Pod documented
160 0 my ($self) = @_;
161  
162 0 return $self->_check_file_contents(
163 '/proc/ide/hd*/model',
164 [
165 # VMWare
166 qr/vmware virtual/ => [ $self->VIRT_VMWARE ],
167  
168 # VirtualPC
169 qr/Virtual [HC]D/i => [ $self->VIRT_VIRTUALPC ],
170  
171 # Qemu / KVM
172 qr/QEMU (?:HARDDISK|DVD-ROM)/i => [
173 $self->VIRT_QEMU,
174 $self->VIRT_KVM,
175 ],
176 ]
177 );
178 }
179  
180 =item detect_mtab ( )
181  
182 Check /etc/mtab for telltale devices
183  
184 =cut
185  
186 sub detect_mtab
187 0 ▶{
Subroutine detect_mtab: not called CC=1 SCAR=6.9
Pod documented
188 0 my ($self) = @_;
189  
190 0 return $self->_check_file_contents(
191 '/etc/mtab',
192 [
193 # vserver
194 qr{^/dev/hdv1 } => [ $self->VIRT_VSERVER ],
195 qr{^simfs } => [ $self->VIRT_OPENVZ ],
196 ]
197 );
198 }
199  
200 =item detect_init_envvars ( )
201  
202 Check /proc/1/environ for LXC environment variables
203  
204 =cut
205  
206 sub detect_init_envvars
207 0 ▶{
Subroutine detect_init_envvars: not called CC=1 SCAR=6.9
Pod documented
208 0 my ($self) = @_;
209 0 return $self->_check_file_contents(
210 '/proc/1/environ',
211 [
212 qr/LIBVIRT_LXC_NAME/ => [ $self->VIRT_LXC ],
213 qr/LIBVIRT_LXC_UUID/ => [ $self->VIRT_LXC ],
214 qr/LIBVIRT_LXC_CMDLINE/ => [ $self->VIRT_LXC ],
215 qr/container=lxc/ => [ $self->VIRT_LXC ],
216 ]
217 );
218 }
219  
220 =item detect_scsi_devices ( )
221  
222 Check /proc/scsi/scsi for telltale model/vendor information.
223  
224 =cut
225  
226 sub detect_scsi_devices
227 0 ▶{
Subroutine detect_scsi_devices: not called CC=1 SCAR=6.9
Pod documented
228 0 my ($self) = @_;
229  
230 0 return $self->_check_file_contents(
231 '/proc/scsi/scsi',
232 [
233 # VMWare
234 qr/Vendor: VMware Model: Virtual disk/ => [ $self->VIRT_VMWARE ],
235 ]
236 );
237 }
238  
239 =item detect_paths ( )
240  
241 Check for particular paths that only exist under virtualization.
242  
243 =cut
244  
245 sub detect_paths
246 0 ▶{
Subroutine detect_paths: not called CC=1 SCAR=6.9
Pod documented
247 0 my ($self) = @_;
248 0 return $self->_check_path_exists([
249 '/dev/vzfs' => [ $self->VIRT_OPENVZ ],
250 '/dev/vzctl' => [ $self->VIRT_OPENVZ_HOST ],
251 '/proc/vz' => [ $self->VIRT_OPENVZ ],
252 '/proc/xen' => [ $self->VIRT_XEN ],
253 '/proc/sys/xen/independent_wallclock' => [ $self->VIRT_XEN ],
254 ]);
255 }
256  
257 =item detect_modules ( )
258  
259 Check for telltale guest modules
260  
261 =cut
262  
263 sub detect_modules
264 0 ▶{
Subroutine detect_modules: not called CC=1 SCAR=6.9
Pod documented
265 0 my ($self) = @_;
266  
267 0 return $self->_check_command_output(
268 $self->_find_bin( 'lsmod' ),
269 [
270 # virtio support exists for kvm and lguest
271 qr/^virtio_(?:blk|pci|net|balloon)/ => [ $self->VIRT_KVM, $self->VIRT_LGUEST ],
272  
273 # similarly, for VMWare
274 qr/^(?:vmmemctl|vmxnet)/ => [ $self->VIRT_VMWARE ],
275  
276 qr/^vboxadd/ => [ $self->VIRT_VIRTUALBOX ],
277 ]
278 );
279 }
280  
281 =back
282  
283 =head1 LICENSE AND COPYRIGHT
284  
285 Copyright (C) 2009 Roaring Penguin Software Inc.
286  
287 This program is free software; you can redistribute it and/or modify it
288 under the terms of either: the GNU General Public License as published
289 by the Free Software Foundation; or the Artistic License.
290  
291 See http://dev.perl.org/licenses/ for more information.
292  
293  
294 =cut
295  
296 3 1;
↑ Summary Virtualization.pm »