]> git.proxmox.com Git - pve-container.git/blob - src/PVE/LXC/Create.pm
ignore ro flag when creating/restoring CT
[pve-container.git] / src / PVE / LXC / Create.pm
1 package PVE::LXC::Create;
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;
11 use PVE::LXC::Setup;
12 use PVE::VZDump::ConvertOVZ;
13 use PVE::Tools;
14
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
26 sub restore_archive {
27 my ($archive, $rootdir, $conf, $no_unpack_error) = @_;
28
29 my ($id_map, $rootuid, $rootgid) = PVE::LXC::parse_id_maps($conf);
30 my $userns_cmd = PVE::LXC::userns_command($id_map);
31
32 my $cmd = [@$userns_cmd, 'tar', 'xpf', $archive, '--totals',
33 @$PVE::LXC::COMMON_TAR_FLAGS,
34 '-C', $rootdir];
35
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';
41 push @$cmd, '--anchored';
42 push @$cmd, '--exclude' , './dev/*';
43
44 if ($archive eq '-') {
45 print "extracting archive from STDIN\n";
46 eval { PVE::Tools::run_command($cmd, input => "<&STDIN"); };
47 } else {
48 print "extracting archive '$archive'\n";
49 eval { PVE::Tools::run_command($cmd); };
50 }
51 die $@ if $@ && !$no_unpack_error;
52
53 # determine file type of /usr/bin/file itself to get guests' architecture
54 $cmd = [@$userns_cmd, '/usr/bin/file', '-b', '-L', "$rootdir/bin/sh"];
55 PVE::Tools::run_command($cmd, outfunc => sub {
56 shift =~ /^ELF (\d{2}-bit)/; # safely assumes x86 linux
57 my $arch_str = $1;
58 $conf->{'arch'} = 'amd64'; # defaults to 64bit
59 if(defined($arch_str)) {
60 $conf->{'arch'} = 'i386' if $arch_str =~ /32/;
61 print "Detected container architecture: $conf->{'arch'}\n";
62 } else {
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";
66 }
67 });
68 }
69
70 sub 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) ||
76 die "unable to open file '$archive'\n";
77
78 my $file;
79 while (defined($file = <$fh>)) {
80 if ($file =~ m!^(\./etc/vzdump/(pct|vps)\.conf)$!) {
81 $file = $1; # untaint
82 last;
83 }
84 }
85
86 kill 15, $pid;
87 waitpid $pid, 0;
88 close $fh;
89
90 die "ERROR: archive contains no configuration file\n" if !$file;
91 chomp $file;
92
93 return $file;
94 }
95
96 sub recover_config {
97 my ($archive) = @_;
98
99 my $conf_file = tar_archive_search_conf($archive);
100
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
109 my $conf;
110 my $mp_param = {};
111
112 if ($conf_file =~ m/pct\.conf/) {
113
114 $conf = PVE::LXC::Config::parse_pct_config("/lxc/0.conf" , $raw);
115
116 delete $conf->{snapshots};
117 delete $conf->{template}; # restored CT is never a template
118
119 PVE::LXC::Config->foreach_mountpoint($conf, sub {
120 my ($ms, $mountpoint) = @_;
121 $mp_param->{$ms} = $conf->{$ms};
122 });
123
124 } elsif ($conf_file =~ m/vps\.conf/) {
125
126 ($conf, $mp_param) = PVE::VZDump::ConvertOVZ::convert_ovz($raw);
127
128 } else {
129
130 die "internal error";
131 }
132
133 return wantarray ? ($conf, $mp_param) : $conf;
134 }
135
136 sub restore_and_configure {
137 my ($vmid, $archive, $rootdir, $conf, $password, $restore, $no_unpack_error, $ssh_keys) = @_;
138
139 restore_archive($archive, $rootdir, $conf, $no_unpack_error);
140
141 if (!$restore) {
142 my $lxc_setup = PVE::LXC::Setup->new($conf, $rootdir); # detect OS
143
144 PVE::LXC::Config->write_config($vmid, $conf); # safe config (after OS detection)
145 $lxc_setup->post_create_hook($password, $ssh_keys);
146 } else {
147 # restore: try to extract configuration from archive
148
149 my $pct_cfg_fn = "$rootdir/etc/vzdump/pct.conf";
150 my $pct_fwcfg_fn = "$rootdir/etc/vzdump/pct.fw";
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::Config::parse_pct_config("/lxc/$vmid.conf", $raw);
155
156 foreach my $key (keys %$oldconf) {
157 next if $key eq 'digest' || $key eq 'rootfs' || $key eq 'snapshots' || $key eq 'unprivileged' || $key eq 'parent';
158 next if $key =~ /^mp\d+$/; # don't recover mountpoints
159 next if $key =~ /^unused\d+$/; # don't recover unused disks
160 $conf->{$key} = $oldconf->{$key} if !defined($conf->{$key});
161 }
162 unlink($pct_cfg_fn);
163
164 if (-f $pct_fwcfg_fn) {
165 my $pve_firewall_dir = '/etc/pve/firewall';
166 mkdir $pve_firewall_dir; # make sure the directory exists
167 PVE::Tools::file_copy($pct_fwcfg_fn, "${pve_firewall_dir}/$vmid.fw");
168 unlink $pct_fwcfg_fn;
169 }
170
171 } elsif (-f $ovz_cfg_fn) {
172 print "###########################################################\n";
173 print "Converting OpenVZ configuration to LXC.\n";
174 print "Please check the configuration and reconfigure the network.\n";
175 print "###########################################################\n";
176
177 my $lxc_setup = PVE::LXC::Setup->new($conf, $rootdir); # detect OS
178 $conf->{ostype} = $lxc_setup->{conf}->{ostype};
179 my $raw = PVE::Tools::file_get_contents($ovz_cfg_fn);
180 my $oldconf = PVE::VZDump::ConvertOVZ::convert_ovz($raw);
181 foreach my $key (keys %$oldconf) {
182 $conf->{$key} = $oldconf->{$key} if !defined($conf->{$key});
183 }
184 unlink($ovz_cfg_fn);
185
186 } else {
187 print "###########################################################\n";
188 print "Backup archive does not contain any configuration\n";
189 print "###########################################################\n";
190 }
191 }
192 }
193
194 sub create_rootfs {
195 my ($storage_cfg, $vmid, $conf, $archive, $password, $restore, $no_unpack_error, $ssh_keys) = @_;
196
197 my $config_fn = PVE::LXC::Config->config_file($vmid);
198 if (-f $config_fn) {
199 die "container exists" if !$restore; # just to be sure
200
201 my $old_conf = PVE::LXC::Config->load_config($vmid);
202
203 # destroy old container volume
204 PVE::LXC::destroy_lxc_container($storage_cfg, $vmid, $old_conf);
205 }
206
207 PVE::LXC::Config->write_config($vmid, $conf);
208
209 eval {
210 my $rootdir = PVE::LXC::mount_all($vmid, $storage_cfg, $conf, 1);
211 restore_and_configure($vmid, $archive, $rootdir, $conf, $password,
212 $restore, $no_unpack_error, $ssh_keys);
213 };
214 my $err = $@;
215 PVE::LXC::umount_all($vmid, $storage_cfg, $conf, $err ? 1 : 0);
216 PVE::Storage::deactivate_volumes($storage_cfg, PVE::LXC::Config->get_vm_volumes($conf));
217 die $err if $err;
218 }
219
220 1;