]> git.proxmox.com Git - pve-container.git/blob - src/PVE/LXC/Setup/SUSE.pm
b7753780b0d2d8facb997239a45639ab55a59d07
[pve-container.git] / src / PVE / LXC / Setup / SUSE.pm
1 package PVE::LXC::Setup::SUSE;
2
3 use strict;
4 use warnings;
5
6 use PVE::LXC::Setup::Base;
7
8 use base qw(PVE::LXC::Setup::Base);
9
10 sub new {
11 my ($class, $conf, $rootdir) = @_;
12
13 my $release = eval { -f "$rootdir/etc/SuSE-release"
14 ? PVE::Tools::file_get_contents("$rootdir/etc/SuSE-release")
15 : PVE::Tools::file_get_contents("$rootdir/etc/SuSE-brand") };
16 die "unable to read version info\n" if $@;
17
18 my $version;
19
20 # Fixme: not sure whether the minor part is optional.
21 if ($release =~ m/^\s*VERSION\s*=\s*(\d+)(?:\.(\d+))?\s*$/m) {
22 $version = "$1.$2";
23 # 13.2 needs an updated AppArmor profile (in lxc *after* 2.0.0.beta2)
24 if ($1 != 13 || ($2//0) > 2) {
25 die "unsupported suse release '$version'\n";
26 }
27 } else {
28 die "unrecognized suse release";
29 }
30
31 my $self = { conf => $conf, rootdir => $rootdir, version => $version };
32
33 $conf->{ostype} = 'opensuse';
34
35 return bless $self, $class;
36 }
37
38 sub template_fixup {
39 my ($self, $conf) = @_;
40
41 $self->setup_securetty($conf);
42 }
43
44 sub setup_init {
45 my ($self, $conf) = @_;
46
47 if ($self->{version} >= 13.2) {
48 $self->setup_container_getty_service($conf);
49 }
50 $self->setup_systemd_console($conf);
51 }
52
53 sub setup_network {
54 my ($self, $conf) = @_;
55
56 my ($gw, $gw6);
57
58 $self->ct_make_path('/etc/sysconfig/network');
59
60 foreach my $k (keys %$conf) {
61 next if $k !~ m/^net(\d+)$/;
62 my $d = PVE::LXC::Config->parse_lxc_network($conf->{$k});
63 next if !$d->{name};
64
65 my $filename = "/etc/sysconfig/network/ifcfg-$d->{name}";
66 my $routefile = "/etc/sysconfig/network/ifroute-$d->{name}";
67 my $routes = '';
68
69 my @DHCPMODES = ('static', 'dhcp4', 'dhcp6', 'dhcp');
70 my ($NONE, $DHCP4, $DHCP6, $BOTH) = (0, 1, 2, 3);
71 my $dhcp = $NONE;
72 my @addrs = ();
73
74 my $data = '';
75 my $is_configured = 0;
76
77 if ($d->{ip} && $d->{ip} ne 'manual') {
78 $is_configured = 1;
79 if ($d->{ip} eq 'dhcp') {
80 $dhcp |= $DHCP4;
81 } else {
82 push @addrs, $d->{ip};
83 if (defined($d->{gw})) {
84 if (!PVE::Network::is_ip_in_cidr($d->{gw}, $d->{ip}, 4)) {
85 $routes .= "$d->{gw} 0.0.0.0 255.255.255.255 $d->{name}\n";
86 }
87 $routes .= "default $d->{gw} 0.0.0.0 $d->{name}\n";
88 }
89 }
90 }
91
92 if ($d->{ip6} && $d->{ip6} ne 'manual') {
93 $is_configured = 1;
94 if ($d->{ip6} eq 'auto') {
95 # FIXME: Not sure what to do here...
96 } elsif ($d->{ip6} eq 'dhcp') {
97 $dhcp |= $DHCP6;
98 } else {
99 push @addrs, $d->{ip6};
100 if (defined($d->{gw6})) {
101 if (!PVE::Network::is_ip_in_cidr($d->{gw6}, $d->{ip6}, 6) &&
102 !PVE::Network::is_ip_in_cidr($d->{gw6}, 'fe80::/10', 6)) {
103 $routes .= "$d->{gw6}/128 - - $d->{name}\n";
104 }
105 $routes .= "default $d->{gw6} - $d->{name}\n";
106 }
107 }
108 }
109
110 if (@addrs > 1) {
111 for my $i (1..@addrs) {
112 $data .= "IPADDR_${i}=$addrs[$i-1]\n";
113 }
114 } elsif (@addrs) {
115 $data .= "IPADDR=$addrs[0]\n";
116 } else {
117 # check for non-manual config with no dhcp and no addresses
118 next if $is_configured && $dhcp == $NONE;
119 }
120
121 $data = "STARTMODE=" . ($is_configured ? 'onboot' : 'manual') . "\n"
122 . "BOOTPROTO=$DHCPMODES[$dhcp]\n"
123 . $data;
124 $self->ct_file_set_contents($filename, $data);
125
126 # To keep user-defined routes in route-$iface we mark ours:
127 $self->ct_modify_file($routefile, $routes, delete => 1, prepend => 1);
128 }
129 }
130
131 1;