]>
Commit | Line | Data |
---|---|---|
93285df8 DM |
1 | #!/usr/bin/perl |
2 | ||
3 | use strict; | |
4 | use warnings; | |
5 | use POSIX; | |
6 | use File::Path; | |
7 | ||
8 | use PVE::SafeSyslog; | |
9 | use PVE::Tools; | |
10 | use PVE::Cluster; | |
11 | use PVE::INotify; | |
12 | use PVE::RPCEnvironment; | |
13 | use PVE::JSONSchema qw(get_standard_option); | |
14 | use PVE::CLIHandler; | |
15 | use PVE::Storage; | |
16 | use PVE::LXC; | |
17 | use PVE::LXCSetup; | |
18 | use Data::Dumper; | |
19 | ||
20 | use base qw(PVE::CLIHandler); | |
21 | ||
22 | $ENV{'PATH'} = '/sbin:/bin:/usr/sbin:/usr/bin'; | |
23 | ||
24 | initlog ('lxc-pve-mount-hook'); | |
25 | ||
26 | die "please run as root\n" if $> != 0; | |
27 | ||
28 | PVE::INotify::inotify_init(); | |
29 | ||
30 | my $rpcenv = PVE::RPCEnvironment->init('cli'); | |
31 | $rpcenv->set_language($ENV{LANG}); | |
32 | $rpcenv->set_user('root@pam'); | |
33 | ||
34 | # we cannot use cfs_read here (permission problem) | |
35 | #$rpcenv->init_request(); | |
36 | ||
37 | my $nodename = PVE::INotify::nodename(); | |
38 | ||
39 | __PACKAGE__->register_method ({ | |
40 | name => 'lxc-pve-mount-hook', | |
41 | path => 'lxc-pve-mount-hook', | |
42 | method => 'GET', | |
43 | description => "Create a new container root directory.", | |
44 | parameters => { | |
45 | additionalProperties => 0, | |
46 | properties => { | |
4fc1b360 | 47 | name => { |
c0c77476 | 48 | description => "The container name. This hook is only active for containers using numeric IDs, where configuration is stored on /etc/pve/lxc/<name>.conf (else it is just a NOP).", |
4fc1b360 DM |
49 | type => 'string', |
50 | pattern => '\S+', | |
51 | maxLength => 64, | |
52 | }, | |
93285df8 DM |
53 | path => { |
54 | description => "The path to the container configuration directory (LXC internal argument - do not pass manually!).", | |
55 | type => 'string', | |
56 | }, | |
57 | rootfs => { | |
58 | description => "The path to the container's rootfs (LXC internal argument - do not pass manually!)", | |
59 | type => 'string', | |
60 | }, | |
61 | }, | |
62 | }, | |
63 | returns => { type => 'null' }, | |
64 | ||
65 | code => sub { | |
66 | my ($param) = @_; | |
67 | ||
68 | my $private = $param->{rootfs}; | |
69 | ||
4fc1b360 | 70 | return undef if $param->{name} !~ m/^\d+$/; |
93285df8 | 71 | |
27916659 DM |
72 | # Note: PVE::INotify::nodename() returns wrong value when run |
73 | # inside container mount hook, so we cannot simply | |
74 | # use PVE::LXC::load_conf(). | |
93285df8 | 75 | |
27916659 DM |
76 | my $config_filename = "/etc/pve/lxc/$param->{name}.conf"; |
77 | ||
78 | return undef if ! -f $config_filename; | |
79 | ||
80 | my $raw = PVE::Tools::file_get_contents($config_filename); | |
81 | my $conf = PVE::LXC::parse_pct_config($config_filename, $raw); | |
82 | ||
5b4657d0 DM |
83 | my $mountpoint = $ENV{LXC_ROOTFS_MOUNT}; |
84 | ||
f5635601 AD |
85 | PVE::LXC::setup_mountpoints($conf, $param->{name}, $mountpoint); |
86 | ||
5b4657d0 | 87 | my $lxc_setup = PVE::LXCSetup->new($conf, $mountpoint); |
d66768a2 | 88 | $lxc_setup->pre_start_hook(); |
27916659 | 89 | |
93285df8 DM |
90 | return undef; |
91 | }}); | |
92 | ||
93 | ||
94 | push @ARGV, 'help' if !scalar(@ARGV); | |
95 | ||
96 | my $param = {}; | |
97 | ||
98 | if ((scalar(@ARGV) == 1) && ($ARGV[0] eq 'printmanpod') || | |
99 | ($ARGV[0] eq 'verifyapi')) { | |
100 | # OK | |
101 | } elsif ((scalar(@ARGV) == 3) && ($ARGV[1] eq 'lxc') && ($ARGV[2] eq 'mount')) { | |
102 | $param->{name} = $ENV{'LXC_NAME'}; | |
103 | die "got wrong name" if $param->{name} ne $ARGV[0]; | |
5b4657d0 | 104 | |
93285df8 DM |
105 | $param->{path} = $ENV{'LXC_CONFIG_FILE'}; |
106 | $param->{rootfs} = $ENV{'LXC_ROOTFS_PATH'}; | |
107 | @ARGV = (); | |
108 | } else { | |
109 | @ARGV = ('help'); | |
110 | } | |
111 | ||
112 | my $cmddef = [ __PACKAGE__, 'lxc-pve-mount-hook', [], $param]; | |
113 | ||
114 | PVE::CLIHandler::handle_simple_cmd($cmddef, \@ARGV, undef, $0); | |
115 | ||
116 | exit 0; | |
117 | ||
118 | __END__ | |
119 | ||
120 | =head1 NAME | |
121 | ||
122 | lxc-pve - LXC mount hook for Proxmox VE | |
123 | ||
124 | =head1 SYNOPSIS | |
125 | ||
126 | =include synopsis | |
127 | ||
128 | =head1 DESCRIPTION | |
129 | ||
4fc1b360 | 130 | This mount hook sets the network and hostname for pve container. |
93285df8 DM |
131 | |
132 | =head1 SEE ALSO | |
133 | ||
134 | lct(1) | |
135 | ||
136 | =include pve_copyright |