]>
git.proxmox.com Git - pve-container.git/blob - src/PVE/LXC/Create.pm
1 package PVE
::LXC
::Create
;
12 use PVE
::VZDump
::ConvertOVZ
;
14 sub next_free_nbd_dev
{
16 for(my $i = 0;;$i++) {
17 my $dev = "/dev/nbd$i";
19 next if -f
"/sys/block/nbd$i/pid"; # busy
22 die "unable to find free nbd device\n";
26 my ($archive, $rootdir, $conf) = @_;
30 # we always use the same mapping: 'b:0:100000:65536'
31 # if ($conf->{'lxc.id_map'}) {
32 # $userns_cmd = ['lxc-usernsexec', '-m', 'b:0:100000:65536', '--'];
33 # PVE::Tools::run_command(['chown', '-R', '100000:100000', $rootdir]);
36 my $cmd = [@$userns_cmd, 'tar', 'xpf', $archive, '--numeric-owner', '--totals',
37 '--sparse', '-C', $rootdir];
39 push @$cmd, '--anchored';
40 push @$cmd, '--exclude' , './dev/*';
42 if ($archive eq '-') {
43 print "extracting archive from STDIN\n";
44 PVE
::Tools
::run_command
($cmd, input
=> "<&STDIN");
46 print "extracting archive '$archive'\n";
47 PVE
::Tools
::run_command
($cmd);
50 # determine file type of /usr/bin/file itself to get guests' architecture
51 $cmd = [@$userns_cmd, '/usr/bin/file', '-b', '-L', "$rootdir/usr/bin/file"];
52 PVE
::Tools
::run_command
($cmd, outfunc
=> sub {
53 shift =~ /^ELF (\d{2}-bit)/; # safely assumes x86 linux
55 $conf->{'arch'} = 'amd64'; # defaults to 64bit
56 if(defined($arch_str)) {
57 $conf->{'arch'} = 'i386' if $arch_str =~ /32/;
58 print "Detected container architecture: $conf->{'arch'}\n";
60 print "CT architecture detection failed, falling back to amd64.\n" .
61 "Edit the config in /etc/pve/nodes/{node}/lxc/{vmid}/config " .
62 "to set another architecture.\n";
67 sub tar_archive_search_conf
{
70 die "ERROR: file '$archive' does not exist\n" if ! -f
$archive;
72 my $pid = open(my $fh, '-|', 'tar', 'tf', $archive) ||
73 die "unable to open file '$archive'\n";
76 while (defined($file = <$fh>)) {
77 if ($file =~ m!^(\./etc/vzdump/(pct|vps)\.conf)$!) {
87 die "ERROR: archive contains no configuration file\n" if !$file;
96 my $conf_file = tar_archive_search_conf
($archive);
104 PVE
::Tools
::run_command
(['tar', '-xpOf', $archive, $conf_file, '--occurrence'], outfunc
=> $out);
109 if ($conf_file =~ m/pct\.conf/) {
111 $conf = PVE
::LXC
::parse_pct_config
("/lxc/0.conf" , $raw);
113 delete $conf->{snapshots
};
114 delete $conf->{template
}; # restored CT is never a template
116 if (defined($conf->{rootfs
})) {
117 my $rootinfo = PVE
::LXC
::parse_ct_mountpoint
($conf->{rootfs
});
118 $disksize = $rootinfo->{size
} if defined($rootinfo->{size
});
121 } elsif ($conf_file =~ m/vps\.conf/) {
123 ($conf, $disksize) = PVE
::VZDump
::ConvertOVZ
::convert_ovz
($raw);
127 die "internal error";
130 return wantarray ?
($conf, $disksize) : $conf;
133 sub restore_and_configure
{
134 my ($vmid, $archive, $rootdir, $conf, $password, $restore) = @_;
136 restore_archive
($archive, $rootdir, $conf);
139 my $lxc_setup = PVE
::LXC
::Setup-
>new($conf, $rootdir); # detect OS
141 PVE
::LXC
::write_config
($vmid, $conf); # safe config (after OS detection)
142 $lxc_setup->post_create_hook($password);
144 # restore: try to extract configuration from archive
146 my $pct_cfg_fn = "$rootdir/etc/vzdump/pct.conf";
147 my $ovz_cfg_fn = "$rootdir/etc/vzdump/vps.conf";
148 if (-f
$pct_cfg_fn) {
149 my $raw = PVE
::Tools
::file_get_contents
($pct_cfg_fn);
150 my $oldconf = PVE
::LXC
::parse_pct_config
("/lxc/$vmid.conf", $raw);
152 foreach my $key (keys %$oldconf) {
153 next if $key eq 'digest' || $key eq 'rootfs' || $key eq 'snapshots';
154 $conf->{$key} = $oldconf->{$key} if !defined($conf->{$key});
157 } elsif (-f
$ovz_cfg_fn) {
158 print "###########################################################\n";
159 print "Converting OpenVZ configuration to LXC.\n";
160 print "Please check the configuration and reconfigure the network.\n";
161 print "###########################################################\n";
163 my $raw = PVE
::Tools
::file_get_contents
($ovz_cfg_fn);
164 my $oldconf = PVE
::VZDump
::ConvertOVZ
::convert_ovz
($raw);
165 foreach my $key (keys %$oldconf) {
166 $conf->{$key} = $oldconf->{$key} if !defined($conf->{$key});
170 print "###########################################################\n";
171 print "Backup archive does not contain any configuration\n";
172 print "###########################################################\n";
178 my ($storage_cfg, $storage, $volid, $vmid, $conf, $archive, $password, $restore) = @_;
180 my $config_fn = PVE
::LXC
::config_file
($vmid);
182 die "container exists" if !$restore; # just to be sure
184 my $old_conf = PVE
::LXC
::load_config
($vmid);
186 # destroy old container volume
187 PVE
::LXC
::destory_lxc_container
($storage_cfg, $vmid, $old_conf);
189 # do not copy all settings to restored container
190 foreach my $opt (qw(rootfs digest snapshots)) {
191 delete $old_conf->{$opt};
193 PVE
::LXC
::update_pct_config
($vmid, $conf, 0, $old_conf);
195 PVE
::LXC
::create_config
($vmid, $conf);
199 PVE
::LXC
::create_config
($vmid, $conf);
202 my $image_path = PVE
::Storage
::path
($storage_cfg, $volid);
203 my $mountpoint_path = "/var/lib/lxc/$vmid/rootfs";
204 my $mountpoint = { volume
=> $volid, mp
=> "/" };
207 PVE
::Storage
::activate_volumes
($storage_cfg, [$volid]);
208 my $loopdevs = PVE
::LXC
::attach_loops
($storage_cfg, [$volid]);
210 if (!-d
$image_path) {
211 my $cmd = ['mkfs.ext4', $image_path];
212 PVE
::Tools
::run_command
($cmd);
215 PVE
::LXC
::mountpoint_mount
($mountpoint, $mountpoint_path, $storage_cfg, $loopdevs);
217 restore_and_configure
($vmid, $archive, $mountpoint_path, $conf, $password, $restore);
221 PVE
::LXC
::dettach_loops
($storage_cfg, [$volid]);
222 PVE
::Storage
::deactivate_volumes
($storage_cfg, [$volid]);
228 PVE
::Tools
::run_command
(['umount', '-l', '-d', $mountpoint_path]);
229 PVE
::LXC
::dettach_loops
($storage_cfg, [$volid]);
230 PVE
::Storage
::deactivate_volumes
($storage_cfg, [$volid]);