]> git.proxmox.com Git - pve-container.git/blob - src/PVE/LXCSetup/Redhat.pm
use mingetty on centos6
[pve-container.git] / src / PVE / LXCSetup / Redhat.pm
1 package PVE::LXCSetup::Redhat;
2
3 use strict;
4 use warnings;
5 use Data::Dumper;
6 use PVE::Tools;
7 use PVE::LXC;
8 use File::Path;
9
10 use PVE::LXCSetup::Base;
11
12 use base qw(PVE::LXCSetup::Base);
13
14 sub new {
15 my ($class, $conf, $rootdir) = @_;
16
17 my $release = PVE::Tools::file_read_firstline("$rootdir/etc/redhat-release");
18 die "unable to read version info\n" if !defined($release);
19
20 my $version;
21
22 if ($release =~ m/release\s+(\d\.\d)\s/) {
23 if ($1 >= 6 && $1 < 7) {
24 $version = $1;
25 }
26 }
27
28 die "unsupported redhat release '$release'\n" if !$version;
29
30 my $self = { conf => $conf, rootdir => $rootdir, version => $version };
31
32 $conf->{'lxc.include'} = "/usr/share/lxc/config/centos.common.conf";
33
34 return bless $self, $class;
35 }
36
37 my $tty_conf = <<__EOD__;
38 # tty - getty
39 #
40 # This service maintains a getty on the specified device.
41 #
42 # Do not edit this file directly. If you want to change the behaviour,
43 # please create a file tty.override and put your changes there.
44
45 stop on runlevel [S016]
46
47 respawn
48 instance \$TTY
49 exec /sbin/mingetty \$TTY
50 usage 'tty TTY=/dev/ttyX - where X is console id'
51 __EOD__
52
53 my $start_ttys_conf = <<__EOD__;
54 #
55 # This service starts the configured number of gettys.
56 #
57 # Do not edit this file directly. If you want to change the behaviour,
58 # please create a file start-ttys.override and put your changes there.
59
60 start on stopped rc RUNLEVEL=[2345]
61
62 env ACTIVE_CONSOLES=/dev/tty[1-6]
63 env X_TTY=/dev/tty1
64 task
65 script
66 . /etc/sysconfig/init
67 for tty in \$(echo \$ACTIVE_CONSOLES) ; do
68 [ "\$RUNLEVEL" = "5" -a "\$tty" = "\$X_TTY" ] && continue
69 initctl start tty TTY=\$tty
70 done
71 end script
72 __EOD__
73
74 sub template_fixup {
75 my ($self, $conf) = @_;
76
77 my $rootdir = $self->{rootdir};
78
79 if ($self->{version} < 7) {
80 # re-create emissing files for tty
81
82 mkpath "$rootdir/etc/init";
83
84 my $filename = "$rootdir/etc/init/tty.conf";
85 PVE::Tools::file_set_contents($filename, $tty_conf)
86 if ! -f $filename;
87
88
89 $filename = "$rootdir/etc/init/start-ttys.conf";
90 PVE::Tools::file_set_contents($filename, $start_ttys_conf)
91 if ! -f $filename;
92
93 # do not start udevd
94 $filename = "$rootdir/etc/rc.d/rc.sysinit";
95 my $data = PVE::Tools::file_get_contents($filename);
96 $data =~ s!^(/sbin/start_udev.*)$!#$1!gm;
97 PVE::Tools::file_set_contents($filename, $data);
98
99 # edit /etc/securetty (enable login on console)
100 $filename = "$rootdir/etc/securetty";
101 $data = PVE::Tools::file_get_contents($filename);
102 chomp $data; $data .= "\n";
103 foreach my $dev (qw(console tty1 tty2 tty3 tty4)) {
104 if ($data !~ m!^lxc/$dev\s*$!m) {
105 $data .= "lxc/$dev\n";
106 }
107 }
108 PVE::Tools::file_set_contents($filename, $data);
109 }
110 }
111
112
113 sub setup_init {
114 my ($self, $conf) = @_;
115
116 my $rootdir = $self->{rootdir};
117
118 # edit/etc/securetty
119
120 my $ttycount = defined($conf->{'lxc.tty'}) ? $conf->{'lxc.tty'} : 4;
121
122
123 }
124
125 sub set_hostname {
126 my ($self, $conf) = @_;
127
128 my $hostname = $conf->{'lxc.utsname'} || 'localhost';
129
130 $hostname =~ s/\..*$//;
131
132 my $rootdir = $self->{rootdir};
133
134 my $hostname_fn = "$rootdir/etc/hostname";
135 my $sysconfig_network = "$rootdir/etc/sysconfig/network";
136
137 my $oldname;
138 if (-f $hostname_fn) {
139 $oldname = PVE::Tools::file_read_firstline($hostname_fn) || 'localhost';
140 } else {
141 my $data = PVE::Tools::file_get_contents($sysconfig_network);
142 if ($data =~ m/^HOSTNAME=\s*(\S+)\s*$/m) {
143 $oldname = $1;
144 }
145 }
146
147 my $hosts_fn = "$rootdir/etc/hosts";
148 my $etc_hosts_data = '';
149 if (-f $hosts_fn) {
150 $etc_hosts_data = PVE::Tools::file_get_contents($hosts_fn);
151 }
152
153 my ($ipv4, $ipv6) = PVE::LXC::get_primary_ips($conf);
154 my $hostip = $ipv4 || $ipv6;
155
156 my ($searchdomains) = PVE::LXCSetup::Base::lookup_dns_conf($conf);
157
158 $etc_hosts_data = PVE::LXCSetup::Base::update_etc_hosts($etc_hosts_data, $hostip, $oldname,
159 $hostname, $searchdomains);
160
161 if (-f $hostname_fn) {
162 PVE::Tools::file_set_contents($hostname_fn, "$hostname\n");
163 } else {
164 my $data = PVE::Tools::file_get_contents($sysconfig_network);
165 if ($data !~ s/^HOSTNAME=\s*(\S+)\s*$/HOSTNAME=$hostname\n/m) {
166 $data .= "HOSTNAME=$hostname\n";
167 }
168 PVE::Tools::file_set_contents($sysconfig_network, $data);
169 }
170
171 PVE::Tools::file_set_contents($hosts_fn, $etc_hosts_data);
172 }
173
174 sub setup_network {
175 my ($self, $conf) = @_;
176
177 my $rootdir = $self->{rootdir};
178 my ($gw, $gw6);
179
180 mkpath "$rootdir/etc/sysconfig/network-scripts";
181
182 foreach my $k (keys %$conf) {
183 next if $k !~ m/^net(\d+)$/;
184 my $d = $conf->{$k};
185 if ($d->{name}) {
186 my $filename = "$rootdir/etc/sysconfig/network-scripts/ifcfg-$d->{name}";
187 my $data = "DEVICE=$d->{name}\n";
188 $data .= "ONBOOT=yes\n";
189 $data .= "BOOTPROTO=none\n";
190 if (defined($d->{ip})) {
191 my $ipinfo = PVE::LXC::parse_ipv4_cidr($d->{ip});
192 $data .= "IPADDR=$ipinfo->{address}\n";
193 $data .= "NETMASK=$ipinfo->{netmask}\n";
194 if (defined($d->{gw})) {
195 $data .= "GATEWAY=$d->{gw}\n";
196 }
197 }
198 if (defined($d->{gw6})) {
199 die "implement me";
200 }
201 if (defined($d->{ip6})) {
202 die "implement me";
203 }
204 PVE::Tools::file_set_contents($filename, $data);
205 }
206 }
207 }
208
209 1;