]> git.proxmox.com Git - pve-container.git/blob - src/PVE/LXC/Setup/SUSE.pm
setup: move setup_container_getty_service to Base
[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 # Nothing to do
40 }
41
42 sub setup_init {
43 my ($self, $conf) = @_;
44
45 $self->setup_securetty($conf, qw(lxc/console lxc/tty1 lxc/tty2 lxc/tty3 lxc/tty4));
46 if ($self->{version} >= 13.2) {
47 $self->setup_container_getty_service();
48 }
49 $self->setup_systemd_console($conf);
50 }
51
52 sub setup_network {
53 my ($self, $conf) = @_;
54
55 my ($gw, $gw6);
56
57 $self->ct_make_path('/etc/sysconfig/network');
58
59 foreach my $k (keys %$conf) {
60 next if $k !~ m/^net(\d+)$/;
61 my $d = PVE::LXC::parse_lxc_network($conf->{$k});
62 next if !$d->{name};
63
64 my $filename = "/etc/sysconfig/network/ifcfg-$d->{name}";
65 my $routefile = "/etc/sysconfig/network/ifroute-$d->{name}";
66 my $routes = '';
67
68 my @DHCPMODES = ('static', 'dhcp4', 'dhcp6', 'dhcp');
69 my ($NONE, $DHCP4, $DHCP6, $BOTH) = (0, 1, 2, 3);
70 my $dhcp = $NONE;
71 my @addrs = ();
72
73 my $data = '';
74 my $is_configured = 0;
75
76 if ($d->{ip} && $d->{ip} ne 'manual') {
77 $is_configured = 1;
78 if ($d->{ip} eq 'dhcp') {
79 $dhcp |= $DHCP4;
80 } else {
81 push @addrs, $d->{ip};
82 if (defined($d->{gw})) {
83 if (!PVE::Network::is_ip_in_cidr($d->{gw}, $d->{ip}, 4)) {
84 $routes .= "$d->{gw} 0.0.0.0 255.255.255.255 $d->{name}\n";
85 }
86 $routes .= "default $d->{gw} 0.0.0.0 $d->{name}\n";
87 }
88 }
89 }
90
91 if ($d->{ip6} && $d->{ip6} ne 'manual') {
92 $is_configured = 1;
93 if ($d->{ip6} eq 'auto') {
94 # FIXME: Not sure what to do here...
95 } elsif ($d->{ip6} eq 'dhcp') {
96 $dhcp |= $DHCP6;
97 } else {
98 push @addrs, $d->{ip6};
99 if (defined($d->{gw6})) {
100 if (!PVE::Network::is_ip_in_cidr($d->{gw6}, $d->{ip6}, 6)) {
101 $routes .= "$d->{gw6}/128 - - $d->{name}\n";
102 }
103 $routes .= "default $d->{gw6} - $d->{name}\n";
104 }
105 }
106 }
107
108 if (@addrs > 1) {
109 for my $i (1..@addrs) {
110 $data .= "IPADDR_${i}=$addrs[$i-1]\n";
111 }
112 } elsif (@addrs) {
113 $data .= "IPADDR=$addrs[0]\n";
114 } else {
115 # check for non-manual config with no dhcp and no addresses
116 next if $is_configured && $dhcp == $NONE;
117 }
118
119 $data = "STARTMODE=" . ($is_configured ? 'onboot' : 'manual') . "\n"
120 . "BOOTPROTO=$DHCPMODES[$dhcp]\n"
121 . $data;
122 $self->ct_file_set_contents($filename, $data);
123
124 # To keep user-defined routes in route-$iface we mark ours:
125 my $head = "# --- BEGIN PVE ROUTES ---\n";
126 my $tail = "# --- END PVE ROUTES ---\n";
127 $self->ct_modify_file_head_portion($routefile, $head, $tail, $routes);
128 }
129 }
130
131 1;