[pve-devel] [PATCH container] pct mount: add --snapshot option

Fabian Grünbichler f.gruenbichler at proxmox.com
Tue Sep 13 11:22:55 CEST 2016


On Thu, Aug 11, 2016 at 11:56:55AM +0200, Wolfgang Bumiller wrote:
> Mounts an existing snapshot instead of the current state.
> ---
> Useful for testing storage implementations ;-)
> 
>  src/PVE/CLI/pct.pm | 17 ++++++++++++++++-
>  src/PVE/LXC.pm     |  6 +++---
>  2 files changed, 19 insertions(+), 4 deletions(-)
> 
> diff --git a/src/PVE/CLI/pct.pm b/src/PVE/CLI/pct.pm
> index cf4a014..8ea23b6 100755
> --- a/src/PVE/CLI/pct.pm
> +++ b/src/PVE/CLI/pct.pm
> @@ -278,6 +278,11 @@ __PACKAGE__->register_method({
>  	additionalProperties => 0,
>  	properties => {
>  	    vmid => get_standard_option('pve-vmid', { completion => \&PVE::LXC::complete_ctid }),
> +	    snapshot => {
> +		type => 'string',
> +		optional => 1,
> +		description => "The snapshot to mount.",
> +	    },
>  	},
>      },
>      returns => { type => 'null' },
> @@ -290,7 +295,17 @@ __PACKAGE__->register_method({
>  	my $storecfg = PVE::Storage::config();
>  	PVE::LXC::Config->lock_config($vmid, sub {
>  	    my $conf = PVE::LXC::Config->set_lock($vmid, 'mounted');
> -	    PVE::LXC::mount_all($vmid, $storecfg, $conf);
> +
> +	    my $snapshot = extract_param($param, 'snapshot');
> +
> +	    if ($snapshot) {
> +		my $snaps = $conf->{snapshots}
> +		    or die "No snapshots available\n";
> +		$conf = $snaps->{$snapshot}
> +		    or die "No such snapshot: $snapshot\n";
> +	    }
> +
> +	    PVE::LXC::mount_all($vmid, $storecfg, $conf, 0, $snapshot);
>  	});
>  	return undef;
>      }});
> diff --git a/src/PVE/LXC.pm b/src/PVE/LXC.pm
> index 35ce796..b07c198 100644
> --- a/src/PVE/LXC.pm
> +++ b/src/PVE/LXC.pm
> @@ -923,13 +923,13 @@ sub umount_all {
>  }
>  
>  sub mount_all {
> -    my ($vmid, $storage_cfg, $conf, $ignore_ro) = @_;
> +    my ($vmid, $storage_cfg, $conf, $ignore_ro, $snapshot) = @_;
>  
>      my $rootdir = "/var/lib/lxc/$vmid/rootfs";
>      File::Path::make_path($rootdir);
>  
>      my $volid_list = PVE::LXC::Config->get_vm_volumes($conf);
> -    PVE::Storage::activate_volumes($storage_cfg, $volid_list);
> +    PVE::Storage::activate_volumes($storage_cfg, $volid_list, $snapshot);
>  
>      eval {
>  	PVE::LXC::Config->foreach_mountpoint($conf, sub {
> @@ -937,7 +937,7 @@ sub mount_all {
>  
>  	    $mountpoint->{ro} = 0 if $ignore_ro;
>  
> -	    mountpoint_mount($mountpoint, $rootdir, $storage_cfg);
> +	    mountpoint_mount($mountpoint, $rootdir, $storage_cfg, $snapshot);
>          });
>      };
>      if (my $err = $@) {
> -- 
> 2.1.4

this would imho need some additional code to store which snapshot was
mounted - otherwise pct unmount will fail in some cases:

# pct set 100 -mp0 somestorage:somesize
# pct snapshot 100 test
# pct set 100 -delete mp0
# pct mount 100 -snapshot test
# pct unmount 100
umount: /var/lib/lxc/100/rootfs: target is busy
        (In some cases useful info about processes that
        use the device is found by lsof(8) or fuser(1).)
command 'umount -d /var/lib/lxc/100/rootfs/' failed: exit code 32

alternatively / additionally, it might be nice to allow parallel
mounting of snapshots by modifying the mountpoint target ("$rootdir").
e.g., the snapshot "test" would then be mounted to
'/var/lib/lxc/100/rootfs-test'. this would allow easy comparison of
snapshots and manual rollbacks on a per file base (similar to what some
backup solutions based on tar+rsync provide).




More information about the pve-devel mailing list