]>
Commit | Line | Data |
---|---|---|
50df544c WB |
1 | #!/usr/bin/perl |
2 | ||
50df544c WB |
3 | use strict; |
4 | use warnings; | |
5 | ||
6 | exit 0 if $ENV{LXC_NAME} && $ENV{LXC_NAME} !~ /^\d+$/; | |
7 | ||
55d14e39 WB |
8 | use File::Path; |
9 | use File::Basename; | |
10 | ||
50df544c WB |
11 | use PVE::Tools; |
12 | ||
13 | my $vmid = $ENV{LXC_NAME}; | |
14 | my $root = $ENV{LXC_ROOTFS_MOUNT}; | |
15 | ||
16 | if (@ARGV != 3 || $ARGV[1] ne 'lxc' || $ARGV[2] ne 'autodev') { | |
17 | die "invalid usage, this is an LXC autodev hook\n"; | |
18 | } | |
19 | ||
20 | if ($vmid ne $ARGV[0]) { | |
21 | die "got wrong name: $ARGV[0] while LXC_NAME=$vmid\n"; | |
22 | } | |
23 | ||
24 | my $devlist_file = "/var/lib/lxc/$vmid/devices"; | |
74e20aeb | 25 | my $fd; |
50df544c | 26 | |
74e20aeb | 27 | if (! open $fd, '<', $devlist_file) { |
50df544c WB |
28 | exit 0 if $!{ENOENT}; # If the list is empty the file might not exist. |
29 | die "failed to open device list: $!\n"; | |
30 | } | |
31 | ||
32 | while (defined(my $line = <$fd>)) { | |
33 | if ($line !~ m@^(b):(\d+):(\d+):/dev/(\S+)\s*$@) { | |
34 | warn "invalid .pve-devices entry: $line\n"; | |
35 | } | |
36 | my ($type, $major, $minor, $dev) = ($1, $2, $3, $4); | |
37 | ||
38 | # Don't break out of $root/dev/ | |
39 | if ($dev =~ /\.\./) { | |
40 | warn "skipping illegal device node entry: $dev\n"; | |
41 | next; | |
42 | } | |
43 | ||
44 | # Never expose /dev/loop-control | |
45 | if ($major == 10 && $minor == 237) { | |
46 | warn "skipping illegal device entry (loop-control) for: $dev\n"; | |
47 | next; | |
48 | } | |
49 | ||
55d14e39 WB |
50 | my $rel_devpath = "/dev/$dev"; |
51 | my $rel_dir = dirname($rel_devpath); | |
52 | File::Path::mkpath("$root/$rel_dir"); | |
53 | ||
50df544c WB |
54 | PVE::Tools::run_command(['mknod', '-m', '666', "$root/dev/$dev", |
55 | $type, $major, $minor]); | |
ada088e6 WB |
56 | |
57 | if ($dev =~ /^dm-\d+$/) { | |
58 | File::Path::mkpath("$root/dev/mapper"); | |
59 | my $mapped_name = PVE::Tools::file_get_contents("/sys/block/$dev/dm/name"); | |
60 | chomp $mapped_name; | |
61 | symlink("/dev/$dev", "$root/dev/mapper/$mapped_name"); | |
62 | } | |
50df544c WB |
63 | } |
64 | close $fd; | |
f067e7ba WB |
65 | |
66 | exit 0; |