my $code = sub {
my $old_conf = PVE::LXC::Config->load_config($vmid);
+ my $was_template;
my $vollist = [];
eval {
if ($is_root && $archive ne '-') {
my $orig_conf;
($orig_conf, $orig_mp_param) = PVE::LXC::Create::recover_config($archive);
+ $was_template = delete $orig_conf->{template};
# When we're root call 'restore_configuration' with ristricted=0,
# causing it to restore the raw lxc entries, among which there may be
# 'lxc.idmap' entries. We need to make sure that the extracted contents
foreach my $mp (keys %$delayed_mp_param) {
$conf->{$mp} = $delayed_mp_param->{$mp};
}
+ # If the template flag was set, we try to convert again to template after restore
+ if ($was_template) {
+ print STDERR "Convert restored container to template...\n";
+ if (my $err = check_storage_supports_templates($conf)) {
+ warn $err;
+ warn "Leave restored backup as container instead of converting to template.\n"
+ } else {
+ PVE::LXC::template_create($vmid, $conf);
+ $conf->{template} = 1;
+ }
+ }
PVE::LXC::Config->write_config($vmid, $conf);
};
if (my $err = $@) {
return $rpcenv->fork_worker($workername, $vmid, $authuser, $realcmd);
}});
+sub check_storage_supports_templates {
+ my ($conf) = @_;
+
+ my $scfg = PVE::Storage::config();
+ eval {
+ PVE::LXC::Config->foreach_mountpoint($conf, sub {
+ my ($ms, $mp) = @_;
+
+ my ($sid) = PVE::Storage::parse_volume_id($mp->{volume}, 0);
+ die "Warning: Directory storage '$sid' does not support container templates!\n"
+ if $scfg->{ids}->{$sid}->{path};
+ });
+ };
+ return $@
+}
+
__PACKAGE__->register_method({
name => 'vmdiridx',
path => '{vmid}',
die "you can't convert a CT to template if the CT is running\n"
if PVE::LXC::check_running($vmid);
- my $scfg = PVE::Storage::config();
- PVE::LXC::Config->foreach_mountpoint($conf, sub {
- my ($ms, $mp) = @_;
-
- my ($sid) =PVE::Storage::parse_volume_id($mp->{volume}, 0);
- die "Directory storage '$sid' does not support container templates!\n"
- if $scfg->{ids}->{$sid}->{path};
- });
+ if (my $err = check_storage_supports_templates($conf)) {
+ die $err;
+ }
my $realcmd = sub {
PVE::LXC::template_create($vmid, $conf);
$conf = PVE::LXC::Config::parse_pct_config("/lxc/0.conf" , $raw);
delete $conf->{snapshots};
- delete $conf->{template}; # restored CT is never a template
PVE::LXC::Config->foreach_mountpoint($conf, sub {
my ($ms, $mountpoint) = @_;
next if $key eq 'digest' || $key eq 'rootfs' || $key eq 'snapshots' || $key eq 'unprivileged' || $key eq 'parent';
next if $key =~ /^mp\d+$/; # don't recover mountpoints
next if $key =~ /^unused\d+$/; # don't recover unused disks
+ # we know if it was a template in the restore API call and check if the target
+ # storage supports creating a template there
+ next if $key =~ /^template$/;
if ($restricted && $key eq 'lxc') {
warn "skipping custom lxc options, restore manually as root:\n";
warn "--------------------------------\n";