]>
git.proxmox.com Git - pve-container.git/blob - src/lxc-pve-mount-hook
8e5c0578f769a857bec34fe013ef58165c641f43
11 use PVE
:: RPCEnvironment
;
12 use PVE
:: JSONSchema
qw(get_standard_option) ;
19 use base
qw(PVE::CLIHandler) ;
21 $ENV { 'PATH' } = '/sbin:/bin:/usr/sbin:/usr/bin' ;
23 initlog
( 'lxc-pve-mount-hook' );
25 die "please run as root \n " if $> != 0 ;
27 my $rpcenv = PVE
:: RPCEnvironment-
> init ( 'cli' );
28 $rpcenv -> set_language ( $ENV { LANG
});
29 $rpcenv -> set_user ( 'root @pam ' );
31 # we cannot use cfs_read here (permission problem)
32 # $rpcenv->init_request();
33 # PVE::INotify::nodename() also returns wrong value!
35 __PACKAGE__-
> register_method ({
36 name
=> 'lxc-pve-mount-hook' ,
37 path
=> 'lxc-pve-mount-hook' ,
39 description
=> "Create a new container root directory." ,
41 additionalProperties
=> 0 ,
44 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)." ,
50 description
=> "The path to the container configuration directory (LXC internal argument - do not pass manually!)." ,
54 description
=> "The path to the container's rootfs (LXC internal argument - do not pass manually!)" ,
59 returns
=> { type
=> 'null' },
64 my $private = $param ->{ rootfs
};
66 return undef if $param ->{ name
} !~ m/^\d+$/ ;
68 my $vmid = $param ->{ name
};
70 # Note: PVE::INotify::nodename() returns wrong value when run
71 # inside container mount hook, so we cannot simply
72 # use PVE::LXC::load_conf().
74 my $config_filename = "/etc/pve/lxc/ $param ->{name}.conf" ;
76 return undef if ! - f
$config_filename ;
78 my $raw = PVE
:: Tools
:: file_get_contents
( $config_filename );
79 my $conf = PVE
:: LXC
:: parse_pct_config
( $config_filename, $raw );
81 my $rootdir = $ENV { LXC_ROOTFS_MOUNT
};
83 # Note: PVE::Storage::config() does not work here
84 my $fn = "/etc/pve/storage.cfg" ;
85 $raw = - f
$fn ? PVE
:: Tools
:: file_get_contents
( $fn ) : '' ;
86 my $storage_cfg = PVE
:: Storage
:: Plugin-
> parse_config ( $fn, $raw );
88 my $bdevs = PVE
:: LXC
:: blockdevices_list
();
89 my $loopdevs = PVE
:: LXC
:: loopdevices_list
();
91 my $setup_mountpoint = sub {
92 my ( $ms, $mountpoint ) = @_ ;
94 return if $ms eq 'rootfs' ;
95 PVE
:: LXC
:: mountpoint_mount
( $mountpoint, $rootdir, $storage_cfg, $loopdevs );
98 my $setup_cgroup_device = sub {
99 my ( $ms, $mountpoint ) = @_ ;
101 my $volid = $mountpoint ->{ volume
};
102 return if ! $volid || $volid =~ m
|^ /dev/ .+|;
104 my $path = PVE
:: LXC
:: volid_path
( $volid, $storage_cfg, $loopdevs );
107 $path = readlink ( $path );
108 $path =~ s/\.\.\/\.\.\//\/d ev\
// ;
111 if ( $bdevs ->{ $path }) {
112 PVE
:: Tools
:: run_command
([ 'mknod' , '-m' , '666' , " $rootdir$path " , 'b' , $bdevs ->{ $path }->{ major
}, $bdevs ->{ $path }->{ minor
}]);
113 PVE
:: LXC
:: write_cgroup_value
( "devices" , $vmid, "devices.allow" , "b $bdevs ->{ $path }->{major}: $bdevs ->{ $path }->{minor} rwm" );
117 PVE
:: LXC
:: foreach_mountpoint
( $conf, $setup_mountpoint );
119 PVE
:: LXC
:: foreach_mountpoint
( $conf, $setup_cgroup_device );
121 my $lxc_setup = PVE
:: LXC
:: Setup-
> new ( $conf, $rootdir );
122 $lxc_setup -> pre_start_hook ();
128 push @ARGV, 'help' if ! scalar ( @ARGV );
132 if (( scalar ( @ARGV ) == 1 ) && ( $ARGV [ 0 ] eq 'printmanpod' ) ||
133 ( $ARGV [ 0 ] eq 'verifyapi' )) {
135 } elsif (( scalar ( @ARGV ) == 3 ) && ( $ARGV [ 1 ] eq 'lxc' ) && ( $ARGV [ 2 ] eq 'mount' )) {
136 $param ->{ name
} = $ENV { 'LXC_NAME' };
137 die "got wrong name" if $param ->{ name
} ne $ARGV [ 0 ];
139 $param ->{ path
} = $ENV { 'LXC_CONFIG_FILE' };
140 $param ->{ rootfs
} = $ENV { 'LXC_ROOTFS_PATH' };
146 my $cmddef = [ __PACKAGE__
, 'lxc-pve-mount-hook' , [], $param ];
148 PVE
:: CLIHandler
:: handle_simple_cmd
( $cmddef, \
@ARGV, undef , $0 );
156 lxc-pve - LXC mount hook for Proxmox VE
164 This mount hook sets the network and hostname for pve container.
170 =include pve_copyright