]> git.proxmox.com Git - pve-container.git/blob - src/PVE/LXCCreate.pm
use new subvolume API for zfspool
[pve-container.git] / src / PVE / LXCCreate.pm
1 package PVE::LXCCreate;
2
3 use strict;
4 use warnings;
5 use File::Basename;
6 use File::Path;
7 use Data::Dumper;
8
9 use PVE::Storage;
10 use PVE::LXC;
11 use PVE::LXCSetup;
12
13 sub next_free_nbd_dev {
14
15 for(my $i = 0;;$i++) {
16 my $dev = "/dev/nbd$i";
17 last if ! -b $dev;
18 next if -f "/sys/block/nbd$i/pid"; # busy
19 return $dev;
20 }
21 die "unable to find free nbd device\n";
22 }
23
24 sub restore_archive {
25 my ($archive, $rootdir, $conf) = @_;
26
27 # we always use the same mapping: 'b:0:100000:65536'
28 my $userns_cmd;
29
30 if ($conf->{'lxc.id_map'}) {
31 $userns_cmd = ['lxc-usernsexec', '-m', 'b:0:100000:65536', '--'];
32 } else {
33 $userns_cmd = [];
34 }
35
36 my $cmd;
37
38 if ($conf->{'lxc.id_map'}) {
39 PVE::Tools::run_command(['chown', '-R', '100000:100000', $rootdir]);
40 }
41
42 $cmd = [@$userns_cmd, 'tar', 'xpf', $archive, '--numeric-owner', '--totals',
43 '--sparse', '-C', $rootdir];
44
45 push @$cmd, '--anchored';
46 push @$cmd, '--exclude' , './dev/*';
47
48 if ($archive eq '-') {
49 print "extracting archive from STDIN\n";
50 PVE::Tools::run_command($cmd, input => "<&STDIN");
51 } else {
52 print "extracting archive '$archive'\n";
53 PVE::Tools::run_command($cmd);
54 }
55
56 # is this really required? what for?
57 #$cmd = [@$userns_cmd, 'mkdir', '-p', "$rootdir/dev/pts"];
58 #PVE::Tools::run_command($cmd);
59
60 # template/OS specific configuration
61 $conf->{'lxc.arch'} = 'i386'; #fixme: || x86_64
62 }
63
64 sub restore_and_configure {
65 my ($vmid, $archive, $rootdir, $conf, $password) = @_;
66
67 restore_archive($archive, $rootdir, $conf);
68
69 PVE::LXC::write_config($vmid, $conf);
70
71 my $lxc_setup = PVE::LXCSetup->new($conf, $rootdir); # detect OS
72
73 PVE::LXC::write_config($vmid, $conf); # safe config (after OS detection)
74
75 $lxc_setup->post_create_hook($password);
76 }
77
78 # directly use a storage directory
79 sub create_rootfs_dir {
80 my ($cleanup, $storage_conf, $storage, $vmid, $conf, $archive, $password) = @_;
81
82 # note: there is no size limit
83 $conf->{'pve.disksize'} = 0;
84
85 my $private = PVE::Storage::get_private_dir($storage_conf, $storage, $vmid);
86 mkdir($private) || die "unable to create container private dir '$private' - $!\n";
87
88 push @{$cleanup->{files}}, $private;
89 $conf->{'lxc.rootfs'} = $private;
90
91 restore_and_configure($vmid, $archive, $private, $conf, $password);
92 }
93
94 # use new subvolume API
95 sub create_rootfs_subvol {
96 my ($cleanup, $storage_conf, $storage, $size, $vmid, $conf, $archive, $password) = @_;
97
98 my $volid = PVE::Storage::vdisk_alloc($storage_conf, $storage, $vmid, 'subvol',
99 "subvol-$vmid-rootfs", $size);
100 push @{$cleanup->{volids}}, $volid;
101
102 my $private = PVE::Storage::path($storage_conf, $volid);
103 (-d $private) || die "unable to get container private dir '$private' - $!\n";
104
105 $conf->{'lxc.rootfs'} = $private;
106 $conf->{'pve.volid'} = $volid;
107
108 restore_and_configure($vmid, $archive, $private, $conf, $password);
109 }
110
111 # create a raw file, then loop mount
112 sub create_rootfs_dir_loop {
113 my ($cleanup, $storage_conf, $storage, $size, $vmid, $conf, $archive, $password) = @_;
114
115 my $volid = PVE::Storage::vdisk_alloc($storage_conf, $storage, $vmid, 'raw', "vm-$vmid-rootfs.raw", $size);
116 $conf->{'pve.disksize'} = $size/(1024*1024);
117
118 push @{$cleanup->{volids}}, $volid;
119
120 my $image_path = PVE::Storage::path($storage_conf, $volid);
121 $conf->{'lxc.rootfs'} = "loop:${image_path}";
122
123 my $cmd = ['mkfs.ext4', $image_path];
124 PVE::Tools::run_command($cmd);
125
126 print "allocated image: $image_path\n";
127
128 my $mountpoint;
129
130 my $loopdev;
131 eval {
132 my $parser = sub {
133 my $line = shift;
134 $loopdev = $line if $line =~m|^/dev/loop\d+$|;
135 };
136 PVE::Tools::run_command(['losetup', '--find', '--show', $image_path], outfunc => $parser);
137
138 my $tmp = "/var/lib/lxc/$vmid/rootfs";
139 File::Path::mkpath($tmp);
140 PVE::Tools::run_command(['mount', '-t', 'ext4', $loopdev, $tmp]);
141 $mountpoint = $tmp;
142
143 $conf->{'pve.volid'} = $volid;
144 restore_and_configure($vmid, $archive, $mountpoint, $conf, $password);
145 };
146 if (my $err = $@) {
147 if ($mountpoint) {
148 eval { PVE::Tools::run_command(['umount', '-d', $mountpoint]) };
149 warn $@ if $@;
150 } else {
151 eval { PVE::Tools::run_command(['losetup', '-d', $loopdev]) if $loopdev; };
152 warn $@ if $@;
153 }
154 die $err;
155 }
156
157 PVE::Tools::run_command(['umount', '-l', '-d', $mountpoint]);
158 }
159
160 # create a file, then mount with qemu-nbd
161 sub create_rootfs_dir_qemu {
162 my ($cleanup, $storage_conf, $storage, $size, $vmid, $conf, $archive, $password) = @_;
163
164 my $format = 'qcow2';
165
166 my $volid = PVE::Storage::vdisk_alloc($storage_conf, $storage, $vmid,
167 $format, "vm-$vmid-rootfs.$format", $size);
168
169 $conf->{'pve.disksize'} = $size/(1024*1024);
170
171 push @{$cleanup->{volids}}, $volid;
172
173 my $image_path = PVE::Storage::path($storage_conf, $volid);
174 $conf->{'lxc.rootfs'} = "nbd:${image_path}";
175
176 print "allocated image: $image_path\n";
177
178 my $mountpoint;
179
180 my $nbd_dev;
181 eval {
182 $nbd_dev = next_free_nbd_dev();
183 PVE::Tools::run_command(['qemu-nbd', '-c', $nbd_dev, $image_path]);
184
185 my $cmd = ['mkfs.ext4', $nbd_dev];
186 PVE::Tools::run_command($cmd);
187
188
189 my $tmp = "/var/lib/lxc/$vmid/rootfs";
190 File::Path::mkpath($tmp);
191 PVE::Tools::run_command(['mount', '-t', 'ext4', $nbd_dev, $tmp]);
192 $mountpoint = $tmp;
193
194 $conf->{'pve.volid'} = $volid;
195 restore_and_configure($vmid, $archive, $mountpoint, $conf, $password);
196 };
197 if (my $err = $@) {
198 if ($mountpoint) {
199 eval { PVE::Tools::run_command(['umount', $mountpoint]); };
200 warn $@ if $@;
201 }
202 if ($nbd_dev) {
203 eval { PVE::Tools::run_command(['qemu-nbd', '-d', $nbd_dev]); };
204 warn $@ if $@;
205 }
206 die $err;
207 }
208
209 PVE::Tools::run_command(['umount', $mountpoint]);
210 PVE::Tools::run_command(['qemu-nbd', '-d', $nbd_dev]);
211 }
212
213 sub create_rootfs {
214 my ($storage_conf, $storage, $disk_size_gb, $vmid, $conf, $archive, $password, $restore) = @_;
215
216 my $config_fn = PVE::LXC::config_file($vmid);
217 if (-f $config_fn) {
218 die "container exists" if !$restore; # just to be sure
219
220 my $old_conf = PVE::LXC::load_config($vmid);
221
222 if (!defined($disk_size_gb) && defined($old_conf->{'pve.disksize'})) {
223 $disk_size_gb = $old_conf->{'pve.disksize'};
224 }
225
226 # we only copy known settings to restored container
227 my $pve_conf = PVE::LXC::lxc_conf_to_pve($vmid, $old_conf);
228 foreach my $opt (qw(disk digest)) {
229 delete $pve_conf->{$opt};
230 }
231 update_lxc_config($vmid, $conf, 0, $pve_conf);
232
233 # destroy old container
234 PVE::LXC::destory_lxc_container($storage_conf, $vmid, $old_conf);
235
236 PVE::LXC::create_config($vmid, $conf);
237
238 } else {
239
240 PVE::LXC::create_config($vmid, $conf);
241 }
242
243 my $size = 4*1024*1024; # defaults to 4G
244
245 $size = int($disk_size_gb*1024) * 1024 if defined($disk_size_gb);
246
247 my $cleanup = { files => [], volids => [] };
248
249 eval {
250 my $scfg = PVE::Storage::storage_config($storage_conf, $storage);
251 if ($scfg->{type} eq 'dir' || $scfg->{type} eq 'nfs') {
252 if ($size > 0) {
253 create_rootfs_dir_loop($cleanup, $storage_conf, $storage, $size, $vmid, $conf, $archive, $password);
254 } else {
255 create_rootfs_dir($cleanup, $storage_conf, $storage, $vmid, $conf, $archive, $password);
256 }
257 } elsif ($scfg->{type} eq 'zfspool') {
258
259 create_rootfs_subvol($cleanup, $storage_conf, $storage, $size,
260 $vmid, $conf, $archive, $password);
261
262 } else {
263
264 die "unable to create containers on storage type '$scfg->{type}'\n";
265 }
266 };
267 if (my $err = $@) {
268 # cleanup
269 File::Path::rmtree($cleanup->{files});
270 foreach my $volid (@{$cleanup->{volids}}) {
271 eval { PVE::Storage::vdisk_free($storage_conf, $volid); };
272 warn $@ if $@;
273 }
274
275 PVE::LXC::destroy_config($vmid);
276
277 die $err;
278 }
279 }
280
281 1;