]> git.proxmox.com Git - pve-container.git/blob - src/PVE/LXCSetup.pm
new rewrite_ssh_host_keys hook
[pve-container.git] / src / PVE / LXCSetup.pm
1 package PVE::LXCSetup;
2
3 use strict;
4 use warnings;
5 use PVE::Tools;
6
7 use PVE::LXCSetup::Debian;
8 use PVE::LXCSetup::Ubuntu;
9 use PVE::LXCSetup::Redhat;
10
11 my $plugins = {
12 debian => 'PVE::LXCSetup::Debian',
13 ubuntu => 'PVE::LXCSetup::Ubuntu',
14 redhat => 'PVE::LXCSetup::Redhat',
15 };
16
17 my $autodetect_type = sub {
18 my ($rootdir) = @_;
19
20 my $lsb_fn = "$rootdir/etc/lsb-release";
21 if (-f $lsb_fn) {
22 my $data = PVE::Tools::file_get_contents($lsb_fn);
23 if ($data =~ m/^DISTRIB_ID=Ubuntu$/im) {
24 return 'ubuntu';
25 }
26 } elsif (-f "$rootdir/etc/debian_version") {
27 return "debian";
28 } elsif (-f "$rootdir/etc/redhat-release") {
29 return "redhat";
30 }
31 die "unable to detect OS disribution\n";
32 };
33
34 sub new {
35 my ($class, $conf, $rootdir, $type) = @_;
36
37 die "no root directory\n" if !$rootdir || $rootdir eq '/';
38
39 my $self = bless { conf => $conf, $rootdir => $rootdir};
40
41 if (!defined($type)) {
42 # try to autodetect type
43 $type = &$autodetect_type($rootdir);
44 }
45
46 my $plugin_class = $plugins->{$type} ||
47 "no such OS type '$type'\n";
48
49 $self->{plugin} = $plugin_class->new($conf, $rootdir);
50
51 return $self;
52 }
53
54 sub template_fixup {
55 my ($self) = @_;
56
57 $self->{plugin}->template_fixup($self->{conf});
58 }
59
60 sub setup_network {
61 my ($self) = @_;
62
63 $self->{plugin}->setup_network($self->{conf});
64 }
65
66 sub set_hostname {
67 my ($self) = @_;
68
69 $self->{plugin}->set_hostname($self->{conf});
70 }
71
72 sub set_dns {
73 my ($self) = @_;
74
75 $self->{plugin}->set_dns($self->{conf});
76 }
77
78 sub setup_init {
79 my ($self) = @_;
80
81 $self->{plugin}->setup_init($self->{conf});
82 }
83
84 sub set_user_password {
85 my ($self, $user, $pw) = @_;
86
87 $self->{plugin}->set_user_password($self->{conf}, $user, $pw);
88 }
89
90 sub rewrite_ssh_host_keys {
91 my ($self) = @_;
92
93 $self->{plugin}->rewrite_ssh_host_keys($self->{conf});
94 }
95
96 sub pre_start_hook {
97 my ($self) = @_;
98
99 $self->{plugin}->pre_start_hook($self->{conf});
100 }
101
102 sub post_create_hook {
103 my ($self, $root_password) = @_;
104
105 $self->{plugin}->post_create_hook($self->{conf}, $root_password);
106 }
107
108 1;