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