1 package PVE
::LXC
::Create
;
13 use PVE
::VZDump
::ConvertOVZ
;
18 my ($rootdir, $elf_fn) = @_;
20 my $child_pid = fork();
21 die "fork failed: $!\n" if !defined($child_pid);
24 # chroot avoids a problem where we check the binary of the host system
25 # if $elf_fn is an absolut symlink (e.g. $rootdir/bin/sh -> /bin/bash)
26 chroot($rootdir) or die "chroot '$rootdir' failed: $!\n";
27 chdir('/') or die "failed to change to root directory\n";
30 open($fh, "<", $elf_fn) or die "open '$elf_fn' failed: $!\n";
34 my $length = read($fh, $data, 5);
35 die "read failed: $!\n" if !defined($length);
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 ($class != 1 && $class != 2);
49 while (waitpid($child_pid, 0) != $child_pid) {}
51 if (my $sig = ($exit_code & 127)) {
52 warn "could not get architecture, got signal $sig\n";
61 my ($archive, $rootdir, $conf, $no_unpack_error) = @_;
63 my ($id_map, $rootuid, $rootgid) = PVE
::LXC
::parse_id_maps
($conf);
64 my $userns_cmd = PVE
::LXC
::userns_command
($id_map);
67 my $tar_input_file = '-';
68 if ($archive ne '-') {
69 sysopen($archive_fh, $archive, O_RDONLY
)
70 or die "failed to open '$archive': $!\n";
71 $tar_input_file = '/proc/self/fd/'.fileno($archive_fh);
72 my $flags = $archive_fh->fcntl(Fcntl
::F_GETFD
(), 0);
73 $archive_fh->fcntl(Fcntl
::F_SETFD
(), $flags & ~(Fcntl
::FD_CLOEXEC
()));
76 my $cmd = [@$userns_cmd, 'tar', 'xpf', $tar_input_file, '--totals',
77 @$PVE::LXC
::COMMON_TAR_FLAGS
,
80 # skip-old-files doesn't have anything to do with time (old/new), but is
81 # simply -k (annoyingly also called --keep-old-files) without the 'treat
82 # existing files as errors' part... iow. it's bsdtar's interpretation of -k
84 push @$cmd, '--skip-old-files';
85 push @$cmd, '--anchored';
86 push @$cmd, '--exclude' , './dev/*';
88 if ($archive eq '-') {
89 print "extracting archive from STDIN\n";
90 eval { PVE
::Tools
::run_command
($cmd, input
=> "<&STDIN"); };
92 print "extracting archive '$archive'\n";
93 eval { PVE
::Tools
::run_command
($cmd); };
96 close($archive_fh) if defined $archive_fh;
97 die $err if $err && !$no_unpack_error;
99 # if arch is set, we do not try to autodetect it
100 return if defined($conf->{arch
});
103 my $elf_class = get_elf_class
($rootdir, '/bin/sh'); # /bin/sh is POSIX mandatory
105 $conf->{'arch'} = 'amd64'; # defaults to 64bit
106 if ($elf_class == 1 || $elf_class == 2) {
107 $conf->{'arch'} = 'i386' if $elf_class == 1;
108 print "Detected container architecture: $conf->{'arch'}\n";
110 print "CT architecture detection failed, falling back to amd64.\n" .
111 "Edit the config in /etc/pve/nodes/{node}/lxc/{vmid}.conf " .
112 "to set another architecture.\n";
119 my ($raw, $conf_file) = PVE
::Storage
::extract_vzdump_config_tar
($archive, qr!(\./etc/vzdump/(pct|vps)\.conf)$!);
123 if ($conf_file =~ m/pct\.conf/) {
125 $conf = PVE
::LXC
::Config
::parse_pct_config
("/lxc/0.conf" , $raw);
127 delete $conf->{snapshots
};
128 delete $conf->{template
}; # restored CT is never a template
130 PVE
::LXC
::Config-
>foreach_mountpoint($conf, sub {
131 my ($ms, $mountpoint) = @_;
132 $mp_param->{$ms} = $conf->{$ms};
135 } elsif ($conf_file =~ m/vps\.conf/) {
137 ($conf, $mp_param) = PVE
::VZDump
::ConvertOVZ
::convert_ovz
($raw);
141 die "internal error";
144 return wantarray ?
($conf, $mp_param) : $conf;
147 sub restore_configuration
{
148 my ($vmid, $rootdir, $conf, $restricted) = @_;
150 # restore: try to extract configuration from archive
152 my $pct_cfg_fn = "$rootdir/etc/vzdump/pct.conf";
153 my $pct_fwcfg_fn = "$rootdir/etc/vzdump/pct.fw";
154 my $ovz_cfg_fn = "$rootdir/etc/vzdump/vps.conf";
155 if (-f
$pct_cfg_fn) {
156 my $raw = PVE
::Tools
::file_get_contents
($pct_cfg_fn);
157 my $oldconf = PVE
::LXC
::Config
::parse_pct_config
("/lxc/$vmid.conf", $raw);
159 foreach my $key (keys %$oldconf) {
160 next if $key eq 'digest' || $key eq 'rootfs' || $key eq 'snapshots' || $key eq 'unprivileged' || $key eq 'parent';
161 next if $key =~ /^mp\d+$/; # don't recover mountpoints
162 next if $key =~ /^unused\d+$/; # don't recover unused disks
163 if ($restricted && $key eq 'lxc') {
164 warn "skipping custom lxc options, restore manually as root:\n";
165 warn "--------------------------------\n";
166 my $lxc_list = $oldconf->{'lxc'};
167 foreach my $lxc_opt (@$lxc_list) {
168 warn "$lxc_opt->[0]: $lxc_opt->[1]\n"
170 warn "--------------------------------\n";
173 $conf->{$key} = $oldconf->{$key} if !defined($conf->{$key});
177 if (-f
$pct_fwcfg_fn) {
178 my $pve_firewall_dir = '/etc/pve/firewall';
179 mkdir $pve_firewall_dir; # make sure the directory exists
180 PVE
::Tools
::file_copy
($pct_fwcfg_fn, "${pve_firewall_dir}/$vmid.fw");
181 unlink $pct_fwcfg_fn;
184 } elsif (-f
$ovz_cfg_fn) {
185 print "###########################################################\n";
186 print "Converting OpenVZ configuration to LXC.\n";
187 print "Please check the configuration and reconfigure the network.\n";
188 print "###########################################################\n";
190 my $lxc_setup = PVE
::LXC
::Setup-
>new($conf, $rootdir); # detect OS
191 $conf->{ostype
} = $lxc_setup->{conf
}->{ostype
};
192 my $raw = PVE
::Tools
::file_get_contents
($ovz_cfg_fn);
193 my $oldconf = PVE
::VZDump
::ConvertOVZ
::convert_ovz
($raw);
194 foreach my $key (keys %$oldconf) {
195 $conf->{$key} = $oldconf->{$key} if !defined($conf->{$key});
200 print "###########################################################\n";
201 print "Backup archive does not contain any configuration\n";
202 print "###########################################################\n";