]>
git.proxmox.com Git - pve-container.git/blob - src/PVE/LXC/Setup.pm
b932be692b41871a751b8037c4064d114bfcf508
1 package PVE
::LXC
::Setup
;
8 use PVE
::LXC
::Setup
::Debian
;
9 use PVE
::LXC
::Setup
::Ubuntu
;
10 use PVE
::LXC
::Setup
::Redhat
;
11 use PVE
::LXC
::Setup
::Fedora
;
12 use PVE
::LXC
::Setup
::SUSE
;
13 use PVE
::LXC
::Setup
::ArchLinux
;
14 use PVE
::LXC
::Setup
::Alpine
;
17 debian
=> 'PVE::LXC::Setup::Debian',
18 ubuntu
=> 'PVE::LXC::Setup::Ubuntu',
19 redhat
=> 'PVE::LXC::Setup::Redhat',
20 fedora
=> 'PVE::LXC::Setup::Fedora',
21 opensuse
=> 'PVE::LXC::Setup::SUSE',
22 archlinux
=> 'PVE::LXC::Setup::ArchLinux',
23 alpine
=> 'PVE::LXC::Setup::Alpine',
26 my $autodetect_type = sub {
29 my $lsb_fn = "$rootdir/etc/lsb-release";
31 my $data = PVE
::Tools
::file_get_contents
($lsb_fn);
32 if ($data =~ m/^DISTRIB_ID=Ubuntu$/im) {
37 if (-f
"$rootdir/etc/debian_version") {
39 } elsif (-f
"$rootdir/etc/SuSE-brand" || -f
"$rootdir/etc/SuSE-release") {
41 } elsif (-f
"$rootdir/etc/fedora-release") {
43 } elsif (-f
"$rootdir/etc/redhat-release") {
45 } elsif (-f
"$rootdir/etc/arch-release") {
47 } elsif (-f
"$rootdir/etc/alpine-release") {
50 die "unable to detect OS disribution\n";
54 my ($class, $conf, $rootdir, $type) = @_;
56 die "no root directory\n" if !$rootdir || $rootdir eq '/';
58 my $self = bless { conf
=> $conf, rootdir
=> $rootdir};
60 if (!defined($type)) {
61 # try to autodetect type
62 $type = &$autodetect_type($rootdir);
65 my $plugin_class = $plugins->{$type} ||
66 "no such OS type '$type'\n";
68 my $plugin = $plugin_class->new($conf, $rootdir);
69 $self->{plugin
} = $plugin;
70 $self->{in_chroot
} = 0;
72 # Cache some host files we need access to:
73 $plugin->{host_resolv_conf
} = PVE
::INotify
::read_file
('resolvconf');
75 # pass on user namespace information:
76 my ($id_map, $rootuid, $rootgid) = PVE
::LXC
::parse_id_maps
($conf);
78 $plugin->{id_map
} = $id_map;
79 $plugin->{rootuid
} = $rootuid;
80 $plugin->{rootgid
} = $rootgid;
87 my ($self, $sub) = @_;
90 return $sub->() if $self->{in_chroot
};
92 my $rootdir = $self->{rootdir
};
93 if (!-d
"$rootdir/dev" && !mkdir("$rootdir/dev")) {
94 die "failed to create temporary /dev directory: $!\n";
98 die "fork failed: $!\n" if !defined($child);
101 # avoid recursive forks
102 $self->{in_chroot
} = 1;
103 $self->{plugin
}->{in_chroot
} = 1;
105 chroot($rootdir) or die "failed to change root to: $rootdir: $!\n";
106 chdir('/') or die "failed to change to root directory\n";
115 while (waitpid($child, 0) != $child) {}
117 my $method = (caller(1))[3];
118 die "error in setup task $method\n";
126 $self->{plugin
}->template_fixup($self->{conf
});
128 $self->protected_call($code);
135 $self->{plugin
}->setup_network($self->{conf
});
137 $self->protected_call($code);
144 $self->{plugin
}->set_hostname($self->{conf
});
146 $self->protected_call($code);
153 $self->{plugin
}->set_dns($self->{conf
});
155 $self->protected_call($code);
162 $self->{plugin
}->setup_init($self->{conf
});
164 $self->protected_call($code);
167 sub set_user_password
{
168 my ($self, $user, $pw) = @_;
171 $self->{plugin
}->set_user_password($self->{conf
}, $user, $pw);
173 $self->protected_call($code);
176 sub rewrite_ssh_host_keys
{
179 my $conf = $self->{conf
};
180 my $plugin = $self->{plugin
};
181 my $rootdir = $self->{rootdir
};
183 return if ! -d
"$rootdir/etc/ssh";
186 rsa1
=> 'ssh_host_key',
187 rsa
=> 'ssh_host_rsa_key',
188 dsa
=> 'ssh_host_dsa_key',
189 ecdsa
=> 'ssh_host_ecdsa_key',
190 ed25519
=> 'ssh_host_ed25519_key',
193 my $hostname = $conf->{hostname
} || 'localhost';
194 $hostname =~ s/\..*$//;
196 # Create temporary keys in /tmp on the host
199 foreach my $keytype (keys %$keynames) {
200 my $basename = $keynames->{$keytype};
201 my $file = "/tmp/$$.$basename";
202 print "Creating SSH host key '$basename' - this may take some time ...\n";
203 my $cmd = ['ssh-keygen', '-q', '-f', $file, '-t', $keytype,
204 '-N', '', '-C', "root\@$hostname"];
205 PVE
::Tools
::run_command
($cmd);
206 $keyfiles->{"/etc/ssh/$basename"} = [PVE
::Tools
::file_get_contents
($file), 0600];
207 $keyfiles->{"/etc/ssh/$basename.pub"} = [PVE
::Tools
::file_get_contents
("$file.pub"), 0644];
212 # Write keys out in a protected call
215 foreach my $file (keys %$keyfiles) {
216 $plugin->ct_file_set_contents($file, @{$keyfiles->{$file}});
219 $self->protected_call($code);
226 # Create /fastboot to skip run fsck
227 $self->{plugin
}->ct_file_set_contents('/fastboot', '');
229 $self->{plugin
}->pre_start_hook($self->{conf
});
231 $self->protected_call($code);
234 sub post_create_hook
{
235 my ($self, $root_password) = @_;
238 $self->{plugin
}->post_create_hook($self->{conf
}, $root_password);
240 $self->protected_call($code);
241 $self->rewrite_ssh_host_keys();