]>
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 | 26 | sub 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; |
f31bd6ae DC |
52 | |
53 | # if arch is set, we do not try to autodetect it | |
54 | return if defined($conf->{arch}); | |
55 | ||
27916659 | 56 | # determine file type of /usr/bin/file itself to get guests' architecture |
6cc47cb0 | 57 | $cmd = [@$userns_cmd, '/usr/bin/file', '-b', '-L', "$rootdir/bin/sh"]; |
a9d131df TL |
58 | PVE::Tools::run_command($cmd, outfunc => sub { |
59 | shift =~ /^ELF (\d{2}-bit)/; # safely assumes x86 linux | |
60 | my $arch_str = $1; | |
27916659 | 61 | $conf->{'arch'} = 'amd64'; # defaults to 64bit |
a9d131df | 62 | if(defined($arch_str)) { |
27916659 DM |
63 | $conf->{'arch'} = 'i386' if $arch_str =~ /32/; |
64 | print "Detected container architecture: $conf->{'arch'}\n"; | |
a9d131df | 65 | } else { |
27916659 | 66 | print "CT architecture detection failed, falling back to amd64.\n" . |
e9e0e69f | 67 | "Edit the config in /etc/pve/nodes/{node}/lxc/{vmid}.conf " . |
27916659 | 68 | "to set another architecture.\n"; |
a9d131df TL |
69 | } |
70 | }); | |
5b4657d0 DM |
71 | } |
72 | ||
f507c3a7 | 73 | sub recover_config { |
effa4f43 | 74 | my ($archive) = @_; |
f507c3a7 | 75 | |
0550795a | 76 | my ($raw, $conf_file) = PVE::Storage::extract_vzdump_config_tar($archive, qr!(\./etc/vzdump/(pct|vps)\.conf)$!); |
effa4f43 | 77 | my $conf; |
db18c1e4 | 78 | my $mp_param = {}; |
f507c3a7 | 79 | |
27916659 | 80 | if ($conf_file =~ m/pct\.conf/) { |
f507c3a7 | 81 | |
1b4cf758 | 82 | $conf = PVE::LXC::Config::parse_pct_config("/lxc/0.conf" , $raw); |
f507c3a7 | 83 | |
27916659 | 84 | delete $conf->{snapshots}; |
bb1ac2de | 85 | delete $conf->{template}; # restored CT is never a template |
db18c1e4 FG |
86 | |
87 | PVE::LXC::Config->foreach_mountpoint($conf, sub { | |
88 | my ($ms, $mountpoint) = @_; | |
89 | $mp_param->{$ms} = $conf->{$ms}; | |
90 | }); | |
91 | ||
effa4f43 | 92 | } elsif ($conf_file =~ m/vps\.conf/) { |
db18c1e4 FG |
93 | |
94 | ($conf, $mp_param) = PVE::VZDump::ConvertOVZ::convert_ovz($raw); | |
95 | ||
effa4f43 DM |
96 | } else { |
97 | ||
27916659 | 98 | die "internal error"; |
f507c3a7 WL |
99 | } |
100 | ||
db18c1e4 | 101 | return wantarray ? ($conf, $mp_param) : $conf; |
f507c3a7 WL |
102 | } |
103 | ||
51665c2d | 104 | sub restore_configuration { |
ba0e2930 | 105 | my ($vmid, $rootdir, $conf, $restricted) = @_; |
51665c2d FG |
106 | |
107 | # restore: try to extract configuration from archive | |
108 | ||
109 | my $pct_cfg_fn = "$rootdir/etc/vzdump/pct.conf"; | |
110 | my $pct_fwcfg_fn = "$rootdir/etc/vzdump/pct.fw"; | |
111 | my $ovz_cfg_fn = "$rootdir/etc/vzdump/vps.conf"; | |
112 | if (-f $pct_cfg_fn) { | |
113 | my $raw = PVE::Tools::file_get_contents($pct_cfg_fn); | |
114 | my $oldconf = PVE::LXC::Config::parse_pct_config("/lxc/$vmid.conf", $raw); | |
115 | ||
116 | foreach my $key (keys %$oldconf) { | |
adcaa73e | 117 | next if $key eq 'digest' || $key eq 'rootfs' || $key eq 'snapshots' || $key eq 'unprivileged' || $key eq 'parent'; |
51665c2d FG |
118 | next if $key =~ /^mp\d+$/; # don't recover mountpoints |
119 | next if $key =~ /^unused\d+$/; # don't recover unused disks | |
ba0e2930 FG |
120 | if ($restricted && $key eq 'lxc') { |
121 | warn "skipping custom lxc options, restore manually as root:\n"; | |
122 | warn "--------------------------------\n"; | |
123 | my $lxc_list = $oldconf->{'lxc'}; | |
124 | foreach my $lxc_opt (@$lxc_list) { | |
125 | warn "$lxc_opt->[0]: $lxc_opt->[1]\n" | |
126 | } | |
127 | warn "--------------------------------\n"; | |
128 | next; | |
129 | } | |
51665c2d FG |
130 | $conf->{$key} = $oldconf->{$key} if !defined($conf->{$key}); |
131 | } | |
132 | unlink($pct_cfg_fn); | |
5b4657d0 | 133 | |
51665c2d FG |
134 | if (-f $pct_fwcfg_fn) { |
135 | my $pve_firewall_dir = '/etc/pve/firewall'; | |
136 | mkdir $pve_firewall_dir; # make sure the directory exists | |
137 | PVE::Tools::file_copy($pct_fwcfg_fn, "${pve_firewall_dir}/$vmid.fw"); | |
138 | unlink $pct_fwcfg_fn; | |
27916659 | 139 | } |
5b4657d0 | 140 | |
51665c2d FG |
141 | } elsif (-f $ovz_cfg_fn) { |
142 | print "###########################################################\n"; | |
143 | print "Converting OpenVZ configuration to LXC.\n"; | |
144 | print "Please check the configuration and reconfigure the network.\n"; | |
145 | print "###########################################################\n"; | |
148d1cb4 | 146 | |
51665c2d FG |
147 | my $lxc_setup = PVE::LXC::Setup->new($conf, $rootdir); # detect OS |
148 | $conf->{ostype} = $lxc_setup->{conf}->{ostype}; | |
149 | my $raw = PVE::Tools::file_get_contents($ovz_cfg_fn); | |
150 | my $oldconf = PVE::VZDump::ConvertOVZ::convert_ovz($raw); | |
151 | foreach my $key (keys %$oldconf) { | |
152 | $conf->{$key} = $oldconf->{$key} if !defined($conf->{$key}); | |
153 | } | |
154 | unlink($ovz_cfg_fn); | |
148d1cb4 | 155 | |
51665c2d FG |
156 | } else { |
157 | print "###########################################################\n"; | |
158 | print "Backup archive does not contain any configuration\n"; | |
159 | print "###########################################################\n"; | |
148d1cb4 | 160 | } |
5b4657d0 DM |
161 | } |
162 | ||
163 | 1; |