]>
git.proxmox.com Git - pve-container.git/blob - src/PVE/LXC/Setup.pm
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
::ArchLinux
;
14 debian
=> 'PVE::LXC::Setup::Debian',
15 ubuntu
=> 'PVE::LXC::Setup::Ubuntu',
16 redhat
=> 'PVE::LXC::Setup::Redhat',
17 archlinux
=> 'PVE::LXC::Setup::ArchLinux',
20 my $autodetect_type = sub {
23 my $lsb_fn = "$rootdir/etc/lsb-release";
25 my $data = PVE
::Tools
::file_get_contents
($lsb_fn);
26 if ($data =~ m/^DISTRIB_ID=Ubuntu$/im) {
29 } elsif (-f
"$rootdir/etc/debian_version") {
31 } elsif (-f
"$rootdir/etc/redhat-release") {
33 } elsif (-f
"$rootdir/etc/arch-release") {
36 die "unable to detect OS disribution\n";
40 my ($class, $conf, $rootdir, $type) = @_;
42 die "no root directory\n" if !$rootdir || $rootdir eq '/';
44 my $self = bless { conf
=> $conf, rootdir
=> $rootdir};
46 if (!defined($type)) {
47 # try to autodetect type
48 $type = &$autodetect_type($rootdir);
51 my $plugin_class = $plugins->{$type} ||
52 "no such OS type '$type'\n";
54 my $plugin = $plugin_class->new($conf, $rootdir);
55 $self->{plugin
} = $plugin;
56 $self->{in_chroot
} = 0;
58 # Cache some host files we need access to:
59 $plugin->{host_resolv_conf
} = PVE
::INotify
::read_file
('resolvconf');
65 my ($self, $sub) = @_;
68 return $sub->() if $self->{in_chroot
};
70 my $rootdir = $self->{rootdir
};
71 if (!-d
"$rootdir/dev" && !mkdir("$rootdir/dev")) {
72 die "failed to create temporary /dev directory: $!\n";
76 die "fork failed: $!\n" if !defined($child);
79 # avoid recursive forks
80 $self->{in_chroot
} = 1;
81 $self->{plugin
}->{in_chroot
} = 1;
83 chroot($rootdir) or die "failed to change root to: $rootdir: $!\n";
84 chdir('/') or die "failed to change to root directory\n";
93 while (waitpid($child, 0) != $child) {}
94 die "setup error" if $? != 0;
101 $self->{plugin
}->template_fixup($self->{conf
});
103 $self->protected_call($code);
110 $self->{plugin
}->setup_network($self->{conf
});
112 $self->protected_call($code);
119 $self->{plugin
}->set_hostname($self->{conf
});
121 $self->protected_call($code);
128 $self->{plugin
}->set_dns($self->{conf
});
130 $self->protected_call($code);
137 $self->{plugin
}->setup_init($self->{conf
});
139 $self->protected_call($code);
142 sub set_user_password
{
143 my ($self, $user, $pw) = @_;
146 $self->{plugin
}->set_user_password($self->{conf
}, $user, $pw);
148 $self->protected_call($code);
151 sub rewrite_ssh_host_keys
{
154 my $conf = $self->{conf
};
155 my $plugin = $self->{plugin
};
156 my $rootdir = $self->{rootdir
};
158 return if ! -d
"$rootdir/etc/ssh";
161 rsa1
=> 'ssh_host_key',
162 rsa
=> 'ssh_host_rsa_key',
163 dsa
=> 'ssh_host_dsa_key',
164 ecdsa
=> 'ssh_host_ecdsa_key',
165 ed25519
=> 'ssh_host_ed25519_key',
168 my $hostname = $conf->{hostname
} || 'localhost';
169 $hostname =~ s/\..*$//;
171 # Create temporary keys in /tmp on the host
174 foreach my $keytype (keys %$keynames) {
175 my $basename = $keynames->{$keytype};
176 my $file = "/tmp/$$.$basename";
177 print "Creating SSH host key '$basename' - this may take some time ...\n";
178 my $cmd = ['ssh-keygen', '-q', '-f', $file, '-t', $keytype,
179 '-N', '', '-C', "root\@$hostname"];
180 PVE
::Tools
::run_command
($cmd);
181 $keyfiles->{"/etc/ssh/$basename"} = PVE
::Tools
::file_get_contents
($file);
182 $keyfiles->{"/etc/ssh/$basename.pub"} = PVE
::Tools
::file_get_contents
("$file.pub");
187 # Write keys out in a protected call
190 foreach my $file (keys %$keyfiles) {
191 $plugin->ct_file_set_contents($file, $keyfiles->{$file});
194 $self->protected_call($code);
201 # Create /fastboot to skip run fsck
202 $self->{plugin
}->ct_file_set_contents('/fastboot', '');
204 $self->{plugin
}->pre_start_hook($self->{conf
});
206 $self->protected_call($code);
209 sub post_create_hook
{
210 my ($self, $root_password) = @_;
213 $self->{plugin
}->post_create_hook($self->{conf
}, $root_password);
215 $self->protected_call($code);
216 $self->rewrite_ssh_host_keys();