1 package PVE
::LXC
::Create
;
12 use PVE
::VZDump
::ConvertOVZ
;
16 sub detect_architecture
{
19 # see https://en.wikipedia.org/wiki/Executable_and_Linkable_Format
21 my $supported_elf_machine = {
28 my $elf_fn = '/bin/sh'; # '/bin/sh' is POSIX mandatory
29 my $detect_arch = sub {
30 # chroot avoids a problem where we check the binary of the host system
31 # if $elf_fn is an absolut symlink (e.g. $rootdir/bin/sh -> /bin/bash)
32 chroot($rootdir) or die "chroot '$rootdir' failed: $!\n";
33 chdir('/') or die "failed to change to root directory\n";
35 open(my $fh, "<", $elf_fn) or die "open '$elf_fn' failed: $!\n";
38 my $length = read($fh, my $data, 20) or die "read failed: $!\n";
40 # 4 bytes ELF magic number and 1 byte ELF class, padding, machine
41 my ($magic, $class, undef, $machine) = unpack("A4CA12n", $data);
43 die "'$elf_fn' does not resolve to an ELF!\n"
44 if (!defined($class) || !defined($magic) || $magic ne "\177ELF");
46 my $arch = $supported_elf_machine->{$machine};
47 die "'$elf_fn' has unknown ELF machine '$machine'!\n"
53 my $arch = eval { PVE
::Tools
::run_fork_with_timeout
(5, $detect_arch) };
56 print "Architecture detection failed: $err\nFalling back to amd64.\n" .
57 "Use `pct set VMID --arch ARCH` to change.\n";
59 print "Detected container architecture: $arch\n";
66 my ($archive, $rootdir, $conf, $no_unpack_error, $bwlimit) = @_;
68 my ($id_map, $rootuid, $rootgid) = PVE
::LXC
::parse_id_maps
($conf);
69 my $userns_cmd = PVE
::LXC
::userns_command
($id_map);
72 my $tar_input = '<&STDIN';
74 if ($archive ne '-') {
75 # GNU tar refuses to autodetect this... *sigh*
76 my %compression_map = (
82 if ($archive =~ /\.tar(\.[^.]+)?$/) {
84 die "unrecognized compression format: $1\n" if (!defined($compression_map{$1}));
85 @compression_opt = $compression_map{$1};
88 die "file does not look like a template archive: $archive\n";
90 sysopen($archive_fh, $archive, O_RDONLY
)
91 or die "failed to open '$archive': $!\n";
92 my $flags = $archive_fh->fcntl(Fcntl
::F_GETFD
(), 0);
93 $archive_fh->fcntl(Fcntl
::F_SETFD
(), $flags & ~(Fcntl
::FD_CLOEXEC
()));
94 $tar_input = '<&'.fileno($archive_fh);
97 my $cmd = [@$userns_cmd, 'tar', 'xpf', '-', @compression_opt, '--totals',
98 @PVE::Storage
::Plugin
::COMMON_TAR_FLAGS
,
101 # skip-old-files doesn't have anything to do with time (old/new), but is
102 # simply -k (annoyingly also called --keep-old-files) without the 'treat
103 # existing files as errors' part... iow. it's bsdtar's interpretation of -k
105 push @$cmd, '--skip-old-files';
106 push @$cmd, '--anchored';
107 push @$cmd, '--exclude' , './dev/*';
109 if (defined($bwlimit)) {
110 $cmd = [ ['cstream', '-t', $bwlimit*1024], $cmd ];
113 if ($archive eq '-') {
114 print "extracting archive from STDIN\n";
116 print "extracting archive '$archive'\n";
118 eval { PVE
::Tools
::run_command
($cmd, input
=> $tar_input); };
120 close($archive_fh) if defined $archive_fh;
121 die $err if $err && !$no_unpack_error;
123 # if arch is set, we do not try to autodetect it
124 return if defined($conf->{arch
});
126 $conf->{arch
} = detect_architecture
($rootdir);
132 my ($raw, $conf_file) = PVE
::Storage
::extract_vzdump_config_tar
($archive, qr!(\./etc/vzdump/(pct|vps)\.conf)$!);
136 if ($conf_file =~ m/pct\.conf/) {
138 $conf = PVE
::LXC
::Config
::parse_pct_config
("/lxc/0.conf" , $raw);
140 delete $conf->{snapshots
};
142 PVE
::LXC
::Config-
>foreach_mountpoint($conf, sub {
143 my ($ms, $mountpoint) = @_;
144 $mp_param->{$ms} = $conf->{$ms};
147 } elsif ($conf_file =~ m/vps\.conf/) {
149 ($conf, $mp_param) = PVE
::VZDump
::ConvertOVZ
::convert_ovz
($raw);
153 die "internal error";
156 return wantarray ?
($conf, $mp_param) : $conf;
159 sub restore_configuration
{
160 my ($vmid, $rootdir, $conf, $restricted, $unique) = @_;
162 # restore: try to extract configuration from archive
164 my $pct_cfg_fn = "$rootdir/etc/vzdump/pct.conf";
165 my $pct_fwcfg_fn = "$rootdir/etc/vzdump/pct.fw";
166 my $ovz_cfg_fn = "$rootdir/etc/vzdump/vps.conf";
167 if (-f
$pct_cfg_fn) {
168 my $raw = PVE
::Tools
::file_get_contents
($pct_cfg_fn);
169 my $oldconf = PVE
::LXC
::Config
::parse_pct_config
("/lxc/$vmid.conf", $raw);
171 foreach my $key (keys %$oldconf) {
172 next if $key eq 'digest' || $key eq 'rootfs' || $key eq 'snapshots' || $key eq 'unprivileged' || $key eq 'parent';
173 next if $key =~ /^mp\d+$/; # don't recover mountpoints
174 next if $key =~ /^unused\d+$/; # don't recover unused disks
175 # we know if it was a template in the restore API call and check if the target
176 # storage supports creating a template there
177 next if $key =~ /^template$/;
178 if ($restricted && $key eq 'lxc') {
179 warn "skipping custom lxc options, restore manually as root:\n";
180 warn "--------------------------------\n";
181 my $lxc_list = $oldconf->{'lxc'};
182 foreach my $lxc_opt (@$lxc_list) {
183 warn "$lxc_opt->[0]: $lxc_opt->[1]\n"
185 warn "--------------------------------\n";
188 if ($unique && $key =~ /^net\d+$/) {
189 my $net = PVE
::LXC
::Config-
>parse_lxc_network($oldconf->{$key});
190 my $dc = PVE
::Cluster
::cfs_read_file
('datacenter.cfg');
191 $net->{hwaddr
} = PVE
::Tools
::random_ether_addr
($dc->{mac_prefix
});
192 $conf->{$key} = PVE
::LXC
::Config-
>print_lxc_network($net);
195 $conf->{$key} = $oldconf->{$key} if !defined($conf->{$key});
199 if (-f
$pct_fwcfg_fn) {
200 my $pve_firewall_dir = '/etc/pve/firewall';
201 mkdir $pve_firewall_dir; # make sure the directory exists
202 PVE
::Tools
::file_copy
($pct_fwcfg_fn, "${pve_firewall_dir}/$vmid.fw");
203 unlink $pct_fwcfg_fn;
206 } elsif (-f
$ovz_cfg_fn) {
207 print "###########################################################\n";
208 print "Converting OpenVZ configuration to LXC.\n";
209 print "Please check the configuration and reconfigure the network.\n";
210 print "###########################################################\n";
212 my $lxc_setup = PVE
::LXC
::Setup-
>new($conf, $rootdir); # detect OS
213 $conf->{ostype
} = $lxc_setup->{conf
}->{ostype
};
214 my $raw = PVE
::Tools
::file_get_contents
($ovz_cfg_fn);
215 my $oldconf = PVE
::VZDump
::ConvertOVZ
::convert_ovz
($raw);
216 foreach my $key (keys %$oldconf) {
217 $conf->{$key} = $oldconf->{$key} if !defined($conf->{$key});
222 print "###########################################################\n";
223 print "Backup archive does not contain any configuration\n";
224 print "###########################################################\n";