@@ -45,6 +45,8 @@ DEFINE_string install_root_basename "${default_install_root_basename}" \
4545 " Name of a root directory where packages will be installed. ${default_install_root_basename@ Q} by default."
4646DEFINE_string forbidden_packages " " \
4747 " Comma-separated list of pairs describing packages that are forbidden in the sysext. Every pair consist of regexp and message, separated with semicolon. The regexp is for matching a package name (<category>/<name>-<version>::<repo>), and message is printed if the regexp matched a package name. Be careful to not include commas in the regexp or message."
48+ DEFINE_boolean selinux " ${FLAGS_FALSE} " \
49+ " Relabel the files in sysext using policies installed in the base squashfs image."
4850
4951FLAGS_HELP=" USAGE: build_sysext [flags] <sysext_name> <binary_package> [<binary_package> ...]
5052
@@ -154,6 +156,8 @@ cleanup() {
154156 " ${THE_INSTALL_ROOT} "
155157 " ${BUILD_DIR} /workdir"
156158 " ${BUILD_DIR} /img-rootfs"
159+ " ${BUILD_DIR} /selinux-root"
160+ " ${BUILD_DIR} /selinux-root-workdir"
157161 )
158162 umount " ${dirs[@]} " 2> /dev/null || true
159163 rm -rf " ${dirs[@]} " || true
@@ -253,6 +257,13 @@ export SOURCE_DATE_EPOCH=$(stat -c '%Y' "${BUILD_DIR}/fs-root/usr/lib/os-release
253257# Unmount in order to get rid of the overlay, but keep fs-root for
254258# now, so we can use selinux file contexts.
255259umount " ${THE_INSTALL_ROOT} "
260+ if [[ ${FLAGS_selinux} = " ${FLAGS_TRUE} " ]]; then
261+ mkdir " ${BUILD_DIR} /selinux-root"
262+ mkdir " ${BUILD_DIR} /selinux-root-workdir"
263+ mount -t overlay overlay -o lowerdir=" ${BUILD_DIR} /fs-root${pkginfo_lowerdirs} " ,upperdir=" ${BUILD_DIR} /selinux-root" ,workdir=" ${BUILD_DIR} /selinux-root-workdir" " ${BUILD_DIR} /selinux-root"
264+ else
265+ umount " ${BUILD_DIR} /fs-root"
266+ fi
256267
257268if [[ " $FLAGS_generate_pkginfo " = " ${FLAGS_TRUE} " ]] ; then
258269 info " Creating pkginfo squashfs '${BUILD_DIR} /${SYSEXTNAME} _pkginfo.raw'"
@@ -266,27 +277,29 @@ info "Writing ${SYSEXTNAME}_packages.txt"
266277ROOT=" ${THE_INSTALL_ROOT} " PORTAGE_CONFIGROOT=" ${THE_INSTALL_ROOT} " \
267278 equery --no-color list --format ' $cpv::$repo' ' *' > " ${BUILD_DIR} /${SYSEXTNAME} _packages.txt"
268279
269- # Check if there are forbidden packages
270- mapfile -t pairs <<< " ${FLAGS_forbidden_packages//,/$'\n'}"
271- declare -A re_msg_pairs=()
272- for pair in " ${pairs[@]} " ; do
273- re=${pair%% ;* }
274- msg=${pair# .;}
275- re_msg_pairs[" ${re} " ]=" ${msg} "
276- done
280+ if [[ -n ${FLAGS_forbidden_packages} ]]; then
281+ # Check if there are forbidden packages
282+ mapfile -t pairs <<< " ${FLAGS_forbidden_packages//,/$'\n'}"
283+ declare -A re_msg_pairs=()
284+ for pair in " ${pairs[@]} " ; do
285+ re=${pair%% ;* }
286+ msg=${pair# .;}
287+ re_msg_pairs[" ${re} " ]=" ${msg} "
288+ done
277289
278- mapfile -t pkgs < " ${BUILD_DIR} /${SYSEXTNAME} _packages.txt"
279- has_forbidden_pkg=
280- for pkg in " ${pkgs[@]} " ; do
281- for re in " ${! re_msg_pairs[@]} " ; do
282- if [[ ${pkg} =~ ${re} ]]; then
283- has_forbidden_pkg=x
284- error " Forbidden package ${pkg} : ${msg} "
285- fi
290+ mapfile -t pkgs < " ${BUILD_DIR} /${SYSEXTNAME} _packages.txt"
291+ has_forbidden_pkg=
292+ for pkg in " ${pkgs[@]} " ; do
293+ for re in " ${! re_msg_pairs[@]} " ; do
294+ if [[ ${pkg} =~ ${re} ]]; then
295+ has_forbidden_pkg=x
296+ error " Forbidden package ${pkg} : ${msg} "
297+ fi
298+ done
286299 done
287- done
288- if [[ -n ${has_forbidden_pkg} ]] ; then
289- die " Forbidden packages encountered "
300+ if [[ -n ${has_forbidden_pkg} ]] ; then
301+ die " Forbidden packages encountered "
302+ fi
290303fi
291304
292305if [[ " ${FLAGS_strip_binaries} " = " ${FLAGS_TRUE} " ]]; then
@@ -337,9 +350,21 @@ if [[ -n "${invalid_files}" ]]; then
337350 die " Invalid file ownership: ${invalid_files} "
338351fi
339352
340- info " Relabeling sysext contents"
341- setfiles -D -E -F -r " ${THE_INSTALL_ROOT} " -v -T 0 " ${BUILD_DIR} /fs-root/usr/share/flatcar/etc/selinux/mcs/contexts/files/file_contexts" " ${THE_INSTALL_ROOT} "
342- umount " ${BUILD_DIR} /fs-root"
353+ if [[ ${FLAGS_selinux} = " ${FLAGS_TRUE} " ]]; then
354+ info " Build temporary selinux modules"
355+ chroot " ${BUILD_DIR} /selinux-root" bash -s << 'EOF '
356+ cd /usr/share/selinux/mcs
357+ set -x
358+ semodule -s mcs -n -i *.pp
359+ EOF
360+
361+ info " Relabeling sysext contents"
362+ spec_file=" ${BUILD_DIR} /selinux-root/etc/selinux/mcs/contexts/files/file_contexts"
363+ setfiles -D -E -F -r " ${THE_INSTALL_ROOT} " -v -T 0 " ${spec_file} " " ${THE_INSTALL_ROOT} "
364+ ls -laRZ " ${THE_INSTALL_ROOT} "
365+ umount " ${BUILD_DIR} /selinux-root"
366+ umount " ${BUILD_DIR} /fs-root"
367+ fi
343368
344369info " Creating squashfs image"
345370mksquashfs " ${THE_INSTALL_ROOT} " " ${BUILD_DIR} /${SYSEXTNAME} .raw" \
0 commit comments