Skip to content

Commit

Permalink
Merge pull request #70 from hadfl/check_snap
Browse files Browse the repository at this point in the history
enhancements
  • Loading branch information
citrus-it authored Aug 13, 2021
2 parents de50a86 + cc84234 commit 2a3c987
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 14 deletions.
27 changes: 17 additions & 10 deletions lib/Zadm/Images.pm
Original file line number Diff line number Diff line change
Expand Up @@ -172,25 +172,32 @@ sub curl($self, $files, $opts = {}) {
}
}

sub zfsRecv($self, $file, $ds) {
my @cmd = ($self->utils->getCmd('zfs'), qw(recv -Fv), $ds);

$self->log->debug(@cmd);

privSet({ add => 1, inherit => 1 }, PRIV_SYS_MOUNT);
open my $zfs, '|-', @cmd or Mojo::Exception->throw("ERROR: receiving zfs stream: $!\n");
privSet({ remove => 1, inherit => 1 }, PRIV_SYS_MOUNT);

sub seedZvol($self, $file, $ds) {
my $decomp = IO::Uncompress::AnyUncompress->new($file->to_string)
or Mojo::Exception->throw("ERROR: decompressing '$file' failed: $AnyUncompressError\n");

my $bytes = 0;
my $start = my $last = time;
my $zvol;
while (my $status = $decomp->read(my $buffer)) {
Mojo::Exception->throw("ERROR: decompressing '$file' failed: $AnyUncompressError\n")
if $status < 0;

print $zfs $buffer;
# detect image file type
if ($bytes == 0) {
my $type = $self->utils->getFileType($buffer, '.' . $file->extname) // '';

my @cmd = $type eq 'ZFS snapshot stream' ? ($self->utils->getCmd('zfs'), qw(recv -Fv), $ds)
: ($self->utils->getCmd('dd'), "of=/dev/zvol/dsk/$ds", 'bs=1M');

$self->log->debug(@cmd);

privSet({ add => 1, inherit => 1 }, PRIV_SYS_MOUNT);
open $zvol, '|-', @cmd or Mojo::Exception->throw("ERROR: receiving zfs stream: $!\n");
privSet({ remove => 1, inherit => 1 }, PRIV_SYS_MOUNT);
}

print $zvol $buffer;
$bytes += $status;

my $now = time;
Expand Down
9 changes: 9 additions & 0 deletions lib/Zadm/Utils.pm
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ my %CMDS = (
ipfstat => '/usr/sbin/ipfstat',
ipmon => '/usr/sbin/ipmon',
ipnat => '/usr/sbin/ipnat',
file => '/usr/bin/file',
dd => '/usr/bin/dd',
);

my %ENVARGS = map {
Expand Down Expand Up @@ -265,6 +267,13 @@ sub getZfsProp($self, $ds, $prop = []) {
return { map { $prop->[$_] => $vals->[$_] } (0 .. $#$prop) };
}

sub getFileType($self, $data, $suffix = '') {
my $f = Mojo::File->new(File::Temp->new($suffix ? (SUFFIX => $suffix) : ()));
$f->spurt($data);

return $self->readProc('file', [ '-b', $f ])->[0];
}

sub domain($self) {
my %domain;

Expand Down
10 changes: 6 additions & 4 deletions lib/Zadm/Zone/KVM.pm
Original file line number Diff line number Diff line change
Expand Up @@ -318,10 +318,8 @@ sub setPreProcess($self, $cfg) {
}

sub install($self, @args) {
my $img = $self->hasimg ? $self->image->image : {};
# just install the zone if no image was provided for the bootdisk
return $self->SUPER::install
if !%$img;
return $self->SUPER::install if !$self->hasimg;

$self->config->{bootdisk} || do {
$self->log->warn('WARNING: no bootdisk attribute specified. Not installing image');
Expand All @@ -334,6 +332,10 @@ sub install($self, @args) {
Mojo::Exception->throw("ERROR: destination has snapshots (eg. $snapshots->[0])\n"
. "must destroy them to overwrite it\n") if @$snapshots;

my $img = $self->image->image;
# just install the zone if no valid image was provided for the bootdisk
return $self->SUPER::install if !%$img;

$img->{_file} && -r $img->{_file} || do {
$self->log->warn('WARNING: no valid image path given. Not installing image');
return $self->SUPER::install;
Expand All @@ -351,7 +353,7 @@ sub install($self, @args) {
}

if ($check !~ /^no?$/i) {
$self->zones->images->zfsRecv($img->{_file}, $self->config->{bootdisk}->{path});
$self->zones->images->seedZvol($img->{_file}, $self->config->{bootdisk}->{path});
# TODO: '-x volsize' for zfs recv seems not to work so we must reset the
# volsize to the original value after receive
privSet({ add => 1, inherit => 1 }, PRIV_SYS_MOUNT);
Expand Down

0 comments on commit 2a3c987

Please sign in to comment.