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_file = '-';
69 if ($archive ne '-') {
70 sysopen($archive_fh, $archive, O_RDONLY
)
71 or die "failed to open '$archive': $!\n";
72 $tar_input_file = '/proc/self/fd/'.fileno($archive_fh);
73 my $flags = $archive_fh->fcntl(Fcntl
::F_GETFD
(), 0);
74 $archive_fh->fcntl(Fcntl
::F_SETFD
(), $flags & ~(Fcntl
::FD_CLOEXEC
()));
77 my $cmd = [@$userns_cmd, 'tar', 'xpf', $tar_input_file, '--totals',
78 @PVE::Storage
::Plugin
::COMMON_TAR_FLAGS
,
81 # skip-old-files doesn't have anything to do with time (old/new), but is
82 # simply -k (annoyingly also called --keep-old-files) without the 'treat
83 # existing files as errors' part... iow. it's bsdtar's interpretation of -k
85 push @$cmd, '--skip-old-files';
86 push @$cmd, '--anchored';
87 push @$cmd, '--exclude' , './dev/*';
89 if ($archive eq '-') {
90 print "extracting archive from STDIN\n";
91 eval { PVE
::Tools
::run_command
($cmd, input
=> "<&STDIN"); };
93 print "extracting archive '$archive'\n";
94 eval { PVE
::Tools
::run_command
($cmd); };
97 close($archive_fh) if defined $archive_fh;
98 die $err if $err && !$no_unpack_error;
100 # if arch is set, we do not try to autodetect it
101 return if defined($conf->{arch
});
103 $conf->{arch
} = detect_architecture
($rootdir);
109 my ($raw, $conf_file) = PVE
::Storage
::extract_vzdump_config_tar
($archive, qr!(\./etc/vzdump/(pct|vps)\.conf)$!);
113 if ($conf_file =~ m/pct\.conf/) {
115 $conf = PVE
::LXC
::Config
::parse_pct_config
("/lxc/0.conf" , $raw);
117 delete $conf->{snapshots
};
118 delete $conf->{template
}; # restored CT is never a template
120 PVE
::LXC
::Config-
>foreach_mountpoint($conf, sub {
121 my ($ms, $mountpoint) = @_;
122 $mp_param->{$ms} = $conf->{$ms};
125 } elsif ($conf_file =~ m/vps\.conf/) {
127 ($conf, $mp_param) = PVE
::VZDump
::ConvertOVZ
::convert_ovz
($raw);
131 die "internal error";
134 return wantarray ?
($conf, $mp_param) : $conf;
137 sub restore_configuration
{
138 my ($vmid, $rootdir, $conf, $restricted) = @_;
140 # restore: try to extract configuration from archive
142 my $pct_cfg_fn = "$rootdir/etc/vzdump/pct.conf";
143 my $pct_fwcfg_fn = "$rootdir/etc/vzdump/pct.fw";
144 my $ovz_cfg_fn = "$rootdir/etc/vzdump/vps.conf";
145 if (-f
$pct_cfg_fn) {
146 my $raw = PVE
::Tools
::file_get_contents
($pct_cfg_fn);
147 my $oldconf = PVE
::LXC
::Config
::parse_pct_config
("/lxc/$vmid.conf", $raw);
149 foreach my $key (keys %$oldconf) {
150 next if $key eq 'digest' || $key eq 'rootfs' || $key eq 'snapshots' || $key eq 'unprivileged' || $key eq 'parent';
151 next if $key =~ /^mp\d+$/; # don't recover mountpoints
152 next if $key =~ /^unused\d+$/; # don't recover unused disks
153 if ($restricted && $key eq 'lxc') {
154 warn "skipping custom lxc options, restore manually as root:\n";
155 warn "--------------------------------\n";
156 my $lxc_list = $oldconf->{'lxc'};
157 foreach my $lxc_opt (@$lxc_list) {
158 warn "$lxc_opt->[0]: $lxc_opt->[1]\n"
160 warn "--------------------------------\n";
163 $conf->{$key} = $oldconf->{$key} if !defined($conf->{$key});
167 if (-f
$pct_fwcfg_fn) {
168 my $pve_firewall_dir = '/etc/pve/firewall';
169 mkdir $pve_firewall_dir; # make sure the directory exists
170 PVE
::Tools
::file_copy
($pct_fwcfg_fn, "${pve_firewall_dir}/$vmid.fw");
171 unlink $pct_fwcfg_fn;
174 } elsif (-f
$ovz_cfg_fn) {
175 print "###########################################################\n";
176 print "Converting OpenVZ configuration to LXC.\n";
177 print "Please check the configuration and reconfigure the network.\n";
178 print "###########################################################\n";
180 my $lxc_setup = PVE
::LXC
::Setup-
>new($conf, $rootdir); # detect OS
181 $conf->{ostype
} = $lxc_setup->{conf
}->{ostype
};
182 my $raw = PVE
::Tools
::file_get_contents
($ovz_cfg_fn);
183 my $oldconf = PVE
::VZDump
::ConvertOVZ
::convert_ovz
($raw);
184 foreach my $key (keys %$oldconf) {
185 $conf->{$key} = $oldconf->{$key} if !defined($conf->{$key});
190 print "###########################################################\n";
191 print "Backup archive does not contain any configuration\n";
192 print "###########################################################\n";