]>
git.proxmox.com Git - pve-container.git/blob - src/PVE/LXC/Create.pm
1 package PVE
::LXC
::Create
;
12 use PVE
::VZDump
::ConvertOVZ
;
14 sub next_free_nbd_dev
{
16 for(my $i = 0;;$i++) {
17 my $dev = "/dev/nbd$i";
19 next if -f
"/sys/block/nbd$i/pid"; # busy
22 die "unable to find free nbd device\n";
26 my ($archive, $rootdir, $conf) = @_;
28 my ($id_map, $rootuid, $rootgid) = PVE
::LXC
::parse_id_maps
($conf);
29 my $userns_cmd = PVE
::LXC
::userns_command
($id_map);
31 my $cmd = [@$userns_cmd, 'tar', 'xpf', $archive, '--totals',
32 @$PVE::LXC
::COMMON_TAR_FLAGS
,
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
39 push @$cmd, '--skip-old-files';
40 push @$cmd, '--anchored';
41 push @$cmd, '--exclude' , './dev/*';
43 if ($archive eq '-') {
44 print "extracting archive from STDIN\n";
45 PVE
::Tools
::run_command
($cmd, input
=> "<&STDIN");
47 print "extracting archive '$archive'\n";
48 PVE
::Tools
::run_command
($cmd);
51 # determine file type of /usr/bin/file itself to get guests' architecture
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
56 $conf->{'arch'} = 'amd64'; # defaults to 64bit
57 if(defined($arch_str)) {
58 $conf->{'arch'} = 'i386' if $arch_str =~ /32/;
59 print "Detected container architecture: $conf->{'arch'}\n";
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";
68 sub tar_archive_search_conf
{
71 die "ERROR: file '$archive' does not exist\n" if ! -f
$archive;
73 my $pid = open(my $fh, '-|', 'tar', 'tf', $archive) ||
74 die "unable to open file '$archive'\n";
77 while (defined($file = <$fh>)) {
78 if ($file =~ m!^(\./etc/vzdump/(pct|vps)\.conf)$!) {
88 die "ERROR: archive contains no configuration file\n" if !$file;
97 my $conf_file = tar_archive_search_conf
($archive);
105 PVE
::Tools
::run_command
(['tar', '-xpOf', $archive, $conf_file, '--occurrence'], outfunc
=> $out);
110 if ($conf_file =~ m/pct\.conf/) {
112 $conf = PVE
::LXC
::parse_pct_config
("/lxc/0.conf" , $raw);
114 delete $conf->{snapshots
};
115 delete $conf->{template
}; # restored CT is never a template
117 if (defined($conf->{rootfs
})) {
118 my $rootinfo = PVE
::LXC
::parse_ct_mountpoint
($conf->{rootfs
});
119 $disksize = $rootinfo->{size
} if defined($rootinfo->{size
});
122 } elsif ($conf_file =~ m/vps\.conf/) {
124 ($conf, $disksize) = PVE
::VZDump
::ConvertOVZ
::convert_ovz
($raw);
128 die "internal error";
131 return wantarray ?
($conf, $disksize) : $conf;
134 sub restore_and_configure
{
135 my ($vmid, $archive, $rootdir, $conf, $password, $restore) = @_;
137 restore_archive
($archive, $rootdir, $conf);
140 my $lxc_setup = PVE
::LXC
::Setup-
>new($conf, $rootdir); # detect OS
142 PVE
::LXC
::write_config
($vmid, $conf); # safe config (after OS detection)
143 $lxc_setup->post_create_hook($password);
145 # restore: try to extract configuration from archive
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);
153 foreach my $key (keys %$oldconf) {
154 next if $key eq 'digest' || $key eq 'rootfs' || $key eq 'snapshots' || $key eq 'unprivileged';
155 $conf->{$key} = $oldconf->{$key} if !defined($conf->{$key});
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";
165 my $lxc_setup = PVE
::LXC
::Setup-
>new($conf, $rootdir); # detect OS
166 $conf->{ostype
} = $lxc_setup->{conf
}->{ostype
};
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});
175 print "###########################################################\n";
176 print "Backup archive does not contain any configuration\n";
177 print "###########################################################\n";
183 my ($storage_cfg, $vmid, $conf, $archive, $password, $restore) = @_;
185 my $config_fn = PVE
::LXC
::config_file
($vmid);
187 die "container exists" if !$restore; # just to be sure
189 my $old_conf = PVE
::LXC
::load_config
($vmid);
191 # destroy old container volume
192 PVE
::LXC
::destroy_lxc_container
($storage_cfg, $vmid, $old_conf);
194 # do not copy all settings to restored container
195 foreach my $opt (qw(rootfs digest snapshots arch ostype unprivileged)) {
196 delete $old_conf->{$opt};
198 foreach my $opt (keys %$old_conf) {
199 delete $old_conf->{$opt} if $opt =~ m/^mp\d+$/;
202 PVE
::LXC
::update_pct_config
($vmid, $conf, 0, $old_conf);
204 PVE
::LXC
::create_config
($vmid, $conf);
208 PVE
::LXC
::create_config
($vmid, $conf);
212 my $rootdir = PVE
::LXC
::mount_all
($vmid, $storage_cfg, $conf);
213 restore_and_configure
($vmid, $archive, $rootdir, $conf, $password, $restore);
216 PVE
::LXC
::umount_all
($vmid, $storage_cfg, $conf, $err ?
1 : 0);
217 PVE
::Storage
::deactivate_volumes
($storage_cfg, PVE
::LXC
::get_vm_volumes
($conf));