]>
Commit | Line | Data |
---|---|---|
1c7f4f65 DM |
1 | package PVE::LXCSetup::Debian; |
2 | ||
3 | use strict; | |
4 | use warnings; | |
55fa4e09 | 5 | use Data::Dumper; |
a1b1a247 | 6 | use PVE::Tools qw($IPV6RE); |
55fa4e09 DM |
7 | use PVE::LXC; |
8 | use File::Path; | |
1c7f4f65 DM |
9 | |
10 | use PVE::LXCSetup::Base; | |
11 | ||
12 | use base qw(PVE::LXCSetup::Base); | |
13 | ||
633a7bd8 | 14 | sub new { |
5b4657d0 | 15 | my ($class, $conf, $rootdir) = @_; |
633a7bd8 | 16 | |
5b4657d0 | 17 | my $version = PVE::Tools::file_read_firstline("$rootdir/etc/debian_version"); |
633a7bd8 DM |
18 | |
19 | die "unable to read version info\n" if !defined($version); | |
adc8f577 DM |
20 | |
21 | die "unable to parse version info\n" | |
22 | if $version !~ m/^(\d+(\.\d+)?)(\.\d+)?/; | |
23 | ||
24 | $version = $1; | |
25 | ||
633a7bd8 DM |
26 | die "unsupported debian version '$version'\n" if $version < 6; |
27 | ||
5b4657d0 DM |
28 | my $self = { conf => $conf, rootdir => $rootdir, version => $version }; |
29 | ||
30 | $conf->{'lxc.include'} = "/usr/share/lxc/config/debian.common.conf"; | |
633a7bd8 DM |
31 | |
32 | return bless $self, $class; | |
33 | } | |
34 | ||
d66768a2 DM |
35 | my $default_inittab = <<__EOD__; |
36 | ||
37 | # The default runlevel. | |
38 | id:2:initdefault: | |
39 | ||
40 | # Boot-time system configuration/initialization script. | |
41 | # This is run first except when booting in emergency (-b) mode. | |
42 | si::sysinit:/etc/init.d/rcS | |
43 | ||
44 | # /etc/init.d executes the S and K scripts upon change | |
45 | # of runlevel. | |
46 | # | |
47 | # Runlevel 0 is halt. | |
48 | # Runlevel 1 is single-user. | |
49 | # Runlevels 2-5 are multi-user. | |
50 | # Runlevel 6 is reboot. | |
51 | ||
52 | l0:0:wait:/etc/init.d/rc 0 | |
53 | l1:1:wait:/etc/init.d/rc 1 | |
54 | l2:2:wait:/etc/init.d/rc 2 | |
55 | l3:3:wait:/etc/init.d/rc 3 | |
56 | l4:4:wait:/etc/init.d/rc 4 | |
57 | l5:5:wait:/etc/init.d/rc 5 | |
58 | l6:6:wait:/etc/init.d/rc 6 | |
59 | # Normally not reached, but fallthrough in case of emergency. | |
60 | z6:6:respawn:/sbin/sulogin | |
61 | ||
62 | # What to do when CTRL-ALT-DEL is pressed. | |
63 | ca:12345:ctrlaltdel:/sbin/shutdown -t1 -a -r now | |
64 | ||
65 | # What to do when the power fails/returns. | |
66 | p0::powerfail:/sbin/init 0 | |
67 | ||
68 | # /sbin/getty invocations for the runlevels. | |
69 | # | |
70 | # The "id" field MUST be the same as the last | |
71 | # characters of the device (after "tty"). | |
72 | # | |
73 | # Format: | |
74 | # <id>:<runlevels>:<action>:<process> | |
75 | # | |
76 | __EOD__ | |
77 | ||
78 | sub setup_init { | |
633a7bd8 | 79 | my ($self, $conf) = @_; |
d66768a2 | 80 | |
5b4657d0 | 81 | my $rootdir = $self->{rootdir}; |
d66768a2 | 82 | |
5b4657d0 | 83 | my $filename = "$rootdir/etc/inittab"; |
d66768a2 DM |
84 | |
85 | if (-f $filename) { | |
86 | my $inittab = $default_inittab; | |
87 | ||
88 | my $ttycount = defined($conf->{'lxc.tty'}) ? $conf->{'lxc.tty'} : 4; | |
89 | for (my $i = 1; $i <= $ttycount; $i++) { | |
90 | next if $i == 7; # reserved for X11 | |
91 | my $levels = ($i == 1) ? '2345' : '23'; | |
5202218d DM |
92 | if ($self->{version} < 7) { |
93 | $inittab .= "$i:$levels:respawn:/sbin/getty -L 38400 tty$i\n"; | |
94 | } else { | |
95 | $inittab .= "$i:$levels:respawn:/sbin/getty --noclear 38400 tty$i\n"; | |
96 | } | |
d66768a2 DM |
97 | } |
98 | ||
99 | PVE::Tools::file_set_contents($filename, $inittab); | |
100 | } | |
101 | } | |
102 | ||
55fa4e09 | 103 | sub setup_network { |
633a7bd8 | 104 | my ($self, $conf) = @_; |
55fa4e09 | 105 | |
5b4657d0 | 106 | my $rootdir = $self->{rootdir}; |
55fa4e09 DM |
107 | |
108 | my $networks = {}; | |
109 | foreach my $k (keys %$conf) { | |
110 | next if $k !~ m/^net(\d+)$/; | |
93285df8 | 111 | my $ind = $1; |
55fa4e09 DM |
112 | my $d = $conf->{$k}; |
113 | if ($d->{name}) { | |
114 | my $net = {}; | |
93285df8 DM |
115 | if (defined($d->{ip})) { |
116 | my $ipinfo = PVE::LXC::parse_ipv4_cidr($d->{ip}); | |
a1b1a247 WB |
117 | $net->{address} = $ipinfo->{address}; |
118 | $net->{netmask} = $ipinfo->{netmask}; | |
55fa4e09 | 119 | } |
93285df8 | 120 | if (defined($d->{'gw'})) { |
a1b1a247 | 121 | $net->{gateway} = $d->{'gw'}; |
55fa4e09 | 122 | } |
93285df8 | 123 | if (defined($d->{ip6})) { |
a1b1a247 WB |
124 | if ($d->{ip6} !~ /^($IPV6RE)\/(\d+)$/) { |
125 | die "unable to parse ipv6 address/prefix\n"; | |
126 | } | |
127 | $net->{address6} = $1; | |
128 | $net->{netmask6} = $2; | |
129 | } | |
130 | if (defined($d->{'gw6'})) { | |
131 | $net->{gateway6} = $d->{'gw6'}; | |
55fa4e09 DM |
132 | } |
133 | $networks->{$d->{name}} = $net; | |
134 | } | |
135 | } | |
136 | ||
f44d23a5 | 137 | return if !scalar(keys %$networks); |
55fa4e09 | 138 | |
5b4657d0 | 139 | my $filename = "$rootdir/etc/network/interfaces"; |
55fa4e09 DM |
140 | my $interfaces = ""; |
141 | ||
142 | my $section; | |
143 | ||
144 | my $done_v4_hash = {}; | |
145 | my $done_v6_hash = {}; | |
146 | ||
147 | my $print_section = sub { | |
148 | my ($new) = @_; | |
149 | ||
150 | return if !$section; | |
151 | ||
152 | my $net = $networks->{$section->{ifname}}; | |
153 | ||
154 | if ($section->{type} eq 'ipv4') { | |
155 | $done_v4_hash->{$section->{ifname}} = 1; | |
156 | ||
157 | $interfaces .= "auto $section->{ifname}\n" if $new; | |
158 | ||
a1b1a247 | 159 | if ($net->{address}) { |
55fa4e09 | 160 | $interfaces .= "iface $section->{ifname} inet static\n"; |
a1b1a247 WB |
161 | $interfaces .= "\taddress $net->{address}\n" if defined($net->{address}); |
162 | $interfaces .= "\tnetmask $net->{netmask}\n" if defined($net->{netmask}); | |
163 | $interfaces .= "\tgateway $net->{gateway}\n" if defined($net->{gateway}); | |
55fa4e09 DM |
164 | foreach my $attr (@{$section->{attr}}) { |
165 | $interfaces .= "\t$attr\n"; | |
166 | } | |
167 | } else { | |
168 | $interfaces .= "iface $section->{ifname} inet manual\n"; | |
169 | } | |
170 | ||
171 | $interfaces .= "\n"; | |
172 | ||
173 | } elsif ($section->{type} eq 'ipv6') { | |
174 | $done_v6_hash->{$section->{ifname}} = 1; | |
175 | ||
a1b1a247 | 176 | if ($net->{address6}) { |
55fa4e09 | 177 | $interfaces .= "iface $section->{ifname} inet6 static\n"; |
a1b1a247 WB |
178 | $interfaces .= "\taddress $net->{address6}\n" if defined($net->{address6}); |
179 | $interfaces .= "\tnetmask $net->{netmask6}\n" if defined($net->{netmask6}); | |
180 | $interfaces .= "\tgateway $net->{gateway6}\n" if defined($net->{gateway6}); | |
55fa4e09 DM |
181 | foreach my $attr (@{$section->{attr}}) { |
182 | $interfaces .= "\t$attr\n"; | |
183 | } | |
184 | } | |
185 | ||
186 | $interfaces .= "\n"; | |
187 | } else { | |
188 | die "unknown section type '$section->{type}'"; | |
189 | } | |
190 | ||
191 | $section = undef; | |
192 | }; | |
193 | ||
194 | if (my $fh = IO::File->new($filename, "r")) { | |
195 | while (defined (my $line = <$fh>)) { | |
196 | chomp $line; | |
197 | if ($line =~ m/^#/) { | |
198 | $interfaces .= "$line\n"; | |
199 | next; | |
200 | } | |
201 | if ($line =~ m/^\s*$/) { | |
202 | if ($section) { | |
203 | &$print_section(); | |
204 | } else { | |
205 | $interfaces .= "$line\n"; | |
206 | } | |
207 | next; | |
208 | } | |
209 | if ($line =~ m/^iface\s+(\S+)\s+inet\s+(\S+)\s*$/) { | |
210 | my $ifname = $1; | |
211 | if (!$networks->{$ifname}) { | |
212 | $interfaces .= "$line\n"; | |
213 | next; | |
214 | } | |
215 | $section = { type => 'ipv4', ifname => $ifname, attr => []}; | |
216 | next; | |
217 | } | |
218 | if ($line =~ m/^iface\s+(\S+)\s+inet6\s+(\S+)\s*$/) { | |
219 | my $ifname = $1; | |
220 | if (!$networks->{$ifname}) { | |
221 | $interfaces .= "$line\n"; | |
222 | next; | |
223 | } | |
224 | $section = { type => 'ipv6', ifname => $ifname, attr => []}; | |
225 | next; | |
226 | } | |
227 | if ($section && $line =~ m/^\s*((\S+)\s(.*))$/) { | |
228 | my ($adata, $aname) = ($1, $2); | |
229 | if ($aname eq 'address' || $aname eq 'netmask' || | |
230 | $aname eq 'gateway' || $aname eq 'broadcast') { | |
231 | # skip | |
232 | } else { | |
233 | push @{$section->{attr}}, $adata; | |
234 | } | |
235 | next; | |
236 | } | |
237 | ||
238 | $interfaces .= "$line\n"; | |
239 | } | |
240 | &$print_section(); | |
241 | ||
242 | } | |
243 | ||
93285df8 | 244 | my $need_separator = 1; |
55fa4e09 DM |
245 | foreach my $ifname (sort keys %$networks) { |
246 | my $net = $networks->{$ifname}; | |
93285df8 | 247 | |
55fa4e09 | 248 | if (!$done_v4_hash->{$ifname}) { |
93285df8 | 249 | if ($need_separator) { $interfaces .= "\n"; $need_separator = 0; }; |
55fa4e09 DM |
250 | $section = { type => 'ipv4', ifname => $ifname, attr => []}; |
251 | &$print_section(1); | |
252 | } | |
a1b1a247 | 253 | if (!$done_v6_hash->{$ifname} && defined($net->{address6})) { |
93285df8 | 254 | if ($need_separator) { $interfaces .= "\n"; $need_separator = 0; }; |
55fa4e09 DM |
255 | $section = { type => 'ipv6', ifname => $ifname, attr => []}; |
256 | &$print_section(1); | |
257 | } | |
258 | } | |
259 | ||
260 | PVE::Tools::file_set_contents($filename, $interfaces); | |
261 | } | |
1c7f4f65 DM |
262 | |
263 | 1; |