]>
git.proxmox.com Git - pve-container.git/blob - src/lxc-pve-mount-hook
6 exit 0 if $ENV { LXC_NAME
} && $ENV { LXC_NAME
} !~ /^\d+$/ ;
14 use PVE
:: RPCEnvironment
;
15 use PVE
:: JSONSchema
qw(get_standard_option) ;
22 use base
qw(PVE::CLIHandler) ;
24 $ENV { 'PATH' } = '/sbin:/bin:/usr/sbin:/usr/bin' ;
26 initlog
( 'lxc-pve-mount-hook' );
28 die "please run as root \n " if $> != 0 ;
30 my $rpcenv = PVE
:: RPCEnvironment-
> init ( 'cli' );
31 $rpcenv -> set_language ( $ENV { LANG
});
32 $rpcenv -> set_user ( 'root @pam ' );
34 # we cannot use cfs_read here (permission problem)
35 # $rpcenv->init_request();
36 # PVE::INotify::nodename() also returns wrong value!
38 __PACKAGE__-
> register_method ({
39 name
=> 'lxc-pve-mount-hook' ,
40 path
=> 'lxc-pve-mount-hook' ,
42 description
=> "Create a new container root directory." ,
44 additionalProperties
=> 0 ,
47 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)." ,
53 description
=> "The path to the container configuration directory (LXC internal argument - do not pass manually!)." ,
57 description
=> "The path to the container's rootfs (LXC internal argument - do not pass manually!)" ,
62 returns
=> { type
=> 'null' },
67 my $private = $param ->{ rootfs
};
69 return undef if $param ->{ name
} !~ m/^\d+$/ ;
71 my $vmid = $param ->{ name
};
73 # Note: PVE::INotify::nodename() returns wrong value when run
74 # inside container mount hook, so we cannot simply
75 # use PVE::LXC::load_conf().
77 my $config_filename = "/etc/pve/lxc/ $param ->{name}.conf" ;
79 return undef if ! - f
$config_filename ;
81 my $raw = PVE
:: Tools
:: file_get_contents
( $config_filename );
82 my $conf = PVE
:: LXC
:: parse_pct_config
( $config_filename, $raw );
84 my $rootdir = $ENV { LXC_ROOTFS_MOUNT
};
86 # Note: PVE::Storage::config() does not work here
87 my $fn = "/etc/pve/storage.cfg" ;
88 $raw = - f
$fn ? PVE
:: Tools
:: file_get_contents
( $fn ) : '' ;
89 my $storage_cfg = PVE
:: Storage
:: Plugin-
> parse_config ( $fn, $raw );
91 my $bdevs = PVE
:: LXC
:: blockdevices_list
();
93 my $setup_mountpoint = sub {
94 my ( $ms, $mountpoint ) = @_ ;
96 return if $ms eq 'rootfs' ;
97 PVE
:: LXC
:: mountpoint_mount
( $mountpoint, $rootdir, $storage_cfg );
100 my $setup_cgroup_device = sub {
101 my ( $ms, $mountpoint ) = @_ ;
103 my $volid = $mountpoint ->{ volume
};
104 return if ! $volid || $volid =~ m
|^ /dev/ .+|;
106 my $path = PVE
:: LXC
:: mountpoint_mount_path
( $mountpoint, $storage_cfg );
109 $path = readlink ( $path );
110 $path =~ s/\.\.\/\.\.\//\/d ev\
// ;
113 if ( $bdevs ->{ $path }) {
114 PVE
:: Tools
:: run_command
([ 'mknod' , '-m' , '666' , " $rootdir$path " , 'b' , $bdevs ->{ $path }->{ major
}, $bdevs ->{ $path }->{ minor
}]);
115 PVE
:: LXC
:: write_cgroup_value
( "devices" , $vmid, "devices.allow" , "b $bdevs ->{ $path }->{major}: $bdevs ->{ $path }->{minor} rwm" );
119 PVE
:: LXC
:: foreach_mountpoint
( $conf, $setup_mountpoint );
121 PVE
:: LXC
:: foreach_mountpoint
( $conf, $setup_cgroup_device );
123 my $lxc_setup = PVE
:: LXC
:: Setup-
> new ( $conf, $rootdir );
124 $lxc_setup -> pre_start_hook ();
130 push @ARGV, 'help' if ! scalar ( @ARGV );
134 if (( scalar ( @ARGV ) == 1 ) && ( $ARGV [ 0 ] eq 'printmanpod' ) ||
135 ( $ARGV [ 0 ] eq 'verifyapi' )) {
137 } elsif (( scalar ( @ARGV ) == 3 ) && ( $ARGV [ 1 ] eq 'lxc' ) && ( $ARGV [ 2 ] eq 'mount' )) {
138 $param ->{ name
} = $ENV { 'LXC_NAME' };
139 die "got wrong name" if $param ->{ name
} ne $ARGV [ 0 ];
141 $param ->{ path
} = $ENV { 'LXC_CONFIG_FILE' };
142 $param ->{ rootfs
} = $ENV { 'LXC_ROOTFS_PATH' };
148 my $cmddef = [ __PACKAGE__
, 'lxc-pve-mount-hook' , [], $param ];
150 PVE
:: CLIHandler
:: handle_simple_cmd
( $cmddef, \
@ARGV, undef , $0 );
158 lxc-pve - LXC mount hook for Proxmox VE
166 This mount hook sets the network and hostname for pve container.
172 =include pve_copyright