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   68744 use 5.006;
  1         4  
4 1     1   14 use strict;
  1         4  
  1         32  
5 1     1   5 use warnings;
  1         2  
  1         28  
6              
7 1     1   748 use Rex::Virtualization::Base;
  1         344  
  1         52  
8 1     1   8 use base qw(Rex::Virtualization::Base);
  1         4  
  1         283  
9              
10             =head1 NAME
11              
12             Rex::Virtualization::CBSD - CBSD virtualization module for bhyve
13              
14             =head1 VERSION
15              
16             Version 1.0.0
17              
18             =cut
19              
20             our $VERSION = '1.0.0';
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 General
72              
73             =head3 cbsd_base_dir
74              
75             This returns the CBSD base dir that the install is stored in.
76              
77             No arguments are taken.
78              
79             This will die upon error.
80              
81             my $cbsd_base_dir=vm 'cbsd_base_dir'
82              
83             =head3 freejname
84              
85             Gets the next available VM name.
86              
87             One argument is required and that is the base VM to use.
88              
89             The optional argument 'lease_time' may be used to specify the number
90             of seconds a lease for the VM name should last. The default is 30.
91              
92             vm 'freejname' => 'foo';
93            
94             # the same thing, but with a 60 second lease time
95             vm 'freejname' => 'foo', lease_time => '60';
96              
97             =head2 bhyve
98              
99             =head3 bcheckpoint_create
100              
101             Creates a checkpoint via the command below.
102              
103             cbsd bcheckpoint mode=create jname=$vm suspend=$suspend name=$name
104              
105             The only required option is the key vm.
106              
107             vm - VM to checkpoint
108            
109             name - name of checkpoint. by default: 'checkpoint'.
110            
111             suspend= - when set to 1 then turn off the domain immediately after checkpoint,
112             for disk consistency. By default - 0, create checkpoint only.
113              
114             This will die upon error.
115              
116             vm 'bcheckpoint_create', vm=>'foo';
117              
118             =head3 bcheckpoint_destroyall
119              
120             Removes all checkpoints via the command below.
121              
122             cbsd bcheckpoint mode=destroyall jname=$vm
123              
124             One argument is taken and that is the name of the VM.
125              
126             This will die upon error.
127              
128             # removes all checkpoints for the VM 'foo'
129             vm 'bcheckpoint_destroyall', 'foo';
130              
131             =head3 bclone
132              
133             This closnes a VM.
134              
135             old - The VM to clone.
136            
137             new - The name of the new VM.
138            
139             checkstate - 0 do not check for VM online. Default is 1 - check
140            
141             promote - Promotes clone to no longer be dependent from origin: 0 or 1.
142             Default is 0 (not promote). This means new new VM operates in
143             copy-on-write mode and will be promoted upon removal of the
144             original.
145            
146             mac_reinit - 0,1 (default 1). 0 - leave old MAC.
147             1 set mac to re-generate
148              
149             This will will die upon error.
150              
151             vm 'bclone' old => 'foo', 'new' => 'bar';
152              
153             =head3 bcreate
154              
155             Creates a new VM.
156              
157             Below is a list of the basic options.
158              
159             name - Name of the VM.
160            
161             bhyve_vnc_tcp_bind - VNC bind, e.g: '127.0.0.1', '0.0.0.0', '::1'.
162            
163             imgsize - VM first/boot disk size, e.g.: '10g', '21474836480000'.
164            
165             interface2 - , create VM with two interfaces, is uplink for nic2,
166             do not confuse with the ci_interface2 parameter.
167            
168             nic_flags - '0' to disable. Pass additional flags for NIC, e.g.: 'private'.
169            
170             nic_flags2 - '0' to disable. Pass additional flags for NIC2, e.g.: 'private'.
171            
172             quiet - 0,1: be quiet, dont output verbose message.
173            
174             removejconf - 0,1: remove jconf after bcreate? 0 - don't remove.
175            
176             runasap - 0,1: when 1 - run a VM immediately (atomic bcreate+bstart).
177            
178             vm_cpus - VM CPUs cores, e.g.: '2'.
179            
180             vm_os_profile - : full config file is: vm-${vm_os_type}-${vm_os_profile}.conf, file
181             must be present in ~cbsd/etc/defaults/ or ~cbsd/etc/ directory.
182            
183             vm_os_type - : full config file is: vm-${vm_os_type}-${vm_os_profile}.conf, file
184             must be present in ~cbsd/etc/defaults/ or ~cbsd/etc/ directory.
185            
186             vm_ram - VM RAM, e.g.: '1g', '2147483648'.
187            
188             zfs_snapsrc - : use ZFS snapshot as data source.
189              
190             Below is a list of options for when using cloud init.
191              
192             ci_gw4 - (cloud-init profile only): set IPv4 gateway for VM.
193              
194             ci_interface2 - configure second interface via cloud-init,
195             do not confuse with the interface2 parameter.
196              
197             ci_interface_mtu - set MTU for NIC1, default: 1500.
198              
199             ci_interface_mtu2 - set MTU for NIC2, default: 1500.
200              
201             ci_ip4_addr - (cloud-init profile only): set IPv4 address for VM,
202             default is: DHCP. Can be: 'DHCPv6' or static IPv4/IPv6.
203              
204             ci_ip4_addr2 - (cloud-init profile only): set IPv4 address for VM
205             NIC2 (see also: ci_interface2,ci_gw42). Possible values same as ci_ip4_addr.
206              
207             ci_user_pubkey - full/relative path to authorized_keys or may contain pubkey
208             string itself, e.g: ci_user_pubkey="ssh-ed25519 XXXXX root@my.domain".
209             (cloud-init profile only): set authorized_keys file for cloud-init user for VM.
210              
211             ci_user_pw_user - set password for cloud-init user.
212              
213             ci_user_pw_root - set password for 'root' user.
214              
215             A minimum of 'vm_os_type', 'vm_os_profile', 'vm_ram', 'vm_cpus', and 'imgsize' is needed.
216              
217             This ran non-interactively.
218              
219             This will die upon a error.
220              
221             # create a x64 FreeBSD 13.0 VM named foo with 1G of ram, 1 CPU, and a 10G disk
222             print Dumper vm 'create', name=>'foo',
223             'vm_os_type'=>'freebsd',
224             'vm_os_profile'=>'FreeBSD-x64-13.0',
225             'vm_ram'=>'1g',
226             'vm_cpus'=>'1',
227             'imgsize'=>'10g';
228              
229             =head3 bdisk_list
230              
231             This returns a list of disks setup for use with Bhyve in CBSD via parsing
232             the output of the command below.
233              
234             cbsd bhyve-dsk-list display=jname,dsk_controller,dsk_path,dsk_size,dsk_sectorsize,bootable,dsk_zfs_guid header=0
235              
236             This returned data is a array of hashes.
237              
238             The keys are as below.
239              
240             vm - The name of the VM in question.
241            
242             controller - Controller type configured for this.
243            
244             path - The path to the disk.
245            
246             size - Size of the disk in question.
247            
248             sectorsize - size of the sectors in question.
249            
250             bootable - If it is bootable. true/false
251            
252             zfs_guid - ZFS GUID of the disk.
253              
254             This dies upon failure.
255              
256             my @disks
257             eval{
258             @disks=vm 'bdisk_list';
259             } or do {
260             my $error = $@ || 'Unknown failure';
261             warn('Failed to the disk list... '.$error);
262             }
263            
264             print Dumper(\@disks);
265              
266             =head3 binfo
267              
268             This fetches the available configuration information for a VM via
269             the command below.
270              
271             cbsd bget jname=$vm
272              
273             The returned value is a flat hash of key value pairs.
274              
275             my %vm_info
276             eval{
277             %vm_info=vm 'binfo' => 'foo';
278             } or do {
279             my $error = $@ || 'Unknown failure';
280             warn('Failed to get settings for the VM... '.$error);
281             }
282            
283             foreach my $vm_info_key (@{keys(%vm_info)}){
284             print $vm_info_key.": ".$vm_info{$vm_info_key}."\n";
285             }
286              
287             =head3 blist
288              
289             List available VMs.
290              
291             The command used is...
292              
293             cbsd bls display=nodename,jname,jid,vm_ram,vm_curmem,vm_cpus,pcpu,vm_os_type,ip4_addr,status,vnc,path header=0
294              
295             The returned array is a hash of hashes. The first level hash is the jname.
296              
297             nodename - The node name that this is set to run on.
298            
299             name - Name of the VM.
300            
301             jid - Jail ID/process ID of the VM if running. IF '0' it is not running.
302            
303             vm_ram - Max RAM for the VM.
304            
305             vm_curmem - Current RAM in use by the VM.
306            
307             vm_cpus - Number of virtual CPUs.
308            
309             pcpu - Current CPU usage.
310            
311             vm_os_type - OS type for the VM.
312            
313             ip4_addr - Expected IPv4 address for the VM.
314            
315             status - Current status of the VM.
316            
317             vnc - VNC address and port for the VM.
318            
319             path - Path to where the VM is stored.
320              
321             This dies upon failure.
322              
323             my %vm_list;
324             eval{
325             %vm_list=vm 'blist';
326             } or do {
327             my $error = $@ || 'Unknown failure';
328             warn('Failed to list the VM... '.$error);
329             }
330            
331             foreach my $vm_name (@{keys( %vm_list )}){
332             print
333             "---------------------------\n".
334             'VM: '.$vm_name."\n".
335             "---------------------------\n".
336             'jid: '.$vm_list{$vm_name}{jid}."\n".
337             'vm_ram: '.$vm_list{$vm_name}{vm_ram}."\n".
338             'vm_curmem: '.$vm_list{$vm_name}{vm_curmem}."\n".
339             'vm_cpus: '.$vm_list{$vm_name}{vm_cpus}."\n".
340             'vm_ram: '.$vm_list{$vm_name}{pcpu}."\n".
341             'vm_os_type: '.$vm_list{$vm_name}{vm_os_type}."\n".
342             'ip4_addr: '.$vm_list{$vm_name}{ip4_addr}."\n".
343             'status: '.$vm_list{$vm_name}{status}."\n".
344             'vnc: '.$vm_list{$vm_name}{vnc}."\n".
345             'path: '.$vm_list{$vm_name}{path}."\n".
346             "\n"
347             }
348              
349             =head3 bnic_list
350              
351             List configured NICs.
352              
353             The command used is as below...
354              
355             cbsd bhyve-nic-list display=nodename,jname,nic_driver,nic_parent,nic_hwaddr,nic_address,nic_mtu,nic_persistent,nic_ratelimit header=0
356              
357             This returned data is a array of hashes.
358              
359             The keys are as below.
360              
361             vm - The name of the VM in question.
362            
363             driver - The driver in use. As of currently either vtnet or e1000.
364            
365             node - The node it is on.
366            
367             parent - Either the name of the parent NIC, example 'bridge1', or set to 'auto'.
368            
369             hwaddr - The MAC address for the NIC.
370            
371             address - Address of the NIC. '0' if not configured.
372            
373             mtu - The MTU of NIC. '0' if default.
374            
375             persistent - 0/1 - 1 mean persistent nic (no managed by CBSD)
376            
377             ratelimit - Rate limit for the interface. '0' is the default.
378             {tx}/{rx} (outgoing/incoming limit), {rxtx} - shared(rx+tx) limit, one value
379              
380             This dies upon failure.
381              
382             my @nics
383             eval{
384             @nics=vm 'bnic_list';
385             } or do {
386             my $error = $@ || 'Unknown failure';
387             warn('Failed to the NIC list... '.$error);
388             }
389            
390             print Dumper(\@nics);
391              
392             =head3 bpause
393              
394             This pauses a VM in question. The following modes are available. If no
395             more is specified, audo is used.
396              
397             auto - (by default) triggering - e.g, if vm active then pause
398             on - pause, stop
399             off - unpause, continue
400              
401             The command called is as below.
402              
403             cbsd 'bpause' => $vm mode=>$mode
404              
405             This dies upon failure.
406              
407             eval{
408             vm 'bpause' => 'foo';
409             } or do {
410             my $error = $@ || 'Unknown failure';
411             warn('Failed to pause the VM foo... '.$error);
412             }
413              
414             =head3 bpci_list
415              
416             List configured PCI devices for a VM.
417              
418             The command used is as below.
419              
420             cbsd bpcibus mode=list jname=$vm
421              
422             This returned data is a array of hashes.
423              
424             The keys are as below.
425              
426             name - Drive name of the PCI item.
427            
428             bus - Bus number.
429            
430             slot - Slot number.
431            
432             function - Function number.
433            
434             desc - Description of the device.
435              
436             One argument is required and that is the name of the VM.
437              
438             This dies upon failure.
439              
440             my @devices
441             eval{
442             @devices=vm 'bnic_list' => 'foo';
443             } or do {
444             my $error = $@ || 'Unknown failure';
445             warn('Failed to the PCI device list... '.$error);
446             }
447            
448             print Dumper(\@devices);
449              
450             =head3 bremove
451              
452             This removes the selected VM and remove the data. This is done via the command...
453              
454             cbsd bremove $vm
455              
456             One argument is taken and that is the name of the VM.
457              
458             This dies upon failure.
459              
460             eval{
461             vm 'bremove' => 'foo'
462             } or do {
463             my $error = $@ || 'Unknown failure';
464             warn('Failed to remove the VM foo... '.$error);
465             }
466              
467             =head3 brestart
468              
469             This restarts the selected VM. This is done via the command...
470              
471             cbsd brestart $vm
472              
473             One argument is taken and that is the name of the VM.
474              
475             This dies upon failure.
476              
477             eval{
478             vm 'brestart' => 'foo'
479             } or do {
480             my $error = $@ || 'Unknown failure';
481             warn('Failed to restart the VM foo... '.$error);
482             }
483              
484             =head3 bset
485              
486             This sets various settings for a VM via the use of...
487              
488             cbsd bset jname=$vm ...
489              
490             One argument is equired and that is the VM name.
491              
492             This will die upon failure. Please note the CBSD currently does
493             not consider non-existent variables such as 'foofoo' to be a failure
494             and silently ignores those.
495              
496             # set the the VM foo to boot from net with a resolution of 800x600
497             vm 'bset' => 'foo',
498             vm_boot => 'net',
499             bhyve_vnc_resolution => '800x600';
500              
501             =head3 bsnapshot_create
502              
503             This creates a disk snapshot of the specified VM via the command below.
504              
505             cbsd bsnapshot mode=create jname=$vm snapname=$name
506              
507             The following argument taken.
508              
509             vm - The VM to snapshot. This is required.
510              
511             name - The snapshot name. If not specified it defaults to snapshot.
512              
513             This will die upon error.
514              
515             vm 'bsnapshot_create', vm=>'foo', name=>'aSnapshot';
516              
517             =head3 bsnapshot_list
518              
519             This lists all the snapshots via the command below.
520              
521             cbsd bsnapshot mode=list display=jname,snapname,creation,refer header=0
522              
523             No arguments are taken.
524              
525             The return is a array of hashes. The hash keys are as below.
526              
527             vm - Name of the VM the snapshot is for.
528            
529             name - Name of the snapshot.
530            
531             creation - The creation date. The format is YYYY-MM-DD__hh:mm.
532            
533             refer - ?
534              
535             This will die upon error.
536              
537             my @snaps=vm 'bsnapshot_list';
538             print Dumper \@snaps;
539              
540             =head3 bsnapshot_remove
541              
542             This removes the specified snapshot for the specified VM.
543              
544             cbsd bsnapshot mode=destroy jname=$vm snapname=$name
545              
546             The following argument required.
547              
548             vm - The VM to snapshot. This is required.
549              
550             name - The snapshot name.
551              
552             This will die upon error.
553              
554             vm 'bsnapshot_remove', vm=>'foo', name=>'aSnapshot';
555              
556             =head3 bsnapshot_removeall
557              
558             This removes specified snapshot for the specified VM.
559              
560             cbsd bsnapshot mode=destroyall jname=$vm
561              
562             The following argument required.
563              
564             vm - The VM to remove all snapshots for.
565              
566             This will die upon error.
567              
568             vm 'bsnapshot_removeall', vm=>'foo';
569              
570             =head3 bsnapshot_rollback
571              
572             This rolls the disks for a VM back to a specified snapshot.
573              
574             cbsd bsnapshot mode=rollback jname=$vm snapname=$name
575              
576             The following argument required.
577              
578             vm - The VM to rollback.
579              
580             name - The snapshot name.
581              
582             This will die upon error.
583              
584             vm 'bsnapshot_rollback', vm=>'foo', name=>'aSnapshot';
585              
586             =head3 bstart
587              
588             This starts a VM. This is done via the command...
589              
590             cbsd bstart jname=$vm
591              
592             One argument is required and that is the name of the VM. If '*' or 'vm*' then
593             start all VM whose names begin with 'vm', e.g. 'vm1', 'vm2'...
594              
595             The following options may be used.
596              
597             checkpoint - The name of the checkpoint to start the VM using.
598              
599             This dies upon failure.
600              
601             eval{
602             vm 'bstart' => 'foo';
603             } or do {
604             my $error = $@ || 'Unknown failure';
605             warn('Failed to start the VM foo... '.$error);
606             }
607              
608             # starts foo from the checkpoint named checkpoint
609             vm 'bstart' => 'foo', checkpoint=>'checkpoint';
610              
611              
612             =head3 bstop
613              
614             This stops a VM. This is done via the command below...
615              
616             cbsd bstop jname=$vm [hard_timeout=$timeout] [noacpi=$noacpi]
617              
618             One argument is required and that is the name of the VM.
619              
620             The following options are optional.
621              
622             hard_timeout - Wait N seconds (30 by default) before hard reset.
623              
624             noacpi - 0,1. Set to 1 to prevent ACPI signal sending, just kill.
625             By default it will attempt to use ACPI to ask it to shutdown.
626              
627             This dies upon failure.
628              
629             eval{
630             vm 'bstop' => 'foo',
631             hard_timeout => 60;
632             } or do {
633             my $error = $@ || 'Unknown failure';
634             warn('Failed to stop the VM foo... '.$error);
635             }
636              
637             =head3 p9shares_add
638              
639             This adds a p9 share for the specified VM. This is done via
640              
641             cbsd bhyve-p9shares mode=attach jname=$vm p9device=$device p9path=$path
642              
643             The keys below are required.
644              
645             vm - Name of the VM the share is for.
646            
647             device - p9 device name, one word.
648            
649             path - The shared path.
650              
651             This will die upon error.
652              
653             vm 'p9shares_add', vm=>'foo', device=>'arc', path='/arc';
654              
655             =head3 p9shares_list
656              
657             This lists the configured p9 shares. This is fetched using the command below.
658              
659             cbsd bhyve-p9shares mode=list header=0 display=jname,p9device,p9path
660              
661             No arguments are taken.
662              
663             The returned data is a array of hashes. The hash keys are as below.
664              
665             vm - Name of the VM the share is for.
666            
667             device - p9 device name, one word.
668            
669             path - The shared path.
670              
671             This will die upon error.
672              
673             my @shares=vm 'p9shares_list';
674             print Dumper \@shares;
675              
676             =head3 p9shares_rm
677              
678             This removes a p9 share for the specified VM. This is done via
679              
680             cbsd bhyve-p9shares mode=deattach jname=$vm p9device=$device
681              
682             The keys below are required.
683              
684             vm - Name of the VM the share is for.
685            
686             device - p9 device name, one word.
687              
688             This will die upon error.
689              
690             vm 'p9shares_add', vm=>'foo', device=>'arc';
691              
692             =head3 vm_os_profiles
693              
694             Get the VM OS profiles for a specified OS type.
695              
696             One argument is required and that is the OS type.
697              
698             An optional argument is taken and that is cloudinit. It is a
699             Perl boolean value and if true it will only return profiles
700             with cloudinit support.
701              
702             The returned value is a array.
703              
704             This will die upon failure.
705              
706             # list the VM OS profiles for FreeBSD
707             my @profiles=vm 'vm_os_profiles' => 'freebsd';
708             print Dumper @profiles;
709            
710             # list the VM OS profiles for FreeBSD
711             @profiles=vm 'vm_os_profiles' => 'freebsd', cloudinit=>1;
712             print Dumper @profiles;
713              
714             =head3 vm_os_profiles_hash
715              
716             Get the VM OS profiles for a specified OS type.
717              
718             One optional argument is taken and that is cloudinit. It is a
719             Perl boolean value and if true it will only return profiles
720             with cloudinit support.
721              
722             The returned value is a two level hash. The keys for the first
723             level are the OS types and the keys for the second level are
724             the OS profile names.
725              
726             This will die upon failure.
727              
728             my %os_profiles=vm 'vm_os_profiles_hash';
729             print Dumper %os_profiles;
730            
731             # print the OS profiles for FreeBSD
732             print Dumper keys( %{ $os_profiles{freebsd} } );
733            
734             my %os_profiles=vm 'vm_os_profiles_hash', cloudinit=>1;
735             print Dumper %os_profiles;
736              
737             =head3 vm_os_types
738              
739             Get the VM OS types there are profiles for.
740              
741             The returned value is a array.
742              
743             This will die upon failure.
744              
745             # get a hash of all OS types and profiles
746             my @os_types=vm 'vm_os_profiles';
747             print Dumper @os_types;
748              
749             =head1 AUTHOR
750              
751             Zane C. Bowers-HAdley, C<< >>
752              
753             =head1 BUGS
754              
755             Please report any bugs or feature requests to C, or through
756             the web interface at L.
757             I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
758              
759              
760              
761              
762             =head1 SUPPORT
763              
764             You can find documentation for this module with the perldoc command.
765              
766             perldoc Rex::Virtualization::CBSD
767              
768              
769             You can also look for information at:
770              
771             =over 4
772              
773             =item * RT: CPAN's request tracker (report bugs here)
774              
775             L
776              
777             =item * AnnoCPAN: Annotated CPAN documentation
778              
779             L
780              
781             =item * Repository
782              
783             L
784              
785             =item * CPAN Ratings
786              
787             L
788              
789             =item * Search CPAN
790              
791             L
792              
793             =back
794              
795              
796             =head1 ACKNOWLEDGEMENTS
797              
798              
799             =head1 LICENSE AND COPYRIGHT
800              
801             This software is Copyright (c) 2020 by Zane C. Bowers-HAdley.
802              
803             This is free software, licensed under:
804              
805             The Artistic License 2.0 (GPL Compatible)
806              
807              
808             =cut
809              
810             1; # End of Rex::Virtualization::CBSD