]>
Commit | Line | Data |
---|---|---|
f76a2828 DM |
1 | package PVE::LXC; |
2 | ||
3 | use strict; | |
4 | use warnings; | |
67afe46e | 5 | |
d14a9a1b | 6 | use POSIX qw(EINTR); |
f76a2828 | 7 | |
34fdb3d7 WB |
8 | use Socket; |
9 | ||
f76a2828 | 10 | use File::Path; |
2cfae16e WB |
11 | use File::Spec; |
12 | use Cwd qw(); | |
ab3722b3 | 13 | use Fcntl qw(O_RDONLY O_NOFOLLOW O_DIRECTORY); |
619f27b4 | 14 | use Errno qw(ELOOP EROFS); |
f76a2828 | 15 | |
f1ba1a4b | 16 | use PVE::Exception qw(raise_perm_exc); |
c65e0a6d | 17 | use PVE::Storage; |
f76a2828 DM |
18 | use PVE::SafeSyslog; |
19 | use PVE::INotify; | |
ab3722b3 | 20 | use PVE::Tools qw($IPV6RE $IPV4RE dir_glob_foreach lock_file lock_file_full O_PATH); |
68fba17b | 21 | use PVE::Network; |
52389a07 | 22 | use PVE::AccessControl; |
228a5a1d | 23 | use PVE::ProcFSTools; |
67afe46e | 24 | use PVE::LXC::Config; |
688afc63 | 25 | use Time::HiRes qw (gettimeofday); |
f76a2828 DM |
26 | |
27 | use Data::Dumper; | |
28 | ||
27916659 DM |
29 | my $nodename = PVE::INotify::nodename(); |
30 | ||
688afc63 WL |
31 | my $cpuinfo= PVE::ProcFSTools::read_cpuinfo(); |
32 | ||
f9897acd | 33 | our $COMMON_TAR_FLAGS = [ '--sparse', '--numeric-owner', '--acls', |
fc4e132e WB |
34 | '--xattrs', |
35 | '--xattrs-include=user.*', | |
4132377b WB |
36 | '--xattrs-include=security.capability', |
37 | '--warning=no-xattr-write' ]; | |
fc4e132e | 38 | |
f76a2828 DM |
39 | sub config_list { |
40 | my $vmlist = PVE::Cluster::get_vmlist(); | |
41 | my $res = {}; | |
42 | return $res if !$vmlist || !$vmlist->{ids}; | |
43 | my $ids = $vmlist->{ids}; | |
44 | ||
45 | foreach my $vmid (keys %$ids) { | |
46 | next if !$vmid; # skip CT0 | |
47 | my $d = $ids->{$vmid}; | |
48 | next if !$d->{node} || $d->{node} ne $nodename; | |
49 | next if !$d->{type} || $d->{type} ne 'lxc'; | |
50 | $res->{$vmid}->{type} = 'lxc'; | |
51 | } | |
52 | return $res; | |
53 | } | |
54 | ||
5b4657d0 DM |
55 | sub destroy_config { |
56 | my ($vmid) = @_; | |
57 | ||
67afe46e | 58 | unlink PVE::LXC::Config->config_file($vmid, $nodename); |
3cc56749 FG |
59 | } |
60 | ||
822de0c3 DM |
61 | # container status helpers |
62 | ||
63 | sub list_active_containers { | |
cbb03fea | 64 | |
822de0c3 DM |
65 | my $filename = "/proc/net/unix"; |
66 | ||
67 | # similar test is used by lcxcontainers.c: list_active_containers | |
68 | my $res = {}; | |
cbb03fea | 69 | |
822de0c3 DM |
70 | my $fh = IO::File->new ($filename, "r"); |
71 | return $res if !$fh; | |
72 | ||
73 | while (defined(my $line = <$fh>)) { | |
28df2cde | 74 | if ($line =~ m/^[a-f0-9]+:\s+\S+\s+\S+\s+\S+\s+\S+\s+\S+\s+\d+\s+(\S+)$/) { |
822de0c3 | 75 | my $path = $1; |
27916659 | 76 | if ($path =~ m!^@/var/lib/lxc/(\d+)/command$!) { |
822de0c3 DM |
77 | $res->{$1} = 1; |
78 | } | |
79 | } | |
80 | } | |
81 | ||
82 | close($fh); | |
cbb03fea | 83 | |
822de0c3 DM |
84 | return $res; |
85 | } | |
f76a2828 | 86 | |
5c752bbf DM |
87 | # warning: this is slow |
88 | sub check_running { | |
89 | my ($vmid) = @_; | |
90 | ||
91 | my $active_hash = list_active_containers(); | |
92 | ||
93 | return 1 if defined($active_hash->{$vmid}); | |
cbb03fea | 94 | |
5c752bbf DM |
95 | return undef; |
96 | } | |
97 | ||
10fc3ba5 | 98 | sub get_container_disk_usage { |
73e03cb7 | 99 | my ($vmid, $pid) = @_; |
10fc3ba5 | 100 | |
73e03cb7 | 101 | return PVE::Tools::df("/proc/$pid/root/", 1); |
10fc3ba5 DM |
102 | } |
103 | ||
688afc63 WL |
104 | my $last_proc_vmid_stat; |
105 | ||
106 | my $parse_cpuacct_stat = sub { | |
107 | my ($vmid) = @_; | |
108 | ||
109 | my $raw = read_cgroup_value('cpuacct', $vmid, 'cpuacct.stat', 1); | |
110 | ||
111 | my $stat = {}; | |
112 | ||
113 | if ($raw =~ m/^user (\d+)\nsystem (\d+)\n/) { | |
114 | ||
115 | $stat->{utime} = $1; | |
116 | $stat->{stime} = $2; | |
117 | ||
118 | } | |
119 | ||
120 | return $stat; | |
121 | }; | |
122 | ||
f76a2828 DM |
123 | sub vmstatus { |
124 | my ($opt_vmid) = @_; | |
125 | ||
126 | my $list = $opt_vmid ? { $opt_vmid => { type => 'lxc' }} : config_list(); | |
127 | ||
822de0c3 | 128 | my $active_hash = list_active_containers(); |
cbb03fea | 129 | |
688afc63 WL |
130 | my $cpucount = $cpuinfo->{cpus} || 1; |
131 | ||
132 | my $cdtime = gettimeofday; | |
133 | ||
134 | my $uptime = (PVE::ProcFSTools::read_proc_uptime(1))[0]; | |
135 | ||
f76a2828 | 136 | foreach my $vmid (keys %$list) { |
f76a2828 | 137 | my $d = $list->{$vmid}; |
10fc3ba5 | 138 | |
d5588ee3 DM |
139 | eval { $d->{pid} = find_lxc_pid($vmid) if defined($active_hash->{$vmid}); }; |
140 | warn $@ if $@; # ignore errors (consider them stopped) | |
cbb03fea | 141 | |
d5588ee3 | 142 | $d->{status} = $d->{pid} ? 'running' : 'stopped'; |
f76a2828 | 143 | |
67afe46e | 144 | my $cfspath = PVE::LXC::Config->cfs_config_path($vmid); |
238a56cb | 145 | my $conf = PVE::Cluster::cfs_read_file($cfspath) || {}; |
cbb03fea | 146 | |
27916659 | 147 | $d->{name} = $conf->{'hostname'} || "CT$vmid"; |
238a56cb | 148 | $d->{name} =~ s/[\s]//g; |
cbb03fea | 149 | |
9db5687d | 150 | $d->{cpus} = $conf->{cpulimit} || $cpucount; |
44da0641 | 151 | |
d0226204 WB |
152 | $d->{lock} = $conf->{lock} || ''; |
153 | ||
d5588ee3 DM |
154 | if ($d->{pid}) { |
155 | my $res = get_container_disk_usage($vmid, $d->{pid}); | |
27916659 DM |
156 | $d->{disk} = $res->{used}; |
157 | $d->{maxdisk} = $res->{total}; | |
158 | } else { | |
159 | $d->{disk} = 0; | |
160 | # use 4GB by default ?? | |
161 | if (my $rootfs = $conf->{rootfs}) { | |
1b4cf758 | 162 | my $rootinfo = PVE::LXC::Config->parse_ct_rootfs($rootfs); |
af02245c | 163 | $d->{maxdisk} = $rootinfo->{size} || (4*1024*1024*1024); |
27916659 DM |
164 | } else { |
165 | $d->{maxdisk} = 4*1024*1024*1024; | |
10fc3ba5 | 166 | } |
238a56cb | 167 | } |
cbb03fea | 168 | |
238a56cb DM |
169 | $d->{mem} = 0; |
170 | $d->{swap} = 0; | |
95df9a12 DM |
171 | $d->{maxmem} = ($conf->{memory}||512)*1024*1024; |
172 | $d->{maxswap} = ($conf->{swap}//0)*1024*1024; | |
e901d418 | 173 | |
238a56cb DM |
174 | $d->{uptime} = 0; |
175 | $d->{cpu} = 0; | |
e901d418 | 176 | |
238a56cb DM |
177 | $d->{netout} = 0; |
178 | $d->{netin} = 0; | |
f76a2828 | 179 | |
238a56cb DM |
180 | $d->{diskread} = 0; |
181 | $d->{diskwrite} = 0; | |
bb1ac2de | 182 | |
67afe46e | 183 | $d->{template} = PVE::LXC::Config->is_template($conf); |
f76a2828 | 184 | } |
cbb03fea | 185 | |
238a56cb DM |
186 | foreach my $vmid (keys %$list) { |
187 | my $d = $list->{$vmid}; | |
d5588ee3 DM |
188 | my $pid = $d->{pid}; |
189 | ||
190 | next if !$pid; # skip stopped CTs | |
f76a2828 | 191 | |
88a8696b TL |
192 | my $ctime = (stat("/proc/$pid"))[10]; # 10 = ctime |
193 | $d->{uptime} = time - $ctime; # the method lxcfs uses | |
22a77285 | 194 | |
b3059d35 TL |
195 | my $memory_stat = read_cgroup_list('memory', $vmid, 'memory.stat'); |
196 | my $mem_usage_in_bytes = read_cgroup_value('memory', $vmid, 'memory.usage_in_bytes'); | |
197 | ||
198 | $d->{mem} = $mem_usage_in_bytes - $memory_stat->{cache}; | |
199 | $d->{swap} = read_cgroup_value('memory', $vmid, 'memory.memsw.usage_in_bytes') - $mem_usage_in_bytes; | |
b5289322 AD |
200 | |
201 | my $blkio_bytes = read_cgroup_value('blkio', $vmid, 'blkio.throttle.io_service_bytes', 1); | |
1e647c7c | 202 | my @bytes = split(/\n/, $blkio_bytes); |
b5289322 | 203 | foreach my $byte (@bytes) { |
1e647c7c DM |
204 | if (my ($key, $value) = $byte =~ /(Read|Write)\s+(\d+)/) { |
205 | $d->{diskread} = $2 if $key eq 'Read'; | |
206 | $d->{diskwrite} = $2 if $key eq 'Write'; | |
207 | } | |
b5289322 | 208 | } |
688afc63 WL |
209 | |
210 | my $pstat = &$parse_cpuacct_stat($vmid); | |
211 | ||
212 | my $used = $pstat->{utime} + $pstat->{stime}; | |
213 | ||
214 | my $old = $last_proc_vmid_stat->{$vmid}; | |
215 | if (!$old) { | |
216 | $last_proc_vmid_stat->{$vmid} = { | |
217 | time => $cdtime, | |
218 | used => $used, | |
219 | cpu => 0, | |
220 | }; | |
221 | next; | |
222 | } | |
223 | ||
224 | my $dtime = ($cdtime - $old->{time}) * $cpucount * $cpuinfo->{user_hz}; | |
225 | ||
226 | if ($dtime > 1000) { | |
227 | my $dutime = $used - $old->{used}; | |
228 | ||
229 | $d->{cpu} = (($dutime/$dtime)* $cpucount) / $d->{cpus}; | |
230 | $last_proc_vmid_stat->{$vmid} = { | |
231 | time => $cdtime, | |
232 | used => $used, | |
233 | cpu => $d->{cpu}, | |
234 | }; | |
235 | } else { | |
236 | $d->{cpu} = $old->{cpu}; | |
237 | } | |
238a56cb | 238 | } |
cbb03fea | 239 | |
68b8f4d1 WL |
240 | my $netdev = PVE::ProcFSTools::read_proc_net_dev(); |
241 | ||
242 | foreach my $dev (keys %$netdev) { | |
243 | next if $dev !~ m/^veth([1-9]\d*)i/; | |
244 | my $vmid = $1; | |
245 | my $d = $list->{$vmid}; | |
246 | ||
247 | next if !$d; | |
248 | ||
249 | $d->{netout} += $netdev->{$dev}->{receive}; | |
250 | $d->{netin} += $netdev->{$dev}->{transmit}; | |
251 | ||
252 | } | |
253 | ||
f76a2828 DM |
254 | return $list; |
255 | } | |
256 | ||
b3059d35 TL |
257 | sub read_cgroup_list { |
258 | my ($group, $vmid, $name) = @_; | |
259 | ||
260 | my $content = read_cgroup_value($group, $vmid, $name, 1); | |
261 | ||
262 | return { split(/\s+/, $content) }; | |
263 | } | |
264 | ||
238a56cb DM |
265 | sub read_cgroup_value { |
266 | my ($group, $vmid, $name, $full) = @_; | |
267 | ||
268 | my $path = "/sys/fs/cgroup/$group/lxc/$vmid/$name"; | |
269 | ||
270 | return PVE::Tools::file_get_contents($path) if $full; | |
271 | ||
272 | return PVE::Tools::file_read_firstline($path); | |
273 | } | |
274 | ||
bf0b8c43 AD |
275 | sub write_cgroup_value { |
276 | my ($group, $vmid, $name, $value) = @_; | |
277 | ||
278 | my $path = "/sys/fs/cgroup/$group/lxc/$vmid/$name"; | |
279 | PVE::ProcFSTools::write_proc_entry($path, $value) if -e $path; | |
280 | ||
281 | } | |
282 | ||
52f1d76b DM |
283 | sub find_lxc_console_pids { |
284 | ||
285 | my $res = {}; | |
286 | ||
287 | PVE::Tools::dir_glob_foreach('/proc', '\d+', sub { | |
288 | my ($pid) = @_; | |
289 | ||
290 | my $cmdline = PVE::Tools::file_read_firstline("/proc/$pid/cmdline"); | |
291 | return if !$cmdline; | |
292 | ||
293 | my @args = split(/\0/, $cmdline); | |
294 | ||
c31ad455 | 295 | # search for lxc-console -n <vmid> |
cbb03fea | 296 | return if scalar(@args) != 3; |
52f1d76b DM |
297 | return if $args[1] ne '-n'; |
298 | return if $args[2] !~ m/^\d+$/; | |
299 | return if $args[0] !~ m|^(/usr/bin/)?lxc-console$|; | |
cbb03fea | 300 | |
52f1d76b | 301 | my $vmid = $args[2]; |
cbb03fea | 302 | |
52f1d76b DM |
303 | push @{$res->{$vmid}}, $pid; |
304 | }); | |
305 | ||
306 | return $res; | |
307 | } | |
308 | ||
bedeaaf1 AD |
309 | sub find_lxc_pid { |
310 | my ($vmid) = @_; | |
311 | ||
312 | my $pid = undef; | |
313 | my $parser = sub { | |
314 | my $line = shift; | |
8b25977f | 315 | $pid = $1 if $line =~ m/^PID:\s+(\d+)$/; |
bedeaaf1 | 316 | }; |
c39aa40a | 317 | PVE::Tools::run_command(['lxc-info', '-n', $vmid, '-p'], outfunc => $parser); |
bedeaaf1 | 318 | |
8b25977f | 319 | die "unable to get PID for CT $vmid (not running?)\n" if !$pid; |
cbb03fea | 320 | |
8b25977f | 321 | return $pid; |
bedeaaf1 AD |
322 | } |
323 | ||
cbb03fea | 324 | # Note: we cannot use Net:IP, because that only allows strict |
55fa4e09 DM |
325 | # CIDR networks |
326 | sub parse_ipv4_cidr { | |
327 | my ($cidr, $noerr) = @_; | |
328 | ||
f7a7b413 WB |
329 | if ($cidr =~ m!^($IPV4RE)(?:/(\d+))$! && ($2 > 7) && ($2 <= 32)) { |
330 | return { address => $1, netmask => $PVE::Network::ipv4_reverse_mask->[$2] }; | |
55fa4e09 | 331 | } |
cbb03fea | 332 | |
55fa4e09 | 333 | return undef if $noerr; |
cbb03fea | 334 | |
55fa4e09 DM |
335 | die "unable to parse ipv4 address/mask\n"; |
336 | } | |
93285df8 | 337 | |
e22af68f | 338 | |
27916659 | 339 | sub update_lxc_config { |
f91f3669 | 340 | my ($vmid, $conf) = @_; |
b80dd50a | 341 | |
bb1ac2de DM |
342 | my $dir = "/var/lib/lxc/$vmid"; |
343 | ||
344 | if ($conf->{template}) { | |
345 | ||
346 | unlink "$dir/config"; | |
347 | ||
348 | return; | |
349 | } | |
350 | ||
27916659 | 351 | my $raw = ''; |
b80dd50a | 352 | |
27916659 DM |
353 | die "missing 'arch' - internal error" if !$conf->{arch}; |
354 | $raw .= "lxc.arch = $conf->{arch}\n"; | |
b80dd50a | 355 | |
425b62cb WB |
356 | my $unprivileged = $conf->{unprivileged}; |
357 | my $custom_idmap = grep { $_->[0] eq 'lxc.id_map' } @{$conf->{lxc}}; | |
358 | ||
27916659 | 359 | my $ostype = $conf->{ostype} || die "missing 'ostype' - internal error"; |
ed027b58 | 360 | if ($ostype =~ /^(?:debian | ubuntu | centos | fedora | opensuse | archlinux | alpine | gentoo | unmanaged)$/x) { |
c34f7efe WB |
361 | my $inc ="/usr/share/lxc/config/$ostype.common.conf"; |
362 | $inc ="/usr/share/lxc/config/common.conf" if !-f $inc; | |
363 | $raw .= "lxc.include = $inc\n"; | |
425b62cb | 364 | if ($unprivileged || $custom_idmap) { |
c34f7efe WB |
365 | $inc = "/usr/share/lxc/config/$ostype.userns.conf"; |
366 | $inc = "/usr/share/lxc/config/userns.conf" if !-f $inc; | |
367 | $raw .= "lxc.include = $inc\n" | |
425b62cb | 368 | } |
27916659 | 369 | } else { |
9a7a910b | 370 | die "implement me (ostype $ostype)"; |
27916659 | 371 | } |
b80dd50a | 372 | |
50df544c WB |
373 | # WARNING: DO NOT REMOVE this without making sure that loop device nodes |
374 | # cannot be exposed to the container with r/w access (cgroup perms). | |
375 | # When this is enabled mounts will still remain in the monitor's namespace | |
376 | # after the container unmounted them and thus will not detach from their | |
377 | # files while the container is running! | |
c16b8890 | 378 | $raw .= "lxc.monitor.unshare = 1\n"; |
58cc92a9 | 379 | |
425b62cb WB |
380 | # Should we read them from /etc/subuid? |
381 | if ($unprivileged && !$custom_idmap) { | |
382 | $raw .= "lxc.id_map = u 0 100000 65536\n"; | |
383 | $raw .= "lxc.id_map = g 0 100000 65536\n"; | |
384 | } | |
385 | ||
d250604f | 386 | if (!PVE::LXC::Config->has_dev_console($conf)) { |
eeaea429 DM |
387 | $raw .= "lxc.console = none\n"; |
388 | $raw .= "lxc.cgroup.devices.deny = c 5:1 rwm\n"; | |
389 | } | |
4f958489 | 390 | |
1b4cf758 | 391 | my $ttycount = PVE::LXC::Config->get_tty_count($conf); |
27916659 | 392 | $raw .= "lxc.tty = $ttycount\n"; |
cbb03fea | 393 | |
c31ad455 | 394 | # some init scripts expect a linux terminal (turnkey). |
a691a5a3 DM |
395 | $raw .= "lxc.environment = TERM=linux\n"; |
396 | ||
27916659 DM |
397 | my $utsname = $conf->{hostname} || "CT$vmid"; |
398 | $raw .= "lxc.utsname = $utsname\n"; | |
cbb03fea | 399 | |
27916659 DM |
400 | my $memory = $conf->{memory} || 512; |
401 | my $swap = $conf->{swap} // 0; | |
402 | ||
403 | my $lxcmem = int($memory*1024*1024); | |
404 | $raw .= "lxc.cgroup.memory.limit_in_bytes = $lxcmem\n"; | |
a12a36e0 | 405 | |
27916659 DM |
406 | my $lxcswap = int(($memory + $swap)*1024*1024); |
407 | $raw .= "lxc.cgroup.memory.memsw.limit_in_bytes = $lxcswap\n"; | |
408 | ||
409 | if (my $cpulimit = $conf->{cpulimit}) { | |
410 | $raw .= "lxc.cgroup.cpu.cfs_period_us = 100000\n"; | |
411 | my $value = int(100000*$cpulimit); | |
412 | $raw .= "lxc.cgroup.cpu.cfs_quota_us = $value\n"; | |
a12a36e0 WL |
413 | } |
414 | ||
27916659 DM |
415 | my $shares = $conf->{cpuunits} || 1024; |
416 | $raw .= "lxc.cgroup.cpu.shares = $shares\n"; | |
417 | ||
fddaa91b DM |
418 | die "missing 'rootfs' configuration\n" |
419 | if !defined($conf->{rootfs}); | |
420 | ||
1b4cf758 | 421 | my $mountpoint = PVE::LXC::Config->parse_ct_rootfs($conf->{rootfs}); |
a3076d81 | 422 | |
c9a5774b | 423 | $raw .= "lxc.rootfs = $dir/rootfs\n"; |
27916659 DM |
424 | |
425 | my $netcount = 0; | |
e756bdd6 | 426 | foreach my $k (sort keys %$conf) { |
27916659 DM |
427 | next if $k !~ m/^net(\d+)$/; |
428 | my $ind = $1; | |
1b4cf758 | 429 | my $d = PVE::LXC::Config->parse_lxc_network($conf->{$k}); |
27916659 DM |
430 | $netcount++; |
431 | $raw .= "lxc.network.type = veth\n"; | |
18862537 | 432 | $raw .= "lxc.network.veth.pair = veth${vmid}i${ind}\n"; |
27916659 DM |
433 | $raw .= "lxc.network.hwaddr = $d->{hwaddr}\n" if defined($d->{hwaddr}); |
434 | $raw .= "lxc.network.name = $d->{name}\n" if defined($d->{name}); | |
435 | $raw .= "lxc.network.mtu = $d->{mtu}\n" if defined($d->{mtu}); | |
a12a36e0 WL |
436 | } |
437 | ||
e576f689 DM |
438 | if (my $lxcconf = $conf->{lxc}) { |
439 | foreach my $entry (@$lxcconf) { | |
440 | my ($k, $v) = @$entry; | |
441 | $netcount++ if $k eq 'lxc.network.type'; | |
442 | $raw .= "$k = $v\n"; | |
443 | } | |
444 | } | |
27916659 | 445 | |
e576f689 DM |
446 | $raw .= "lxc.network.type = empty\n" if !$netcount; |
447 | ||
27916659 DM |
448 | File::Path::mkpath("$dir/rootfs"); |
449 | ||
450 | PVE::Tools::file_set_contents("$dir/config", $raw); | |
b80dd50a DM |
451 | } |
452 | ||
117636e5 DM |
453 | # verify and cleanup nameserver list (replace \0 with ' ') |
454 | sub verify_nameserver_list { | |
455 | my ($nameserver_list) = @_; | |
456 | ||
457 | my @list = (); | |
458 | foreach my $server (PVE::Tools::split_list($nameserver_list)) { | |
459 | PVE::JSONSchema::pve_verify_ip($server); | |
460 | push @list, $server; | |
461 | } | |
462 | ||
463 | return join(' ', @list); | |
464 | } | |
465 | ||
466 | sub verify_searchdomain_list { | |
467 | my ($searchdomain_list) = @_; | |
468 | ||
469 | my @list = (); | |
470 | foreach my $server (PVE::Tools::split_list($searchdomain_list)) { | |
471 | # todo: should we add checks for valid dns domains? | |
472 | push @list, $server; | |
473 | } | |
474 | ||
475 | return join(' ', @list); | |
476 | } | |
477 | ||
aca816ad DM |
478 | sub get_console_command { |
479 | my ($vmid, $conf) = @_; | |
480 | ||
1b4cf758 | 481 | my $cmode = PVE::LXC::Config->get_cmode($conf); |
aca816ad DM |
482 | |
483 | if ($cmode eq 'console') { | |
484 | return ['lxc-console', '-n', $vmid, '-t', 0]; | |
485 | } elsif ($cmode eq 'tty') { | |
486 | return ['lxc-console', '-n', $vmid]; | |
487 | } elsif ($cmode eq 'shell') { | |
488 | return ['lxc-attach', '--clear-env', '-n', $vmid]; | |
489 | } else { | |
490 | die "internal error"; | |
491 | } | |
492 | } | |
493 | ||
c325b32f DM |
494 | sub get_primary_ips { |
495 | my ($conf) = @_; | |
496 | ||
497 | # return data from net0 | |
cbb03fea | 498 | |
27916659 | 499 | return undef if !defined($conf->{net0}); |
1b4cf758 | 500 | my $net = PVE::LXC::Config->parse_lxc_network($conf->{net0}); |
c325b32f DM |
501 | |
502 | my $ipv4 = $net->{ip}; | |
db78a181 WB |
503 | if ($ipv4) { |
504 | if ($ipv4 =~ /^(dhcp|manual)$/) { | |
505 | $ipv4 = undef | |
506 | } else { | |
507 | $ipv4 =~ s!/\d+$!!; | |
508 | } | |
509 | } | |
65e5eaa3 | 510 | my $ipv6 = $net->{ip6}; |
db78a181 | 511 | if ($ipv6) { |
5f291c7d | 512 | if ($ipv6 =~ /^(auto|dhcp|manual)$/) { |
db78a181 WB |
513 | $ipv6 = undef; |
514 | } else { | |
515 | $ipv6 =~ s!/\d+$!!; | |
516 | } | |
517 | } | |
cbb03fea | 518 | |
c325b32f DM |
519 | return ($ipv4, $ipv6); |
520 | } | |
148d1cb4 | 521 | |
b407293b WB |
522 | sub delete_mountpoint_volume { |
523 | my ($storage_cfg, $vmid, $volume) = @_; | |
524 | ||
d250604f | 525 | return if PVE::LXC::Config->classify_mountpoint($volume) ne 'volume'; |
b407293b WB |
526 | |
527 | my ($vtype, $name, $owner) = PVE::Storage::parse_volname($storage_cfg, $volume); | |
528 | PVE::Storage::vdisk_free($storage_cfg, $volume) if $vmid == $owner; | |
529 | } | |
ef241384 | 530 | |
27916659 | 531 | sub destroy_lxc_container { |
bccaa371 | 532 | my ($storage_cfg, $vmid, $conf, $replacement_conf) = @_; |
148d1cb4 | 533 | |
d250604f | 534 | PVE::LXC::Config->foreach_mountpoint($conf, sub { |
db8989e1 | 535 | my ($ms, $mountpoint) = @_; |
b407293b | 536 | delete_mountpoint_volume($storage_cfg, $vmid, $mountpoint->{volume}); |
db8989e1 WB |
537 | }); |
538 | ||
27916659 DM |
539 | rmdir "/var/lib/lxc/$vmid/rootfs"; |
540 | unlink "/var/lib/lxc/$vmid/config"; | |
541 | rmdir "/var/lib/lxc/$vmid"; | |
bccaa371 FG |
542 | if (defined $replacement_conf) { |
543 | PVE::LXC::Config->write_config($vmid, $replacement_conf); | |
544 | } else { | |
545 | destroy_config($vmid); | |
546 | } | |
27916659 DM |
547 | |
548 | #my $cmd = ['lxc-destroy', '-n', $vmid ]; | |
549 | #PVE::Tools::run_command($cmd); | |
148d1cb4 | 550 | } |
68fba17b | 551 | |
ef241384 | 552 | sub vm_stop_cleanup { |
5fa890f0 | 553 | my ($storage_cfg, $vmid, $conf, $keepActive) = @_; |
ef241384 DM |
554 | |
555 | eval { | |
556 | if (!$keepActive) { | |
bf9d912c | 557 | |
d250604f | 558 | my $vollist = PVE::LXC::Config->get_vm_volumes($conf); |
a8b6b8a7 | 559 | PVE::Storage::deactivate_volumes($storage_cfg, $vollist); |
ef241384 DM |
560 | } |
561 | }; | |
562 | warn $@ if $@; # avoid errors - just warn | |
563 | } | |
564 | ||
93cdbbfb AD |
565 | my $safe_num_ne = sub { |
566 | my ($a, $b) = @_; | |
567 | ||
568 | return 0 if !defined($a) && !defined($b); | |
569 | return 1 if !defined($a); | |
570 | return 1 if !defined($b); | |
571 | ||
572 | return $a != $b; | |
573 | }; | |
574 | ||
575 | my $safe_string_ne = sub { | |
576 | my ($a, $b) = @_; | |
577 | ||
578 | return 0 if !defined($a) && !defined($b); | |
579 | return 1 if !defined($a); | |
580 | return 1 if !defined($b); | |
581 | ||
582 | return $a ne $b; | |
583 | }; | |
584 | ||
585 | sub update_net { | |
bedeaaf1 | 586 | my ($vmid, $conf, $opt, $newnet, $netid, $rootdir) = @_; |
93cdbbfb | 587 | |
18862537 WB |
588 | if ($newnet->{type} ne 'veth') { |
589 | # for when there are physical interfaces | |
590 | die "cannot update interface of type $newnet->{type}"; | |
591 | } | |
592 | ||
593 | my $veth = "veth${vmid}i${netid}"; | |
93cdbbfb AD |
594 | my $eth = $newnet->{name}; |
595 | ||
18862537 | 596 | if (my $oldnetcfg = $conf->{$opt}) { |
1b4cf758 | 597 | my $oldnet = PVE::LXC::Config->parse_lxc_network($oldnetcfg); |
18862537 WB |
598 | |
599 | if (&$safe_string_ne($oldnet->{hwaddr}, $newnet->{hwaddr}) || | |
600 | &$safe_string_ne($oldnet->{name}, $newnet->{name})) { | |
93cdbbfb | 601 | |
18862537 | 602 | PVE::Network::veth_delete($veth); |
bedeaaf1 | 603 | delete $conf->{$opt}; |
67afe46e | 604 | PVE::LXC::Config->write_config($vmid, $conf); |
93cdbbfb | 605 | |
18862537 | 606 | hotplug_net($vmid, $conf, $opt, $newnet, $netid); |
bedeaaf1 | 607 | |
380962c7 WB |
608 | } else { |
609 | if (&$safe_string_ne($oldnet->{bridge}, $newnet->{bridge}) || | |
610 | &$safe_num_ne($oldnet->{tag}, $newnet->{tag}) || | |
611 | &$safe_num_ne($oldnet->{firewall}, $newnet->{firewall})) { | |
bedeaaf1 | 612 | |
18862537 | 613 | if ($oldnet->{bridge}) { |
bedeaaf1 | 614 | PVE::Network::tap_unplug($veth); |
18862537 WB |
615 | foreach (qw(bridge tag firewall)) { |
616 | delete $oldnet->{$_}; | |
617 | } | |
1b4cf758 | 618 | $conf->{$opt} = PVE::LXC::Config->print_lxc_network($oldnet); |
67afe46e | 619 | PVE::LXC::Config->write_config($vmid, $conf); |
bedeaaf1 | 620 | } |
93cdbbfb | 621 | |
380962c7 WB |
622 | PVE::Network::tap_plug($veth, $newnet->{bridge}, $newnet->{tag}, $newnet->{firewall}, $newnet->{trunks}, $newnet->{rate}); |
623 | # This includes the rate: | |
624 | foreach (qw(bridge tag firewall rate)) { | |
18862537 WB |
625 | $oldnet->{$_} = $newnet->{$_} if $newnet->{$_}; |
626 | } | |
380962c7 WB |
627 | } elsif (&$safe_string_ne($oldnet->{rate}, $newnet->{rate})) { |
628 | # Rate can be applied on its own but any change above needs to | |
629 | # include the rate in tap_plug since OVS resets everything. | |
630 | PVE::Network::tap_rate_limit($veth, $newnet->{rate}); | |
631 | $oldnet->{rate} = $newnet->{rate} | |
632 | } | |
633 | $conf->{$opt} = PVE::LXC::Config->print_lxc_network($oldnet); | |
634 | PVE::LXC::Config->write_config($vmid, $conf); | |
93cdbbfb AD |
635 | } |
636 | } else { | |
18862537 | 637 | hotplug_net($vmid, $conf, $opt, $newnet, $netid); |
93cdbbfb AD |
638 | } |
639 | ||
bedeaaf1 | 640 | update_ipconfig($vmid, $conf, $opt, $eth, $newnet, $rootdir); |
93cdbbfb AD |
641 | } |
642 | ||
643 | sub hotplug_net { | |
18862537 | 644 | my ($vmid, $conf, $opt, $newnet, $netid) = @_; |
93cdbbfb | 645 | |
18862537 | 646 | my $veth = "veth${vmid}i${netid}"; |
cbb03fea | 647 | my $vethpeer = $veth . "p"; |
93cdbbfb AD |
648 | my $eth = $newnet->{name}; |
649 | ||
650 | PVE::Network::veth_create($veth, $vethpeer, $newnet->{bridge}, $newnet->{hwaddr}); | |
380962c7 | 651 | PVE::Network::tap_plug($veth, $newnet->{bridge}, $newnet->{tag}, $newnet->{firewall}, $newnet->{trunks}, $newnet->{rate}); |
93cdbbfb | 652 | |
cbb03fea | 653 | # attach peer in container |
93cdbbfb AD |
654 | my $cmd = ['lxc-device', '-n', $vmid, 'add', $vethpeer, "$eth" ]; |
655 | PVE::Tools::run_command($cmd); | |
656 | ||
cbb03fea | 657 | # link up peer in container |
93cdbbfb AD |
658 | $cmd = ['lxc-attach', '-n', $vmid, '-s', 'NETWORK', '--', '/sbin/ip', 'link', 'set', $eth ,'up' ]; |
659 | PVE::Tools::run_command($cmd); | |
bedeaaf1 | 660 | |
18862537 WB |
661 | my $done = { type => 'veth' }; |
662 | foreach (qw(bridge tag firewall hwaddr name)) { | |
663 | $done->{$_} = $newnet->{$_} if $newnet->{$_}; | |
664 | } | |
1b4cf758 | 665 | $conf->{$opt} = PVE::LXC::Config->print_lxc_network($done); |
bedeaaf1 | 666 | |
67afe46e | 667 | PVE::LXC::Config->write_config($vmid, $conf); |
93cdbbfb AD |
668 | } |
669 | ||
68a05bb3 | 670 | sub update_ipconfig { |
bedeaaf1 AD |
671 | my ($vmid, $conf, $opt, $eth, $newnet, $rootdir) = @_; |
672 | ||
f2104b80 | 673 | my $lxc_setup = PVE::LXC::Setup->new($conf, $rootdir); |
bedeaaf1 | 674 | |
1b4cf758 | 675 | my $optdata = PVE::LXC::Config->parse_lxc_network($conf->{$opt}); |
84e0c123 WB |
676 | my $deleted = []; |
677 | my $added = []; | |
8d723477 WB |
678 | my $nscmd = sub { |
679 | my $cmdargs = shift; | |
680 | PVE::Tools::run_command(['lxc-attach', '-n', $vmid, '-s', 'NETWORK', '--', @_], %$cmdargs); | |
84e0c123 | 681 | }; |
8d723477 | 682 | my $ipcmd = sub { &$nscmd({}, '/sbin/ip', @_) }; |
2bfd1615 | 683 | |
84e0c123 | 684 | my $change_ip_config = sub { |
f39002a6 DM |
685 | my ($ipversion) = @_; |
686 | ||
687 | my $family_opt = "-$ipversion"; | |
688 | my $suffix = $ipversion == 4 ? '' : $ipversion; | |
84e0c123 WB |
689 | my $gw= "gw$suffix"; |
690 | my $ip= "ip$suffix"; | |
bedeaaf1 | 691 | |
6178b0dd WB |
692 | my $newip = $newnet->{$ip}; |
693 | my $newgw = $newnet->{$gw}; | |
694 | my $oldip = $optdata->{$ip}; | |
695 | ||
696 | my $change_ip = &$safe_string_ne($oldip, $newip); | |
697 | my $change_gw = &$safe_string_ne($optdata->{$gw}, $newgw); | |
bedeaaf1 | 698 | |
84e0c123 | 699 | return if !$change_ip && !$change_gw; |
68a05bb3 | 700 | |
84e0c123 | 701 | # step 1: add new IP, if this fails we cancel |
292aff54 WB |
702 | my $is_real_ip = ($newip && $newip !~ /^(?:auto|dhcp|manual)$/); |
703 | if ($change_ip && $is_real_ip) { | |
8d723477 | 704 | eval { &$ipcmd($family_opt, 'addr', 'add', $newip, 'dev', $eth); }; |
84e0c123 WB |
705 | if (my $err = $@) { |
706 | warn $err; | |
707 | return; | |
708 | } | |
bedeaaf1 | 709 | } |
bedeaaf1 | 710 | |
84e0c123 WB |
711 | # step 2: replace gateway |
712 | # If this fails we delete the added IP and cancel. | |
713 | # If it succeeds we save the config and delete the old IP, ignoring | |
714 | # errors. The config is then saved. | |
715 | # Note: 'ip route replace' can add | |
716 | if ($change_gw) { | |
6178b0dd | 717 | if ($newgw) { |
292aff54 WB |
718 | eval { |
719 | if ($is_real_ip && !PVE::Network::is_ip_in_cidr($newgw, $newip, $ipversion)) { | |
720 | &$ipcmd($family_opt, 'route', 'add', $newgw, 'dev', $eth); | |
721 | } | |
722 | &$ipcmd($family_opt, 'route', 'replace', 'default', 'via', $newgw); | |
723 | }; | |
84e0c123 WB |
724 | if (my $err = $@) { |
725 | warn $err; | |
726 | # the route was not replaced, the old IP is still available | |
727 | # rollback (delete new IP) and cancel | |
728 | if ($change_ip) { | |
8d723477 | 729 | eval { &$ipcmd($family_opt, 'addr', 'del', $newip, 'dev', $eth); }; |
84e0c123 WB |
730 | warn $@ if $@; # no need to die here |
731 | } | |
732 | return; | |
733 | } | |
734 | } else { | |
8d723477 | 735 | eval { &$ipcmd($family_opt, 'route', 'del', 'default'); }; |
84e0c123 WB |
736 | # if the route was not deleted, the guest might have deleted it manually |
737 | # warn and continue | |
738 | warn $@ if $@; | |
739 | } | |
2bfd1615 | 740 | } |
2bfd1615 | 741 | |
6178b0dd | 742 | # from this point on we save the configuration |
84e0c123 | 743 | # step 3: delete old IP ignoring errors |
6178b0dd | 744 | if ($change_ip && $oldip && $oldip !~ /^(?:auto|dhcp)$/) { |
8d723477 WB |
745 | # We need to enable promote_secondaries, otherwise our newly added |
746 | # address will be removed along with the old one. | |
747 | my $promote = 0; | |
748 | eval { | |
749 | if ($ipversion == 4) { | |
750 | &$nscmd({ outfunc => sub { $promote = int(shift) } }, | |
751 | 'cat', "/proc/sys/net/ipv4/conf/$eth/promote_secondaries"); | |
752 | &$nscmd({}, 'sysctl', "net.ipv4.conf.$eth.promote_secondaries=1"); | |
753 | } | |
754 | &$ipcmd($family_opt, 'addr', 'del', $oldip, 'dev', $eth); | |
755 | }; | |
84e0c123 | 756 | warn $@ if $@; # no need to die here |
8d723477 WB |
757 | |
758 | if ($ipversion == 4) { | |
759 | &$nscmd({}, 'sysctl', "net.ipv4.conf.$eth.promote_secondaries=$promote"); | |
760 | } | |
bedeaaf1 AD |
761 | } |
762 | ||
84e0c123 WB |
763 | foreach my $property ($ip, $gw) { |
764 | if ($newnet->{$property}) { | |
765 | $optdata->{$property} = $newnet->{$property}; | |
766 | } else { | |
767 | delete $optdata->{$property}; | |
768 | } | |
bedeaaf1 | 769 | } |
1b4cf758 | 770 | $conf->{$opt} = PVE::LXC::Config->print_lxc_network($optdata); |
67afe46e | 771 | PVE::LXC::Config->write_config($vmid, $conf); |
84e0c123 WB |
772 | $lxc_setup->setup_network($conf); |
773 | }; | |
bedeaaf1 | 774 | |
f39002a6 DM |
775 | &$change_ip_config(4); |
776 | &$change_ip_config(6); | |
489e960d WL |
777 | |
778 | } | |
779 | ||
34fdb3d7 WB |
780 | my $enter_namespace = sub { |
781 | my ($vmid, $pid, $which, $type) = @_; | |
782 | sysopen my $fd, "/proc/$pid/ns/$which", O_RDONLY | |
783 | or die "failed to open $which namespace of container $vmid: $!\n"; | |
784 | PVE::Tools::setns(fileno($fd), $type) | |
785 | or die "failed to enter $which namespace of container $vmid: $!\n"; | |
786 | close $fd; | |
787 | }; | |
788 | ||
789 | my $do_syncfs = sub { | |
790 | my ($vmid, $pid, $socket) = @_; | |
791 | ||
792 | &$enter_namespace($vmid, $pid, 'mnt', PVE::Tools::CLONE_NEWNS); | |
793 | ||
794 | # Tell the parent process to start reading our /proc/mounts | |
795 | print {$socket} "go\n"; | |
796 | $socket->flush(); | |
797 | ||
798 | # Receive /proc/self/mounts | |
799 | my $mountdata = do { local $/ = undef; <$socket> }; | |
800 | close $socket; | |
801 | ||
802 | # Now sync all mountpoints... | |
803 | my $mounts = PVE::ProcFSTools::parse_mounts($mountdata); | |
804 | foreach my $mp (@$mounts) { | |
805 | my ($what, $dir, $fs) = @$mp; | |
806 | next if $fs eq 'fuse.lxcfs'; | |
807 | eval { PVE::Tools::sync_mountpoint($dir); }; | |
808 | warn $@ if $@; | |
809 | } | |
810 | }; | |
811 | ||
812 | sub sync_container_namespace { | |
813 | my ($vmid) = @_; | |
814 | my $pid = find_lxc_pid($vmid); | |
815 | ||
816 | # SOCK_DGRAM is nicer for barriers but cannot be slurped | |
817 | socketpair my $pfd, my $cfd, AF_UNIX, SOCK_STREAM, PF_UNSPEC | |
818 | or die "failed to create socketpair: $!\n"; | |
819 | ||
820 | my $child = fork(); | |
821 | die "fork failed: $!\n" if !defined($child); | |
822 | ||
823 | if (!$child) { | |
824 | eval { | |
825 | close $pfd; | |
826 | &$do_syncfs($vmid, $pid, $cfd); | |
827 | }; | |
828 | if (my $err = $@) { | |
829 | warn $err; | |
830 | POSIX::_exit(1); | |
831 | } | |
832 | POSIX::_exit(0); | |
833 | } | |
834 | close $cfd; | |
835 | my $go = <$pfd>; | |
836 | die "failed to enter container namespace\n" if $go ne "go\n"; | |
837 | ||
838 | open my $mounts, '<', "/proc/$child/mounts" | |
839 | or die "failed to open container's /proc/mounts: $!\n"; | |
840 | my $mountdata = do { local $/ = undef; <$mounts> }; | |
841 | close $mounts; | |
842 | print {$pfd} $mountdata; | |
843 | close $pfd; | |
844 | ||
845 | while (waitpid($child, 0) != $child) {} | |
846 | die "failed to sync container namespace\n" if $? != 0; | |
847 | } | |
848 | ||
bb1ac2de DM |
849 | sub template_create { |
850 | my ($vmid, $conf) = @_; | |
851 | ||
852 | my $storecfg = PVE::Storage::config(); | |
853 | ||
1b4cf758 | 854 | my $rootinfo = PVE::LXC::Config->parse_ct_rootfs($conf->{rootfs}); |
bb1ac2de DM |
855 | my $volid = $rootinfo->{volume}; |
856 | ||
857 | die "Template feature is not available for '$volid'\n" | |
858 | if !PVE::Storage::volume_has_feature($storecfg, 'template', $volid); | |
859 | ||
860 | PVE::Storage::activate_volumes($storecfg, [$volid]); | |
861 | ||
862 | my $template_volid = PVE::Storage::vdisk_create_base($storecfg, $volid); | |
863 | $rootinfo->{volume} = $template_volid; | |
1b4cf758 | 864 | $conf->{rootfs} = PVE::LXC::Config->print_ct_mountpoint($rootinfo, 1); |
bb1ac2de | 865 | |
67afe46e | 866 | PVE::LXC::Config->write_config($vmid, $conf); |
bb1ac2de DM |
867 | } |
868 | ||
52389a07 | 869 | sub check_ct_modify_config_perm { |
f1ba1a4b | 870 | my ($rpcenv, $authuser, $vmid, $pool, $newconf, $delete) = @_; |
52389a07 | 871 | |
c81f19d1 | 872 | return 1 if $authuser eq 'root@pam'; |
52389a07 | 873 | |
f1ba1a4b WB |
874 | my $check = sub { |
875 | my ($opt, $delete) = @_; | |
52389a07 DM |
876 | if ($opt eq 'cpus' || $opt eq 'cpuunits' || $opt eq 'cpulimit') { |
877 | $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.CPU']); | |
e59a61ed | 878 | } elsif ($opt eq 'rootfs' || $opt =~ /^mp\d+$/) { |
52389a07 | 879 | $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.Disk']); |
f1ba1a4b | 880 | return if $delete; |
1b4cf758 FG |
881 | my $data = $opt eq 'rootfs' ? PVE::LXC::Config->parse_ct_rootfs($newconf->{$opt}) |
882 | : PVE::LXC::Config->parse_ct_mountpoint($newconf->{$opt}); | |
f1ba1a4b | 883 | raise_perm_exc("mountpoint type $data->{type}") if $data->{type} ne 'volume'; |
52389a07 DM |
884 | } elsif ($opt eq 'memory' || $opt eq 'swap') { |
885 | $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.Memory']); | |
886 | } elsif ($opt =~ m/^net\d+$/ || $opt eq 'nameserver' || | |
887 | $opt eq 'searchdomain' || $opt eq 'hostname') { | |
888 | $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.Network']); | |
889 | } else { | |
890 | $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.Options']); | |
891 | } | |
f1ba1a4b WB |
892 | }; |
893 | ||
894 | foreach my $opt (keys %$newconf) { | |
895 | &$check($opt, 0); | |
896 | } | |
897 | foreach my $opt (@$delete) { | |
898 | &$check($opt, 1); | |
52389a07 DM |
899 | } |
900 | ||
901 | return 1; | |
902 | } | |
903 | ||
9622e848 | 904 | sub umount_all { |
da629848 | 905 | my ($vmid, $storage_cfg, $conf, $noerr) = @_; |
9622e848 DM |
906 | |
907 | my $rootdir = "/var/lib/lxc/$vmid/rootfs"; | |
d250604f | 908 | my $volid_list = PVE::LXC::Config->get_vm_volumes($conf); |
9622e848 | 909 | |
d250604f | 910 | PVE::LXC::Config->foreach_mountpoint_reverse($conf, sub { |
9622e848 DM |
911 | my ($ms, $mountpoint) = @_; |
912 | ||
913 | my $volid = $mountpoint->{volume}; | |
914 | my $mount = $mountpoint->{mp}; | |
915 | ||
916 | return if !$volid || !$mount; | |
917 | ||
d18f96b4 | 918 | my $mount_path = "$rootdir/$mount"; |
f845a93d | 919 | $mount_path =~ s!/+!/!g; |
9622e848 | 920 | |
228a5a1d WL |
921 | return if !PVE::ProcFSTools::is_mounted($mount_path); |
922 | ||
9622e848 | 923 | eval { |
d18f96b4 | 924 | PVE::Tools::run_command(['umount', '-d', $mount_path]); |
9622e848 DM |
925 | }; |
926 | if (my $err = $@) { | |
927 | if ($noerr) { | |
928 | warn $err; | |
929 | } else { | |
930 | die $err; | |
931 | } | |
932 | } | |
933 | }); | |
9622e848 DM |
934 | } |
935 | ||
936 | sub mount_all { | |
25321b68 | 937 | my ($vmid, $storage_cfg, $conf, $ignore_ro) = @_; |
9622e848 DM |
938 | |
939 | my $rootdir = "/var/lib/lxc/$vmid/rootfs"; | |
1adc7e53 | 940 | File::Path::make_path($rootdir); |
9622e848 | 941 | |
d250604f | 942 | my $volid_list = PVE::LXC::Config->get_vm_volumes($conf); |
9622e848 DM |
943 | PVE::Storage::activate_volumes($storage_cfg, $volid_list); |
944 | ||
945 | eval { | |
d250604f | 946 | PVE::LXC::Config->foreach_mountpoint($conf, sub { |
9622e848 DM |
947 | my ($ms, $mountpoint) = @_; |
948 | ||
25321b68 FG |
949 | $mountpoint->{ro} = 0 if $ignore_ro; |
950 | ||
da629848 | 951 | mountpoint_mount($mountpoint, $rootdir, $storage_cfg); |
9622e848 DM |
952 | }); |
953 | }; | |
954 | if (my $err = $@) { | |
e2007ac2 | 955 | warn "mounting container failed\n"; |
9622e848 | 956 | umount_all($vmid, $storage_cfg, $conf, 1); |
e2007ac2 | 957 | die $err; |
9622e848 DM |
958 | } |
959 | ||
da629848 | 960 | return $rootdir; |
9622e848 DM |
961 | } |
962 | ||
963 | ||
b15c75fc | 964 | sub mountpoint_mount_path { |
da629848 | 965 | my ($mountpoint, $storage_cfg, $snapname) = @_; |
b15c75fc | 966 | |
da629848 | 967 | return mountpoint_mount($mountpoint, undef, $storage_cfg, $snapname); |
b15c75fc | 968 | } |
cc6b0307 | 969 | |
21f292ff WB |
970 | sub query_loopdev { |
971 | my ($path) = @_; | |
972 | my $found; | |
973 | my $parser = sub { | |
974 | my $line = shift; | |
975 | if ($line =~ m@^(/dev/loop\d+):@) { | |
976 | $found = $1; | |
977 | } | |
978 | }; | |
979 | my $cmd = ['losetup', '--associated', $path]; | |
980 | PVE::Tools::run_command($cmd, outfunc => $parser); | |
981 | return $found; | |
982 | } | |
983 | ||
50df544c WB |
984 | # Run a function with a file attached to a loop device. |
985 | # The loop device is always detached afterwards (or set to autoclear). | |
986 | # Returns the loop device. | |
987 | sub run_with_loopdev { | |
988 | my ($func, $file) = @_; | |
54d11e5c WB |
989 | my $device = query_loopdev($file); |
990 | # Try to reuse an existing device | |
991 | if ($device) { | |
992 | # We assume that whoever setup the loop device is responsible for | |
993 | # detaching it. | |
994 | &$func($device); | |
995 | return $device; | |
996 | } | |
997 | ||
50df544c WB |
998 | my $parser = sub { |
999 | my $line = shift; | |
1000 | if ($line =~ m@^(/dev/loop\d+)$@) { | |
1001 | $device = $1; | |
1002 | } | |
1003 | }; | |
1004 | PVE::Tools::run_command(['losetup', '--show', '-f', $file], outfunc => $parser); | |
1005 | die "failed to setup loop device for $file\n" if !$device; | |
1006 | eval { &$func($device); }; | |
1007 | my $err = $@; | |
1008 | PVE::Tools::run_command(['losetup', '-d', $device]); | |
1009 | die $err if $err; | |
1010 | return $device; | |
1011 | } | |
1012 | ||
ab3722b3 WB |
1013 | # In scalar mode: returns a file handle to the deepest directory node. |
1014 | # In list context: returns a list of: | |
1015 | # * the deepest directory node | |
1016 | # * the 2nd deepest directory (parent of the above) | |
1017 | # * directory name of the last directory | |
1018 | # So that the path $2/$3 should lead to $1 afterwards. | |
1019 | sub walk_tree_nofollow($$$) { | |
1020 | my ($start, $subdir, $mkdir) = @_; | |
1021 | ||
1022 | # splitdir() returns '' for empty components including the leading / | |
1023 | my @comps = grep { length($_)>0 } File::Spec->splitdir($subdir); | |
1024 | ||
1025 | sysopen(my $fd, $start, O_PATH | O_DIRECTORY) | |
1026 | or die "failed to open start directory $start: $!\n"; | |
1027 | ||
1028 | my $dir = $start; | |
1029 | my $last_component = undef; | |
1030 | my $second = $fd; | |
1031 | foreach my $component (@comps) { | |
1032 | $dir .= "/$component"; | |
1033 | my $next = PVE::Tools::openat(fileno($fd), $component, O_NOFOLLOW | O_DIRECTORY); | |
1034 | ||
1035 | if (!$next) { | |
1036 | # failed, check for symlinks and try to create the path | |
1037 | die "symlink encountered at: $dir\n" if $! == ELOOP; | |
1038 | die "cannot open directory $dir: $!\n" if !$mkdir; | |
1039 | ||
1040 | # We don't check for errors on mkdirat() here and just try to | |
1041 | # openat() again, since at least one error (EEXIST) is an | |
1042 | # expected possibility if multiple containers start | |
1043 | # simultaneously. If someone else injects a symlink now then | |
1044 | # the subsequent openat() will fail due to O_NOFOLLOW anyway. | |
1045 | PVE::Tools::mkdirat(fileno($fd), $component, 0755); | |
1046 | ||
1047 | $next = PVE::Tools::openat(fileno($fd), $component, O_NOFOLLOW | O_DIRECTORY); | |
1048 | die "failed to create path: $dir: $!\n" if !$next; | |
1049 | } | |
1050 | ||
1051 | close $second if defined($last_component); | |
1052 | $last_component = $component; | |
1053 | $second = $fd; | |
1054 | $fd = $next; | |
1055 | } | |
1056 | ||
1057 | return ($fd, defined($last_component) && $second, $last_component) if wantarray; | |
1058 | close $second if defined($last_component); | |
1059 | return $fd; | |
1060 | } | |
1061 | ||
619f27b4 WB |
1062 | # To guard against symlink attack races against other currently running |
1063 | # containers with shared recursive bind mount hierarchies we prepare a | |
1064 | # directory handle for the directory we're mounting over to verify the | |
1065 | # mountpoint afterwards. | |
1066 | sub __bindmount_prepare { | |
1067 | my ($hostroot, $dir) = @_; | |
1068 | my $srcdh = walk_tree_nofollow($hostroot, $dir, 0); | |
1069 | return $srcdh; | |
1070 | } | |
ab3722b3 | 1071 | |
619f27b4 WB |
1072 | # Assuming we mount to rootfs/a/b/c, verify with the directory handle to 'b' |
1073 | # ($parentfd) that 'b/c' (openat($parentfd, 'c')) really leads to the directory | |
1074 | # we intended to bind mount. | |
1075 | sub __bindmount_verify { | |
1076 | my ($srcdh, $parentfd, $last_dir, $ro) = @_; | |
ab3722b3 WB |
1077 | my $destdh; |
1078 | if ($parentfd) { | |
1079 | # Open the mount point path coming from the parent directory since the | |
1080 | # filehandle we would have gotten as first result of walk_tree_nofollow | |
1081 | # earlier is still a handle to the underlying directory instead of the | |
1082 | # mounted path. | |
619f27b4 WB |
1083 | $destdh = PVE::Tools::openat(fileno($parentfd), $last_dir, PVE::Tools::O_PATH | O_NOFOLLOW | O_DIRECTORY); |
1084 | die "failed to open mount point: $!\n" if !$destdh; | |
1085 | if ($ro) { | |
1086 | my $dot = '.'; | |
1087 | # 269: faccessat() | |
1088 | # no separate function because 99% of the time it's the wrong thing to use. | |
1089 | if (syscall(269, fileno($destdh), $dot, &POSIX::W_OK, 0) != -1) { | |
1090 | die "failed to mark bind mount read only\n"; | |
1091 | } | |
1092 | die "read-only check failed: $!\n" if $! != EROFS; | |
1093 | } | |
ab3722b3 WB |
1094 | } else { |
1095 | # For the rootfs we don't have a parentfd so we open the path directly. | |
1096 | # Note that this means bindmounting any prefix of the host's | |
1097 | # /var/lib/lxc/$vmid path into another container is considered a grave | |
1098 | # security error. | |
1099 | sysopen $destdh, $last_dir, O_PATH | O_DIRECTORY; | |
619f27b4 | 1100 | die "failed to open mount point: $!\n" if !$destdh; |
ab3722b3 | 1101 | } |
ab3722b3 WB |
1102 | |
1103 | my ($srcdev, $srcinode) = stat($srcdh); | |
1104 | my ($dstdev, $dstinode) = stat($destdh); | |
1105 | close $srcdh; | |
1106 | close $destdh; | |
1107 | ||
619f27b4 WB |
1108 | return ($srcdev == $dstdev && $srcinode == $dstinode); |
1109 | } | |
1110 | ||
1111 | # Perform the actual bind mounting: | |
1112 | sub __bindmount_do { | |
1113 | my ($dir, $dest, $ro, @extra_opts) = @_; | |
1114 | PVE::Tools::run_command(['mount', '-o', 'bind', @extra_opts, $dir, $dest]); | |
1115 | if ($ro) { | |
1116 | eval { PVE::Tools::run_command(['mount', '-o', 'bind,remount,ro', $dest]); }; | |
1117 | if (my $err = $@) { | |
1118 | warn "bindmount error\n"; | |
1119 | # don't leave writable bind-mounts behind... | |
1120 | PVE::Tools::run_command(['umount', $dest]); | |
1121 | die $err; | |
1122 | } | |
1123 | } | |
1124 | } | |
1125 | ||
1126 | sub bindmount { | |
1127 | my ($dir, $parentfd, $last_dir, $dest, $ro, @extra_opts) = @_; | |
1128 | ||
1129 | my $srcdh = __bindmount_prepare('/', $dir); | |
1130 | ||
1131 | __bindmount_do($dir, $dest, $ro, @extra_opts); | |
1132 | ||
1133 | if (!__bindmount_verify($srcdh, $parentfd, $last_dir, $ro)) { | |
ab3722b3 WB |
1134 | PVE::Tools::run_command(['umount', $dest]); |
1135 | die "detected mount path change at: $dir\n"; | |
1136 | } | |
c2744c97 WB |
1137 | } |
1138 | ||
619f27b4 WB |
1139 | # Cleanup $rootdir a bit (double and trailing slashes), build the mount path |
1140 | # from $rootdir and $mount and walk the path from $rootdir to the final | |
1141 | # directory to check for symlinks. | |
1142 | sub __mount_prepare_rootdir { | |
1143 | my ($rootdir, $mount) = @_; | |
1144 | $rootdir =~ s!/+!/!g; | |
1145 | $rootdir =~ s!/+$!!; | |
1146 | my $mount_path = "$rootdir/$mount"; | |
1147 | my ($mpfd, $parentfd, $last_dir) = walk_tree_nofollow($rootdir, $mount, 1); | |
1148 | return ($rootdir, $mount_path, $mpfd, $parentfd, $last_dir); | |
1149 | } | |
1150 | ||
b15c75fc | 1151 | # use $rootdir = undef to just return the corresponding mount path |
cc6b0307 | 1152 | sub mountpoint_mount { |
da629848 | 1153 | my ($mountpoint, $rootdir, $storage_cfg, $snapname) = @_; |
cc6b0307 AD |
1154 | |
1155 | my $volid = $mountpoint->{volume}; | |
1156 | my $mount = $mountpoint->{mp}; | |
7c921c80 | 1157 | my $type = $mountpoint->{type}; |
50df544c WB |
1158 | my $quota = !$snapname && !$mountpoint->{ro} && $mountpoint->{quota}; |
1159 | my $mounted_dev; | |
b15c75fc | 1160 | |
cc6b0307 AD |
1161 | return if !$volid || !$mount; |
1162 | ||
ab3722b3 WB |
1163 | $mount =~ s!/+!/!g; |
1164 | ||
b15c75fc | 1165 | my $mount_path; |
ab3722b3 | 1166 | my ($mpfd, $parentfd, $last_dir); |
b15c75fc DM |
1167 | |
1168 | if (defined($rootdir)) { | |
619f27b4 WB |
1169 | ($rootdir, $mount_path, $mpfd, $parentfd, $last_dir) = |
1170 | __mount_prepare_rootdir($rootdir, $mount); | |
116ce06f | 1171 | } |
b15c75fc DM |
1172 | |
1173 | my ($storage, $volname) = PVE::Storage::parse_volume_id($volid, 1); | |
cc6b0307 | 1174 | |
b15c75fc | 1175 | die "unknown snapshot path for '$volid'" if !$storage && defined($snapname); |
cc6b0307 | 1176 | |
471dd315 | 1177 | my $optstring = ''; |
719129ea WB |
1178 | my $acl = $mountpoint->{acl}; |
1179 | if (defined($acl)) { | |
1180 | $optstring .= ($acl ? 'acl' : 'noacl'); | |
471dd315 | 1181 | } |
c2744c97 | 1182 | my $readonly = $mountpoint->{ro}; |
471dd315 | 1183 | |
c019c56a | 1184 | my @extra_opts = ('-o', $optstring) if $optstring; |
471dd315 | 1185 | |
b15c75fc DM |
1186 | if ($storage) { |
1187 | ||
1188 | my $scfg = PVE::Storage::storage_config($storage_cfg, $storage); | |
7c138f58 WB |
1189 | |
1190 | # early sanity checks: | |
1191 | # we otherwise call realpath on the rbd url | |
1192 | die "containers on rbd storage without krbd are not supported\n" | |
1193 | if $scfg->{type} eq 'rbd' && !$scfg->{krbd}; | |
1194 | ||
b15c75fc DM |
1195 | my $path = PVE::Storage::path($storage_cfg, $volid, $snapname); |
1196 | ||
1197 | my ($vtype, undef, undef, undef, undef, $isBase, $format) = | |
1198 | PVE::Storage::parse_volname($storage_cfg, $volid); | |
1199 | ||
c87b9dd8 DM |
1200 | $format = 'iso' if $vtype eq 'iso'; # allow to handle iso files |
1201 | ||
b15c75fc | 1202 | if ($format eq 'subvol') { |
30de33be DM |
1203 | if ($mount_path) { |
1204 | if ($snapname) { | |
e84f7f5d DM |
1205 | if ($scfg->{type} eq 'zfspool') { |
1206 | my $path_arg = $path; | |
1207 | $path_arg =~ s!^/+!!; | |
471dd315 | 1208 | PVE::Tools::run_command(['mount', '-o', 'ro', @extra_opts, '-t', 'zfs', $path_arg, $mount_path]); |
e84f7f5d | 1209 | } else { |
30de33be DM |
1210 | die "cannot mount subvol snapshots for storage type '$scfg->{type}'\n"; |
1211 | } | |
e84f7f5d | 1212 | } else { |
719129ea WB |
1213 | if (defined($acl) && $scfg->{type} eq 'zfspool') { |
1214 | my $acltype = ($acl ? 'acltype=posixacl' : 'acltype=noacl'); | |
1215 | my (undef, $name) = PVE::Storage::parse_volname($storage_cfg, $volid); | |
1216 | $name .= "\@$snapname" if defined($snapname); | |
1217 | PVE::Tools::run_command(['zfs', 'set', $acltype, "$scfg->{pool}/$name"]); | |
1218 | } | |
ab3722b3 | 1219 | bindmount($path, $parentfd, $last_dir//$rootdir, $mount_path, $readonly, @extra_opts); |
50df544c | 1220 | warn "cannot enable quota control for bind mounted subvolumes\n" if $quota; |
30de33be | 1221 | } |
b15c75fc | 1222 | } |
5f6280cf | 1223 | return wantarray ? ($path, 0, undef) : $path; |
c87b9dd8 | 1224 | } elsif ($format eq 'raw' || $format eq 'iso') { |
ada088e6 WB |
1225 | # NOTE: 'mount' performs canonicalization without the '-c' switch, which for |
1226 | # device-mapper devices is special-cased to use the /dev/mapper symlinks. | |
1227 | # Our autodev hook expects the /dev/dm-* device currently | |
1228 | # and will create the /dev/mapper symlink accordingly | |
e439a713 WB |
1229 | $path = Cwd::realpath($path); |
1230 | die "failed to get device path\n" if !$path; | |
1231 | ($path) = ($path =~ /^(.*)$/s); #untaint | |
50df544c WB |
1232 | my $domount = sub { |
1233 | my ($path) = @_; | |
1234 | if ($mount_path) { | |
1235 | if ($format eq 'iso') { | |
1236 | PVE::Tools::run_command(['mount', '-o', 'ro', @extra_opts, $path, $mount_path]); | |
1237 | } elsif ($isBase || defined($snapname)) { | |
1238 | PVE::Tools::run_command(['mount', '-o', 'ro,noload', @extra_opts, $path, $mount_path]); | |
1239 | } else { | |
1240 | if ($quota) { | |
1241 | push @extra_opts, '-o', 'usrjquota=aquota.user,grpjquota=aquota.group,jqfmt=vfsv0'; | |
1242 | } | |
c2744c97 | 1243 | push @extra_opts, '-o', 'ro' if $readonly; |
50df544c WB |
1244 | PVE::Tools::run_command(['mount', @extra_opts, $path, $mount_path]); |
1245 | } | |
1246 | } | |
1247 | }; | |
30de33be | 1248 | my $use_loopdev = 0; |
b15c75fc | 1249 | if ($scfg->{path}) { |
50df544c | 1250 | $mounted_dev = run_with_loopdev($domount, $path); |
30de33be | 1251 | $use_loopdev = 1; |
2e879877 DM |
1252 | } elsif ($scfg->{type} eq 'drbd' || $scfg->{type} eq 'lvm' || |
1253 | $scfg->{type} eq 'rbd' || $scfg->{type} eq 'lvmthin') { | |
50df544c WB |
1254 | $mounted_dev = $path; |
1255 | &$domount($path); | |
b15c75fc DM |
1256 | } else { |
1257 | die "unsupported storage type '$scfg->{type}'\n"; | |
1258 | } | |
50df544c | 1259 | return wantarray ? ($path, $use_loopdev, $mounted_dev) : $path; |
b15c75fc DM |
1260 | } else { |
1261 | die "unsupported image format '$format'\n"; | |
1262 | } | |
7c921c80 | 1263 | } elsif ($type eq 'device') { |
c9bc95a1 | 1264 | push @extra_opts, '-o', 'ro' if $readonly; |
0d449e38 WB |
1265 | push @extra_opts, '-o', 'usrjquota=aquota.user,grpjquota=aquota.group,jqfmt=vfsv0' if $quota; |
1266 | # See the NOTE above about devicemapper canonicalization | |
1267 | my ($devpath) = (Cwd::realpath($volid) =~ /^(.*)$/s); # realpath() taints | |
471dd315 | 1268 | PVE::Tools::run_command(['mount', @extra_opts, $volid, $mount_path]) if $mount_path; |
0d449e38 | 1269 | return wantarray ? ($volid, 0, $devpath) : $volid; |
e2007ac2 DM |
1270 | } elsif ($type eq 'bind') { |
1271 | die "directory '$volid' does not exist\n" if ! -d $volid; | |
ab3722b3 | 1272 | bindmount($volid, $parentfd, $last_dir//$rootdir, $mount_path, $readonly, @extra_opts) if $mount_path; |
50df544c WB |
1273 | warn "cannot enable quota control for bind mounts\n" if $quota; |
1274 | return wantarray ? ($volid, 0, undef) : $volid; | |
b15c75fc DM |
1275 | } |
1276 | ||
1277 | die "unsupported storage"; | |
cc6b0307 AD |
1278 | } |
1279 | ||
6c871c36 | 1280 | sub mkfs { |
d216e891 | 1281 | my ($dev, $rootuid, $rootgid) = @_; |
6c871c36 | 1282 | |
d216e891 WB |
1283 | PVE::Tools::run_command(['mkfs.ext4', '-O', 'mmp', |
1284 | '-E', "root_owner=$rootuid:$rootgid", | |
1285 | $dev]); | |
6c871c36 DM |
1286 | } |
1287 | ||
1288 | sub format_disk { | |
d216e891 | 1289 | my ($storage_cfg, $volid, $rootuid, $rootgid) = @_; |
6c871c36 DM |
1290 | |
1291 | if ($volid =~ m!^/dev/.+!) { | |
1292 | mkfs($volid); | |
1293 | return; | |
1294 | } | |
1295 | ||
1296 | my ($storage, $volname) = PVE::Storage::parse_volume_id($volid, 1); | |
1297 | ||
1298 | die "cannot format volume '$volid' with no storage\n" if !$storage; | |
1299 | ||
08ca136d DM |
1300 | PVE::Storage::activate_volumes($storage_cfg, [$volid]); |
1301 | ||
6c871c36 DM |
1302 | my $path = PVE::Storage::path($storage_cfg, $volid); |
1303 | ||
1304 | my ($vtype, undef, undef, undef, undef, $isBase, $format) = | |
1305 | PVE::Storage::parse_volname($storage_cfg, $volid); | |
1306 | ||
1307 | die "cannot format volume '$volid' (format == $format)\n" | |
1308 | if $format ne 'raw'; | |
1309 | ||
d216e891 | 1310 | mkfs($path, $rootuid, $rootgid); |
6c871c36 DM |
1311 | } |
1312 | ||
1313 | sub destroy_disks { | |
1314 | my ($storecfg, $vollist) = @_; | |
1315 | ||
1316 | foreach my $volid (@$vollist) { | |
1317 | eval { PVE::Storage::vdisk_free($storecfg, $volid); }; | |
1318 | warn $@ if $@; | |
1319 | } | |
1320 | } | |
1321 | ||
1322 | sub create_disks { | |
1323 | my ($storecfg, $vmid, $settings, $conf) = @_; | |
1324 | ||
1325 | my $vollist = []; | |
1326 | ||
1327 | eval { | |
d216e891 WB |
1328 | my (undef, $rootuid, $rootgid) = PVE::LXC::parse_id_maps($conf); |
1329 | my $chown_vollist = []; | |
1330 | ||
d250604f | 1331 | PVE::LXC::Config->foreach_mountpoint($settings, sub { |
6c871c36 DM |
1332 | my ($ms, $mountpoint) = @_; |
1333 | ||
1334 | my $volid = $mountpoint->{volume}; | |
1335 | my $mp = $mountpoint->{mp}; | |
1336 | ||
1337 | my ($storage, $volname) = PVE::Storage::parse_volume_id($volid, 1); | |
1338 | ||
e2007ac2 | 1339 | if ($storage && ($volid =~ m/^([^:\s]+):(\d+(\.\d+)?)$/)) { |
8ed5ff9d | 1340 | my ($storeid, $size_gb) = ($1, $2); |
6c871c36 | 1341 | |
8ed5ff9d | 1342 | my $size_kb = int(${size_gb}*1024) * 1024; |
6c871c36 DM |
1343 | |
1344 | my $scfg = PVE::Storage::storage_config($storecfg, $storage); | |
1345 | # fixme: use better naming ct-$vmid-disk-X.raw? | |
1346 | ||
1347 | if ($scfg->{type} eq 'dir' || $scfg->{type} eq 'nfs') { | |
8ed5ff9d | 1348 | if ($size_kb > 0) { |
6c871c36 | 1349 | $volid = PVE::Storage::vdisk_alloc($storecfg, $storage, $vmid, 'raw', |
8ed5ff9d | 1350 | undef, $size_kb); |
d216e891 | 1351 | format_disk($storecfg, $volid, $rootuid, $rootgid); |
6c871c36 DM |
1352 | } else { |
1353 | $volid = PVE::Storage::vdisk_alloc($storecfg, $storage, $vmid, 'subvol', | |
1354 | undef, 0); | |
d216e891 | 1355 | push @$chown_vollist, $volid; |
6c871c36 DM |
1356 | } |
1357 | } elsif ($scfg->{type} eq 'zfspool') { | |
1358 | ||
1359 | $volid = PVE::Storage::vdisk_alloc($storecfg, $storage, $vmid, 'subvol', | |
8ed5ff9d | 1360 | undef, $size_kb); |
d216e891 | 1361 | push @$chown_vollist, $volid; |
2e879877 | 1362 | } elsif ($scfg->{type} eq 'drbd' || $scfg->{type} eq 'lvm' || $scfg->{type} eq 'lvmthin') { |
6c871c36 | 1363 | |
8ed5ff9d | 1364 | $volid = PVE::Storage::vdisk_alloc($storecfg, $storage, $vmid, 'raw', undef, $size_kb); |
d216e891 | 1365 | format_disk($storecfg, $volid, $rootuid, $rootgid); |
6c871c36 DM |
1366 | |
1367 | } elsif ($scfg->{type} eq 'rbd') { | |
1368 | ||
1369 | die "krbd option must be enabled on storage type '$scfg->{type}'\n" if !$scfg->{krbd}; | |
8ed5ff9d | 1370 | $volid = PVE::Storage::vdisk_alloc($storecfg, $storage, $vmid, 'raw', undef, $size_kb); |
d216e891 | 1371 | format_disk($storecfg, $volid, $rootuid, $rootgid); |
6c871c36 DM |
1372 | } else { |
1373 | die "unable to create containers on storage type '$scfg->{type}'\n"; | |
1374 | } | |
1375 | push @$vollist, $volid; | |
71c780b9 WB |
1376 | $mountpoint->{volume} = $volid; |
1377 | $mountpoint->{size} = $size_kb * 1024; | |
1b4cf758 | 1378 | $conf->{$ms} = PVE::LXC::Config->print_ct_mountpoint($mountpoint, $ms eq 'rootfs'); |
6c871c36 | 1379 | } else { |
e2007ac2 | 1380 | # use specified/existing volid/dir/device |
1b4cf758 | 1381 | $conf->{$ms} = PVE::LXC::Config->print_ct_mountpoint($mountpoint, $ms eq 'rootfs'); |
6c871c36 DM |
1382 | } |
1383 | }); | |
d216e891 WB |
1384 | |
1385 | PVE::Storage::activate_volumes($storecfg, $chown_vollist, undef); | |
1386 | foreach my $volid (@$chown_vollist) { | |
1387 | my $path = PVE::Storage::path($storecfg, $volid, undef); | |
1388 | chown($rootuid, $rootgid, $path); | |
1389 | } | |
1390 | PVE::Storage::deactivate_volumes($storecfg, $chown_vollist, undef); | |
6c871c36 DM |
1391 | }; |
1392 | # free allocated images on error | |
1393 | if (my $err = $@) { | |
1394 | destroy_disks($storecfg, $vollist); | |
1395 | die $err; | |
1396 | } | |
1397 | return $vollist; | |
1398 | } | |
1399 | ||
68e8f3c5 DM |
1400 | # bash completion helper |
1401 | ||
1402 | sub complete_os_templates { | |
1403 | my ($cmdname, $pname, $cvalue) = @_; | |
1404 | ||
1405 | my $cfg = PVE::Storage::config(); | |
1406 | ||
9e9bc3a6 | 1407 | my $storeid; |
68e8f3c5 DM |
1408 | |
1409 | if ($cvalue =~ m/^([^:]+):/) { | |
1410 | $storeid = $1; | |
1411 | } | |
1412 | ||
1413 | my $vtype = $cmdname eq 'restore' ? 'backup' : 'vztmpl'; | |
1414 | my $data = PVE::Storage::template_list($cfg, $storeid, $vtype); | |
1415 | ||
1416 | my $res = []; | |
1417 | foreach my $id (keys %$data) { | |
1418 | foreach my $item (@{$data->{$id}}) { | |
1419 | push @$res, $item->{volid} if defined($item->{volid}); | |
1420 | } | |
1421 | } | |
1422 | ||
1423 | return $res; | |
1424 | } | |
1425 | ||
68e8f3c5 DM |
1426 | my $complete_ctid_full = sub { |
1427 | my ($running) = @_; | |
1428 | ||
1429 | my $idlist = vmstatus(); | |
1430 | ||
1431 | my $active_hash = list_active_containers(); | |
1432 | ||
1433 | my $res = []; | |
1434 | ||
1435 | foreach my $id (keys %$idlist) { | |
1436 | my $d = $idlist->{$id}; | |
1437 | if (defined($running)) { | |
1438 | next if $d->{template}; | |
1439 | next if $running && !$active_hash->{$id}; | |
1440 | next if !$running && $active_hash->{$id}; | |
1441 | } | |
1442 | push @$res, $id; | |
1443 | ||
1444 | } | |
1445 | return $res; | |
1446 | }; | |
1447 | ||
1448 | sub complete_ctid { | |
1449 | return &$complete_ctid_full(); | |
1450 | } | |
1451 | ||
1452 | sub complete_ctid_stopped { | |
1453 | return &$complete_ctid_full(0); | |
1454 | } | |
1455 | ||
1456 | sub complete_ctid_running { | |
1457 | return &$complete_ctid_full(1); | |
1458 | } | |
1459 | ||
c6a605f9 WB |
1460 | sub parse_id_maps { |
1461 | my ($conf) = @_; | |
1462 | ||
1463 | my $id_map = []; | |
1464 | my $rootuid = 0; | |
1465 | my $rootgid = 0; | |
1466 | ||
1467 | my $lxc = $conf->{lxc}; | |
1468 | foreach my $entry (@$lxc) { | |
1469 | my ($key, $value) = @$entry; | |
1470 | next if $key ne 'lxc.id_map'; | |
1471 | if ($value =~ /^([ug])\s+(\d+)\s+(\d+)\s+(\d+)\s*$/) { | |
1472 | my ($type, $ct, $host, $length) = ($1, $2, $3, $4); | |
1473 | push @$id_map, [$type, $ct, $host, $length]; | |
1474 | if ($ct == 0) { | |
1475 | $rootuid = $host if $type eq 'u'; | |
1476 | $rootgid = $host if $type eq 'g'; | |
1477 | } | |
1478 | } else { | |
1479 | die "failed to parse id_map: $value\n"; | |
1480 | } | |
1481 | } | |
1482 | ||
1483 | if (!@$id_map && $conf->{unprivileged}) { | |
1484 | # Should we read them from /etc/subuid? | |
1485 | $id_map = [ ['u', '0', '100000', '65536'], | |
1486 | ['g', '0', '100000', '65536'] ]; | |
1487 | $rootuid = $rootgid = 100000; | |
1488 | } | |
1489 | ||
1490 | return ($id_map, $rootuid, $rootgid); | |
1491 | } | |
1492 | ||
01dce99b WB |
1493 | sub userns_command { |
1494 | my ($id_map) = @_; | |
1495 | if (@$id_map) { | |
1496 | return ['lxc-usernsexec', (map { ('-m', join(':', @$_)) } @$id_map), '--']; | |
1497 | } | |
1498 | return []; | |
1499 | } | |
1500 | ||
846a66b0 | 1501 | |
f76a2828 | 1502 | 1; |