]>
Commit | Line | Data |
---|---|---|
52389a07 DM |
1 | package PVE::API2::LXC::Config; |
2 | ||
3 | use strict; | |
4 | use warnings; | |
5 | ||
6 | use PVE::SafeSyslog; | |
7 | use PVE::Tools qw(extract_param run_command); | |
8 | use PVE::Exception qw(raise raise_param_exc); | |
9 | use PVE::INotify; | |
10 | use PVE::Cluster qw(cfs_read_file); | |
11 | use PVE::AccessControl; | |
12 | use PVE::Firewall; | |
13 | use PVE::Storage; | |
14 | use PVE::RESTHandler; | |
15 | use PVE::RPCEnvironment; | |
16 | use PVE::LXC; | |
17 | use PVE::LXC::Create; | |
52389a07 DM |
18 | use PVE::JSONSchema qw(get_standard_option); |
19 | use base qw(PVE::RESTHandler); | |
20 | ||
21 | use Data::Dumper; # fixme: remove | |
22 | ||
23 | __PACKAGE__->register_method({ | |
24 | name => 'vm_config', | |
25 | path => '', | |
26 | method => 'GET', | |
27 | proxyto => 'node', | |
28 | description => "Get container configuration.", | |
29 | permissions => { | |
30 | check => ['perm', '/vms/{vmid}', [ 'VM.Audit' ]], | |
31 | }, | |
32 | parameters => { | |
33 | additionalProperties => 0, | |
34 | properties => { | |
35 | node => get_standard_option('pve-node'), | |
68e8f3c5 | 36 | vmid => get_standard_option('pve-vmid', { completion => \&PVE::LXC::complete_ctid }), |
52389a07 DM |
37 | }, |
38 | }, | |
39 | returns => { | |
40 | type => "object", | |
41 | properties => { | |
42 | digest => { | |
43 | type => 'string', | |
44 | description => 'SHA1 digest of configuration file. This can be used to prevent concurrent modifications.', | |
45 | } | |
46 | }, | |
47 | }, | |
48 | code => sub { | |
49 | my ($param) = @_; | |
50 | ||
67afe46e | 51 | my $conf = PVE::LXC::Config->load_config($param->{vmid}); |
52389a07 DM |
52 | |
53 | delete $conf->{snapshots}; | |
52389a07 DM |
54 | |
55 | return $conf; | |
56 | }}); | |
57 | ||
58 | my $vm_config_perm_list = [ | |
59 | 'VM.Config.Disk', | |
60 | 'VM.Config.CPU', | |
61 | 'VM.Config.Memory', | |
62 | 'VM.Config.Network', | |
63 | 'VM.Config.Options', | |
64 | ]; | |
65 | ||
66 | __PACKAGE__->register_method({ | |
67 | name => 'update_vm', | |
68 | path => '', | |
69 | method => 'PUT', | |
70 | protected => 1, | |
71 | proxyto => 'node', | |
72 | description => "Set container options.", | |
73 | permissions => { | |
74 | check => ['perm', '/vms/{vmid}', $vm_config_perm_list, any => 1], | |
75 | }, | |
76 | parameters => { | |
77 | additionalProperties => 0, | |
1b4cf758 | 78 | properties => PVE::LXC::Config->json_config_properties( |
52389a07 DM |
79 | { |
80 | node => get_standard_option('pve-node'), | |
68e8f3c5 | 81 | vmid => get_standard_option('pve-vmid', { completion => \&PVE::LXC::complete_ctid }), |
52389a07 DM |
82 | delete => { |
83 | type => 'string', format => 'pve-configid-list', | |
84 | description => "A list of settings you want to delete.", | |
85 | optional => 1, | |
86 | }, | |
87 | digest => { | |
88 | type => 'string', | |
89 | description => 'Prevent changes if current configuration file has different SHA1 digest. This can be used to prevent concurrent modifications.', | |
90 | maxLength => 40, | |
91 | optional => 1, | |
92 | } | |
93 | }), | |
94 | }, | |
95 | returns => { type => 'null'}, | |
96 | code => sub { | |
97 | my ($param) = @_; | |
98 | ||
99 | my $rpcenv = PVE::RPCEnvironment::get(); | |
100 | ||
101 | my $authuser = $rpcenv->get_user(); | |
102 | ||
103 | my $node = extract_param($param, 'node'); | |
104 | ||
105 | my $vmid = extract_param($param, 'vmid'); | |
106 | ||
107 | my $digest = extract_param($param, 'digest'); | |
108 | ||
109 | die "no options specified\n" if !scalar(keys %$param); | |
110 | ||
111 | my $delete_str = extract_param($param, 'delete'); | |
112 | my @delete = PVE::Tools::split_list($delete_str); | |
113 | ||
f1ba1a4b | 114 | PVE::LXC::check_ct_modify_config_perm($rpcenv, $authuser, $vmid, undef, {}, [@delete]); |
52389a07 DM |
115 | |
116 | foreach my $opt (@delete) { | |
117 | raise_param_exc({ delete => "you can't use '-$opt' and " . | |
118 | "-delete $opt' at the same time" }) | |
119 | if defined($param->{$opt}); | |
120 | ||
1b4cf758 | 121 | if (!PVE::LXC::Config->option_exists($opt)) { |
52389a07 DM |
122 | raise_param_exc({ delete => "unknown option '$opt'" }); |
123 | } | |
124 | } | |
125 | ||
f1ba1a4b | 126 | PVE::LXC::check_ct_modify_config_perm($rpcenv, $authuser, $vmid, undef, $param, []); |
52389a07 DM |
127 | |
128 | my $storage_cfg = cfs_read_file("storage.cfg"); | |
129 | ||
130 | my $code = sub { | |
131 | ||
67afe46e FG |
132 | my $conf = PVE::LXC::Config->load_config($vmid); |
133 | PVE::LXC::Config->check_lock($conf); | |
52389a07 DM |
134 | |
135 | PVE::Tools::assert_if_modified($digest, $conf->{digest}); | |
136 | ||
137 | my $running = PVE::LXC::check_running($vmid); | |
138 | ||
1b4cf758 | 139 | PVE::LXC::Config->update_pct_config($vmid, $conf, $running, $param, \@delete); |
52389a07 | 140 | |
67afe46e | 141 | PVE::LXC::Config->write_config($vmid, $conf); |
f91f3669 | 142 | PVE::LXC::update_lxc_config($vmid, $conf); |
52389a07 DM |
143 | }; |
144 | ||
67afe46e | 145 | PVE::LXC::Config->lock_config($vmid, $code); |
52389a07 DM |
146 | |
147 | return undef; | |
148 | }}); | |
149 | ||
150 | 1; |