]> git.proxmox.com Git - pve-container.git/blame - src/PVE/LXC/Create.pm
mountpoint_mount: remove unused parameter $ms
[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
27916659 28 my $userns_cmd = [];
5b4657d0 29
27916659
DM
30# we always use the same mapping: 'b:0:100000:65536'
31# if ($conf->{'lxc.id_map'}) {
32# $userns_cmd = ['lxc-usernsexec', '-m', 'b:0:100000:65536', '--'];
33# PVE::Tools::run_command(['chown', '-R', '100000:100000', $rootdir]);
34# }
5b4657d0 35
27916659 36 my $cmd = [@$userns_cmd, 'tar', 'xpf', $archive, '--numeric-owner', '--totals',
5b4657d0
DM
37 '--sparse', '-C', $rootdir];
38
39 push @$cmd, '--anchored';
40 push @$cmd, '--exclude' , './dev/*';
41
6034ae50
DM
42 if ($archive eq '-') {
43 print "extracting archive from STDIN\n";
44 PVE::Tools::run_command($cmd, input => "<&STDIN");
27916659 45 } else {
6034ae50
DM
46 print "extracting archive '$archive'\n";
47 PVE::Tools::run_command($cmd);
27916659 48 }
6034ae50 49
27916659 50 # determine file type of /usr/bin/file itself to get guests' architecture
a9d131df
TL
51 $cmd = [@$userns_cmd, '/usr/bin/file', '-b', '-L', "$rootdir/usr/bin/file"];
52 PVE::Tools::run_command($cmd, outfunc => sub {
53 shift =~ /^ELF (\d{2}-bit)/; # safely assumes x86 linux
54 my $arch_str = $1;
27916659 55 $conf->{'arch'} = 'amd64'; # defaults to 64bit
a9d131df 56 if(defined($arch_str)) {
27916659
DM
57 $conf->{'arch'} = 'i386' if $arch_str =~ /32/;
58 print "Detected container architecture: $conf->{'arch'}\n";
a9d131df 59 } else {
27916659
DM
60 print "CT architecture detection failed, falling back to amd64.\n" .
61 "Edit the config in /etc/pve/nodes/{node}/lxc/{vmid}/config " .
62 "to set another architecture.\n";
a9d131df
TL
63 }
64 });
5b4657d0
DM
65}
66
f507c3a7
WL
67sub tar_archive_search_conf {
68 my ($archive) = @_;
69
70 die "ERROR: file '$archive' does not exist\n" if ! -f $archive;
71
72 my $pid = open(my $fh, '-|', 'tar', 'tf', $archive) ||
27916659 73 die "unable to open file '$archive'\n";
f507c3a7
WL
74
75 my $file;
effa4f43 76 while (defined($file = <$fh>)) {
27916659 77 if ($file =~ m!^(\./etc/vzdump/(pct|vps)\.conf)$!) {
effa4f43
DM
78 $file = $1; # untaint
79 last;
80 }
f507c3a7
WL
81 }
82
83 kill 15, $pid;
84 waitpid $pid, 0;
85 close $fh;
86
effa4f43 87 die "ERROR: archive contains no configuration file\n" if !$file;
f507c3a7
WL
88 chomp $file;
89
90 return $file;
91}
92
93sub recover_config {
effa4f43 94 my ($archive) = @_;
f507c3a7
WL
95
96 my $conf_file = tar_archive_search_conf($archive);
27916659 97
f507c3a7
WL
98 my $raw = '';
99 my $out = sub {
100 my $output = shift;
101 $raw .= "$output\n";
102 };
103
104 PVE::Tools::run_command(['tar', '-xpOf', $archive, $conf_file, '--occurrence'], outfunc => $out);
105
effa4f43 106 my $conf;
27916659 107 my $disksize;
f507c3a7 108
27916659 109 if ($conf_file =~ m/pct\.conf/) {
f507c3a7 110
3381b5c2 111 $conf = PVE::LXC::parse_pct_config("/lxc/0.conf" , $raw);
f507c3a7 112
27916659 113 delete $conf->{snapshots};
bb1ac2de 114 delete $conf->{template}; # restored CT is never a template
27916659
DM
115
116 if (defined($conf->{rootfs})) {
117 my $rootinfo = PVE::LXC::parse_ct_mountpoint($conf->{rootfs});
118 $disksize = $rootinfo->{size} if defined($rootinfo->{size});
119 }
120
effa4f43 121 } elsif ($conf_file =~ m/vps\.conf/) {
27916659
DM
122
123 ($conf, $disksize) = PVE::VZDump::ConvertOVZ::convert_ovz($raw);
124
effa4f43
DM
125 } else {
126
27916659 127 die "internal error";
f507c3a7
WL
128 }
129
27916659 130 return wantarray ? ($conf, $disksize) : $conf;
f507c3a7
WL
131}
132
5b4657d0 133sub restore_and_configure {
f507c3a7 134 my ($vmid, $archive, $rootdir, $conf, $password, $restore) = @_;
5b4657d0
DM
135
136 restore_archive($archive, $rootdir, $conf);
137
f507c3a7 138 if (!$restore) {
7af97ad5 139 my $lxc_setup = PVE::LXC::Setup->new($conf, $rootdir); # detect OS
5b4657d0 140
f507c3a7
WL
141 PVE::LXC::write_config($vmid, $conf); # safe config (after OS detection)
142 $lxc_setup->post_create_hook($password);
27916659
DM
143 } else {
144 # restore: try to extract configuration from archive
5b4657d0 145
27916659
DM
146 my $pct_cfg_fn = "$rootdir/etc/vzdump/pct.conf";
147 my $ovz_cfg_fn = "$rootdir/etc/vzdump/vps.conf";
148 if (-f $pct_cfg_fn) {
149 my $raw = PVE::Tools::file_get_contents($pct_cfg_fn);
150 my $oldconf = PVE::LXC::parse_pct_config("/lxc/$vmid.conf", $raw);
5b4657d0 151
27916659
DM
152 foreach my $key (keys %$oldconf) {
153 next if $key eq 'digest' || $key eq 'rootfs' || $key eq 'snapshots';
154 $conf->{$key} = $oldconf->{$key} if !defined($conf->{$key});
155 }
156
157 } elsif (-f $ovz_cfg_fn) {
158 print "###########################################################\n";
159 print "Converting OpenVZ configuration to LXC.\n";
160 print "Please check the configuration and reconfigure the network.\n";
161 print "###########################################################\n";
162
163 my $raw = PVE::Tools::file_get_contents($ovz_cfg_fn);
164 my $oldconf = PVE::VZDump::ConvertOVZ::convert_ovz($raw);
165 foreach my $key (keys %$oldconf) {
166 $conf->{$key} = $oldconf->{$key} if !defined($conf->{$key});
167 }
5b4657d0 168
27916659
DM
169 } else {
170 print "###########################################################\n";
171 print "Backup archive does not contain any configuration\n";
172 print "###########################################################\n";
173 }
174 }
5b4657d0
DM
175}
176
5b4657d0 177sub create_rootfs {
27916659 178 my ($storage_cfg, $storage, $volid, $vmid, $conf, $archive, $password, $restore) = @_;
148d1cb4
DM
179
180 my $config_fn = PVE::LXC::config_file($vmid);
181 if (-f $config_fn) {
182 die "container exists" if !$restore; # just to be sure
183
184 my $old_conf = PVE::LXC::load_config($vmid);
27916659
DM
185
186 # destroy old container volume
187 PVE::LXC::destory_lxc_container($storage_cfg, $vmid, $old_conf);
148d1cb4 188
27916659
DM
189 # do not copy all settings to restored container
190 foreach my $opt (qw(rootfs digest snapshots)) {
191 delete $old_conf->{$opt};
f09ce711 192 }
27916659 193 PVE::LXC::update_pct_config($vmid, $conf, 0, $old_conf);
5b4657d0 194
148d1cb4 195 PVE::LXC::create_config($vmid, $conf);
5b4657d0 196
148d1cb4
DM
197 } else {
198
199 PVE::LXC::create_config($vmid, $conf);
200 }
201
7fc16e9e
AD
202 my $image_path = PVE::Storage::path($storage_cfg, $volid);
203 my $mountpoint_path = "/var/lib/lxc/$vmid/rootfs";
204 my $mountpoint = { volume => $volid, mp => "/" };
7fc16e9e
AD
205
206 eval {
207 PVE::Storage::activate_volumes($storage_cfg, [$volid]);
208 my $loopdevs = PVE::LXC::attach_loops($storage_cfg, [$volid]);
209
210 if (!-d $image_path) {
211 my $cmd = ['mkfs.ext4', $image_path];
212 PVE::Tools::run_command($cmd);
3d8cbc23 213 }
7fc16e9e 214
1e8f01be 215 PVE::LXC::mountpoint_mount($mountpoint, $mountpoint_path, $storage_cfg, $loopdevs);
7fc16e9e
AD
216
217 restore_and_configure($vmid, $archive, $mountpoint_path, $conf, $password, $restore);
218 };
219 if (my $err = $@) {
220 eval {
221 PVE::LXC::dettach_loops($storage_cfg, [$volid]);
222 PVE::Storage::deactivate_volumes($storage_cfg, [$volid]);
223 };
224 warn $@ if $@;
225 die $err;
5b4657d0 226 }
7fc16e9e
AD
227
228 PVE::Tools::run_command(['umount', '-l', '-d', $mountpoint_path]);
229 PVE::LXC::dettach_loops($storage_cfg, [$volid]);
230 PVE::Storage::deactivate_volumes($storage_cfg, [$volid]);
231
5b4657d0
DM
232}
233
2341;