minor changes to bclone and add p9shares_add

This commit is contained in:
Zane C. B-H 2021-10-16 15:48:57 -05:00
parent ab39f72b75
commit c0151f534c
4 changed files with 106 additions and 5 deletions

View File

@ -512,6 +512,20 @@ This dies upon failure.
=head3 p9shares_list
This adds a p9 share for the specified VM. This is done via
cbsd bhyve-p9shares mode=attach jname=$vm p9device=$device p9path=$path
The keys below are required.
vm - Name of the VM the share is for.
device - p9 device name, one word.
path - The shared path.
=head3 p9shares_list
This lists the configured p9 shares. This is fetched using the command below.
cbsd bhyve-p9shares mode=list header=0 display=jname,p9device,p9path
@ -521,7 +535,9 @@ No arguments are taken.
The returned data is a array of hashes. The hash keys are as below.
vm - Name of the VM the share is for.
device - p9 device name, one word.
path - The shared path.
This will die upon error.

View File

@ -26,25 +26,29 @@ sub execute {
# make sure it does not contain any possible characters we don't want
if ( $opts{$key} =~ /[\t\ \=\\\/\'\"\n\;\&]/ ) {
die 'The variable "' . $key . '" matched /[\t\ \=\/\\\'\"\n\;\&]/, meaning it is not a valid variable name';
die 'The value for "'
. $key . '", "'
. $opts{$key}
. '", matched /[\t\ \=\/\\\'\"\n\;\&]/, meaning it is not a valid value';
}
}
# the command to use
my $command = 'cbsd bclone old='.$opts{old}.' new='.$opts{new};
my $command = 'cbsd bclone old=' . $opts{old} . ' new=' . $opts{new};
# make sure all the variables are sane
# and if set and sane add it
my @bool_vars = ( 'checkstate', 'promote', 'mac_reinit' );
foreach my $key (@bool_vars) {
# make sure that the it is either 0 or 1
if ( defined( $opts{$key} ) && ( $opts{$key} !~ /^[01]$/ ) ) {
die ( 'Key "'.$key.'" defined and is "'.$opts{key}.'", which does not match /^[01]$/' );
die( 'Key "' . $key . '" defined and is "' . $opts{key} . '", which does not match /^[01]$/' );
}
# if we get here it is sane and if defined, set it
if (defined( $opts{key} )) {
$command=$command.' '.$key.'='.$opts{$key};
if ( defined( $opts{key} ) ) {
$command = $command . ' ' . $key . '=' . $opts{$key};
}
}

View File

@ -0,0 +1,68 @@
#
# (c) Zane C. Bowers-Hadley <vvelox@vvelox.net>
#
package Rex::Virtualization::CBSD::p9shares_add;
use strict;
use warnings;
our $VERSION = '0.0.1'; # VERSION
use Rex::Logger;
use Rex::Helper::Run;
use Term::ANSIColor qw(colorstrip);
sub execute {
my ( $class, %opts ) = @_;
# make sure all the keys are sane
my @required_keys = ( 'vm', 'device', 'path' );
foreach my $key (@required_keys) {
if ( !defined( $opts{$key} ) ) {
die 'The required variable "' . $key . '" is not set';
}
# make sure it does not contain any possible characters we don't want
if ( ( $key ne 'path' )
&& ( $opts{$key} =~ /[\t\ \=\\\/\'\"\n\;\&]/ ) )
{
die 'The value for "'
. $key . '", "'
. $opts{$key}
. '", matched /[\t\ \=\/\\\'\"\n\;\&]/, meaning it is not a valid value';
}
elsif ( $opts{$key} =~ /[\t\'\"\n\;\&]/ ) {
die 'The value for "'
. $key . '", "'
. $opts{$key}
. '", matched /[\t\'\"\n\;\&]/, meaning it is not a valid value';
}
}
# put together the command
my $command
= 'cbsd bhyve-p9shares mode=attach jname='
. $opts{vm}
. ' device='
. $opts{device}
. " path='"
. $opts{path} . "'";
Rex::Logger::debug( "Adding CBSD p9share via... " . $command );
my $returned = i_run( $command, fail_ok => 1 );
# the output is colorized, if there is an error
$returned = colorstrip($returned);
# check for this second as no VM will also exit non-zero
if ( $? != 0 ) {
die( "Error running '" . $command . "'" );
}
return 1;
}
1;

View File

@ -0,0 +1,13 @@
#!perl
use 5.006;
use strict;
use warnings;
use Test::More;
plan tests => 1;
BEGIN {
use_ok( 'Rex::Virtualization::CBSD::p9shares_add' ) || print "Bail out!\n";
}
diag( "Testing Rex::Virtualization::CBSD::p9shares_add $Rex::Virtualization::CBSD::p9shares_add::VERSION, Perl $], $^X" );