File Coverage

blib/lib/Rex/Virtualization/CBSD.pm
Criterion Covered Total %
statement 14 19 73.6
branch n/a
condition 0 3 0.0
subroutine 5 6 83.3
pod 0 1 0.0
total 19 29 65.5


line stmt bran cond sub pod time code
1             package Rex::Virtualization::CBSD;
2              
3 1     1   67694 use 5.006;
  1         3  
4 1     1   6 use strict;
  1         1  
  1         20  
5 1     1   5 use warnings;
  1         2  
  1         23  
6              
7 1     1   510 use Rex::Virtualization::Base;
  1         331  
  1         33  
8 1     1   7 use base qw(Rex::Virtualization::Base);
  1         2  
  1         210  
9              
10             =head1 NAME
11              
12             Rex::Virtualization::CBSD - CBSD virtualization module for bhyve
13              
14             =head1 VERSION
15              
16             Version 0.0.1
17              
18             =cut
19              
20             our $VERSION = '0.0.1';
21              
22             =head1 SYNOPSIS
23              
24             use Rex::Commands::Virtualization;
25              
26             set virtualization => "CBSD";
27            
28             vm 'create', name=>'foo',
29             'vm_os_type'=>'freebsd',
30             'vm_os_profile'=>'FreeBSD-x64-13.0',
31             'vm_ram'=>'1g',
32             'vm_cpus'=>'1',
33             'imgsize'=>'10g';
34            
35             vm 'start' => 'foo';
36            
37             # list the basic settings for the VM foo from the VM list
38             my %vm_list = vm 'list';
39             print Dumper \%{ $vm_list{foo} };
40            
41             # get all the config info for the VM foo and display it
42             %vm_info=vm 'info' => 'foo';
43             foreach my $vm_info_key (@{keys(%vm_info)}){
44             print $vm_info_key.": ".$vm_info{$vm_info_key}."\n";
45             }
46              
47             # stop the VM foo
48             vm 'stop' => 'foo';
49            
50             # remove the VM foo
51             vm 'remove' => 'foo';
52            
53             # show all VM
54             my %vm_list = vm 'list';
55             print Dumper \%vm_list;
56              
57             =cut
58              
59             sub new {
60 0     0 0   my $that = shift;
61 0   0       my $proto = ref($that) || $that;
62 0           my $self = {@_};
63              
64 0           bless( $self, $proto );
65              
66 0           return $self;
67             }
68              
69             =head1 Methods
70              
71             =head2 cbsd_base_dir
72              
73             This returns the CBSD base dir that the install is stored in.
74              
75             No arguments are taken.
76              
77             This will die upon error.
78              
79             my $cbsd_base_dir=vm 'cbsd_base_dir'
80              
81             =head2 create
82              
83             Creates a new VM.
84              
85             Below is a list of the basic options.
86              
87             name - Name of the VM.
88            
89             bhyve_vnc_tcp_bind - VNC bind, e.g: '127.0.0.1', '0.0.0.0', '::1'.
90            
91             imgsize - VM first/boot disk size, e.g.: '10g', '21474836480000'.
92            
93             interface2 - , create VM with two interfaces, is uplink for nic2,
94             do not confuse with the ci_interface2 parameter.
95            
96             nic_flags - '0' to disable. Pass additional flags for NIC, e.g.: 'private'.
97            
98             nic_flags2 - '0' to disable. Pass additional flags for NIC2, e.g.: 'private'.
99            
100             quiet - 0,1: be quiet, dont output verbose message.
101            
102             removejconf - 0,1: remove jconf after bcreate? 0 - don't remove.
103            
104             runasap - 0,1: when 1 - run a VM immediately (atomic bcreate+bstart).
105            
106             vm_cpus - VM CPUs cores, e.g.: '2'.
107            
108             vm_os_profile - : full config file is: vm-${vm_os_type}-${vm_os_profile}.conf, file
109             must be present in ~cbsd/etc/defaults/ or ~cbsd/etc/ directory.
110            
111             vm_os_type - : full config file is: vm-${vm_os_type}-${vm_os_profile}.conf, file
112             must be present in ~cbsd/etc/defaults/ or ~cbsd/etc/ directory.
113            
114             vm_ram - VM RAM, e.g.: '1g', '2147483648'.
115            
116             zfs_snapsrc - : use ZFS snapshot as data source.
117              
118             Below is a list of options for when using cloud init.
119              
120             ci_gw4 - (cloud-init profile only): set IPv4 gateway for VM.
121              
122             ci_interface2 - configure second interface via cloud-init,
123             do not confuse with the interface2 parameter.
124              
125             ci_interface_mtu - set MTU for NIC1, default: 1500.
126              
127             ci_interface_mtu2 - set MTU for NIC2, default: 1500.
128              
129             ci_ip4_addr - (cloud-init profile only): set IPv4 address for VM,
130             default is: DHCP. Can be: 'DHCPv6' or static IPv4/IPv6.
131              
132             ci_ip4_addr2 - (cloud-init profile only): set IPv4 address for VM
133             NIC2 (see also: ci_interface2,ci_gw42). Possible values same as ci_ip4_addr.
134              
135             ci_user_pubkey - full/relative path to authorized_keys or may contain pubkey
136             string itself, e.g: ci_user_pubkey="ssh-ed25519 XXXXX root@my.domain".
137             (cloud-init profile only): set authorized_keys file for cloud-init user for VM.
138              
139             ci_user_pw_user - set password for cloud-init user.
140              
141             ci_user_pw_root - set password for 'root' user.
142              
143             A minimum of 'vm_os_type', 'vm_os_profile', 'vm_ram', 'vm_cpus', and 'imgsize' is needed.
144              
145             This ran non-interactively.
146              
147             This will die upon a error.
148              
149             # create a x64 FreeBSD 13.0 VM named foo with 1G of ram, 1 CPU, and a 10G disk
150             print Dumper vm 'create', name=>'foo',
151             'vm_os_type'=>'freebsd',
152             'vm_os_profile'=>'FreeBSD-x64-13.0',
153             'vm_ram'=>'1g',
154             'vm_cpus'=>'1',
155             'imgsize'=>'10g';
156              
157             =head2 disk_list
158              
159             This returns a list of disks setup for use with Bhyve in CBSD via parsing
160             the output of the command below.
161              
162             cbsd bhyve-dsk-list display=jname,dsk_controller,dsk_path,dsk_size,dsk_sectorsize,bootable,dsk_zfs_guid header=0
163              
164             This returned data is a array of hashes.
165              
166             The keys are as below.
167              
168             vm - The name of the VM in question.
169            
170             controller - Controller type configured for this.
171            
172             path - The path to the disk.
173            
174             size - Size of the disk in question.
175            
176             sectorsize - size of the sectors in question.
177            
178             bootable - If it is bootable. true/false
179            
180             zfs_guid - ZFS GUID of the disk.
181              
182             This dies upon failure.
183              
184             my @disks
185             eval{
186             @disks=vm 'disk_list';
187             } or do {
188             my $error = $@ || 'Unknown failure';
189             warn('Failed to the disk list... '.$error);
190             }
191            
192             print Dumper(\@disks);
193              
194             =head2 freejname
195              
196             Gets the next available VM name.
197              
198             One argument is required and that is the base VM to use.
199              
200             The optional argument 'lease_time' may be used to specify the number
201             of seconds a lease for the VM name should last. The default is 30.
202              
203             vm 'freejname' => 'foo';
204            
205             # the same thing, but with a 60 second lease time
206             vm 'freejname' => 'foo', lease_time => '60';
207              
208             =head2 info
209              
210             This fetches the available configuration information for a VM via
211             the command below.
212              
213             cbsd bget jname=$vm
214              
215             The returned value is a flat hash of key value pairs.
216              
217             my %vm_info
218             eval{
219             %vm_info=vm 'info' => 'foo';
220             } or do {
221             my $error = $@ || 'Unknown failure';
222             warn('Failed to get settings for the VM... '.$error);
223             }
224            
225             foreach my $vm_info_key (@{keys(%vm_info)}){
226             print $vm_info_key.": ".$vm_info{$vm_info_key}."\n";
227             }
228              
229             =head2 list
230              
231             List available VMs.
232              
233             The returned array is a hash of hashes. The first level hash is the jname.
234              
235             nodename - The node name that this is set to run on.
236            
237             name - Name of the VM.
238            
239             jid - Jail ID/process ID of the VM if running. IF '0' it is not running.
240            
241             vm_ram - Max RAM for the VM.
242            
243             vm_curmem - Current RAM in use by the VM.
244            
245             vm_cpus - Number of virtual CPUs.
246            
247             pcpu - Current CPU usage.
248            
249             vm_os_type - OS type for the VM.
250            
251             ip4_addr - Expected IPv4 address for the VM.
252            
253             status - Current status of the VM.
254            
255             vnc - VNC address and port for the VM.
256            
257             path - Path to where the VM is stored.
258              
259             This dies upon failure.
260              
261             my %vm_list;
262             eval{
263             %vm_list=vm 'list';
264             } or do {
265             my $error = $@ || 'Unknown failure';
266             warn('Failed to list the VM... '.$error);
267             }
268            
269             foreach my $vm_name (@{keys( %vm_list )}){
270             print
271             "---------------------------\n".
272             'VM: '.$vm_name."\n".
273             "---------------------------\n".
274             'jid: '.$vm_list{$vm_name}{jid}."\n".
275             'vm_ram: '.$vm_list{$vm_name}{vm_ram}."\n".
276             'vm_curmem: '.$vm_list{$vm_name}{vm_curmem}."\n".
277             'vm_cpus: '.$vm_list{$vm_name}{vm_cpus}."\n".
278             'vm_ram: '.$vm_list{$vm_name}{pcpu}."\n".
279             'vm_os_type: '.$vm_list{$vm_name}{vm_os_type}."\n".
280             'ip4_addr: '.$vm_list{$vm_name}{ip4_addr}."\n".
281             'status: '.$vm_list{$vm_name}{status}."\n".
282             'vnc: '.$vm_list{$vm_name}{vnc}."\n".
283             'path: '.$vm_list{$vm_name}{path}."\n".
284             "\n"
285             }
286              
287             =head2 nic_list
288              
289             List configured NICs.
290              
291             This returned data is a array of hashes.
292              
293             The keys are as below.
294              
295             vm - The name of the VM in question.
296            
297             driver - The driver in use. As of currently either vtnet or e1000.
298            
299             node - The node it is on.
300            
301             parent - Either the name of the parent NIC, example 'bridge1', or set to 'auto'.
302            
303             hwaddr - The MAC address for the NIC.
304            
305             address - Address of the NIC. '0' if not configured.
306            
307             mtu - The MTU of NIC. '0' if default.
308            
309             persistent - 0/1 - 1 mean persistent nic (no managed by CBSD)
310            
311             ratelimit - Rate limit for the interface. '0' is the default.
312             {tx}/{rx} (outgoing/incoming limit), {rxtx} - shared(rx+tx) limit, one value
313              
314             This dies upon failure.
315              
316             my @nics
317             eval{
318             @nics=vm nic_list;
319             } or do {
320             my $error = $@ || 'Unknown failure';
321             warn('Failed to the NIC list... '.$error);
322             }
323            
324             print Dumper(\@nics);
325              
326             =head2 pause
327              
328             This pauses a VM in question. The following modes are available. If no
329             more is specified, audo is used.
330              
331             auto - (by default) triggering - e.g, if vm active then pause
332             on - pause, stop
333             off - unpause, continue
334              
335             The command called is as below.
336              
337             cbsd bpause $vm mode=$mode
338              
339             This dies upon failure.
340              
341             eval{
342             vm 'pause' => 'foo';
343             } or do {
344             my $error = $@ || 'Unknown failure';
345             warn('Failed to pause the VM foo... '.$error);
346             }
347              
348             =head2 pci_list
349              
350             List configured PCI devices for a VM.
351              
352             This returned data is a array of hashes.
353              
354             The keys are as below.
355              
356             name - Drive name of the PCI item.
357            
358             bus - Bus number.
359            
360             slot - Slot number.
361            
362             function - Function number.
363            
364             desc - Description of the device.
365              
366             One argument is required and that is the name of the VM.
367              
368             This dies upon failure.
369              
370             my @devices
371             eval{
372             @devices=vm 'nic_list' => 'foo';
373             } or do {
374             my $error = $@ || 'Unknown failure';
375             warn('Failed to the PCI device list... '.$error);
376             }
377            
378             print Dumper(\@devices);
379              
380             =head2 remove
381              
382             This removes the selected VM and remove the data. This is done via the command...
383              
384             cbsd bdestroy $vm
385              
386             One argument is taken and that is the name of the VM.
387              
388             This dies upon failure.
389              
390             eval{
391             vm 'remove' => 'foo'
392             } or do {
393             my $error = $@ || 'Unknown failure';
394             warn('Failed to remove the VM foo... '.$error);
395             }
396              
397             =head2 restart
398              
399             This restarts the selected VM. This is done via the command...
400              
401             cbsd brestart $vm
402              
403             One argument is taken and that is the name of the VM.
404              
405             This dies upon failure.
406              
407             eval{
408             vm 'restart' => 'foo'
409             } or do {
410             my $error = $@ || 'Unknown failure';
411             warn('Failed to restart the VM foo... '.$error);
412             }
413              
414             =head2 set
415              
416             This sets various settings for a VM via the use of...
417              
418             cbsd bset jname=$vm ...
419              
420             One argument is equired and that is the VM name.
421              
422             This will die upon failure. Please note the CBSD currently does
423             not consider non-existent variables such as 'foofoo' to be a failure
424             and silently ignores those.
425              
426             # set the the VM foo to boot from net with a resolution of 800x600
427             vm 'set' => 'foo',
428             vm_boot => 'net',
429             bhyve_vnc_resolution => '800x600';
430              
431             =head2 start
432              
433             This starts a VM. This is done via the command...
434              
435             cbsd bstart jname=$vm
436              
437             One argument is taken and that is the name of the VM. If '*' or 'vm*' then
438             start all VM whose names begin with 'vm', e.g. 'vm1', 'vm2'...
439              
440             This dies upon failure.
441              
442             eval{
443             vm 'start' => 'foo'
444             } or do {
445             my $error = $@ || 'Unknown failure';
446             warn('Failed to start the VM foo... '.$error);
447             }
448              
449              
450             =head2 stop
451              
452             This stops a VM. This is done via the command below...
453              
454             cbsd bstop jname=$vm [hard_timeout=$timeout] [noacpi=$noacpi]
455              
456             One argument is required and that is the name of the VM.
457              
458             The following options are optional.
459              
460             hard_timeout - Wait N seconds (30 by default) before hard reset.
461              
462             noacpi - 0,1. Set to 1 to prevent ACPI signal sending, just kill.
463             By default it will attempt to use ACPI to ask it to shutdown.
464              
465             This dies upon failure.
466              
467             eval{
468             vm 'stop' => 'foo',
469             hard_timeout => 60;
470             } or do {
471             my $error = $@ || 'Unknown failure';
472             warn('Failed to stop the VM foo... '.$error);
473             }
474              
475             =head2 vm_os_profiles
476              
477             Get the VM OS profiles for a specified OS type.
478              
479             One argument is required and that is the OS type.
480              
481             An optional argument is taken and that is cloudinit. It is a
482             Perl boolean value and if true it will only return profiles
483             with cloudinit support.
484              
485             The returned value is a array.
486              
487             This will die upon failure.
488              
489             # list the VM OS profiles for FreeBSD
490             my @profiles=vm 'vm_os_profiles' => 'freebsd';
491             print Dumper @profiles;
492            
493             # list the VM OS profiles for FreeBSD
494             @profiles=vm 'vm_os_profiles' => 'freebsd', cloudinit=>1;
495             print Dumper @profiles;
496              
497             =head2 vm_os_profiles_hash
498              
499             Get the VM OS profiles for a specified OS type.
500              
501             One optional argument is taken and that is cloudinit. It is a
502             Perl boolean value and if true it will only return profiles
503             with cloudinit support.
504              
505             The returned value is a two level hash. The keys for the first
506             level are the OS types and the keys for the second level are
507             the OS profile names.
508              
509             This will die upon failure.
510              
511             my %os_profiles=vm 'vm_os_profiles_hash';
512             print Dumper %os_profiles;
513            
514             # print the OS profiles for FreeBSD
515             print Dumper keys( %{ $os_profiles{freebsd} } );
516            
517             my %os_profiles=vm 'vm_os_profiles_hash', cloudinit=>1;
518             print Dumper %os_profiles;
519              
520             =head2 vm_os_types
521              
522             Get the VM OS types there are profiles for.
523              
524             The returned value is a array.
525              
526             This will die upon failure.
527              
528             # get a hash of all OS types and profiles
529             my @os_types=vm 'vm_os_profiles';
530             print Dumper @os_types;
531              
532             =head1 AUTHOR
533              
534             Zane C. Bowers-HAdley, C<< >>
535              
536             =head1 BUGS
537              
538             Please report any bugs or feature requests to C, or through
539             the web interface at L.
540             I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
541              
542              
543              
544              
545             =head1 SUPPORT
546              
547             You can find documentation for this module with the perldoc command.
548              
549             perldoc Rex::Virtualization::CBSD
550              
551              
552             You can also look for information at:
553              
554             =over 4
555              
556             =item * RT: CPAN's request tracker (report bugs here)
557              
558             L
559              
560             =item * AnnoCPAN: Annotated CPAN documentation
561              
562             L
563              
564             =item * Repository
565              
566             L
567              
568             =item * CPAN Ratings
569              
570             L
571              
572             =item * Search CPAN
573              
574             L
575              
576             =back
577              
578              
579             =head1 ACKNOWLEDGEMENTS
580              
581              
582             =head1 LICENSE AND COPYRIGHT
583              
584             This software is Copyright (c) 2020 by Zane C. Bowers-HAdley.
585              
586             This is free software, licensed under:
587              
588             The Artistic License 2.0 (GPL Compatible)
589              
590              
591             =cut
592              
593             1; # End of Rex::Virtualization::CBSD