Skip to content

Commit 06a0fa2

Browse files
authored
Merge pull request #815 from stefanor/lxc.generator-tweaks
Tidy up lxc.generator a bit
2 parents 194842c + c7e3d8f commit 06a0fa2

File tree

1 file changed

+43
-35
lines changed

1 file changed

+43
-35
lines changed

Diff for: distrobuilder/lxc.generator

+43-35
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@ export LC_ALL=C
88
## Helper functions
99
# is_lxc_container succeeds if we're running inside a LXC container
1010
is_lxc_container() {
11-
grep -qa container=lxc /proc/1/environ
11+
grep -q --text container=lxc /proc/1/environ
1212
}
1313

1414
is_lxc_privileged_container() {
15+
# The full positive 32-bit range is available
1516
grep -qw 4294967295$ /proc/self/uid_map
1617
}
1718

@@ -40,9 +41,10 @@ is_in_path() {
4041
fix_ro_paths() {
4142
mkdir -p "/run/systemd/system/$1.d"
4243
cat <<-EOF > "/run/systemd/system/$1.d/zzz-lxc-ropath.conf"
43-
[Service]
44-
BindReadOnlyPaths=/sys /proc
45-
EOF
44+
# This file was created by distrobuilder
45+
[Service]
46+
BindReadOnlyPaths=/sys /proc
47+
EOF
4648
}
4749

4850
# fix_nm_link_state forces the network interface to a DOWN state ahead of NetworkManager starting up
@@ -57,19 +59,22 @@ fix_nm_link_state() {
5759
return 0
5860
fi
5961
cat <<-EOF > /run/systemd/system/network-device-down.service
60-
[Unit]
61-
Description=Turn off network device
62-
Before=NetworkManager.service
63-
Before=systemd-networkd.service
64-
[Service]
65-
# do not turn off if there is a default route to 169.254.0.1, i.e. the device is a routed nic
66-
ExecCondition=/bin/sh -c '! /usr/bin/grep -qs 00000000.0100FEA9 /proc/net/route'
67-
ExecStart=-${ip_path} link set $1 down
68-
Type=oneshot
69-
RemainAfterExit=true
70-
[Install]
71-
WantedBy=default.target
72-
EOF
62+
# This file was created by distrobuilder
63+
[Unit]
64+
Description=Turn off network device
65+
Before=NetworkManager.service
66+
Before=systemd-networkd.service
67+
68+
[Service]
69+
# do not turn off if there is a default route to 169.254.0.1, i.e. the device is a routed nic
70+
ExecCondition=/bin/sh -c '! /usr/bin/grep -qs 00000000.0100FEA9 /proc/net/route'
71+
ExecStart=-${ip_path} link set $1 down
72+
Type=oneshot
73+
RemainAfterExit=true
74+
75+
[Install]
76+
WantedBy=default.target
77+
EOF
7378
mkdir -p /run/systemd/system/default.target.wants
7479
ln -sf /run/systemd/system/network-device-down.service /run/systemd/system/default.target.wants/network-device-down.service
7580
}
@@ -125,11 +130,12 @@ fix_systemd_udev_trigger() {
125130

126131
mkdir -p /run/systemd/system/systemd-udev-trigger.service.d
127132
cat <<-EOF > /run/systemd/system/systemd-udev-trigger.service.d/zzz-lxc-override.conf
128-
[Service]
129-
ExecStart=
130-
ExecStart=-${cmd} trigger --type=subsystems --action=add
131-
ExecStart=-${cmd} trigger --type=devices --action=add
132-
EOF
133+
# This file was created by distrobuilder
134+
[Service]
135+
ExecStart=
136+
ExecStart=-${cmd} trigger --type=subsystems --action=add
137+
ExecStart=-${cmd} trigger --type=devices --action=add
138+
EOF
133139
}
134140

135141
# fix_systemd_sysctl overrides the systemd-sysctl.service to use "ExecStart=-" instead of "ExecStart=".
@@ -138,10 +144,11 @@ fix_systemd_sysctl() {
138144
! [ -e "${cmd}" ] && cmd=/lib/systemd/systemd-sysctl
139145
mkdir -p /run/systemd/system/systemd-sysctl.service.d
140146
cat <<-EOF > /run/systemd/system/systemd-sysctl.service.d/zzz-lxc-override.conf
141-
[Service]
142-
ExecStart=
143-
ExecStart=-${cmd}
144-
EOF
147+
# This file was created by distrobuilder
148+
[Service]
149+
ExecStart=
150+
ExecStart=-${cmd}
151+
EOF
145152
}
146153

147154
## Main logic
@@ -167,6 +174,7 @@ done
167174
# Determine distro name and release
168175
ID=""
169176
if [ -e /etc/os-release ]; then
177+
# shellcheck disable=SC1091
170178
. /etc/os-release
171179
fi
172180

@@ -211,15 +219,15 @@ fi
211219
if [ -d /etc/udev ]; then
212220
mkdir -p /run/udev/rules.d
213221
cat <<-EOF > /run/udev/rules.d/90-lxc-net.rules
214-
# This file was created by distrobuilder.
215-
#
216-
# Its purpose is to convince NetworkManager to treat the eth0 veth
217-
# interface like a regular Ethernet. NetworkManager ordinarily doesn't
218-
# like to manage the veth interfaces, because they are typically configured
219-
# by container management tooling for specialized purposes.
220-
221-
ACTION=="add|change|move", ENV{ID_NET_DRIVER}=="veth", ENV{INTERFACE}=="eth[0-9]*", ENV{NM_UNMANAGED}="0"
222-
EOF
222+
# This file was created by distrobuilder.
223+
#
224+
# Its purpose is to convince NetworkManager to treat the eth0 veth
225+
# interface like a regular Ethernet. NetworkManager ordinarily doesn't
226+
# like to manage the veth interfaces, because they are typically configured
227+
# by container management tooling for specialized purposes.
228+
229+
ACTION=="add|change|move", ENV{ID_NET_DRIVER}=="veth", ENV{INTERFACE}=="eth[0-9]*", ENV{NM_UNMANAGED}="0"
230+
EOF
223231
fi
224232

225233
# Workarounds for NetworkManager in containers

0 commit comments

Comments
 (0)