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