]>
git.proxmox.com Git - pve-container.git/blob - src/PVE/LXC/Setup.pm
1 package PVE
::LXC
::Setup
;
8 use PVE
::LXC
::Setup
::Debian
;
9 use PVE
::LXC
::Setup
::Ubuntu
;
10 use PVE
::LXC
::Setup
::CentOS
;
11 use PVE
::LXC
::Setup
::Fedora
;
12 use PVE
::LXC
::Setup
::SUSE
;
13 use PVE
::LXC
::Setup
::ArchLinux
;
14 use PVE
::LXC
::Setup
::Alpine
;
15 use PVE
::LXC
::Setup
::Gentoo
;
18 debian
=> 'PVE::LXC::Setup::Debian',
19 ubuntu
=> 'PVE::LXC::Setup::Ubuntu',
20 centos
=> 'PVE::LXC::Setup::CentOS',
21 fedora
=> 'PVE::LXC::Setup::Fedora',
22 opensuse
=> 'PVE::LXC::Setup::SUSE',
23 archlinux
=> 'PVE::LXC::Setup::ArchLinux',
24 arch
=> 'PVE::LXC::Setup::ArchLinux',
25 alpine
=> 'PVE::LXC::Setup::Alpine',
26 gentoo
=> 'PVE::LXC::Setup::Gentoo',
29 my $autodetect_type = sub {
32 my $lsb_fn = "$rootdir/etc/lsb-release";
34 my $data = PVE
::Tools
::file_get_contents
($lsb_fn);
35 if ($data =~ m/^DISTRIB_ID=Ubuntu$/im) {
40 if (-f
"$rootdir/etc/debian_version") {
42 } elsif (-f
"$rootdir/etc/SuSE-brand" || -f
"$rootdir/etc/SuSE-release") {
44 } elsif (-f
"$rootdir/etc/fedora-release") {
46 } elsif (-f
"$rootdir/etc/centos-release" || -f
"$rootdir/etc/redhat-release") {
48 } elsif (-f
"$rootdir/etc/arch-release") {
50 } elsif (-f
"$rootdir/etc/alpine-release") {
52 } elsif (-f
"$rootdir/etc/gentoo-release") {
55 die "unable to detect OS distribution\n";
59 my ($class, $conf, $rootdir, $type) = @_;
61 die "no root directory\n" if !$rootdir || $rootdir eq '/';
63 my $self = bless { conf
=> $conf, rootdir
=> $rootdir};
65 if ($conf->{ostype
} && $conf->{ostype
} eq 'unmanaged') {
67 } elsif (!defined($type)) {
68 # try to autodetect type
69 $type = &$autodetect_type($rootdir);
70 my $expected_type = $conf->{ostype
} || $type;
72 die "got unexpected ostype ($type != $expected_type)\n"
73 if $type ne $expected_type;
76 my $plugin_class = $plugins->{$type} ||
77 "no such OS type '$type'\n";
79 my $plugin = $plugin_class->new($conf, $rootdir);
80 $self->{plugin
} = $plugin;
81 $self->{in_chroot
} = 0;
83 # Cache some host files we need access to:
84 $plugin->{host_resolv_conf
} = PVE
::INotify
::read_file
('resolvconf');
86 # pass on user namespace information:
87 my ($id_map, $rootuid, $rootgid) = PVE
::LXC
::parse_id_maps
($conf);
89 $plugin->{id_map
} = $id_map;
90 $plugin->{rootuid
} = $rootuid;
91 $plugin->{rootgid
} = $rootgid;
97 # Forks into a chroot and executes $sub
99 my ($self, $sub) = @_;
102 return $sub->() if $self->{in_chroot
};
104 my $rootdir = $self->{rootdir
};
105 if (!-d
"$rootdir/dev" && !mkdir("$rootdir/dev")) {
106 die "failed to create temporary /dev directory: $!\n";
109 pipe(my $res_in, my $res_out) or die "pipe failed: $!\n";
112 die "fork failed: $!\n" if !defined($child);
116 # avoid recursive forks
117 $self->{in_chroot
} = 1;
119 chroot($rootdir) or die "failed to change root to: $rootdir: $!\n";
120 chdir('/') or die "failed to change to root directory\n";
123 print {$res_out} "$res";
134 my $result = do { local $/ = undef; <$res_in> };
135 while (waitpid($child, 0) != $child) {}
137 my $method = (caller(1))[3];
138 die "error in setup task $method\n";
146 return if !$self->{plugin
}; # unmanaged
149 $self->{plugin
}->template_fixup($self->{conf
});
151 $self->protected_call($code);
157 return if !$self->{plugin
}; # unmanaged
160 $self->{plugin
}->setup_network($self->{conf
});
162 $self->protected_call($code);
168 return if !$self->{plugin
}; # unmanaged
171 $self->{plugin
}->set_hostname($self->{conf
});
173 $self->protected_call($code);
179 return if !$self->{plugin
}; # unmanaged
182 $self->{plugin
}->set_dns($self->{conf
});
184 $self->protected_call($code);
190 return if !$self->{plugin
}; # unmanaged
193 $self->{plugin
}->setup_init($self->{conf
});
195 $self->protected_call($code);
198 sub set_user_password
{
199 my ($self, $user, $pw) = @_;
201 return if !$self->{plugin
}; # unmanaged
204 $self->{plugin
}->set_user_password($self->{conf
}, $user, $pw);
206 $self->protected_call($code);
209 sub rewrite_ssh_host_keys
{
212 return if !$self->{plugin
}; # unmanaged
214 my $conf = $self->{conf
};
215 my $plugin = $self->{plugin
};
216 my $rootdir = $self->{rootdir
};
218 return if ! -d
"$rootdir/etc/ssh";
221 rsa
=> 'ssh_host_rsa_key',
222 dsa
=> 'ssh_host_dsa_key',
223 ecdsa
=> 'ssh_host_ecdsa_key',
224 ed25519
=> 'ssh_host_ed25519_key',
227 my $hostname = $conf->{hostname
} || 'localhost';
228 $hostname =~ s/\..*$//;
229 my $ssh_comment = "root\@$hostname";
231 my $keygen_outfunc = sub {
234 print "done: $line\n"
235 if $line =~ m/^(?:[0-9a-f]{2}:)+[0-9a-f]{2}\s+\Q$ssh_comment\E$/i ||
236 $line =~ m/^SHA256:[0-9a-z+\/]{43}\s
+\Q
$ssh_comment\E$/i;
239 # Create temporary keys in /tmp on the host
241 foreach my $keytype (keys %$keynames) {
242 my $basename = $keynames->{$keytype};
243 my $file = "/tmp/$$.$basename";
244 print "Creating SSH host key '$basename' - this may take some time ...\n";
245 my $cmd = ['ssh-keygen', '-f', $file, '-t', $keytype,
246 '-N', '', '-E', 'sha256', '-C', $ssh_comment];
247 PVE
::Tools
::run_command
($cmd, outfunc
=> $keygen_outfunc);
248 $keyfiles->{"/etc/ssh/$basename"} = [PVE
::Tools
::file_get_contents
($file), 0600];
249 $keyfiles->{"/etc/ssh/$basename.pub"} = [PVE
::Tools
::file_get_contents
("$file.pub"), 0644];
254 # Write keys out in a protected call
257 foreach my $file (keys %$keyfiles) {
258 $plugin->ct_file_set_contents($file, @{$keyfiles->{$file}});
261 $self->protected_call($code);
267 return if !$self->{plugin
}; # unmanaged
270 # Create /fastboot to skip run fsck
271 $self->{plugin
}->ct_file_set_contents('/fastboot', '');
273 $self->{plugin
}->pre_start_hook($self->{conf
});
275 $self->protected_call($code);
278 sub post_create_hook
{
279 my ($self, $root_password, $ssh_keys) = @_;
281 return if !$self->{plugin
}; # unmanaged
284 $self->{plugin
}->post_create_hook($self->{conf
}, $root_password, $ssh_keys);
286 $self->protected_call($code);
287 $self->rewrite_ssh_host_keys();