Skip to content

Comments

hooks: add script to remove unneeded apparmor profiles#181

Open
zyga wants to merge 1 commit intocanonical:masterfrom
zyga:core20-aa-trim
Open

hooks: add script to remove unneeded apparmor profiles#181
zyga wants to merge 1 commit intocanonical:masterfrom
zyga:core20-aa-trim

Conversation

@zyga
Copy link
Contributor

@zyga zyga commented Jan 28, 2026

This speeds up boot time as we prevent many calls to apparmor_parser.

This patch is amended compared to the core26 counterpart, to provide the correct set of expected profiles (in this case, none). Those differ from base to base.

Thanks for helping us make a better ubuntu core!

Before continuing with the PR, you might want to consider also creating a PR for the new core-base repository
at https://github.com/snapcore/core-base, which contains newer core versions (22+), if the PR is also relevant
for newer core versions.

This speeds up boot time as we prevent many calls to apparmor_parser.

This patch is amended compared to the core26 counterpart, to provide the
correct set of expected profiles (in this case, none). Those differ from
base to base.
Copilot AI review requested due to automatic review settings January 28, 2026 16:41
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds a hook script to remove unneeded AppArmor profiles during the core20 image build process, improving boot time by preventing unnecessary calls to apparmor_parser. The implementation is adapted from the core26 base, with the key difference being that core20 expects zero AppArmor profiles to remain after cleanup.

Changes:

  • Added hook script that iterates through AppArmor profiles and removes those without matching binaries
  • Added test to verify that only expected profiles (none for core20) remain after hook execution

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
hooks/800-remove-unneeded-profiles.chroot Hook script that removes AppArmor profiles when their corresponding binaries don't exist in specified directories
hook-tests/800-remove-unneeded-profiles.test Test script that verifies no profiles remain after cleanup (core20 expects zero profiles)

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

#!/bin/bash -e
set -x
APPARMOR_PROF_D="etc/apparmor.d"

Copy link

Copilot AI Jan 28, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The allowed_profs associative array is intentionally empty to indicate that no AppArmor profiles are expected in core20. Consider adding a comment here to make this explicit, such as: # Core20 expects no AppArmor profiles to remain after cleanup. This would improve code clarity for future maintainers.

Suggested change
# Core20 expects no AppArmor profiles to remain after cleanup, so this is intentionally empty

Copilot uses AI. Check for mistakes.
Comment on lines +33 to +34
if [ "$filename" = unix-chkpwd ]
then filename=unix_chkpwd
Copy link

Copilot AI Jan 28, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The if-then statement should follow consistent formatting. The comparison value should be quoted for safety, and either put the then on the same line as if with a semicolon, or put then on a new line. Consider: if [ "$filename" = "unix-chkpwd" ]; then on one line, or split across lines with proper indentation matching the codebase style.

Suggested change
if [ "$filename" = unix-chkpwd ]
then filename=unix_chkpwd
if [ "$filename" = "unix-chkpwd" ]; then
filename=unix_chkpwd

Copilot uses AI. Check for mistakes.
echo "Starting AppArmor profile cleanup..."

# Iterate through files in /etc/apparmor.d/
for profile in "$APPARMOR_PROF_D"/*; do
Copy link

Copilot AI Jan 28, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider adding a check for directory existence before the loop or setting the nullglob option to handle the case where /etc/apparmor.d doesn't exist or is empty. While the current checks at lines 22-27 will prevent errors, adding 'shopt -s nullglob' before the loop would make the intent clearer and follow bash best practices for glob iteration.

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants