]> git.proxmox.com Git - pve-container.git/blame - src/PVE/LXC/Create.pm
update changelog
[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;
580b6916 13use PVE::Tools;
5b4657d0 14
6ed8c6dd
DM
15sub 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 26sub restore_archive {
9c23d567 27 my ($archive, $rootdir, $conf, $no_unpack_error) = @_;
5b4657d0 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";
9c23d567 46 eval { PVE::Tools::run_command($cmd, input => "<&STDIN"); };
27916659 47 } else {
6034ae50 48 print "extracting archive '$archive'\n";
9c23d567 49 eval { PVE::Tools::run_command($cmd); };
27916659 50 }
9c23d567 51 die $@ if $@ && !$no_unpack_error;
6034ae50 52
27916659 53 # determine file type of /usr/bin/file itself to get guests' architecture
6cc47cb0 54 $cmd = [@$userns_cmd, '/usr/bin/file', '-b', '-L', "$rootdir/bin/sh"];
a9d131df
TL
55 PVE::Tools::run_command($cmd, outfunc => sub {
56 shift =~ /^ELF (\d{2}-bit)/; # safely assumes x86 linux
57 my $arch_str = $1;
27916659 58 $conf->{'arch'} = 'amd64'; # defaults to 64bit
a9d131df 59 if(defined($arch_str)) {
27916659
DM
60 $conf->{'arch'} = 'i386' if $arch_str =~ /32/;
61 print "Detected container architecture: $conf->{'arch'}\n";
a9d131df 62 } else {
27916659
DM
63 print "CT architecture detection failed, falling back to amd64.\n" .
64 "Edit the config in /etc/pve/nodes/{node}/lxc/{vmid}/config " .
65 "to set another architecture.\n";
a9d131df
TL
66 }
67 });
5b4657d0
DM
68}
69
f507c3a7
WL
70sub tar_archive_search_conf {
71 my ($archive) = @_;
72
73 die "ERROR: file '$archive' does not exist\n" if ! -f $archive;
74
75 my $pid = open(my $fh, '-|', 'tar', 'tf', $archive) ||
27916659 76 die "unable to open file '$archive'\n";
f507c3a7
WL
77
78 my $file;
effa4f43 79 while (defined($file = <$fh>)) {
27916659 80 if ($file =~ m!^(\./etc/vzdump/(pct|vps)\.conf)$!) {
effa4f43
DM
81 $file = $1; # untaint
82 last;
83 }
f507c3a7
WL
84 }
85
86 kill 15, $pid;
87 waitpid $pid, 0;
88 close $fh;
89
effa4f43 90 die "ERROR: archive contains no configuration file\n" if !$file;
f507c3a7
WL
91 chomp $file;
92
93 return $file;
94}
95
96sub recover_config {
effa4f43 97 my ($archive) = @_;
f507c3a7
WL
98
99 my $conf_file = tar_archive_search_conf($archive);
27916659 100
f507c3a7
WL
101 my $raw = '';
102 my $out = sub {
103 my $output = shift;
104 $raw .= "$output\n";
105 };
106
107 PVE::Tools::run_command(['tar', '-xpOf', $archive, $conf_file, '--occurrence'], outfunc => $out);
108
effa4f43 109 my $conf;
3650ed50 110 my $rootfsinfo;
f507c3a7 111
27916659 112 if ($conf_file =~ m/pct\.conf/) {
f507c3a7 113
1b4cf758 114 $conf = PVE::LXC::Config::parse_pct_config("/lxc/0.conf" , $raw);
f507c3a7 115
27916659 116 delete $conf->{snapshots};
bb1ac2de 117 delete $conf->{template}; # restored CT is never a template
27916659
DM
118
119 if (defined($conf->{rootfs})) {
3650ed50 120 $rootfsinfo = PVE::LXC::Config->parse_ct_rootfs($conf->{rootfs});
27916659
DM
121 }
122
effa4f43 123 } elsif ($conf_file =~ m/vps\.conf/) {
27916659 124
3650ed50 125 ($conf, $rootfsinfo) = PVE::VZDump::ConvertOVZ::convert_ovz($raw);
27916659 126
effa4f43
DM
127 } else {
128
27916659 129 die "internal error";
f507c3a7
WL
130 }
131
3650ed50 132 return wantarray ? ($conf, $rootfsinfo) : $conf;
f507c3a7
WL
133}
134
5b4657d0 135sub restore_and_configure {
e404c3ec 136 my ($vmid, $archive, $rootdir, $conf, $password, $restore, $no_unpack_error, $ssh_keys) = @_;
5b4657d0 137
9c23d567 138 restore_archive($archive, $rootdir, $conf, $no_unpack_error);
5b4657d0 139
f507c3a7 140 if (!$restore) {
7af97ad5 141 my $lxc_setup = PVE::LXC::Setup->new($conf, $rootdir); # detect OS
5b4657d0 142
67afe46e 143 PVE::LXC::Config->write_config($vmid, $conf); # safe config (after OS detection)
e404c3ec 144 $lxc_setup->post_create_hook($password, $ssh_keys);
27916659
DM
145 } else {
146 # restore: try to extract configuration from archive
5b4657d0 147
27916659 148 my $pct_cfg_fn = "$rootdir/etc/vzdump/pct.conf";
580b6916 149 my $pct_fwcfg_fn = "$rootdir/etc/vzdump/pct.fw";
27916659
DM
150 my $ovz_cfg_fn = "$rootdir/etc/vzdump/vps.conf";
151 if (-f $pct_cfg_fn) {
152 my $raw = PVE::Tools::file_get_contents($pct_cfg_fn);
1b4cf758 153 my $oldconf = PVE::LXC::Config::parse_pct_config("/lxc/$vmid.conf", $raw);
5b4657d0 154
27916659 155 foreach my $key (keys %$oldconf) {
a9dd8015 156 next if $key eq 'digest' || $key eq 'rootfs' || $key eq 'snapshots' || $key eq 'unprivileged' || $key eq 'parent';
ad2884cb 157 next if $key =~ /^mp\d+$/; # don't recover mountpoints
a9dd8015 158 next if $key =~ /^unused\d+$/; # don't recover unused disks
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) {
febb2af7
WL
164 my $pve_firewall_dir = '/etc/pve/firewall';
165 mkdir $pve_firewall_dir; # make sure the directory exists
166 PVE::Tools::file_copy($pct_fwcfg_fn, "${pve_firewall_dir}/$vmid.fw");
580b6916
WL
167 unlink $pct_fwcfg_fn;
168 }
169
27916659
DM
170 } elsif (-f $ovz_cfg_fn) {
171 print "###########################################################\n";
172 print "Converting OpenVZ configuration to LXC.\n";
173 print "Please check the configuration and reconfigure the network.\n";
174 print "###########################################################\n";
175
d394e3c9
WB
176 my $lxc_setup = PVE::LXC::Setup->new($conf, $rootdir); # detect OS
177 $conf->{ostype} = $lxc_setup->{conf}->{ostype};
27916659
DM
178 my $raw = PVE::Tools::file_get_contents($ovz_cfg_fn);
179 my $oldconf = PVE::VZDump::ConvertOVZ::convert_ovz($raw);
180 foreach my $key (keys %$oldconf) {
181 $conf->{$key} = $oldconf->{$key} if !defined($conf->{$key});
182 }
43a9184a 183 unlink($ovz_cfg_fn);
5b4657d0 184
27916659
DM
185 } else {
186 print "###########################################################\n";
187 print "Backup archive does not contain any configuration\n";
188 print "###########################################################\n";
189 }
190 }
5b4657d0
DM
191}
192
5b4657d0 193sub create_rootfs {
e404c3ec 194 my ($storage_cfg, $vmid, $conf, $archive, $password, $restore, $no_unpack_error, $ssh_keys) = @_;
148d1cb4 195
67afe46e 196 my $config_fn = PVE::LXC::Config->config_file($vmid);
148d1cb4
DM
197 if (-f $config_fn) {
198 die "container exists" if !$restore; # just to be sure
199
67afe46e 200 my $old_conf = PVE::LXC::Config->load_config($vmid);
27916659
DM
201
202 # destroy old container volume
077d7669 203 PVE::LXC::destroy_lxc_container($storage_cfg, $vmid, $old_conf);
148d1cb4
DM
204 }
205
c63f405d
FG
206 PVE::LXC::Config->write_config($vmid, $conf);
207
7fc16e9e 208 eval {
da629848 209 my $rootdir = PVE::LXC::mount_all($vmid, $storage_cfg, $conf);
e404c3ec
FG
210 restore_and_configure($vmid, $archive, $rootdir, $conf, $password,
211 $restore, $no_unpack_error, $ssh_keys);
7fc16e9e 212 };
4497bfe7
WB
213 my $err = $@;
214 PVE::LXC::umount_all($vmid, $storage_cfg, $conf, $err ? 1 : 0);
d250604f 215 PVE::Storage::deactivate_volumes($storage_cfg, PVE::LXC::Config->get_vm_volumes($conf));
4497bfe7 216 die $err if $err;
5b4657d0
DM
217}
218
2191;