1 package PVE
::LXC
::Create
;
12 use PVE
::VZDump
::ConvertOVZ
;
17 my ($rootdir, $elf_fn) = @_;
19 my $child_pid = fork();
20 die "fork failed: $!\n" if !defined($child_pid);
23 # chroot avoids a problem where we check the binary of the host system
24 # if $elf_fn is an absolut symlink (e.g. $rootdir/bin/sh -> /bin/bash)
25 chroot($rootdir) or die "chroot '$rootdir' failed: $!\n";
26 chdir('/') or die "failed to change to root directory\n";
29 open($fh, "<", $elf_fn) or die "open '$elf_fn' failed: $!\n";
33 my $length = read($fh, $data, 5);
34 die "read failed: $!\n" if !defined($length);
36 # 4 bytes ELF magic number and 1 byte ELF class
37 my ($magic, $class) = unpack("A4C", $data);
39 die "'$elf_fn' does not resolve to an ELF!\n"
40 if (!defined($class) || !defined($magic) || $magic ne "\177ELF");
42 die "'$elf_fn' has unknown ELF class '$class'!\n"
43 if ($class != 1 && $class != 2);
48 while (waitpid($child_pid, 0) != $child_pid) {}
50 if (my $sig = ($exit_code & 127)) {
51 warn "could not get architecture, got signal $sig\n";
60 my ($archive, $rootdir, $conf, $no_unpack_error) = @_;
62 my ($id_map, $rootuid, $rootgid) = PVE
::LXC
::parse_id_maps
($conf);
63 my $userns_cmd = PVE
::LXC
::userns_command
($id_map);
65 my $cmd = [@$userns_cmd, 'tar', 'xpf', $archive, '--totals',
66 @$PVE::LXC
::COMMON_TAR_FLAGS
,
69 # skip-old-files doesn't have anything to do with time (old/new), but is
70 # simply -k (annoyingly also called --keep-old-files) without the 'treat
71 # existing files as errors' part... iow. it's bsdtar's interpretation of -k
73 push @$cmd, '--skip-old-files';
74 push @$cmd, '--anchored';
75 push @$cmd, '--exclude' , './dev/*';
77 if ($archive eq '-') {
78 print "extracting archive from STDIN\n";
79 eval { PVE
::Tools
::run_command
($cmd, input
=> "<&STDIN"); };
81 print "extracting archive '$archive'\n";
82 eval { PVE
::Tools
::run_command
($cmd); };
84 die $@ if $@ && !$no_unpack_error;
86 # if arch is set, we do not try to autodetect it
87 return if defined($conf->{arch
});
90 my $elf_class = get_elf_class
($rootdir, '/bin/sh'); # /bin/sh is POSIX mandatory
92 $conf->{'arch'} = 'amd64'; # defaults to 64bit
93 if ($elf_class == 1 || $elf_class == 2) {
94 $conf->{'arch'} = 'i386' if $elf_class == 1;
95 print "Detected container architecture: $conf->{'arch'}\n";
97 print "CT architecture detection failed, falling back to amd64.\n" .
98 "Edit the config in /etc/pve/nodes/{node}/lxc/{vmid}.conf " .
99 "to set another architecture.\n";
106 my ($raw, $conf_file) = PVE
::Storage
::extract_vzdump_config_tar
($archive, qr!(\./etc/vzdump/(pct|vps)\.conf)$!);
110 if ($conf_file =~ m/pct\.conf/) {
112 $conf = PVE
::LXC
::Config
::parse_pct_config
("/lxc/0.conf" , $raw);
114 delete $conf->{snapshots
};
115 delete $conf->{template
}; # restored CT is never a template
117 PVE
::LXC
::Config-
>foreach_mountpoint($conf, sub {
118 my ($ms, $mountpoint) = @_;
119 $mp_param->{$ms} = $conf->{$ms};
122 } elsif ($conf_file =~ m/vps\.conf/) {
124 ($conf, $mp_param) = PVE
::VZDump
::ConvertOVZ
::convert_ovz
($raw);
128 die "internal error";
131 return wantarray ?
($conf, $mp_param) : $conf;
134 sub restore_configuration
{
135 my ($vmid, $rootdir, $conf, $restricted) = @_;
137 # restore: try to extract configuration from archive
139 my $pct_cfg_fn = "$rootdir/etc/vzdump/pct.conf";
140 my $pct_fwcfg_fn = "$rootdir/etc/vzdump/pct.fw";
141 my $ovz_cfg_fn = "$rootdir/etc/vzdump/vps.conf";
142 if (-f
$pct_cfg_fn) {
143 my $raw = PVE
::Tools
::file_get_contents
($pct_cfg_fn);
144 my $oldconf = PVE
::LXC
::Config
::parse_pct_config
("/lxc/$vmid.conf", $raw);
146 foreach my $key (keys %$oldconf) {
147 next if $key eq 'digest' || $key eq 'rootfs' || $key eq 'snapshots' || $key eq 'unprivileged' || $key eq 'parent';
148 next if $key =~ /^mp\d+$/; # don't recover mountpoints
149 next if $key =~ /^unused\d+$/; # don't recover unused disks
150 if ($restricted && $key eq 'lxc') {
151 warn "skipping custom lxc options, restore manually as root:\n";
152 warn "--------------------------------\n";
153 my $lxc_list = $oldconf->{'lxc'};
154 foreach my $lxc_opt (@$lxc_list) {
155 warn "$lxc_opt->[0]: $lxc_opt->[1]\n"
157 warn "--------------------------------\n";
160 $conf->{$key} = $oldconf->{$key} if !defined($conf->{$key});
164 if (-f
$pct_fwcfg_fn) {
165 my $pve_firewall_dir = '/etc/pve/firewall';
166 mkdir $pve_firewall_dir; # make sure the directory exists
167 PVE
::Tools
::file_copy
($pct_fwcfg_fn, "${pve_firewall_dir}/$vmid.fw");
168 unlink $pct_fwcfg_fn;
171 } elsif (-f
$ovz_cfg_fn) {
172 print "###########################################################\n";
173 print "Converting OpenVZ configuration to LXC.\n";
174 print "Please check the configuration and reconfigure the network.\n";
175 print "###########################################################\n";
177 my $lxc_setup = PVE
::LXC
::Setup-
>new($conf, $rootdir); # detect OS
178 $conf->{ostype
} = $lxc_setup->{conf
}->{ostype
};
179 my $raw = PVE
::Tools
::file_get_contents
($ovz_cfg_fn);
180 my $oldconf = PVE
::VZDump
::ConvertOVZ
::convert_ovz
($raw);
181 foreach my $key (keys %$oldconf) {
182 $conf->{$key} = $oldconf->{$key} if !defined($conf->{$key});
187 print "###########################################################\n";
188 print "Backup archive does not contain any configuration\n";
189 print "###########################################################\n";