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