]>
git.proxmox.com Git - pve-container.git/blob - src/PVE/LXC/Create.pm
b146ea7e71025b3d60fd3aabfa489a021a3f13d9
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) = @_;
28 my ($id_map, $rootuid, $rootgid) = PVE
::LXC
::parse_id_maps
($conf);
29 my $userns_cmd = PVE
::LXC
::userns_command
($id_map);
31 PVE
::Tools
::run_command
(['chown', '-R', "$rootuid:$rootgid", $rootdir]);
34 my $cmd = [@$userns_cmd, 'tar', 'xpf', $archive, '--totals',
35 @$PVE::LXC
::COMMON_TAR_FLAGS
,
38 # skip-old-files doesn't have anything to do with time (old/new), but is
39 # simply -k (annoyingly also called --keep-old-files) without the 'treat
40 # existing files as errors' part... iow. it's bsdtar's interpretation of -k
42 push @$cmd, '--skip-old-files';
43 push @$cmd, '--anchored';
44 push @$cmd, '--exclude' , './dev/*';
46 if ($archive eq '-') {
47 print "extracting archive from STDIN\n";
48 PVE
::Tools
::run_command
($cmd, input
=> "<&STDIN");
50 print "extracting archive '$archive'\n";
51 PVE
::Tools
::run_command
($cmd);
54 # determine file type of /usr/bin/file itself to get guests' architecture
55 $cmd = [@$userns_cmd, '/usr/bin/file', '-b', '-L', "$rootdir/usr/bin/file"];
56 PVE
::Tools
::run_command
($cmd, outfunc
=> sub {
57 shift =~ /^ELF (\d{2}-bit)/; # safely assumes x86 linux
59 $conf->{'arch'} = 'amd64'; # defaults to 64bit
60 if(defined($arch_str)) {
61 $conf->{'arch'} = 'i386' if $arch_str =~ /32/;
62 print "Detected container architecture: $conf->{'arch'}\n";
64 print "CT architecture detection failed, falling back to amd64.\n" .
65 "Edit the config in /etc/pve/nodes/{node}/lxc/{vmid}/config " .
66 "to set another architecture.\n";
71 sub tar_archive_search_conf
{
74 die "ERROR: file '$archive' does not exist\n" if ! -f
$archive;
76 my $pid = open(my $fh, '-|', 'tar', 'tf', $archive) ||
77 die "unable to open file '$archive'\n";
80 while (defined($file = <$fh>)) {
81 if ($file =~ m!^(\./etc/vzdump/(pct|vps)\.conf)$!) {
91 die "ERROR: archive contains no configuration file\n" if !$file;
100 my $conf_file = tar_archive_search_conf
($archive);
108 PVE
::Tools
::run_command
(['tar', '-xpOf', $archive, $conf_file, '--occurrence'], outfunc
=> $out);
113 if ($conf_file =~ m/pct\.conf/) {
115 $conf = PVE
::LXC
::parse_pct_config
("/lxc/0.conf" , $raw);
117 delete $conf->{snapshots
};
118 delete $conf->{template
}; # restored CT is never a template
120 if (defined($conf->{rootfs
})) {
121 my $rootinfo = PVE
::LXC
::parse_ct_mountpoint
($conf->{rootfs
});
122 $disksize = $rootinfo->{size
} if defined($rootinfo->{size
});
125 } elsif ($conf_file =~ m/vps\.conf/) {
127 ($conf, $disksize) = PVE
::VZDump
::ConvertOVZ
::convert_ovz
($raw);
131 die "internal error";
134 return wantarray ?
($conf, $disksize) : $conf;
137 sub restore_and_configure
{
138 my ($vmid, $archive, $rootdir, $conf, $password, $restore) = @_;
140 restore_archive
($archive, $rootdir, $conf);
143 my $lxc_setup = PVE
::LXC
::Setup-
>new($conf, $rootdir); # detect OS
145 PVE
::LXC
::write_config
($vmid, $conf); # safe config (after OS detection)
146 $lxc_setup->post_create_hook($password);
148 # restore: try to extract configuration from archive
150 my $pct_cfg_fn = "$rootdir/etc/vzdump/pct.conf";
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 $conf->{$key} = $oldconf->{$key} if !defined($conf->{$key});
162 } elsif (-f
$ovz_cfg_fn) {
163 print "###########################################################\n";
164 print "Converting OpenVZ configuration to LXC.\n";
165 print "Please check the configuration and reconfigure the network.\n";
166 print "###########################################################\n";
168 my $lxc_setup = PVE
::LXC
::Setup-
>new($conf, $rootdir); # detect OS
169 $conf->{ostype
} = $lxc_setup->{conf
}->{ostype
};
170 my $raw = PVE
::Tools
::file_get_contents
($ovz_cfg_fn);
171 my $oldconf = PVE
::VZDump
::ConvertOVZ
::convert_ovz
($raw);
172 foreach my $key (keys %$oldconf) {
173 $conf->{$key} = $oldconf->{$key} if !defined($conf->{$key});
178 print "###########################################################\n";
179 print "Backup archive does not contain any configuration\n";
180 print "###########################################################\n";
186 my ($storage_cfg, $vmid, $conf, $archive, $password, $restore) = @_;
188 my $config_fn = PVE
::LXC
::config_file
($vmid);
190 die "container exists" if !$restore; # just to be sure
192 my $old_conf = PVE
::LXC
::load_config
($vmid);
194 # destroy old container volume
195 PVE
::LXC
::destroy_lxc_container
($storage_cfg, $vmid, $old_conf);
197 # do not copy all settings to restored container
198 foreach my $opt (qw(rootfs digest snapshots arch ostype unprivileged)) {
199 delete $old_conf->{$opt};
201 foreach my $opt (keys %$old_conf) {
202 delete $old_conf->{$opt} if $opt =~ m/^mp\d+$/;
205 PVE
::LXC
::update_pct_config
($vmid, $conf, 0, $old_conf);
207 PVE
::LXC
::create_config
($vmid, $conf);
211 PVE
::LXC
::create_config
($vmid, $conf);
215 my $rootdir = PVE
::LXC
::mount_all
($vmid, $storage_cfg, $conf);
216 restore_and_configure
($vmid, $archive, $rootdir, $conf, $password, $restore);
220 PVE
::LXC
::umount_all
($vmid, $storage_cfg, $conf, 1);
222 PVE
::LXC
::umount_all
($vmid, $storage_cfg, $conf, 0);
225 PVE
::Storage
::deactivate_volumes
($storage_cfg, PVE
::LXC
::get_vm_volumes
($conf));