]>
git.proxmox.com Git - pve-container.git/blob - src/PVE/LXC/Setup/Debian.pm
1 package PVE
::LXC
::Setup
::Debian
;
6 use PVE
::Tools
qw($IPV6RE);
10 use PVE
::LXC
::Setup
::Base
;
12 use base
qw(PVE::LXC::Setup::Base);
15 my ($class, $conf, $rootdir) = @_;
17 my $version = PVE
::Tools
::file_read_firstline
("$rootdir/etc/debian_version");
19 die "unable to read version info\n" if !defined($version);
21 die "unable to parse version info\n"
22 if $version !~ m/^(\d+(\.\d+)?)(\.\d+)?/;
26 die "unsupported debian version '$version'\n"
27 if !($version >= 4 && $version < 9);
29 my $self = { conf
=> $conf, rootdir
=> $rootdir, version
=> $version };
31 $conf->{ostype
} = "debian";
33 return bless $self, $class;
37 my ($self, $conf) = @_;
39 my $filename = "/etc/inittab";
40 return if !$self->ct_file_exists($filename);
42 my $ttycount = PVE
::LXC
::get_tty_count
($conf);
43 my $inittab = $self->ct_file_get_contents($filename);
47 !/^\s*\d+:\d+:[^:]*:.*getty/ &&
50 } split(/\n/, $inittab);
52 $inittab = join("\n", @lines) . "\n";
54 $inittab .= "p0::powerfail:/sbin/init 0\n";
56 my $version = $self->{version
};
57 for (my $id = 1; $id <= $ttycount; $id++) {
58 next if $id == 7; # reserved for X11
59 my $levels = ($id == 1) ?
'2345' : '23';
61 $inittab .= "$id:$levels:respawn:/sbin/getty -L 38400 tty$id\n";
63 $inittab .= "$id:$levels:respawn:/sbin/getty --noclear 38400 tty$id\n";
67 $self->ct_file_set_contents($filename, $inittab);
71 my ($self, $conf) = @_;
74 foreach my $k (keys %$conf) {
75 next if $k !~ m/^net(\d+)$/;
77 my $d = PVE
::LXC
::parse_lxc_network
($conf->{$k});
80 if (defined($d->{ip
})) {
81 if ($d->{ip
} =~ /^(?:dhcp|manual)$/) {
82 $net->{address
} = $d->{ip
};
84 my $ipinfo = PVE
::LXC
::parse_ipv4_cidr
($d->{ip
});
85 $net->{address
} = $ipinfo->{address
};
86 $net->{netmask
} = $ipinfo->{netmask
};
89 if (defined($d->{'gw'})) {
90 $net->{gateway
} = $d->{'gw'};
92 if (defined($d->{ip6
})) {
93 if ($d->{ip6
} =~ /^(?:auto|dhcp|manual)$/) {
94 $net->{address6
} = $d->{ip6
};
95 } elsif ($d->{ip6
} !~ /^($IPV6RE)\/(\d
+)$/) {
96 die "unable to parse ipv6 address/prefix\n";
98 $net->{address6
} = $1;
99 $net->{netmask6
} = $2;
102 if (defined($d->{'gw6'})) {
103 $net->{gateway6
} = $d->{'gw6'};
105 $networks->{$d->{name
}} = $net if keys %$net;
109 return if !scalar(keys %$networks);
111 my $filename = "/etc/network/interfaces";
117 my $done_v4_hash = {};
118 my $done_v6_hash = {};
120 my $print_section = sub {
125 my $net = $networks->{$section->{ifname
}};
127 if ($new && !$done_auto->{$section->{ifname
}}) {
128 $interfaces .= "auto $section->{ifname}\n";
129 $done_auto->{$section->{ifname
}} = 1;
132 if ($section->{type
} eq 'ipv4') {
133 $done_v4_hash->{$section->{ifname
}} = 1;
135 if ($net->{address
} =~ /^(dhcp|manual)$/) {
136 $interfaces .= "iface $section->{ifname} inet $1\n";
138 $interfaces .= "iface $section->{ifname} inet static\n";
139 $interfaces .= "\taddress $net->{address}\n" if defined($net->{address
});
140 $interfaces .= "\tnetmask $net->{netmask}\n" if defined($net->{netmask
});
141 $interfaces .= "\tgateway $net->{gateway}\n" if defined($net->{gateway
});
142 foreach my $attr (@{$section->{attr
}}) {
143 $interfaces .= "\t$attr\n";
149 } elsif ($section->{type
} eq 'ipv6') {
150 $done_v6_hash->{$section->{ifname
}} = 1;
152 if ($net->{address6
} =~ /^(auto|dhcp|manual)$/) {
153 $interfaces .= "iface $section->{ifname} inet6 $1\n";
155 $interfaces .= "iface $section->{ifname} inet6 static\n";
156 $interfaces .= "\taddress $net->{address6}\n" if defined($net->{address6
});
157 $interfaces .= "\tnetmask $net->{netmask6}\n" if defined($net->{netmask6
});
158 $interfaces .= "\tgateway $net->{gateway6}\n" if defined($net->{gateway6
});
159 foreach my $attr (@{$section->{attr
}}) {
160 $interfaces .= "\t$attr\n";
166 die "unknown section type '$section->{type}'";
172 if (my $fh = $self->ct_open_file($filename, "r")) {
173 while (defined (my $line = <$fh>)) {
175 if ($line =~ m/^#/) {
176 $interfaces .= "$line\n";
179 if ($line =~ m/^\s*$/) {
183 $interfaces .= "$line\n";
187 if ($line =~ m/^\s*iface\s+(\S+)\s+inet\s+(\S+)\s*$/) {
189 &$print_section(); # print previous section
190 if (!$networks->{$ifname}) {
191 $interfaces .= "$line\n";
194 $section = { type
=> 'ipv4', ifname
=> $ifname, attr
=> []};
197 if ($line =~ m/^\s*iface\s+(\S+)\s+inet6\s+(\S+)\s*$/) {
199 &$print_section(); # print previous section
200 if (!$networks->{$ifname}) {
201 $interfaces .= "$line\n";
204 $section = { type
=> 'ipv6', ifname
=> $ifname, attr
=> []};
208 if ($line =~ m/^\s*auto\s*(.*)$/) {
209 foreach my $iface (split(/\s+/, $1)) {
210 $done_auto->{$iface} = 1;
213 $interfaces .= "$line\n";
216 # Handle other section delimiters:
217 if ($line =~ m
/^\s
*(?
:mapping\s
223 $interfaces .= "$line\n";
226 if ($section && $line =~ m/^\s*((\S+)\s(.*))$/) {
227 my ($adata, $aname) = ($1, $2);
228 if ($aname eq 'address' || $aname eq 'netmask' ||
229 $aname eq 'gateway' || $aname eq 'broadcast') {
232 push @{$section->{attr
}}, $adata;
237 $interfaces .= "$line\n";
243 my $need_separator = length($interfaces) && ($interfaces !~ /\n\n$/);
244 foreach my $ifname (sort keys %$networks) {
245 my $net = $networks->{$ifname};
247 if (!$done_v4_hash->{$ifname} && defined($net->{address
})) {
248 if ($need_separator) { $interfaces .= "\n"; $need_separator = 0; };
249 $section = { type
=> 'ipv4', ifname
=> $ifname, attr
=> []};
252 if (!$done_v6_hash->{$ifname} && defined($net->{address6
})) {
253 if ($need_separator) { $interfaces .= "\n"; $need_separator = 0; };
254 $section = { type
=> 'ipv6', ifname
=> $ifname, attr
=> []};
259 $self->ct_file_set_contents($filename, $interfaces);