diff --git a/CONTAINER_FIX_SUMMARY.md b/CONTAINER_FIX_SUMMARY.md new file mode 100644 index 00000000..bacdd854 --- /dev/null +++ b/CONTAINER_FIX_SUMMARY.md @@ -0,0 +1,96 @@ +# apt-ostree Container Installation Fix + +## Problem Solved +The `apt-ostree` package was failing to install in container environments due to systemd service configuration attempts that require a running systemd instance. + +## Root Cause +The original `check_systemd()` function only checked if the `systemctl` command existed, but didn't verify: +1. If systemd was actually running as PID 1 +2. If the system was in a container environment +3. If the systemd D-Bus interface was available + +## Solution Implemented +Updated the `check_systemd()` function in `debian/apt-ostree.postinst` to: + +1. **Check for systemctl command availability** +2. **Detect container environments** using multiple indicators: + - `/.dockerenv` file (Docker) + - `container` environment variable (Podman, Docker, etc.) + - `/run/.containerenv` file (Podman) + - Docker in `/proc/1/cgroup` (additional Docker detection) +3. **Verify systemd is running as PID 1** using `ps -p 1 -o comm=` +4. **Check systemd D-Bus availability** using `systemctl is-system-running` + +## Key Changes + +### Before (Original Code) +```bash +check_systemd() { + if ! command -v systemctl >/dev/null 2>&1; then + log "Warning: systemd not available, skipping service setup" + return 1 + fi + return 0 +} +``` + +### After (Fixed Code) +```bash +check_systemd() { + # Check if systemctl command exists + if ! command -v systemctl >/dev/null 2>&1; then + log "Warning: systemd not available, skipping service setup" + return 1 + fi + + # Check if we're in a container environment + if [ -f /.dockerenv ] || [ -n "${container:-}" ] || [ -f /run/.containerenv ] || \ + ([ -f /proc/1/cgroup ] && grep -q docker /proc/1/cgroup 2>/dev/null); then + log "Warning: Running in container environment, skipping systemd service setup" + return 1 + fi + + # Check if systemd is actually running as PID 1 + if ! ps -p 1 -o comm= | grep -q systemd; then + log "Warning: systemd not running as PID 1, skipping service setup" + return 1 + fi + + # Check if systemd D-Bus is available + if ! systemctl is-system-running >/dev/null 2>&1; then + log "Warning: systemd not running, skipping service setup" + return 1 + fi + + return 0 +} +``` + +## Additional Improvements +- Enhanced `setup_service()` function to provide helpful manual configuration instructions when skipping systemd setup +- Added proper parentheses to fix logical operator precedence in container detection + +## Testing Results +✅ **Container Environment**: Package installs successfully, skips systemd configuration gracefully +✅ **Real System**: Package installs and configures systemd services normally +✅ **Podman Container**: Verified working with actual Podman container build + +## Files Modified +- `debian/apt-ostree.postinst` - Updated `check_systemd()` and `setup_service()` functions + +## Impact +- **Fixes**: Container builds and CI/CD pipelines that install apt-ostree +- **Maintains**: Full functionality on real systems with systemd +- **Improves**: User experience with clear error messages and manual configuration instructions + +## Verification Commands +```bash +# Test in container (should skip systemd) +docker run --rm -it debian:unstable bash -c "apt update && apt install -y apt-ostree" + +# Test on real system (should configure systemd) +apt install -y apt-ostree && systemctl status apt-ostreed +``` + +## Status +✅ **COMPLETED** - Fix implemented and tested successfully diff --git a/debian/apt-ostree.postinst b/debian/apt-ostree.postinst index fb318ae4..fa73ebf2 100755 --- a/debian/apt-ostree.postinst +++ b/debian/apt-ostree.postinst @@ -28,18 +28,43 @@ setup_completions() { fi } -# Function to check if systemd is available +# Function to check if systemd is available and running check_systemd() { + # Check if systemctl command exists if ! command -v systemctl >/dev/null 2>&1; then log "Warning: systemd not available, skipping service setup" return 1 fi + + # Check if we're in a container environment + if [ -f /.dockerenv ] || [ -n "${container:-}" ] || [ -f /run/.containerenv ] || \ + ([ -f /proc/1/cgroup ] && grep -q docker /proc/1/cgroup 2>/dev/null); then + log "Warning: Running in container environment, skipping systemd service setup" + return 1 + fi + + # Check if systemd is actually running as PID 1 + if ! ps -p 1 -o comm= | grep -q systemd; then + log "Warning: systemd not running as PID 1, skipping service setup" + return 1 + fi + + # Check if systemd D-Bus is available + if ! systemctl is-system-running >/dev/null 2>&1; then + log "Warning: systemd not running, skipping service setup" + return 1 + fi + return 0 } # Function to enable and start the service setup_service() { if ! check_systemd; then + log "Skipping systemd service configuration" + log "To configure services manually on a real system, run:" + log " systemctl enable apt-ostreed.service" + log " systemctl start apt-ostreed.service" return 0 fi