]> git.proxmox.com Git - pve-container.git/blobdiff - src/PVE/LXC/Config.pm
cleanup replication config update
[pve-container.git] / src / PVE / LXC / Config.pm
index 0bc20d161d0e7dfa45dc7c66a543b9185ed2d1b0..8e1645c2a3e24d82629246d5d46e419998f03f5d 100644 (file)
@@ -8,6 +8,7 @@ use PVE::Cluster qw(cfs_register_file);
 use PVE::INotify;
 use PVE::JSONSchema qw(get_standard_option);
 use PVE::Tools;
+use PVE::ReplicationTools;
 
 use base qw(PVE::AbstractConfig);
 
@@ -49,6 +50,8 @@ sub mountpoint_backup_enabled {
 
     return 1 if $mp_key eq 'rootfs';
 
+    return 0 if $mountpoint->{type} ne 'volume';
+
     return 1 if $mountpoint->{backup};
 
     return 0;
@@ -122,7 +125,9 @@ sub __snapshot_delete_remove_drive {
        my $value = $snap->{$remove_drive};
        my $mountpoint = $remove_drive eq 'rootfs' ? $class->parse_ct_rootfs($value, 1) : $class->parse_ct_mountpoint($value, 1);
        delete $snap->{$remove_drive};
-       $class->add_unused_volume($snap, $mountpoint->{volume});
+
+       $class->add_unused_volume($snap, $mountpoint->{volume})
+           if ($mountpoint->{type} eq 'volume');
     }
 }
 
@@ -135,6 +140,9 @@ sub __snapshot_delete_vmstate_file {
 sub __snapshot_delete_vol_snapshot {
     my ($class, $vmid, $ms, $mountpoint, $snapname, $unused) = @_;
 
+    return if $snapname eq 'vzdump' &&
+       !$class->mountpoint_backup_enabled($ms, $mountpoint);
+
     my $storecfg = PVE::Storage::config();
     PVE::Storage::volume_snapshot_delete($storecfg, $mountpoint->{volume}, $snapname);
     push @$unused, $mountpoint->{volume};
@@ -167,6 +175,35 @@ sub __snapshot_rollback_vm_start {
     die "implement me - save vmstate\n";
 }
 
+sub __snapshot_rollback_get_unused {
+    my ($class, $conf, $snap) = @_;
+
+    my $unused = [];
+
+    $class->__snapshot_foreach_volume($conf, sub {
+       my ($vs, $volume) = @_;
+
+       return if $volume->{type} ne 'volume';
+
+       my $found = 0;
+       my $volid = $volume->{volume};
+
+       $class->__snapshot_foreach_volume($snap, sub {
+           my ($ms, $mountpoint) = @_;
+
+           return if $found;
+           return if ($mountpoint->{type} ne 'volume');
+
+           $found = 1
+               if ($mountpoint->{volume} && $mountpoint->{volume} eq $volid);
+       });
+
+       push @$unused, $volid if !$found;
+    });
+
+    return $unused;
+}
+
 sub __snapshot_foreach_volume {
     my ($class, $conf, $func) = @_;
 
@@ -201,7 +238,7 @@ my $rootfs_desc = {
     },
     ro => {
        type => 'boolean',
-       description => 'Read-only mountpoint (not supported with bind mounts)',
+       description => 'Read-only mount point',
        optional => 1,
     },
     quota => {
@@ -209,6 +246,19 @@ my $rootfs_desc = {
        description => 'Enable user quotas inside the container (not supported with zfs subvolumes)',
        optional => 1,
     },
+    replica => {
+       type => 'boolean',
+       description => 'Will include this volume to a storage replica job.',
+       optional => 1,
+       default => 1,
+    },
+    shared => {
+       type => 'boolean',
+       description => 'Mark this non-volume mount point as available on multiple nodes (see \'nodes\')',
+       verbose_description => "Mark this non-volume mount point as available on all nodes.\n\nWARNING: This option does not share the mount point automatically, it assumes it is shared already!",
+       optional => 1,
+       default => 0,
+    },
 };
 
 PVE::JSONSchema::register_standard_option('pve-ct-rootfs', {
@@ -270,6 +320,13 @@ my $confdesc = {
        maximum => 6,
        default => 2,
     },
+    cores => {
+       optional => 1,
+       type => 'integer',
+       description => "The number of cores assigned to the container. A container can use all available cores by default.",
+       minimum => 1,
+       maximum => 128,
+    },
     cpulimit => {
        optional => 1,
        type => 'number',
@@ -334,6 +391,31 @@ my $confdesc = {
        type => 'integer',
        minimum => 0,
     },
+    replica => {
+       optional => 1,
+       description => "Storage replica for local storage.",
+       type => 'boolean',
+       default => 0,
+    },
+    replica_rate_limit => {
+       optional => 1,
+       description => "Storage replica rate limit in KBytes/s.",
+       type => 'integer',
+       minimum => 1,
+    },
+    replica_target => {
+       optional => 1,
+       description => "Storage replica target node.",
+       type => 'string',
+    },
+    replica_interval => {
+       optional => 1,
+       description => "Storage replica sync interval.",
+       type => 'integer',
+       minimum => 1,
+       maximum => 1440,
+       default => 15,
+    },
     cmode => {
        optional => 1,
        description => "Console mode. By default, the console command tries to open a connection to one of the available tty devices. By setting cmode to 'console' it tries to attach to /dev/console instead. If you set cmode to 'shell', it simply invokes a shell inside the container (no login).",
@@ -389,8 +471,9 @@ my $valid_lxc_conf_keys = {
     'lxc.rootfs' => 'lxc.rootfs is auto generated from rootfs',
     'lxc.rootfs.mount' => 1,
     'lxc.rootfs.options' => 'lxc.rootfs.options is not supported' .
-                            ', please use mountpoint options in the "rootfs" key',
+                            ', please use mount point options in the "rootfs" key',
     # lxc.cgroup.*
+    # lxc.limit.*
     'lxc.cap.drop' => 1,
     'lxc.cap.keep' => 1,
     'lxc.aa_profile' => 1,
@@ -483,8 +566,8 @@ our $netconf_desc = {
     },
     tag => {
        type => 'integer',
-       minimum => '1',
-       maximum => '4094',
+       minimum => 1,
+       maximum => 4094,
        description => "VLAN tag for this interface.",
        optional => 1,
     },
@@ -535,14 +618,19 @@ my $mp_desc = {
     %$rootfs_desc,
     backup => {
        type => 'boolean',
-       description => 'Whether to include the mountpoint in backups.',
+       description => 'Whether to include the mount point in backups.',
+       verbose_description => 'Whether to include the mount point in backups '.
+                              '(only used for volume mount points).',
        optional => 1,
     },
     mp => {
        type => 'string',
        format => 'pve-lxc-mp-string',
        format_description => 'Path',
-       description => 'Path to the mountpoint as seen from inside the container.',
+       description => 'Path to the mount point as seen from inside the container '.
+                      '(must not contain symlinks).',
+       verbose_description => "Path to the mount point as seen from inside the container.\n\n".
+                              "NOTE: Must not contain any symlinks for security reasons."
     },
 };
 PVE::JSONSchema::register_format('pve-ct-mountpoint', $mp_desc);
@@ -606,7 +694,7 @@ sub parse_pct_config {
            my $key = $1;
            my $value = $3;
            my $validity = $valid_lxc_conf_keys->{$key} || 0;
-           if ($validity eq 1 || $key =~ m/^lxc\.cgroup\./) {
+           if ($validity eq 1 || $key =~ m/^lxc\.(?:cgroup|limit)\./) {
                push @{$conf->{lxc}}, [$key, $value];
            } elsif (my $errmsg = $validity) {
                warn "vm $vmid - $key: $errmsg\n";
@@ -721,12 +809,14 @@ sub update_pct_config {
     if (defined($delete)) {
        foreach my $opt (@$delete) {
            if (!exists($conf->{$opt})) {
-               warn "no such option: $opt\n";
+               # silently ignore
                next;
            }
 
-           if ($opt eq 'hostname' || $opt eq 'memory' || $opt eq 'rootfs') {
+           if ($opt eq 'memory' || $opt eq 'rootfs') {
                die "unable to delete required option '$opt'\n";
+           } elsif ($opt eq 'hostname') {
+               delete $conf->{$opt};
            } elsif ($opt eq 'swap') {
                delete $conf->{$opt};
                PVE::LXC::write_cgroup_value("memory", $vmid,
@@ -737,6 +827,8 @@ sub update_pct_config {
                     $opt eq 'tty' || $opt eq 'console' || $opt eq 'cmode') {
                next if $hotplug_error->($opt);
                delete $conf->{$opt};
+           } elsif ($opt eq 'cores') {
+               delete $conf->{$opt}; # rest is handled by pvestatd
            } elsif ($opt eq 'cpulimit') {
                PVE::LXC::write_cgroup_value("cpu", $vmid, "cpu.cfs_quota_us", -1);
                delete $conf->{$opt};
@@ -765,6 +857,15 @@ sub update_pct_config {
                }
            } elsif ($opt eq 'unprivileged') {
                die "unable to delete read-only option: '$opt'\n";
+           }  elsif ($opt eq "replica" || $opt eq "replica_target") {
+               delete $conf->{$opt};
+               delete $conf->{replica} if $opt eq "replica_target";
+
+               # job_remove required updated lxc conf
+               PVE::ReplicationTools::job_remove($vmid);
+           } elsif ($opt eq "replica_interval" || $opt eq "replica_rate_limit") {
+               delete $conf->{$opt};
+               PVE::ReplicationTools::update_conf($vmid, $opt, $param->{$opt});
            } else {
                die "implement me (delete: $opt)"
            }
@@ -822,7 +923,8 @@ sub update_pct_config {
     foreach my $opt (keys %$param) {
        my $value = $param->{$opt};
        my $check_protection_msg = "can't update CT $vmid drive '$opt'";
-       if ($opt eq 'hostname') {
+       my $update;
+       if ($opt eq 'hostname' || $opt eq 'arch') {
            $conf->{$opt} = $value;
        } elsif ($opt eq 'onboot') {
            $conf->{$opt} = $value ? 1 : 0;
@@ -839,8 +941,14 @@ sub update_pct_config {
            next if $hotplug_error->($opt);
            my $list = PVE::LXC::verify_searchdomain_list($value);
            $conf->{$opt} = $list;
+       } elsif ($opt eq 'cores') {
+           $conf->{$opt} = $value;# rest is handled by pvestatd
        } elsif ($opt eq 'cpulimit') {
-           PVE::LXC::write_cgroup_value("cpu", $vmid, "cpu.cfs_quota_us", int(100000*$value));
+           if ($value == 0) {
+               PVE::LXC::write_cgroup_value("cpu", $vmid, "cpu.cfs_quota_us", -1);
+           } else {
+               PVE::LXC::write_cgroup_value("cpu", $vmid, "cpu.cfs_quota_us", int(100000*$value));
+           }
            $conf->{$opt} = $value;
        } elsif ($opt eq 'cpuunits') {
            $conf->{$opt} = $value;
@@ -896,10 +1004,33 @@ sub update_pct_config {
        } elsif ($opt eq 'ostype') {
            next if $hotplug_error->($opt);
            $conf->{$opt} = $value;
+       } elsif ($opt eq "replica") {
+           die "Not all volumes are syncable, please check your config\n"
+               if !PVE::ReplicationTools::check_guest_volumes_syncable($conf, 'lxc');
+           $conf->{$opt} = $param->{$opt};
+           die "replica_target is required\n" if !$conf->{replica_target}
+           && !$param->{replica_target};
+           if ($param->{replica}) {
+               PVE::ReplicationTools::job_enable($vmid);
+           } else {
+               PVE::ReplicationTools::job_disable($vmid);
+           }
+           $update = 1;
+       } elsif ($opt eq "replica_interval" || $opt eq "replica_rate_limit") {
+           $conf->{$opt} = $param->{$opt};
+           PVE::ReplicationTools::update_conf($vmid, $opt, $param->{$opt});
+           $update = 1;
+       } elsif ($opt eq "replica_target") {
+           die "Node: $param->{$opt} does not exists in Cluster.\n"
+               if !PVE::Cluster::check_node_exists($param->{$opt});
+           $update = 1;
+           PVE::ReplicationTools::update_conf($vmid, $opt, $param->{$opt})
+               if defined($conf->{$opt});
+           $conf->{$opt} = $param->{$opt};
        } else {
            die "implement me: $opt";
        }
-       PVE::LXC::Config->write_config($vmid, $conf) if $running;
+       PVE::LXC::Config->write_config($vmid, $conf) if $running || $update;
     }
 
     # Apply deletions and creations of new volumes
@@ -1038,7 +1169,10 @@ sub parse_lxc_network {
     $res = PVE::JSONSchema::parse_property_string($netconf_desc, $data);
 
     $res->{type} = 'veth';
-    $res->{hwaddr} = PVE::Tools::random_ether_addr() if !$res->{hwaddr};
+    if (!$res->{hwaddr}) {
+       my $dc = PVE::Cluster::cfs_read_file('datacenter.cfg');
+       $res->{hwaddr} = PVE::Tools::random_ether_addr($dc->{mac_prefix});
+    }
 
     return $res;
 }
@@ -1085,6 +1219,19 @@ sub has_dev_console {
     return !(defined($conf->{console}) && !$conf->{console});
 }
 
+sub has_lxc_entry {
+    my ($class, $conf, $keyname) = @_;
+
+    if (my $lxcconf = $conf->{lxc}) {
+       foreach my $entry (@$lxcconf) {
+           my ($key, undef) = @$entry;
+           return 1 if $key eq $keyname;
+       }
+    }
+
+    return 0;
+}
+
 sub get_tty_count {
     my ($class, $conf) = @_;
 
@@ -1110,7 +1257,7 @@ sub mountpoint_names {
 }
 
 sub foreach_mountpoint_full {
-    my ($class, $conf, $reverse, $func) = @_;
+    my ($class, $conf, $reverse, $func, @param) = @_;
 
     foreach my $key ($class->mountpoint_names($reverse)) {
        my $value = $conf->{$key};
@@ -1118,20 +1265,20 @@ sub foreach_mountpoint_full {
        my $mountpoint = $key eq 'rootfs' ? $class->parse_ct_rootfs($value, 1) : $class->parse_ct_mountpoint($value, 1);
        next if !defined($mountpoint);
 
-       &$func($key, $mountpoint);
+       &$func($key, $mountpoint, @param);
     }
 }
 
 sub foreach_mountpoint {
-    my ($class, $conf, $func) = @_;
+    my ($class, $conf, $func, @param) = @_;
 
-    $class->foreach_mountpoint_full($conf, 0, $func);
+    $class->foreach_mountpoint_full($conf, 0, $func, @param);
 }
 
 sub foreach_mountpoint_reverse {
-    my ($class, $conf, $func) = @_;
+    my ($class, $conf, $func, @param) = @_;
 
-    $class->foreach_mountpoint_full($conf, 1, $func);
+    $class->foreach_mountpoint_full($conf, 1, $func, @param);
 }
 
 sub get_vm_volumes {