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