]> git.proxmox.com Git - pve-container.git/blame - src/PVE/LXC/Create.pm
add firewall config to vzdump
[pve-container.git] / src / PVE / LXC / Create.pm
CommitLineData
7af97ad5 1package PVE::LXC::Create;
5b4657d0
DM
2
3use strict;
4use warnings;
5use File::Basename;
6use File::Path;
7use Data::Dumper;
8
9use PVE::Storage;
10use PVE::LXC;
7af97ad5 11use PVE::LXC::Setup;
f507c3a7 12use PVE::VZDump::ConvertOVZ;
5b4657d0 13
6ed8c6dd
DM
14sub next_free_nbd_dev {
15
16 for(my $i = 0;;$i++) {
17 my $dev = "/dev/nbd$i";
18 last if ! -b $dev;
19 next if -f "/sys/block/nbd$i/pid"; # busy
20 return $dev;
21 }
22 die "unable to find free nbd device\n";
23}
24
5b4657d0
DM
25sub restore_archive {
26 my ($archive, $rootdir, $conf) = @_;
27
c6a605f9 28 my ($id_map, $rootuid, $rootgid) = PVE::LXC::parse_id_maps($conf);
01dce99b 29 my $userns_cmd = PVE::LXC::userns_command($id_map);
5b4657d0 30
fc4e132e
WB
31 my $cmd = [@$userns_cmd, 'tar', 'xpf', $archive, '--totals',
32 @$PVE::LXC::COMMON_TAR_FLAGS,
33 '-C', $rootdir];
5b4657d0 34
112aeeb4
WB
35 # skip-old-files doesn't have anything to do with time (old/new), but is
36 # simply -k (annoyingly also called --keep-old-files) without the 'treat
37 # existing files as errors' part... iow. it's bsdtar's interpretation of -k
38 # *sigh*, gnu...
39 push @$cmd, '--skip-old-files';
5b4657d0
DM
40 push @$cmd, '--anchored';
41 push @$cmd, '--exclude' , './dev/*';
42
6034ae50
DM
43 if ($archive eq '-') {
44 print "extracting archive from STDIN\n";
45 PVE::Tools::run_command($cmd, input => "<&STDIN");
27916659 46 } else {
6034ae50
DM
47 print "extracting archive '$archive'\n";
48 PVE::Tools::run_command($cmd);
27916659 49 }
6034ae50 50
27916659 51 # determine file type of /usr/bin/file itself to get guests' architecture
a9d131df
TL
52 $cmd = [@$userns_cmd, '/usr/bin/file', '-b', '-L', "$rootdir/usr/bin/file"];
53 PVE::Tools::run_command($cmd, outfunc => sub {
54 shift =~ /^ELF (\d{2}-bit)/; # safely assumes x86 linux
55 my $arch_str = $1;
27916659 56 $conf->{'arch'} = 'amd64'; # defaults to 64bit
a9d131df 57 if(defined($arch_str)) {
27916659
DM
58 $conf->{'arch'} = 'i386' if $arch_str =~ /32/;
59 print "Detected container architecture: $conf->{'arch'}\n";
a9d131df 60 } else {
27916659
DM
61 print "CT architecture detection failed, falling back to amd64.\n" .
62 "Edit the config in /etc/pve/nodes/{node}/lxc/{vmid}/config " .
63 "to set another architecture.\n";
a9d131df
TL
64 }
65 });
5b4657d0
DM
66}
67
f507c3a7
WL
68sub tar_archive_search_conf {
69 my ($archive) = @_;
70
71 die "ERROR: file '$archive' does not exist\n" if ! -f $archive;
72
73 my $pid = open(my $fh, '-|', 'tar', 'tf', $archive) ||
27916659 74 die "unable to open file '$archive'\n";
f507c3a7
WL
75
76 my $file;
effa4f43 77 while (defined($file = <$fh>)) {
27916659 78 if ($file =~ m!^(\./etc/vzdump/(pct|vps)\.conf)$!) {
effa4f43
DM
79 $file = $1; # untaint
80 last;
81 }
f507c3a7
WL
82 }
83
84 kill 15, $pid;
85 waitpid $pid, 0;
86 close $fh;
87
effa4f43 88 die "ERROR: archive contains no configuration file\n" if !$file;
f507c3a7
WL
89 chomp $file;
90
91 return $file;
92}
93
94sub recover_config {
effa4f43 95 my ($archive) = @_;
f507c3a7
WL
96
97 my $conf_file = tar_archive_search_conf($archive);
27916659 98
f507c3a7
WL
99 my $raw = '';
100 my $out = sub {
101 my $output = shift;
102 $raw .= "$output\n";
103 };
104
105 PVE::Tools::run_command(['tar', '-xpOf', $archive, $conf_file, '--occurrence'], outfunc => $out);
106
effa4f43 107 my $conf;
27916659 108 my $disksize;
f507c3a7 109
27916659 110 if ($conf_file =~ m/pct\.conf/) {
f507c3a7 111
3381b5c2 112 $conf = PVE::LXC::parse_pct_config("/lxc/0.conf" , $raw);
f507c3a7 113
27916659 114 delete $conf->{snapshots};
bb1ac2de 115 delete $conf->{template}; # restored CT is never a template
27916659
DM
116
117 if (defined($conf->{rootfs})) {
118 my $rootinfo = PVE::LXC::parse_ct_mountpoint($conf->{rootfs});
119 $disksize = $rootinfo->{size} if defined($rootinfo->{size});
120 }
121
effa4f43 122 } elsif ($conf_file =~ m/vps\.conf/) {
27916659
DM
123
124 ($conf, $disksize) = PVE::VZDump::ConvertOVZ::convert_ovz($raw);
125
effa4f43
DM
126 } else {
127
27916659 128 die "internal error";
f507c3a7
WL
129 }
130
27916659 131 return wantarray ? ($conf, $disksize) : $conf;
f507c3a7
WL
132}
133
5b4657d0 134sub restore_and_configure {
7c78b6cc 135 my ($vmid, $archive, $rootdir, $conf, $password, $restore, $no_unpack_error) = @_;
5b4657d0 136
7c78b6cc
WB
137 eval { restore_archive($archive, $rootdir, $conf) };
138 die $@ if $@ && !$no_unpack_error;
5b4657d0 139
f507c3a7 140 if (!$restore) {
7af97ad5 141 my $lxc_setup = PVE::LXC::Setup->new($conf, $rootdir); # detect OS
5b4657d0 142
f507c3a7
WL
143 PVE::LXC::write_config($vmid, $conf); # safe config (after OS detection)
144 $lxc_setup->post_create_hook($password);
27916659
DM
145 } else {
146 # restore: try to extract configuration from archive
5b4657d0 147
27916659
DM
148 my $pct_cfg_fn = "$rootdir/etc/vzdump/pct.conf";
149 my $ovz_cfg_fn = "$rootdir/etc/vzdump/vps.conf";
150 if (-f $pct_cfg_fn) {
151 my $raw = PVE::Tools::file_get_contents($pct_cfg_fn);
152 my $oldconf = PVE::LXC::parse_pct_config("/lxc/$vmid.conf", $raw);
5b4657d0 153
27916659 154 foreach my $key (keys %$oldconf) {
425b62cb 155 next if $key eq 'digest' || $key eq 'rootfs' || $key eq 'snapshots' || $key eq 'unprivileged';
27916659
DM
156 $conf->{$key} = $oldconf->{$key} if !defined($conf->{$key});
157 }
43a9184a 158 unlink($pct_cfg_fn);
27916659
DM
159
160 } elsif (-f $ovz_cfg_fn) {
161 print "###########################################################\n";
162 print "Converting OpenVZ configuration to LXC.\n";
163 print "Please check the configuration and reconfigure the network.\n";
164 print "###########################################################\n";
165
d394e3c9
WB
166 my $lxc_setup = PVE::LXC::Setup->new($conf, $rootdir); # detect OS
167 $conf->{ostype} = $lxc_setup->{conf}->{ostype};
27916659
DM
168 my $raw = PVE::Tools::file_get_contents($ovz_cfg_fn);
169 my $oldconf = PVE::VZDump::ConvertOVZ::convert_ovz($raw);
170 foreach my $key (keys %$oldconf) {
171 $conf->{$key} = $oldconf->{$key} if !defined($conf->{$key});
172 }
43a9184a 173 unlink($ovz_cfg_fn);
5b4657d0 174
27916659
DM
175 } else {
176 print "###########################################################\n";
177 print "Backup archive does not contain any configuration\n";
178 print "###########################################################\n";
179 }
180 }
5b4657d0
DM
181}
182
5b4657d0 183sub create_rootfs {
7c78b6cc 184 my ($storage_cfg, $vmid, $conf, $archive, $password, $restore, $no_unpack_error) = @_;
148d1cb4
DM
185
186 my $config_fn = PVE::LXC::config_file($vmid);
187 if (-f $config_fn) {
188 die "container exists" if !$restore; # just to be sure
189
190 my $old_conf = PVE::LXC::load_config($vmid);
27916659
DM
191
192 # destroy old container volume
077d7669 193 PVE::LXC::destroy_lxc_container($storage_cfg, $vmid, $old_conf);
148d1cb4 194
27916659 195 # do not copy all settings to restored container
425b62cb 196 foreach my $opt (qw(rootfs digest snapshots arch ostype unprivileged)) {
27916659 197 delete $old_conf->{$opt};
f09ce711 198 }
ed680718
DM
199 foreach my $opt (keys %$old_conf) {
200 delete $old_conf->{$opt} if $opt =~ m/^mp\d+$/;
201 }
202
27916659 203 PVE::LXC::update_pct_config($vmid, $conf, 0, $old_conf);
5b4657d0 204
148d1cb4 205 PVE::LXC::create_config($vmid, $conf);
5b4657d0 206
148d1cb4
DM
207 } else {
208
209 PVE::LXC::create_config($vmid, $conf);
210 }
211
7fc16e9e 212 eval {
da629848 213 my $rootdir = PVE::LXC::mount_all($vmid, $storage_cfg, $conf);
7c78b6cc 214 restore_and_configure($vmid, $archive, $rootdir, $conf, $password, $restore, $no_unpack_error);
7fc16e9e 215 };
4497bfe7
WB
216 my $err = $@;
217 PVE::LXC::umount_all($vmid, $storage_cfg, $conf, $err ? 1 : 0);
bce1b15d 218 PVE::Storage::deactivate_volumes($storage_cfg, PVE::LXC::get_vm_volumes($conf));
4497bfe7 219 die $err if $err;
5b4657d0
DM
220}
221
2221;