]> git.proxmox.com Git - pve-container.git/blob - src/PVE/LXCSetup/Redhat.pm
debian setup: dhcp, manual and unmanaged network
[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 my $power_status_changed_conf = <<__EOD__;
75 # power-status-changed - shutdown on SIGPWR
76 #
77 start on power-status-changed
78
79 exec /sbin/shutdown -h now "SIGPWR received"
80 __EOD__
81
82 sub template_fixup {
83 my ($self, $conf) = @_;
84
85 my $rootdir = $self->{rootdir};
86
87 if ($self->{version} < 7) {
88 # re-create emissing files for tty
89
90 mkpath "$rootdir/etc/init";
91
92 my $filename = "$rootdir/etc/init/tty.conf";
93 PVE::Tools::file_set_contents($filename, $tty_conf)
94 if ! -f $filename;
95
96
97 $filename = "$rootdir/etc/init/start-ttys.conf";
98 PVE::Tools::file_set_contents($filename, $start_ttys_conf)
99 if ! -f $filename;
100
101 $filename = "$rootdir/etc/init/power-status-changed.conf";
102 PVE::Tools::file_set_contents($filename, $power_status_changed_conf)
103 if ! -f $filename;
104
105 # do not start udevd
106 $filename = "$rootdir/etc/rc.d/rc.sysinit";
107 my $data = PVE::Tools::file_get_contents($filename);
108 $data =~ s!^(/sbin/start_udev.*)$!#$1!gm;
109 PVE::Tools::file_set_contents($filename, $data);
110
111 # edit /etc/securetty (enable login on console)
112 $filename = "$rootdir/etc/securetty";
113 $data = PVE::Tools::file_get_contents($filename);
114 chomp $data; $data .= "\n";
115 foreach my $dev (qw(console tty1 tty2 tty3 tty4)) {
116 if ($data !~ m!^lxc/$dev\s*$!m) {
117 $data .= "lxc/$dev\n";
118 }
119 }
120 PVE::Tools::file_set_contents($filename, $data);
121 }
122 }
123
124
125 sub setup_init {
126 my ($self, $conf) = @_;
127
128 my $rootdir = $self->{rootdir};
129
130 # edit/etc/securetty
131
132 my $ttycount = defined($conf->{'lxc.tty'}) ? $conf->{'lxc.tty'} : 4;
133
134
135 }
136
137 sub set_hostname {
138 my ($self, $conf) = @_;
139
140 my $hostname = $conf->{'lxc.utsname'} || 'localhost';
141
142 $hostname =~ s/\..*$//;
143
144 my $rootdir = $self->{rootdir};
145
146 my $hostname_fn = "$rootdir/etc/hostname";
147 my $sysconfig_network = "$rootdir/etc/sysconfig/network";
148
149 my $oldname;
150 if (-f $hostname_fn) {
151 $oldname = PVE::Tools::file_read_firstline($hostname_fn) || 'localhost';
152 } else {
153 my $data = PVE::Tools::file_get_contents($sysconfig_network);
154 if ($data =~ m/^HOSTNAME=\s*(\S+)\s*$/m) {
155 $oldname = $1;
156 }
157 }
158
159 my $hosts_fn = "$rootdir/etc/hosts";
160 my $etc_hosts_data = '';
161 if (-f $hosts_fn) {
162 $etc_hosts_data = PVE::Tools::file_get_contents($hosts_fn);
163 }
164
165 my ($ipv4, $ipv6) = PVE::LXC::get_primary_ips($conf);
166 my $hostip = $ipv4 || $ipv6;
167
168 my ($searchdomains) = PVE::LXCSetup::Base::lookup_dns_conf($conf);
169
170 $etc_hosts_data = PVE::LXCSetup::Base::update_etc_hosts($etc_hosts_data, $hostip, $oldname,
171 $hostname, $searchdomains);
172
173 if (-f $hostname_fn) {
174 PVE::Tools::file_set_contents($hostname_fn, "$hostname\n");
175 } else {
176 my $data = PVE::Tools::file_get_contents($sysconfig_network);
177 if ($data !~ s/^HOSTNAME=\s*(\S+)\s*$/HOSTNAME=$hostname\n/m) {
178 $data .= "HOSTNAME=$hostname\n";
179 }
180 PVE::Tools::file_set_contents($sysconfig_network, $data);
181 }
182
183 PVE::Tools::file_set_contents($hosts_fn, $etc_hosts_data);
184 }
185
186 sub setup_network {
187 my ($self, $conf) = @_;
188
189 my $rootdir = $self->{rootdir};
190 my ($gw, $gw6);
191
192 mkpath "$rootdir/etc/sysconfig/network-scripts";
193
194 foreach my $k (keys %$conf) {
195 next if $k !~ m/^net(\d+)$/;
196 my $d = $conf->{$k};
197 if ($d->{name}) {
198 my $filename = "$rootdir/etc/sysconfig/network-scripts/ifcfg-$d->{name}";
199 my $data = "DEVICE=$d->{name}\n";
200 $data .= "ONBOOT=yes\n";
201 $data .= "BOOTPROTO=none\n";
202 if (defined($d->{ip})) {
203 my $ipinfo = PVE::LXC::parse_ipv4_cidr($d->{ip});
204 $data .= "IPADDR=$ipinfo->{address}\n";
205 $data .= "NETMASK=$ipinfo->{netmask}\n";
206 if (defined($d->{gw})) {
207 $data .= "GATEWAY=$d->{gw}\n";
208 }
209 }
210 if (defined($d->{ip6})) {
211 $data .= "IPV6ADDR=$d->{ip6}\n";
212 }
213 if (defined($d->{gw6})) {
214 $data .= "IPV6_DEFAULTGW=$d->{gw6}\n";
215 }
216 PVE::Tools::file_set_contents($filename, $data);
217 }
218 }
219 }
220
221 1;