]> git.proxmox.com Git - pve-container.git/blame - src/PVE/LXC/Setup.pm
bump version to 1.0-22
[pve-container.git] / src / PVE / LXC / Setup.pm
CommitLineData
7af97ad5 1package PVE::LXC::Setup;
1c7f4f65
DM
2
3use strict;
4use warnings;
f08b2779 5use POSIX;
a8e58e9c 6use PVE::Tools;
1c7f4f65 7
7af97ad5
DM
8use PVE::LXC::Setup::Debian;
9use PVE::LXC::Setup::Ubuntu;
10use PVE::LXC::Setup::Redhat;
11use PVE::LXC::Setup::ArchLinux;
1c7f4f65
DM
12
13my $plugins = {
7af97ad5
DM
14 debian => 'PVE::LXC::Setup::Debian',
15 ubuntu => 'PVE::LXC::Setup::Ubuntu',
16 redhat => 'PVE::LXC::Setup::Redhat',
17 archlinux => 'PVE::LXC::Setup::ArchLinux',
1c7f4f65
DM
18};
19
bdd4194c 20my $autodetect_type = sub {
5b4657d0 21 my ($rootdir) = @_;
a8e58e9c
DM
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") {
bdd4194c 30 return "debian";
c0eae401
DM
31 } elsif (-f "$rootdir/etc/redhat-release") {
32 return "redhat";
c1d32b55
WB
33 } elsif (-f "$rootdir/etc/arch-release") {
34 return "archlinux";
bdd4194c
DM
35 }
36 die "unable to detect OS disribution\n";
37};
38
1c7f4f65 39sub new {
5b4657d0 40 my ($class, $conf, $rootdir, $type) = @_;
1c7f4f65 41
5b4657d0
DM
42 die "no root directory\n" if !$rootdir || $rootdir eq '/';
43
5b3614f8 44 my $self = bless { conf => $conf, rootdir => $rootdir};
1c7f4f65 45
bdd4194c
DM
46 if (!defined($type)) {
47 # try to autodetect type
5b4657d0 48 $type = &$autodetect_type($rootdir);
bdd4194c
DM
49 }
50
633a7bd8 51 my $plugin_class = $plugins->{$type} ||
1c7f4f65
DM
52 "no such OS type '$type'\n";
53
23d928a1
WB
54 my $plugin = $plugin_class->new($conf, $rootdir);
55 $self->{plugin} = $plugin;
f08b2779 56 $self->{in_chroot} = 0;
23d928a1
WB
57
58 # Cache some host files we need access to:
59 $plugin->{host_resolv_conf} = PVE::INotify::read_file('resolvconf');
633a7bd8 60
1c7f4f65
DM
61 return $self;
62}
63
f08b2779
WB
64sub 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
8ba6ef14
WB
78 # can't bind to /proc/$pid/root/dev, it'll bind to the host's /dev
79 my $mountdev = ($rootdir !~ m@^/proc@);
80
f08b2779
WB
81 if (!$child) {
82 # avoid recursive forks
83 $self->{in_chroot} = 1;
84 $self->{plugin}->{in_chroot} = 1;
85 eval {
8ba6ef14 86 PVE::Tools::run_command(['mount', '--bind', '/dev', "$rootdir/dev"]) if $mountdev;
f08b2779
WB
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) {}
8ba6ef14
WB
98 my $status = $? == 0;
99 if ($mountdev) {
100 eval { PVE::Tools::run_command(['umount', "$rootdir/dev"]); };
101 warn $@ if $@;
102 }
103 return $status;
f08b2779
WB
104}
105
142444d5
DM
106sub template_fixup {
107 my ($self) = @_;
108
f08b2779
WB
109 my $code = sub {
110 $self->{plugin}->template_fixup($self->{conf});
111 };
112 $self->protected_call($code);
142444d5
DM
113}
114
1c7f4f65 115sub setup_network {
55fa4e09 116 my ($self) = @_;
1c7f4f65 117
f08b2779
WB
118 my $code = sub {
119 $self->{plugin}->setup_network($self->{conf});
120 };
121 $self->protected_call($code);
1c7f4f65
DM
122}
123
124sub set_hostname {
125 my ($self) = @_;
126
f08b2779
WB
127 my $code = sub {
128 $self->{plugin}->set_hostname($self->{conf});
129 };
130 $self->protected_call($code);
1c7f4f65
DM
131}
132
c325b32f
DM
133sub set_dns {
134 my ($self) = @_;
135
f08b2779
WB
136 my $code = sub {
137 $self->{plugin}->set_dns($self->{conf});
138 };
139 $self->protected_call($code);
c325b32f
DM
140}
141
d66768a2
DM
142sub setup_init {
143 my ($self) = @_;
144
f08b2779
WB
145 my $code = sub {
146 $self->{plugin}->setup_init($self->{conf});
147 };
148 $self->protected_call($code);
d66768a2
DM
149}
150
168d6b07
DM
151sub set_user_password {
152 my ($self, $user, $pw) = @_;
153
f08b2779
WB
154 my $code = sub {
155 $self->{plugin}->set_user_password($self->{conf}, $user, $pw);
156 };
157 $self->protected_call($code);
1c7f4f65
DM
158}
159
7ee31468
DM
160sub rewrite_ssh_host_keys {
161 my ($self) = @_;
162
f08b2779
WB
163 my $code = sub {
164 $self->{plugin}->rewrite_ssh_host_keys($self->{conf});
165 };
166 $self->protected_call($code);
7ee31468
DM
167}
168
d66768a2
DM
169sub pre_start_hook {
170 my ($self) = @_;
171
f08b2779
WB
172 my $code = sub {
173 # Create /fastboot to skip run fsck
174 $self->{plugin}->ct_file_set_contents('/fastboot', '');
2988dbbc 175
f08b2779
WB
176 $self->{plugin}->pre_start_hook($self->{conf});
177 };
178 $self->protected_call($code);
d66768a2
DM
179}
180
181sub post_create_hook {
168d6b07 182 my ($self, $root_password) = @_;
1c7f4f65 183
f08b2779
WB
184 my $code = sub {
185 $self->{plugin}->post_create_hook($self->{conf}, $root_password);
186 };
187 $self->protected_call($code);
1c7f4f65
DM
188}
189
1901;