]>
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 => { | |
47 | name => get_standard_option('pve-vmid'), | |
48 | path => { | |
49 | description => "The path to the container configuration directory (LXC internal argument - do not pass manually!).", | |
50 | type => 'string', | |
51 | }, | |
52 | rootfs => { | |
53 | description => "The path to the container's rootfs (LXC internal argument - do not pass manually!)", | |
54 | type => 'string', | |
55 | }, | |
56 | }, | |
57 | }, | |
58 | returns => { type => 'null' }, | |
59 | ||
60 | code => sub { | |
61 | my ($param) = @_; | |
62 | ||
63 | my $private = $param->{rootfs}; | |
64 | ||
65 | die "got strange path '$param->{path}'\n" | |
66 | if $param->{path} !~ m!^/etc/pve/lxc/$param->{name}/config$!; | |
67 | ||
68 | my $raw = PVE::Tools::file_get_contents($param->{path}); | |
69 | my $conf = PVE::LXC::parse_lxc_config($param->{path}, $raw); | |
70 | ||
71 | my $lxc_setup = PVE::LXCSetup->new($conf); | |
72 | $lxc_setup->set_hostname(); | |
73 | $lxc_setup->setup_network(); | |
74 | ||
75 | return undef; | |
76 | }}); | |
77 | ||
78 | ||
79 | push @ARGV, 'help' if !scalar(@ARGV); | |
80 | ||
81 | my $param = {}; | |
82 | ||
83 | if ((scalar(@ARGV) == 1) && ($ARGV[0] eq 'printmanpod') || | |
84 | ($ARGV[0] eq 'verifyapi')) { | |
85 | # OK | |
86 | } elsif ((scalar(@ARGV) == 3) && ($ARGV[1] eq 'lxc') && ($ARGV[2] eq 'mount')) { | |
87 | $param->{name} = $ENV{'LXC_NAME'}; | |
88 | die "got wrong name" if $param->{name} ne $ARGV[0]; | |
89 | ||
90 | $param->{path} = $ENV{'LXC_CONFIG_FILE'}; | |
91 | $param->{rootfs} = $ENV{'LXC_ROOTFS_PATH'}; | |
92 | @ARGV = (); | |
93 | } else { | |
94 | @ARGV = ('help'); | |
95 | } | |
96 | ||
97 | my $cmddef = [ __PACKAGE__, 'lxc-pve-mount-hook', [], $param]; | |
98 | ||
99 | PVE::CLIHandler::handle_simple_cmd($cmddef, \@ARGV, undef, $0); | |
100 | ||
101 | exit 0; | |
102 | ||
103 | __END__ | |
104 | ||
105 | =head1 NAME | |
106 | ||
107 | lxc-pve - LXC mount hook for Proxmox VE | |
108 | ||
109 | =head1 SYNOPSIS | |
110 | ||
111 | =include synopsis | |
112 | ||
113 | =head1 DESCRIPTION | |
114 | ||
115 | This mount hook sets the network and hostname for the container. | |
116 | ||
117 | =head1 SEE ALSO | |
118 | ||
119 | lct(1) | |
120 | ||
121 | =include pve_copyright |