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