1 package PVE
::LXC
::Create
;
13 use PVE
::VZDump
::ConvertOVZ
;
17 sub detect_architecture
{
20 my $supported_elf_class = {
25 my $elf_fn = '/bin/sh'; # '/bin/sh' is POSIX mandatory
26 my $detect_arch = sub {
27 # chroot avoids a problem where we check the binary of the host system
28 # if $elf_fn is an absolut symlink (e.g. $rootdir/bin/sh -> /bin/bash)
29 chroot($rootdir) or die "chroot '$rootdir' failed: $!\n";
30 chdir('/') or die "failed to change to root directory\n";
32 open(my $fh, "<", $elf_fn) or die "open '$elf_fn' failed: $!\n";
35 my $length = read($fh, my $data, 5) or die "read failed: $!\n";
37 # 4 bytes ELF magic number and 1 byte ELF class
38 my ($magic, $class) = unpack("A4C", $data);
40 die "'$elf_fn' does not resolve to an ELF!\n"
41 if (!defined($class) || !defined($magic) || $magic ne "\177ELF");
43 die "'$elf_fn' has unknown ELF class '$class'!\n"
44 if !defined($supported_elf_class->{$class});
46 return $supported_elf_class->{$class};
49 my $arch = eval { PVE
::Tools
::run_fork_with_timeout
(5, $detect_arch) };
52 print "Architecture detection failed: $err\nFalling back to amd64.\n" .
53 "Use `pct set VMID --arch ARCH` to change.\n";
55 print "Detected container architecture: $arch\n";
62 my ($archive, $rootdir, $conf, $no_unpack_error) = @_;
64 my ($id_map, $rootuid, $rootgid) = PVE
::LXC
::parse_id_maps
($conf);
65 my $userns_cmd = PVE
::LXC
::userns_command
($id_map);
68 my $tar_input = '<&STDIN';
70 if ($archive ne '-') {
71 # GNU tar refuses to autodetect this... *sigh*
72 my %compression_map = (
77 if ($archive =~ /\.tar(\.[^.]+)?$/) {
79 @compression_opt = $compression_map{$1}
80 or die "unrecognized compression format: $1\n";
83 die "file does not look like a template archive: $archive\n";
85 sysopen($archive_fh, $archive, O_RDONLY
)
86 or die "failed to open '$archive': $!\n";
87 my $flags = $archive_fh->fcntl(Fcntl
::F_GETFD
(), 0);
88 $archive_fh->fcntl(Fcntl
::F_SETFD
(), $flags & ~(Fcntl
::FD_CLOEXEC
()));
89 $tar_input = '<&'.fileno($archive_fh);
92 my $cmd = [@$userns_cmd, 'tar', 'xpf', '-', @compression_opt, '--totals',
93 @PVE::Storage
::Plugin
::COMMON_TAR_FLAGS
,
96 # skip-old-files doesn't have anything to do with time (old/new), but is
97 # simply -k (annoyingly also called --keep-old-files) without the 'treat
98 # existing files as errors' part... iow. it's bsdtar's interpretation of -k
100 push @$cmd, '--skip-old-files';
101 push @$cmd, '--anchored';
102 push @$cmd, '--exclude' , './dev/*';
104 if ($archive eq '-') {
105 print "extracting archive from STDIN\n";
107 print "extracting archive '$archive'\n";
109 eval { PVE
::Tools
::run_command
($cmd, input
=> $tar_input); };
111 close($archive_fh) if defined $archive_fh;
112 die $err if $err && !$no_unpack_error;
114 # if arch is set, we do not try to autodetect it
115 return if defined($conf->{arch
});
117 $conf->{arch
} = detect_architecture
($rootdir);
123 my ($raw, $conf_file) = PVE
::Storage
::extract_vzdump_config_tar
($archive, qr!(\./etc/vzdump/(pct|vps)\.conf)$!);
127 if ($conf_file =~ m/pct\.conf/) {
129 $conf = PVE
::LXC
::Config
::parse_pct_config
("/lxc/0.conf" , $raw);
131 delete $conf->{snapshots
};
132 delete $conf->{template
}; # restored CT is never a template
134 PVE
::LXC
::Config-
>foreach_mountpoint($conf, sub {
135 my ($ms, $mountpoint) = @_;
136 $mp_param->{$ms} = $conf->{$ms};
139 } elsif ($conf_file =~ m/vps\.conf/) {
141 ($conf, $mp_param) = PVE
::VZDump
::ConvertOVZ
::convert_ovz
($raw);
145 die "internal error";
148 return wantarray ?
($conf, $mp_param) : $conf;
151 sub restore_configuration
{
152 my ($vmid, $rootdir, $conf, $restricted) = @_;
154 # restore: try to extract configuration from archive
156 my $pct_cfg_fn = "$rootdir/etc/vzdump/pct.conf";
157 my $pct_fwcfg_fn = "$rootdir/etc/vzdump/pct.fw";
158 my $ovz_cfg_fn = "$rootdir/etc/vzdump/vps.conf";
159 if (-f
$pct_cfg_fn) {
160 my $raw = PVE
::Tools
::file_get_contents
($pct_cfg_fn);
161 my $oldconf = PVE
::LXC
::Config
::parse_pct_config
("/lxc/$vmid.conf", $raw);
163 foreach my $key (keys %$oldconf) {
164 next if $key eq 'digest' || $key eq 'rootfs' || $key eq 'snapshots' || $key eq 'unprivileged' || $key eq 'parent';
165 next if $key =~ /^mp\d+$/; # don't recover mountpoints
166 next if $key =~ /^unused\d+$/; # don't recover unused disks
167 if ($restricted && $key eq 'lxc') {
168 warn "skipping custom lxc options, restore manually as root:\n";
169 warn "--------------------------------\n";
170 my $lxc_list = $oldconf->{'lxc'};
171 foreach my $lxc_opt (@$lxc_list) {
172 warn "$lxc_opt->[0]: $lxc_opt->[1]\n"
174 warn "--------------------------------\n";
177 $conf->{$key} = $oldconf->{$key} if !defined($conf->{$key});
181 if (-f
$pct_fwcfg_fn) {
182 my $pve_firewall_dir = '/etc/pve/firewall';
183 mkdir $pve_firewall_dir; # make sure the directory exists
184 PVE
::Tools
::file_copy
($pct_fwcfg_fn, "${pve_firewall_dir}/$vmid.fw");
185 unlink $pct_fwcfg_fn;
188 } elsif (-f
$ovz_cfg_fn) {
189 print "###########################################################\n";
190 print "Converting OpenVZ configuration to LXC.\n";
191 print "Please check the configuration and reconfigure the network.\n";
192 print "###########################################################\n";
194 my $lxc_setup = PVE
::LXC
::Setup-
>new($conf, $rootdir); # detect OS
195 $conf->{ostype
} = $lxc_setup->{conf
}->{ostype
};
196 my $raw = PVE
::Tools
::file_get_contents
($ovz_cfg_fn);
197 my $oldconf = PVE
::VZDump
::ConvertOVZ
::convert_ovz
($raw);
198 foreach my $key (keys %$oldconf) {
199 $conf->{$key} = $oldconf->{$key} if !defined($conf->{$key});
204 print "###########################################################\n";
205 print "Backup archive does not contain any configuration\n";
206 print "###########################################################\n";