]>
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 | ||
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 |
68 | sub 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 | ||
94 | sub 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 | 134 | sub restore_and_configure { |
f507c3a7 | 135 | my ($vmid, $archive, $rootdir, $conf, $password, $restore) = @_; |
5b4657d0 DM |
136 | |
137 | restore_archive($archive, $rootdir, $conf); | |
138 | ||
f507c3a7 | 139 | if (!$restore) { |
7af97ad5 | 140 | my $lxc_setup = PVE::LXC::Setup->new($conf, $rootdir); # detect OS |
5b4657d0 | 141 | |
f507c3a7 WL |
142 | PVE::LXC::write_config($vmid, $conf); # safe config (after OS detection) |
143 | $lxc_setup->post_create_hook($password); | |
27916659 DM |
144 | } else { |
145 | # restore: try to extract configuration from archive | |
5b4657d0 | 146 | |
27916659 DM |
147 | my $pct_cfg_fn = "$rootdir/etc/vzdump/pct.conf"; |
148 | my $ovz_cfg_fn = "$rootdir/etc/vzdump/vps.conf"; | |
149 | if (-f $pct_cfg_fn) { | |
150 | my $raw = PVE::Tools::file_get_contents($pct_cfg_fn); | |
151 | my $oldconf = PVE::LXC::parse_pct_config("/lxc/$vmid.conf", $raw); | |
5b4657d0 | 152 | |
27916659 | 153 | foreach my $key (keys %$oldconf) { |
425b62cb | 154 | next if $key eq 'digest' || $key eq 'rootfs' || $key eq 'snapshots' || $key eq 'unprivileged'; |
27916659 DM |
155 | $conf->{$key} = $oldconf->{$key} if !defined($conf->{$key}); |
156 | } | |
43a9184a | 157 | unlink($pct_cfg_fn); |
27916659 DM |
158 | |
159 | } elsif (-f $ovz_cfg_fn) { | |
160 | print "###########################################################\n"; | |
161 | print "Converting OpenVZ configuration to LXC.\n"; | |
162 | print "Please check the configuration and reconfigure the network.\n"; | |
163 | print "###########################################################\n"; | |
164 | ||
d394e3c9 WB |
165 | my $lxc_setup = PVE::LXC::Setup->new($conf, $rootdir); # detect OS |
166 | $conf->{ostype} = $lxc_setup->{conf}->{ostype}; | |
27916659 DM |
167 | my $raw = PVE::Tools::file_get_contents($ovz_cfg_fn); |
168 | my $oldconf = PVE::VZDump::ConvertOVZ::convert_ovz($raw); | |
169 | foreach my $key (keys %$oldconf) { | |
170 | $conf->{$key} = $oldconf->{$key} if !defined($conf->{$key}); | |
171 | } | |
43a9184a | 172 | unlink($ovz_cfg_fn); |
5b4657d0 | 173 | |
27916659 DM |
174 | } else { |
175 | print "###########################################################\n"; | |
176 | print "Backup archive does not contain any configuration\n"; | |
177 | print "###########################################################\n"; | |
178 | } | |
179 | } | |
5b4657d0 DM |
180 | } |
181 | ||
5b4657d0 | 182 | sub create_rootfs { |
eb35f9c0 | 183 | my ($storage_cfg, $vmid, $conf, $archive, $password, $restore) = @_; |
148d1cb4 DM |
184 | |
185 | my $config_fn = PVE::LXC::config_file($vmid); | |
186 | if (-f $config_fn) { | |
187 | die "container exists" if !$restore; # just to be sure | |
188 | ||
189 | my $old_conf = PVE::LXC::load_config($vmid); | |
27916659 DM |
190 | |
191 | # destroy old container volume | |
077d7669 | 192 | PVE::LXC::destroy_lxc_container($storage_cfg, $vmid, $old_conf); |
148d1cb4 | 193 | |
27916659 | 194 | # do not copy all settings to restored container |
425b62cb | 195 | foreach my $opt (qw(rootfs digest snapshots arch ostype unprivileged)) { |
27916659 | 196 | delete $old_conf->{$opt}; |
f09ce711 | 197 | } |
ed680718 DM |
198 | foreach my $opt (keys %$old_conf) { |
199 | delete $old_conf->{$opt} if $opt =~ m/^mp\d+$/; | |
200 | } | |
201 | ||
27916659 | 202 | PVE::LXC::update_pct_config($vmid, $conf, 0, $old_conf); |
5b4657d0 | 203 | |
148d1cb4 | 204 | PVE::LXC::create_config($vmid, $conf); |
5b4657d0 | 205 | |
148d1cb4 DM |
206 | } else { |
207 | ||
208 | PVE::LXC::create_config($vmid, $conf); | |
209 | } | |
210 | ||
7fc16e9e | 211 | eval { |
da629848 | 212 | my $rootdir = PVE::LXC::mount_all($vmid, $storage_cfg, $conf); |
9622e848 | 213 | restore_and_configure($vmid, $archive, $rootdir, $conf, $password, $restore); |
7fc16e9e | 214 | }; |
4497bfe7 WB |
215 | my $err = $@; |
216 | PVE::LXC::umount_all($vmid, $storage_cfg, $conf, $err ? 1 : 0); | |
bce1b15d | 217 | PVE::Storage::deactivate_volumes($storage_cfg, PVE::LXC::get_vm_volumes($conf)); |
4497bfe7 | 218 | die $err if $err; |
5b4657d0 DM |
219 | } |
220 | ||
221 | 1; |