]> git.proxmox.com Git - pve-container.git/blob - src/PVE/LXC/Setup.pm
LXC::Setup: Load required host files in new()
[pve-container.git] / src / PVE / LXC / Setup.pm
1 package PVE::LXC::Setup;
2
3 use strict;
4 use warnings;
5 use POSIX;
6 use PVE::Tools;
7
8 use PVE::LXC::Setup::Debian;
9 use PVE::LXC::Setup::Ubuntu;
10 use PVE::LXC::Setup::Redhat;
11 use PVE::LXC::Setup::ArchLinux;
12
13 my $plugins = {
14 debian => 'PVE::LXC::Setup::Debian',
15 ubuntu => 'PVE::LXC::Setup::Ubuntu',
16 redhat => 'PVE::LXC::Setup::Redhat',
17 archlinux => 'PVE::LXC::Setup::ArchLinux',
18 };
19
20 my $autodetect_type = sub {
21 my ($rootdir) = @_;
22
23 my $lsb_fn = "$rootdir/etc/lsb-release";
24 if (-f $lsb_fn) {
25 my $data = PVE::Tools::file_get_contents($lsb_fn);
26 if ($data =~ m/^DISTRIB_ID=Ubuntu$/im) {
27 return 'ubuntu';
28 }
29 } elsif (-f "$rootdir/etc/debian_version") {
30 return "debian";
31 } elsif (-f "$rootdir/etc/redhat-release") {
32 return "redhat";
33 } elsif (-f "$rootdir/etc/arch-release") {
34 return "archlinux";
35 }
36 die "unable to detect OS disribution\n";
37 };
38
39 sub new {
40 my ($class, $conf, $rootdir, $type) = @_;
41
42 die "no root directory\n" if !$rootdir || $rootdir eq '/';
43
44 my $self = bless { conf => $conf, rootdir => $rootdir};
45
46 if (!defined($type)) {
47 # try to autodetect type
48 $type = &$autodetect_type($rootdir);
49 }
50
51 my $plugin_class = $plugins->{$type} ||
52 "no such OS type '$type'\n";
53
54 my $plugin = $plugin_class->new($conf, $rootdir);
55 $self->{plugin} = $plugin;
56 $self->{in_chroot} = 0;
57
58 # Cache some host files we need access to:
59 $plugin->{host_resolv_conf} = PVE::INotify::read_file('resolvconf');
60
61 return $self;
62 }
63
64 sub protected_call {
65 my ($self, $sub) = @_;
66
67 # avoid recursion:
68 return $sub->() if $self->{in_chroot};
69
70 my $rootdir = $self->{rootdir};
71 if (!-d "$rootdir/dev" && !mkdir("$rootdir/dev")) {
72 die "failed to create temporary /dev directory: $!\n";
73 }
74
75 my $child = fork();
76 die "fork failed: $!\n" if !defined($child);
77
78 # can't bind to /proc/$pid/root/dev, it'll bind to the host's /dev
79 my $mountdev = ($rootdir !~ m@^/proc@);
80
81 if (!$child) {
82 # avoid recursive forks
83 $self->{in_chroot} = 1;
84 $self->{plugin}->{in_chroot} = 1;
85 eval {
86 PVE::Tools::run_command(['mount', '--bind', '/dev', "$rootdir/dev"]) if $mountdev;
87 chroot($rootdir) or die "failed to change root to: $rootdir: $!\n";
88 chdir('/') or die "failed to change to root directory\n";
89 $sub->();
90 };
91 if (my $err = $@) {
92 print STDERR "$err\n";
93 POSIX::_exit(1);
94 }
95 POSIX::_exit(0);
96 }
97 while (waitpid($child, 0) != $child) {}
98 my $status = $? == 0;
99 if ($mountdev) {
100 eval { PVE::Tools::run_command(['umount', "$rootdir/dev"]); };
101 warn $@ if $@;
102 }
103 return $status;
104 }
105
106 sub template_fixup {
107 my ($self) = @_;
108
109 my $code = sub {
110 $self->{plugin}->template_fixup($self->{conf});
111 };
112 $self->protected_call($code);
113 }
114
115 sub setup_network {
116 my ($self) = @_;
117
118 my $code = sub {
119 $self->{plugin}->setup_network($self->{conf});
120 };
121 $self->protected_call($code);
122 }
123
124 sub set_hostname {
125 my ($self) = @_;
126
127 my $code = sub {
128 $self->{plugin}->set_hostname($self->{conf});
129 };
130 $self->protected_call($code);
131 }
132
133 sub set_dns {
134 my ($self) = @_;
135
136 my $code = sub {
137 $self->{plugin}->set_dns($self->{conf});
138 };
139 $self->protected_call($code);
140 }
141
142 sub setup_init {
143 my ($self) = @_;
144
145 my $code = sub {
146 $self->{plugin}->setup_init($self->{conf});
147 };
148 $self->protected_call($code);
149 }
150
151 sub set_user_password {
152 my ($self, $user, $pw) = @_;
153
154 my $code = sub {
155 $self->{plugin}->set_user_password($self->{conf}, $user, $pw);
156 };
157 $self->protected_call($code);
158 }
159
160 sub rewrite_ssh_host_keys {
161 my ($self) = @_;
162
163 my $code = sub {
164 $self->{plugin}->rewrite_ssh_host_keys($self->{conf});
165 };
166 $self->protected_call($code);
167 }
168
169 sub pre_start_hook {
170 my ($self) = @_;
171
172 my $code = sub {
173 # Create /fastboot to skip run fsck
174 $self->{plugin}->ct_file_set_contents('/fastboot', '');
175
176 $self->{plugin}->pre_start_hook($self->{conf});
177 };
178 $self->protected_call($code);
179 }
180
181 sub post_create_hook {
182 my ($self, $root_password) = @_;
183
184 my $code = sub {
185 $self->{plugin}->post_create_hook($self->{conf}, $root_password);
186 };
187 $self->protected_call($code);
188 }
189
190 1;