]> git.proxmox.com Git - pve-container.git/blobdiff - src/PVE/VZDump/LXC.pm
Refactor mountpoint and general conf methods
[pve-container.git] / src / PVE / VZDump / LXC.pm
index 1ed65476b96a7d414ce537c5ff40ae55d36393c3..396a94d5dfe4c4e41eab23368290ba7831738f33 100644 (file)
@@ -94,32 +94,10 @@ my $check_mountpoint_empty = sub {
     });
 };
 
-my $lockconfig = sub {
-    my ($self, $vmid) = @_;
-
-    my $conf = PVE::LXC::load_config($vmid);
-
-    PVE::LXC::check_lock($conf);
-    $conf->{lock} = 'backup';
-
-    PVE::LXC::write_config($vmid, $conf);
-};
-
-my $unlockconfig = sub {
-    my ($self, $vmid) = @_;
-
-    my $conf = PVE::LXC::load_config($vmid);
-
-    if ($conf->{lock} && $conf->{lock} eq 'backup') {
-       delete $conf->{lock};
-       PVE::LXC::write_config($vmid, $conf);
-    }
-};
-
 sub prepare {
     my ($self, $task, $vmid, $mode) = @_;
 
-    my $conf = $self->{vmlist}->{$vmid} = PVE::LXC::load_config($vmid);
+    my $conf = $self->{vmlist}->{$vmid} = PVE::LXC::Config->load_config($vmid);
     my $storage_cfg = $self->{storecfg};
 
     my $running = PVE::LXC::check_running($vmid);
@@ -132,7 +110,8 @@ sub prepare {
     my ($id_map, $rootuid, $rootgid) = PVE::LXC::parse_id_maps($conf);
     $task->{userns_cmd} = PVE::LXC::userns_command($id_map);
 
-    PVE::LXC::foreach_mountpoint($conf, sub {
+    my $volids = $task->{volids} = [];
+    PVE::LXC::Config->foreach_mountpoint($conf, sub {
        my ($name, $data) = @_;
        my $volid = $data->{volume};
        my $mount = $data->{mp};
@@ -146,17 +125,20 @@ sub prepare {
        }
 
        push @$disks, $data;
+       push @$volids, $volid
+           if $type eq 'volume';
     });
-    my $volid_list = [map { $_->{volume} } @$disks];
 
     if ($mode eq 'snapshot') {
-       if (!PVE::LXC::has_feature('snapshot', $conf, $storage_cfg)) {
+       if (!PVE::LXC::Config->has_feature('snapshot', $conf, $storage_cfg, undef, undef, 1)) {
            die "mode failure - some volumes do not support snapshots\n";
        }
 
+       unlock_vm($self, $vmid);
+
        if ($conf->{snapshots} && $conf->{snapshots}->{vzdump}) {
            $self->loginfo("found old vzdump snapshot (force removal)");
-           PVE::LXC::snapshot_delete($vmid, 'vzdump', 1);
+           PVE::LXC::Config->snapshot_delete($vmid, 'vzdump', 1);
        }
 
        my $rootdir = $default_mount_point;
@@ -164,21 +146,20 @@ sub prepare {
        &$check_mountpoint_empty($rootdir);
 
        # set snapshot_count (freezes CT if snapshot_count > 1)
-       $task->{snapshot_count} = scalar(@$volid_list);
+       $task->{snapshot_count} = scalar(@$volids);
     } elsif ($mode eq 'stop') {
-       &$lockconfig($self, $vmid);
        my $rootdir = $default_mount_point;
        mkpath $rootdir;
        &$check_mountpoint_empty($rootdir);
-       PVE::Storage::activate_volumes($storage_cfg, $volid_list);
+       PVE::Storage::activate_volumes($storage_cfg, $volids);
     } elsif ($mode eq 'suspend') {
-       &$lockconfig($self, $vmid);
        my $pid = PVE::LXC::find_lxc_pid($vmid);
        foreach my $disk (@$disks) {
            $disk->{dir} = "/proc/$pid/root$disk->{mp}";
        }
        $task->{snapdir} = $task->{tmpdir};
     } else {
+       unlock_vm($self, $vmid);
        die "unknown mode '$mode'\n"; # should not happen
     }
 
@@ -193,15 +174,35 @@ sub prepare {
 sub lock_vm {
     my ($self, $vmid) = @_;
 
-    PVE::LXC::lock_aquire($vmid);
+    my $lockconfig = sub {
+       my ($self, $vmid) = @_;
+
+       my $conf = PVE::LXC::Config->load_config($vmid);
+
+       PVE::LXC::Config->check_lock($conf);
+       $conf->{lock} = 'backup';
+
+       PVE::LXC::Config->write_config($vmid, $conf);
+    };
+
+    PVE::LXC::Config->lock_config($vmid, $lockconfig, ($self, $vmid));
 }
 
 sub unlock_vm {
     my ($self, $vmid) = @_;
 
-    &$unlockconfig($self, $vmid);
+    my $unlockconfig = sub {
+       my ($self, $vmid) = @_;
+
+       my $conf = PVE::LXC::Config->load_config($vmid);
 
-    PVE::LXC::lock_release($vmid);
+       if ($conf->{lock} && $conf->{lock} eq 'backup') {
+           delete $conf->{lock};
+           PVE::LXC::Config->write_config($vmid, $conf);
+       }
+    };
+
+    PVE::LXC::Config->lock_config($vmid, $unlockconfig, ($self, $vmid));
 }
 
 sub snapshot {
@@ -210,21 +211,21 @@ sub snapshot {
     $self->loginfo("create storage snapshot 'vzdump'");
 
     # todo: freeze/unfreeze if we have more than one volid
-    PVE::LXC::snapshot_create($vmid, 'vzdump', "vzdump backup snapshot");
+    PVE::LXC::Config->snapshot_create($vmid, 'vzdump', 0, "vzdump backup snapshot");
     $task->{cleanup}->{remove_snapshot} = 1;
     
     # reload config
-    my $conf = $self->{vmlist}->{$vmid} = PVE::LXC::load_config($vmid);
+    my $conf = $self->{vmlist}->{$vmid} = PVE::LXC::Config->load_config($vmid);
     die "unable to read vzdump snapshot config - internal error"
        if !($conf->{snapshots} && $conf->{snapshots}->{vzdump});
 
     my $disks = $task->{disks};
-    my $volid_list = [map { $_->{volume} } @$disks];
+    my $volids = $task->{volids};
 
     my $rootdir = $default_mount_point;
     my $storage_cfg = $self->{storecfg};
 
-    PVE::Storage::activate_volumes($storage_cfg, $volid_list, 'vzdump');
+    PVE::Storage::activate_volumes($storage_cfg, $volids, 'vzdump');
     foreach my $disk (@$disks) {
        $disk->{dir} = "${rootdir}$disk->{mp}";
        PVE::LXC::mountpoint_mount($disk, $rootdir, $storage_cfg, 'vzdump');
@@ -287,7 +288,7 @@ sub assemble {
 
     mkpath "$tmpdir/etc/vzdump/";
 
-    my $conf = PVE::LXC::load_config($vmid);
+    my $conf = PVE::LXC::Config->load_config($vmid);
     delete $conf->{lock};
     delete $conf->{snapshots};
     delete $conf->{'pve.parent'};
@@ -318,6 +319,12 @@ sub archive {
            push @sources, ".$disk->{mp}";
        }
        $task->{snapdir} = $rootdir;
+    } elsif ($task->{mode} eq 'snapshot') {
+       # mounting the vzdump snapshots and setting $snapdir is already done,
+       # but we need to include all mountpoints here!
+       foreach my $disk (@$disks) {
+           push @sources, ".$disk->{mp}";
+       }
     } else {
        # the data was rsynced to a temporary location, only use '.' to avoid
        # having mountpoints duplicated
@@ -369,7 +376,7 @@ sub archive {
 sub cleanup {
     my ($self, $task, $vmid) = @_;
 
-    my $conf = PVE::LXC::load_config($vmid);
+    my $conf = PVE::LXC::Config->load_config($vmid);
 
     if ($task->{mode} ne 'suspend') {
        my $rootdir = $default_mount_point;
@@ -381,7 +388,7 @@ sub cleanup {
 
     if ($task->{cleanup}->{remove_snapshot}) {
        $self->loginfo("remove vzdump snapshot");
-       PVE::LXC::snapshot_delete($vmid, 'vzdump', 0);
+       PVE::LXC::Config->snapshot_delete($vmid, 'vzdump', 0);
     }
 }