]>
git.proxmox.com Git - pve-container.git/blob - src/PVE/LXC/Create.pm
d6a6a0d0c7b84d2af8742b1ed82ecb0d1dea4e8d
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 # skip-old-files doesn't have anything to do with time (old/new), but is
40 # simply -k (annoyingly also called --keep-old-files) without the 'treat
41 # existing files as errors' part... iow. it's bsdtar's interpretation of -k
43 push @$cmd, '--skip-old-files';
44 push @$cmd, '--anchored';
45 push @$cmd, '--exclude' , './dev/*';
47 if ($archive eq '-') {
48 print "extracting archive from STDIN\n";
49 PVE
::Tools
::run_command
($cmd, input
=> "<&STDIN");
51 print "extracting archive '$archive'\n";
52 PVE
::Tools
::run_command
($cmd);
55 # determine file type of /usr/bin/file itself to get guests' architecture
56 $cmd = [@$userns_cmd, '/usr/bin/file', '-b', '-L', "$rootdir/usr/bin/file"];
57 PVE
::Tools
::run_command
($cmd, outfunc
=> sub {
58 shift =~ /^ELF (\d{2}-bit)/; # safely assumes x86 linux
60 $conf->{'arch'} = 'amd64'; # defaults to 64bit
61 if(defined($arch_str)) {
62 $conf->{'arch'} = 'i386' if $arch_str =~ /32/;
63 print "Detected container architecture: $conf->{'arch'}\n";
65 print "CT architecture detection failed, falling back to amd64.\n" .
66 "Edit the config in /etc/pve/nodes/{node}/lxc/{vmid}/config " .
67 "to set another architecture.\n";
72 sub tar_archive_search_conf
{
75 die "ERROR: file '$archive' does not exist\n" if ! -f
$archive;
77 my $pid = open(my $fh, '-|', 'tar', 'tf', $archive) ||
78 die "unable to open file '$archive'\n";
81 while (defined($file = <$fh>)) {
82 if ($file =~ m!^(\./etc/vzdump/(pct|vps)\.conf)$!) {
92 die "ERROR: archive contains no configuration file\n" if !$file;
101 my $conf_file = tar_archive_search_conf
($archive);
109 PVE
::Tools
::run_command
(['tar', '-xpOf', $archive, $conf_file, '--occurrence'], outfunc
=> $out);
114 if ($conf_file =~ m/pct\.conf/) {
116 $conf = PVE
::LXC
::parse_pct_config
("/lxc/0.conf" , $raw);
118 delete $conf->{snapshots
};
119 delete $conf->{template
}; # restored CT is never a template
121 if (defined($conf->{rootfs
})) {
122 my $rootinfo = PVE
::LXC
::parse_ct_mountpoint
($conf->{rootfs
});
123 $disksize = $rootinfo->{size
} if defined($rootinfo->{size
});
126 } elsif ($conf_file =~ m/vps\.conf/) {
128 ($conf, $disksize) = PVE
::VZDump
::ConvertOVZ
::convert_ovz
($raw);
132 die "internal error";
135 return wantarray ?
($conf, $disksize) : $conf;
138 sub restore_and_configure
{
139 my ($vmid, $archive, $rootdir, $conf, $password, $restore) = @_;
141 restore_archive
($archive, $rootdir, $conf);
144 my $lxc_setup = PVE
::LXC
::Setup-
>new($conf, $rootdir); # detect OS
146 PVE
::LXC
::write_config
($vmid, $conf); # safe config (after OS detection)
147 $lxc_setup->post_create_hook($password);
149 # restore: try to extract configuration from archive
151 my $pct_cfg_fn = "$rootdir/etc/vzdump/pct.conf";
152 my $ovz_cfg_fn = "$rootdir/etc/vzdump/vps.conf";
153 if (-f
$pct_cfg_fn) {
154 my $raw = PVE
::Tools
::file_get_contents
($pct_cfg_fn);
155 my $oldconf = PVE
::LXC
::parse_pct_config
("/lxc/$vmid.conf", $raw);
157 foreach my $key (keys %$oldconf) {
158 next if $key eq 'digest' || $key eq 'rootfs' || $key eq 'snapshots';
159 $conf->{$key} = $oldconf->{$key} if !defined($conf->{$key});
163 } elsif (-f
$ovz_cfg_fn) {
164 print "###########################################################\n";
165 print "Converting OpenVZ configuration to LXC.\n";
166 print "Please check the configuration and reconfigure the network.\n";
167 print "###########################################################\n";
169 my $lxc_setup = PVE
::LXC
::Setup-
>new($conf, $rootdir); # detect OS
170 $conf->{ostype
} = $lxc_setup->{conf
}->{ostype
};
171 my $raw = PVE
::Tools
::file_get_contents
($ovz_cfg_fn);
172 my $oldconf = PVE
::VZDump
::ConvertOVZ
::convert_ovz
($raw);
173 foreach my $key (keys %$oldconf) {
174 $conf->{$key} = $oldconf->{$key} if !defined($conf->{$key});
179 print "###########################################################\n";
180 print "Backup archive does not contain any configuration\n";
181 print "###########################################################\n";
187 my ($storage_cfg, $vmid, $conf, $archive, $password, $restore) = @_;
189 my $config_fn = PVE
::LXC
::config_file
($vmid);
191 die "container exists" if !$restore; # just to be sure
193 my $old_conf = PVE
::LXC
::load_config
($vmid);
195 # destroy old container volume
196 PVE
::LXC
::destroy_lxc_container
($storage_cfg, $vmid, $old_conf);
198 # do not copy all settings to restored container
199 foreach my $opt (qw(rootfs digest snapshots arch ostype)) {
200 delete $old_conf->{$opt};
202 foreach my $opt (keys %$old_conf) {
203 delete $old_conf->{$opt} if $opt =~ m/^mp\d+$/;
206 PVE
::LXC
::update_pct_config
($vmid, $conf, 0, $old_conf);
208 PVE
::LXC
::create_config
($vmid, $conf);
212 PVE
::LXC
::create_config
($vmid, $conf);
216 my $rootdir = PVE
::LXC
::mount_all
($vmid, $storage_cfg, $conf);
217 restore_and_configure
($vmid, $archive, $rootdir, $conf, $password, $restore);
221 PVE
::LXC
::umount_all
($vmid, $storage_cfg, $conf, 1);
223 PVE
::LXC
::umount_all
($vmid, $storage_cfg, $conf, 0);
226 PVE
::Storage
::deactivate_volumes
($storage_cfg, PVE
::LXC
::get_vm_volumes
($conf));