]>
git.proxmox.com Git - pve-container.git/blob - src/PVE/LXCCreate.pm
1 package PVE
::LXCCreate
;
13 sub next_free_nbd_dev
{
15 for(my $i = 0;;$i++) {
16 my $dev = "/dev/nbd$i";
18 next if -f
"/sys/block/nbd$i/pid"; # busy
21 die "unable to find free nbd device\n";
25 my ($archive, $rootdir, $conf) = @_;
27 # we always use the same mapping: 'b:0:100000:65536'
30 if ($conf->{'lxc.id_map'}) {
31 $userns_cmd = ['lxc-usernsexec', '-m', 'b:0:100000:65536', '--'];
38 if ($conf->{'lxc.id_map'}) {
39 PVE
::Tools
::run_command
(['chown', '-R', '100000:100000', $rootdir]);
42 $cmd = [@$userns_cmd, 'tar', 'xpf', $archive, '--numeric-owner', '--totals',
43 '--sparse', '-C', $rootdir];
45 push @$cmd, '--anchored';
46 push @$cmd, '--exclude' , './dev/*';
48 if ($archive eq '-') {
49 print "extracting archive from STDIN\n";
50 PVE
::Tools
::run_command
($cmd, input
=> "<&STDIN");
52 print "extracting archive '$archive'\n";
53 PVE
::Tools
::run_command
($cmd);
56 # is this really required? what for?
57 #$cmd = [@$userns_cmd, 'mkdir', '-p', "$rootdir/dev/pts"];
58 #PVE::Tools::run_command($cmd);
60 # template/OS specific configuration
61 $conf->{'lxc.arch'} = 'i386'; #fixme: || x86_64
64 sub restore_and_configure
{
65 my ($vmid, $archive, $rootdir, $conf, $password) = @_;
67 restore_archive
($archive, $rootdir, $conf);
69 PVE
::LXC
::write_config
($vmid, $conf);
71 my $lxc_setup = PVE
::LXCSetup-
>new($conf, $rootdir); # detect OS
73 PVE
::LXC
::write_config
($vmid, $conf); # safe config (after OS detection)
75 $lxc_setup->post_create_hook($password);
78 # directly use a storage directory
79 sub create_rootfs_dir
{
80 my ($cleanup, $storage_conf, $storage, $vmid, $conf, $archive, $password) = @_;
82 # note: there is no size limit
83 $conf->{'pve.disksize'} = 0;
85 my $private = PVE
::Storage
::get_private_dir
($storage_conf, $storage, $vmid);
86 mkdir($private) || die "unable to create container private dir '$private' - $!\n";
88 push @{$cleanup->{files
}}, $private;
89 $conf->{'lxc.rootfs'} = $private;
91 restore_and_configure
($vmid, $archive, $private, $conf, $password);
94 # use new subvolume API
95 sub create_rootfs_subvol
{
96 my ($cleanup, $storage_conf, $storage, $size, $vmid, $conf, $archive, $password) = @_;
98 my $volid = PVE
::Storage
::vdisk_alloc
($storage_conf, $storage, $vmid, 'subvol',
99 "subvol-$vmid-rootfs", $size);
100 push @{$cleanup->{volids
}}, $volid;
102 my $private = PVE
::Storage
::path
($storage_conf, $volid);
103 (-d
$private) || die "unable to get container private dir '$private' - $!\n";
105 $conf->{'lxc.rootfs'} = $private;
106 $conf->{'pve.volid'} = $volid;
108 restore_and_configure
($vmid, $archive, $private, $conf, $password);
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) = @_;
115 my $volid = PVE
::Storage
::vdisk_alloc
($storage_conf, $storage, $vmid, 'raw', "vm-$vmid-rootfs.raw", $size);
116 $conf->{'pve.disksize'} = $size/(1024*1024);
118 push @{$cleanup->{volids
}}, $volid;
120 my $image_path = PVE
::Storage
::path
($storage_conf, $volid);
121 $conf->{'lxc.rootfs'} = "loop:${image_path}";
123 my $cmd = ['mkfs.ext4', $image_path];
124 PVE
::Tools
::run_command
($cmd);
126 print "allocated image: $image_path\n";
134 $loopdev = $line if $line =~m
|^/dev/loop\d
+$|;
136 PVE
::Tools
::run_command
(['losetup', '--find', '--show', $image_path], outfunc
=> $parser);
138 my $tmp = "/var/lib/lxc/$vmid/rootfs";
139 File
::Path
::mkpath
($tmp);
140 PVE
::Tools
::run_command
(['mount', '-t', 'ext4', $loopdev, $tmp]);
143 $conf->{'pve.volid'} = $volid;
144 restore_and_configure
($vmid, $archive, $mountpoint, $conf, $password);
148 eval { PVE
::Tools
::run_command
(['umount', '-d', $mountpoint]) };
151 eval { PVE
::Tools
::run_command
(['losetup', '-d', $loopdev]) if $loopdev; };
157 PVE
::Tools
::run_command
(['umount', '-l', '-d', $mountpoint]);
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) = @_;
164 my $format = 'qcow2';
166 my $volid = PVE
::Storage
::vdisk_alloc
($storage_conf, $storage, $vmid,
167 $format, "vm-$vmid-rootfs.$format", $size);
169 $conf->{'pve.disksize'} = $size/(1024*1024);
171 push @{$cleanup->{volids
}}, $volid;
173 my $image_path = PVE
::Storage
::path
($storage_conf, $volid);
174 $conf->{'lxc.rootfs'} = "nbd:${image_path}";
176 print "allocated image: $image_path\n";
182 $nbd_dev = next_free_nbd_dev
();
183 PVE
::Tools
::run_command
(['qemu-nbd', '-c', $nbd_dev, $image_path]);
185 my $cmd = ['mkfs.ext4', $nbd_dev];
186 PVE
::Tools
::run_command
($cmd);
189 my $tmp = "/var/lib/lxc/$vmid/rootfs";
190 File
::Path
::mkpath
($tmp);
191 PVE
::Tools
::run_command
(['mount', '-t', 'ext4', $nbd_dev, $tmp]);
194 $conf->{'pve.volid'} = $volid;
195 restore_and_configure
($vmid, $archive, $mountpoint, $conf, $password);
199 eval { PVE
::Tools
::run_command
(['umount', $mountpoint]); };
203 eval { PVE
::Tools
::run_command
(['qemu-nbd', '-d', $nbd_dev]); };
209 PVE
::Tools
::run_command
(['umount', $mountpoint]);
210 PVE
::Tools
::run_command
(['qemu-nbd', '-d', $nbd_dev]);
214 my ($storage_conf, $storage, $disk_size_gb, $vmid, $conf, $archive, $password, $restore) = @_;
216 my $config_fn = PVE
::LXC
::config_file
($vmid);
218 die "container exists" if !$restore; # just to be sure
220 my $old_conf = PVE
::LXC
::load_config
($vmid);
222 if (!defined($disk_size_gb) && defined($old_conf->{'pve.disksize'})) {
223 $disk_size_gb = $old_conf->{'pve.disksize'};
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};
231 update_lxc_config
($vmid, $conf, 0, $pve_conf);
233 # destroy old container
234 PVE
::LXC
::destory_lxc_container
($storage_conf, $vmid, $old_conf);
236 PVE
::LXC
::create_config
($vmid, $conf);
240 PVE
::LXC
::create_config
($vmid, $conf);
243 my $size = 4*1024*1024; # defaults to 4G
245 $size = int($disk_size_gb*1024) * 1024 if defined($disk_size_gb);
247 my $cleanup = { files
=> [], volids
=> [] };
250 my $scfg = PVE
::Storage
::storage_config
($storage_conf, $storage);
251 if ($scfg->{type
} eq 'dir' || $scfg->{type
} eq 'nfs') {
253 create_rootfs_dir_loop
($cleanup, $storage_conf, $storage, $size, $vmid, $conf, $archive, $password);
255 create_rootfs_dir
($cleanup, $storage_conf, $storage, $vmid, $conf, $archive, $password);
257 } elsif ($scfg->{type
} eq 'zfspool') {
259 create_rootfs_subvol
($cleanup, $storage_conf, $storage, $size,
260 $vmid, $conf, $archive, $password);
264 die "unable to create containers on storage type '$scfg->{type}'\n";
269 File
::Path
::rmtree
($cleanup->{files
});
270 foreach my $volid (@{$cleanup->{volids
}}) {
271 eval { PVE
::Storage
::vdisk_free
($storage_conf, $volid); };
275 PVE
::LXC
::destroy_config
($vmid);