]>
git.proxmox.com Git - pve-container.git/blob - src/PVE/LXC/Create.pm
1 package PVE
::LXC
::Create
;
12 use PVE
::VZDump
::ConvertOVZ
;
15 sub next_free_nbd_dev
{
17 for(my $i = 0;;$i++) {
18 my $dev = "/dev/nbd$i";
20 next if -f
"/sys/block/nbd$i/pid"; # busy
23 die "unable to find free nbd device\n";
27 my ($archive, $rootdir, $conf) = @_;
29 my ($id_map, $rootuid, $rootgid) = PVE
::LXC
::parse_id_maps
($conf);
30 my $userns_cmd = PVE
::LXC
::userns_command
($id_map);
32 my $cmd = [@$userns_cmd, 'tar', 'xpf', $archive, '--totals',
33 @$PVE::LXC
::COMMON_TAR_FLAGS
,
36 # skip-old-files doesn't have anything to do with time (old/new), but is
37 # simply -k (annoyingly also called --keep-old-files) without the 'treat
38 # existing files as errors' part... iow. it's bsdtar's interpretation of -k
40 push @$cmd, '--skip-old-files';
41 push @$cmd, '--anchored';
42 push @$cmd, '--exclude' , './dev/*';
44 if ($archive eq '-') {
45 print "extracting archive from STDIN\n";
46 PVE
::Tools
::run_command
($cmd, input
=> "<&STDIN");
48 print "extracting archive '$archive'\n";
49 PVE
::Tools
::run_command
($cmd);
52 # determine file type of /usr/bin/file itself to get guests' architecture
53 $cmd = [@$userns_cmd, '/usr/bin/file', '-b', '-L', "$rootdir/usr/bin/file"];
54 PVE
::Tools
::run_command
($cmd, outfunc
=> sub {
55 shift =~ /^ELF (\d{2}-bit)/; # safely assumes x86 linux
57 $conf->{'arch'} = 'amd64'; # defaults to 64bit
58 if(defined($arch_str)) {
59 $conf->{'arch'} = 'i386' if $arch_str =~ /32/;
60 print "Detected container architecture: $conf->{'arch'}\n";
62 print "CT architecture detection failed, falling back to amd64.\n" .
63 "Edit the config in /etc/pve/nodes/{node}/lxc/{vmid}/config " .
64 "to set another architecture.\n";
69 sub tar_archive_search_conf
{
72 die "ERROR: file '$archive' does not exist\n" if ! -f
$archive;
74 my $pid = open(my $fh, '-|', 'tar', 'tf', $archive) ||
75 die "unable to open file '$archive'\n";
78 while (defined($file = <$fh>)) {
79 if ($file =~ m!^(\./etc/vzdump/(pct|vps)\.conf)$!) {
89 die "ERROR: archive contains no configuration file\n" if !$file;
98 my $conf_file = tar_archive_search_conf
($archive);
106 PVE
::Tools
::run_command
(['tar', '-xpOf', $archive, $conf_file, '--occurrence'], outfunc
=> $out);
111 if ($conf_file =~ m/pct\.conf/) {
113 $conf = PVE
::LXC
::parse_pct_config
("/lxc/0.conf" , $raw);
115 delete $conf->{snapshots
};
116 delete $conf->{template
}; # restored CT is never a template
118 if (defined($conf->{rootfs
})) {
119 my $rootinfo = PVE
::LXC
::parse_ct_mountpoint
($conf->{rootfs
});
120 $disksize = $rootinfo->{size
} if defined($rootinfo->{size
});
123 } elsif ($conf_file =~ m/vps\.conf/) {
125 ($conf, $disksize) = PVE
::VZDump
::ConvertOVZ
::convert_ovz
($raw);
129 die "internal error";
132 return wantarray ?
($conf, $disksize) : $conf;
135 sub restore_and_configure
{
136 my ($vmid, $archive, $rootdir, $conf, $password, $restore, $no_unpack_error) = @_;
138 eval { restore_archive
($archive, $rootdir, $conf) };
139 die $@ if $@ && !$no_unpack_error;
142 my $lxc_setup = PVE
::LXC
::Setup-
>new($conf, $rootdir); # detect OS
144 PVE
::LXC
::write_config
($vmid, $conf); # safe config (after OS detection)
145 $lxc_setup->post_create_hook($password);
147 # restore: try to extract configuration from archive
149 my $pct_cfg_fn = "$rootdir/etc/vzdump/pct.conf";
150 my $pct_fwcfg_fn = "$rootdir/etc/vzdump/pct.fw";
151 my $ovz_cfg_fn = "$rootdir/etc/vzdump/vps.conf";
152 if (-f
$pct_cfg_fn) {
153 my $raw = PVE
::Tools
::file_get_contents
($pct_cfg_fn);
154 my $oldconf = PVE
::LXC
::parse_pct_config
("/lxc/$vmid.conf", $raw);
156 foreach my $key (keys %$oldconf) {
157 next if $key eq 'digest' || $key eq 'rootfs' || $key eq 'snapshots' || $key eq 'unprivileged';
158 next if $key =~ /^mp\d+$/; # don't recover mountpoints
159 $conf->{$key} = $oldconf->{$key} if !defined($conf->{$key});
163 if (-f
$pct_fwcfg_fn) {
164 PVE
::Tools
::file_copy
($pct_fwcfg_fn, "/etc/pve/firewall/$vmid.fw");
165 unlink $pct_fwcfg_fn;
168 } elsif (-f
$ovz_cfg_fn) {
169 print "###########################################################\n";
170 print "Converting OpenVZ configuration to LXC.\n";
171 print "Please check the configuration and reconfigure the network.\n";
172 print "###########################################################\n";
174 my $lxc_setup = PVE
::LXC
::Setup-
>new($conf, $rootdir); # detect OS
175 $conf->{ostype
} = $lxc_setup->{conf
}->{ostype
};
176 my $raw = PVE
::Tools
::file_get_contents
($ovz_cfg_fn);
177 my $oldconf = PVE
::VZDump
::ConvertOVZ
::convert_ovz
($raw);
178 foreach my $key (keys %$oldconf) {
179 $conf->{$key} = $oldconf->{$key} if !defined($conf->{$key});
184 print "###########################################################\n";
185 print "Backup archive does not contain any configuration\n";
186 print "###########################################################\n";
192 my ($storage_cfg, $vmid, $conf, $archive, $password, $restore, $no_unpack_error) = @_;
194 my $config_fn = PVE
::LXC
::config_file
($vmid);
196 die "container exists" if !$restore; # just to be sure
198 my $old_conf = PVE
::LXC
::load_config
($vmid);
200 # destroy old container volume
201 PVE
::LXC
::destroy_lxc_container
($storage_cfg, $vmid, $old_conf);
203 # do not copy all settings to restored container
204 foreach my $opt (qw(rootfs digest snapshots arch ostype unprivileged)) {
205 delete $old_conf->{$opt};
207 foreach my $opt (keys %$old_conf) {
208 delete $old_conf->{$opt} if $opt =~ m/^mp\d+$/;
211 PVE
::LXC
::update_pct_config
($vmid, $conf, 0, $old_conf);
213 PVE
::LXC
::create_config
($vmid, $conf);
217 PVE
::LXC
::create_config
($vmid, $conf);
221 my $rootdir = PVE
::LXC
::mount_all
($vmid, $storage_cfg, $conf);
222 restore_and_configure
($vmid, $archive, $rootdir, $conf, $password, $restore, $no_unpack_error);
225 PVE
::LXC
::umount_all
($vmid, $storage_cfg, $conf, $err ?
1 : 0);
226 PVE
::Storage
::deactivate_volumes
($storage_cfg, PVE
::LXC
::get_vm_volumes
($conf));