]> git.proxmox.com Git - pve-container.git/blob - src/lxc-pve
initial commit
[pve-container.git] / src / lxc-pve
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 Data::Dumper;
18
19 use base qw(PVE::CLIHandler);
20
21 $ENV{'PATH'} = '/sbin:/bin:/usr/sbin:/usr/bin';
22
23 initlog ('lxc-pve');
24
25 die "please run as root\n" if $> != 0;
26
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 my $nodename = PVE::INotify::nodename();
35
36 __PACKAGE__->register_method ({
37 name => 'lxc-pve',
38 path => 'lxc-pve',
39 method => 'GET',
40 description => "Create a new container root directory.",
41 parameters => {
42 additionalProperties => 0,
43 properties => {
44 name => get_standard_option('pve-vmid'),
45 path => {
46 description => "The path to the container configuration directory (LXC internal argument - do not pass manually!).",
47 type => 'string',
48 },
49 rootfs => {
50 description => "The path to the container's rootfs (LXC internal argument - do not pass manually!)",
51 type => 'string',
52 },
53 storage => get_standard_option('pve-storage-id'),
54 'mapped-uid' => {
55 description => " A uid map (user namespaces - LXC internal argument - do not pass manually!)",
56 type => 'string',
57 optional => 1,
58 },
59 'mapped-gid' => {
60 description => " A gid map (user namespaces - LXC internal argument - do not pass manually!)",
61 type => 'string',
62 optional => 1,
63 },
64 },
65 },
66 returns => { type => 'null' },
67
68 code => sub {
69 my ($param) = @_;
70
71 # check that we have all variables we need
72 if (!(defined($ENV{LXC_NAME}) && defined($ENV{LXC_PATH}) && defined($ENV{LXC_ROOTFS}))) {
73 die "ERROR: Not running through LXC.\n"
74 }
75
76 # only orinial user can access /etc/pve
77 $< = $param->{'mapped-uid'} if defined($param->{'mapped-uid'});
78 $( = $param->{'mapped-gid'} if defined($param->{'mapped-gid'});
79
80 my $userns = defined($param->{'mapped-uid'}) ? 1 : 0;
81
82 my $private = $param->{rootfs};
83
84 die "got strange path '$param->{path}'\n"
85 if $param->{path} !~ m!^/etc/pve/lxc/$param->{name}/?$!;
86
87 # print "TEST $< $(\n";
88
89 $rpcenv->init_request();
90
91 print "TEST2\n";
92
93 my $conf = PVE::LXC::load_config($param->{name});
94 print Dumper($conf);
95
96 my $storage_cfg = PVE::Cluster::cfs_read_file("storage.cfg");
97
98 my $ostemplate = "local:vztmpl/debian-7.0-standard_7.0-2_i386.tar.gz";
99 print "Template: $ostemplate\n";
100
101 my $archive = PVE::Storage::abs_filesystem_path($storage_cfg, $ostemplate);
102
103 #$< = $( = 0; # switch back to container root
104 #print "TEST $< $(\n";
105
106 my $cmd = ['tar', 'xpf', $archive, '--numeric-owner', '--totals', '--sparse', '-C', $private];
107 push @$cmd, '--anchored';
108 push @$cmd, '--exclude' , '/dev/*';
109 # push @$cmd, '--exclude' , '/dev/loop*';
110 # push @$cmd, '--exclude' , '/dev/ram*';
111 # push @$cmd, '--exclude' , '/dev/pty*';
112 # push @$cmd, '--exclude' , '/dev/tty*';
113 # push @$cmd, '--exclude' , '/dev/pty*';
114 # push @$cmd, '--exclude' , '/dev/null';
115 # push @$cmd, '--exclude' , '/dev/random';
116 # push @$cmd, '--exclude' , '/dev/urandom';
117 # push @$cmd, '--exclude' , '/dev/console';
118 # push @$cmd, '--exclude' , '/dev/zero';
119 # push @$cmd, '--exclude' , '/dev/ptmx';
120 # push @$cmd, '--exclude' , '/dev/port';
121 # push @$cmd, '--exclude' , '/dev/mem';
122 # push @$cmd, '--exclude' , '/dev/kmem';
123 # push @$cmd, '--exclude' , '/dev/full';
124
125 print "extracting archive '$archive' to '$private'\n";
126 PVE::Tools::run_command($cmd);
127
128 File::Path::make_path("$private/dev/pts");
129
130 # template/OS specific configuration
131 $conf->{'lxc.arch'} = 'i386'; # || x86_64
132
133 $conf->{'lxc.include'} = "/usr/share/lxc/config/debian.common.conf";
134
135 if ($userns) {
136 die "also include me";
137 $conf->{'lxc.include'} = "/usr/share/lxc/config/debian.userns.conf";
138 }
139
140 PVE::LXC::write_config($param->{name}, $conf);
141
142 return undef;
143 }});
144
145 my $cmddef = [ __PACKAGE__, 'lxc-pve' ];
146
147 push @ARGV, 'help' if !scalar(@ARGV);
148
149 PVE::CLIHandler::handle_simple_cmd($cmddef, \@ARGV, undef, $0);
150
151 exit 0;
152
153 __END__
154
155 =head1 NAME
156
157 lxc-pve - create containers from Proxmox VE templates
158
159 =head1 SYNOPSIS
160
161 =include synopsis
162
163 =head1 DESCRIPTION
164
165 This script creates containers from Proxmox VE templates. This is always
166 called from within lxc-create, so please do not try to use this directly.
167 Use 'lct' instead.
168
169 =head1 SEE ALSO
170
171 lct(1)
172
173 =include pve_copyright