Skip to content

Commit 4a20778

Browse files
authored
Merge pull request #52 from JedMeister/add-hostname-to-dhcp-requests
Add hostname to dhcp requests (and other refactoring)
2 parents a56cfb6 + 485ff71 commit 4a20778

File tree

2 files changed

+44
-30
lines changed

2 files changed

+44
-30
lines changed

firstboot.d/01ipconfig

Lines changed: 43 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,36 +3,49 @@
33

44
. /etc/default/inithooks
55

6-
[ -e $INITHOOKS_CONF ] && . $INITHOOKS_CONF
7-
[ -z "$IP_CONFIG" ] && exit 0
8-
[ "$IP_CONFIG" != "manual" ] && [ "$IP_CONFIG" != "static" ] && [ "$IP_CONFIG" != "dhcp" ] && exit 1
9-
[ ! -e /etc/network/interfaces ] && exit 1
10-
11-
# if IP_CONFIG is not changed skip this script and avoid a interface reconfiguration
12-
grep "iface eth0 inet $IP_CONFIG" /etc/network/interfaces >/dev/null && exit 0
13-
14-
ifdown --exclude=lo -a
15-
16-
# since debian 8 (systemd) ifdown does no longer take the interface down
17-
# if we change between manual, static or dhcp this does not work anymore
18-
# as ifup won't do anything
19-
ip link set eth0 down
20-
21-
echo "# interfaces(5) file used by ifup(8) and ifdown(8)" >/etc/network/interfaces
22-
echo >>/etc/network/interfaces
23-
echo "auto lo" >>/etc/network/interfaces
24-
echo "iface lo inet loopback" >>/etc/network/interfaces
25-
echo >>/etc/network/interfaces
26-
echo "auto eth0" >>/etc/network/interfaces
27-
echo "iface eth0 inet $IP_CONFIG" >>/etc/network/interfaces
28-
echo " hostname $(head -1 /etc/hostname)" >>/etc/network/interfaces
29-
30-
if [ "$IP_CONFIG" = "static" ]; then
31-
echo " address $IP_ADDRESS" >>/etc/network/interfaces
32-
echo " netmask $IP_NETMASK" >>/etc/network/interfaces
33-
echo " gateway $IP_GW" >>/etc/network/interfaces
34-
echo " dns-nameservers $IP_DNS1 $IP_DNS2" >>/etc/network/interfaces
35-
fi
6+
fatal() { echo "fatal $@" >&2; exit 1; }
7+
8+
[[ -e $INITHOOKS_CONF ]] && . $INITHOOKS_CONF
9+
[[ -z "$IP_CONFIG" ]] && exit 0
10+
[[ "$IP_CONFIG" != "manual" ]] \
11+
&& [[ "$IP_CONFIG" != "static" ]] \
12+
&& [[ "$IP_CONFIG" != "dhcp" ]] \
13+
&& fatal "IP_CONFIG set incorrectly"
14+
[[ ! -e /etc/network/interfaces ]] && fatal "intefaces file not found"
15+
16+
APP=$(turnkey-version -n)
17+
18+
IP_IFACE="eth0"
19+
[[ "$APP" == "lxc" ]] && IP_IFACE="br0"
20+
21+
# if IP_CONFIG is not changed skip this script and avoid a interface
22+
# reconfiguration
23+
grep "iface $IP_IFACE inet $IP_CONFIG" /etc/network/interfaces >/dev/null \
24+
&& exit 0
25+
26+
# since debian 8 (systemd) ifdown no longer takes the interface down if we
27+
# change between manual, static or dhcp so using 'ip' instead
28+
ip link set $IP_IFACE down
29+
30+
cat > /etc/network/interfaces <<EOF
31+
# interfaces(5) file - generated by TurnKey firstboot
32+
33+
auto lo
34+
iface lo inet loopback
35+
36+
auto $IP_IFACE
37+
iface $IP_IFACE inet $IP_CONFIG
38+
hostname $(head -1 /etc/hostname)
39+
EOF
40+
41+
if [[ "$IP_CONFIG" == "static" ]]; then
42+
cat > /etc/network/interfaces <<EOF
43+
hostname $(head -1 /etc/hostname)
44+
address $IP_ADDRESS
45+
netmask $IP_NETMASK
46+
gateway $IP_GW
47+
dns-nameservers $IP_DNS1 $IP_DNS2
48+
EOF
3649

3750
ifup --exclude=lo -a
3851

firstboot.d/09hostname

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ for file in \
1414
/etc/printcap \
1515
/etc/hostname \
1616
/etc/hosts \
17+
/etc/network/interfaces \
1718
/etc/ssh/ssh_host_rsa_key.pub \
1819
/etc/ssh/ssh_host_dsa_key.pub \
1920
/etc/ssh/ssh_host_ecdsa_key.pub \

0 commit comments

Comments
 (0)