]>
git.proxmox.com Git - pve-container.git/blob - src/PVE/LXC/Setup/Redhat.pm
956e1da7843970fecdcb733a9f913fac4063e443
1 package PVE
::LXC
::Setup
::Redhat
;
10 use PVE
::LXC
::Setup
::Base
;
12 use base
qw(PVE::LXC::Setup::Base);
15 my ($class, $conf, $rootdir) = @_;
17 my $release = PVE
::Tools
::file_read_firstline
("$rootdir/etc/redhat-release");
18 die "unable to read version info\n" if !defined($release);
22 if ($release =~ m/release\s+(\d+\.\d+)(\.\d+)?/) {
23 if ($1 >= 6 && $1 < 8) {
28 die "unsupported redhat release '$release'\n" if !$version;
30 my $self = { conf
=> $conf, rootdir
=> $rootdir, version
=> $version };
32 $conf->{ostype
} = "centos";
34 return bless $self, $class;
37 my $tty_conf = <<__EOD__;
40 # This service maintains a getty on the specified device.
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.
45 stop on runlevel [S016]
49 exec /sbin/mingetty \$TTY
50 usage 'tty TTY=/dev/ttyX - where X is console id'
53 my $start_ttys_conf = <<__EOD__;
55 # This service starts the configured number of gettys.
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.
60 start on stopped rc RUNLEVEL=[2345]
62 env ACTIVE_CONSOLES=/dev/tty[1-6]
67 for tty in \$(echo \$ACTIVE_CONSOLES) ; do
68 [ "\$RUNLEVEL" = "5" -a "\$tty" = "\$X_TTY" ] && continue
69 initctl start tty TTY=\$tty
74 my $power_status_changed_conf = <<__EOD__;
75 # power-status-changed - shutdown on SIGPWR
77 start on power-status-changed
79 exec /sbin/shutdown -h now "SIGPWR received"
83 my ($self, $conf) = @_;
85 if ($self->{version
} < 7) {
86 # re-create emissing files for tty
88 $self->ct_make_path('/etc/init');
90 my $filename = "/etc/init/tty.conf";
91 $self->ct_file_set_contents($filename, $tty_conf)
92 if ! $self->ct_file_exists($filename);
94 $filename = "/etc/init/start-ttys.conf";
95 $self->ct_file_set_contents($filename, $start_ttys_conf)
96 if ! $self->ct_file_exists($filename);
98 $filename = "/etc/init/power-status-changed.conf";
99 $self->ct_file_set_contents($filename, $power_status_changed_conf)
100 if ! $self->ct_file_exists($filename);
103 $filename = "/etc/rc.d/rc.sysinit";
104 my $data = $self->ct_file_get_contents($filename);
105 $data =~ s!^(/sbin/start_udev.*)$!#$1!gm;
106 $self->ct_file_set_contents($filename, $data);
108 # edit /etc/securetty (enable login on console)
109 $self->setup_securetty($conf, qw(lxc/console lxc/tty1 lxc/tty2 lxc/tty3 lxc/tty4));
114 my ($self, $conf) = @_;
118 $self->setup_systemd_console($conf);
122 my ($self, $conf) = @_;
124 # Redhat wants the fqdn in /etc/sysconfig/network's HOSTNAME
125 my $hostname = $conf->{hostname
} || 'localhost';
127 my $hostname_fn = "/etc/hostname";
128 my $sysconfig_network = "/etc/sysconfig/network";
131 if ($self->ct_file_exists($hostname_fn)) {
132 $oldname = $self->ct_file_read_firstline($hostname_fn) || 'localhost';
134 my $data = $self->ct_file_get_contents($sysconfig_network);
135 if ($data =~ m/^HOSTNAME=\s*(\S+)\s*$/m) {
140 my $hosts_fn = "/etc/hosts";
141 my $etc_hosts_data = '';
142 if ($self->ct_file_exists($hosts_fn)) {
143 $etc_hosts_data = $self->ct_file_get_contents($hosts_fn);
146 my ($ipv4, $ipv6) = PVE
::LXC
::get_primary_ips
($conf);
147 my $hostip = $ipv4 || $ipv6;
149 my ($searchdomains) = $self->lookup_dns_conf($conf);
151 $etc_hosts_data = PVE
::LXC
::Setup
::Base
::update_etc_hosts
($etc_hosts_data, $hostip, $oldname,
152 $hostname, $searchdomains);
154 if ($self->ct_file_exists($hostname_fn)) {
155 $self->ct_file_set_contents($hostname_fn, "$hostname\n");
157 my $data = $self->ct_file_get_contents($sysconfig_network);
158 if ($data !~ s/^HOSTNAME=\s*(\S+)\s*$/HOSTNAME=$hostname/m) {
159 $data .= "HOSTNAME=$hostname\n";
161 my ($has_ipv4, $has_ipv6);
162 foreach my $k (keys %$conf) {
163 next if $k !~ m/^net(\d+)$/;
164 my $d = PVE
::LXC
::parse_lxc_network
($conf->{$k});
166 $has_ipv4 = 1 if defined($d->{ip
});
167 $has_ipv6 = 1 if defined($d->{ip6
});
170 if ($data !~ s/(NETWORKING)=\S+/$1=yes/) {
171 $data .= "NETWORKING=yes\n";
175 if ($data !~ s/(NETWORKING_IPV6)=\S+/$1=yes/) {
176 $data .= "NETWORKING_IPV6=yes\n";
179 $self->ct_file_set_contents($sysconfig_network, $data);
182 $self->ct_file_set_contents($hosts_fn, $etc_hosts_data);
186 my ($self, $conf) = @_;
190 $self->ct_make_path('/etc/sysconfig/network-scripts');
192 foreach my $k (keys %$conf) {
193 next if $k !~ m/^net(\d+)$/;
194 my $d = PVE
::LXC
::parse_lxc_network
($conf->{$k});
197 my $filename = "/etc/sysconfig/network-scripts/ifcfg-$d->{name}";
198 my $routefile = "/etc/sysconfig/network-scripts/route-$d->{name}";
201 my $header = "DEVICE=$d->{name}\nONBOOT=yes\n";
205 if ($d->{ip
} && $d->{ip
} ne 'manual') {
206 if ($d->{ip
} eq 'dhcp') {
210 my $ipinfo = PVE
::LXC
::parse_ipv4_cidr
($d->{ip
});
211 $data .= "IPADDR=$ipinfo->{address}\n";
212 $data .= "NETMASK=$ipinfo->{netmask}\n";
213 if (defined($d->{gw
})) {
214 $data .= "GATEWAY=$d->{gw}\n";
215 if (!PVE
::Network
::is_ip_in_cidr
($d->{gw
}, $d->{ip
}, 4)) {
216 $routes .= "$d->{gw} dev $d->{name}\n";
217 $routes .= "default via $d->{gw}\n";
223 if ($d->{ip6
} && $d->{ip6
} ne 'manual') {
224 $bootproto = 'none' if !$bootproto;
225 $data .= "IPV6INIT=yes\n";
226 if ($d->{ip6
} eq 'auto') {
227 $data .= "IPV6_AUTOCONF=yes\n";
229 $data .= "IPV6_AUTOCONF=no\n";
231 if ($d->{ip6
} eq 'dhcp') {
232 $data .= "DHCPV6C=yes\n";
234 $data .= "IPV6ADDR=$d->{ip6}\n";
235 if (defined($d->{gw6
})) {
236 $data .= "IPV6_DEFAULTGW=$d->{gw6}\n";
237 if (!PVE
::Network
::is_ip_in_cidr
($d->{gw6
}, $d->{ip6
}, 6)) {
238 $routes .= "$d->{gw6} dev $d->{name}\n";
239 $routes .= "default via $d->{gw6}\n";
246 $header .= "BOOTPROTO=$bootproto\n";
247 $self->ct_file_set_contents($filename, $header . $data);
249 # To keep user-defined routes in route-$iface we mark ours:
250 my $head = "# --- BEGIN PVE ROUTES ---\n";
251 my $tail = "# --- END PVE ROUTES ---\n";
252 $routes = $head . $routes . $tail if $routes;
253 if ($self->ct_file_exists($routefile)) {
254 # if it exists we update by first removing our old rules
255 my $old = $self->ct_file_get_contents($routefile);
256 $old =~ s/(?:^|(?<=\n))\Q$head\E.*\Q$tail\E//gs;
259 $self->ct_file_set_contents($routefile, $routes . $old . "\n");
261 # or delete if we aren't adding routes and the file's now empty
262 $self->ct_unlink($routefile);
265 $self->ct_file_set_contents($routefile, $routes);