Script to list user installed packages using apk instead of opkg?

I have been using this script in the past for OpenWRT updates: list-user-installed-packages-sh

Has someone come up doing the same using apk?

Or should I go with “Attended Sysupgrade” which sounds very promising.

Discussion in

2 Likes

Assuming you only want to find packages installed on the router after installing OpenWrt, I'll give you two options:

1. If you installed "owut," then:

owut list

2. If you prefer, you can create a script that finds only packages without dependencies, starting from the list of default packages installed on the router:

#!/bin/sh

USER_LIST="/tmp/my-user-installed-packages"
TMP_INST="/tmp/my-installed-temp"
TMP_WD="/tmp/my-wd-output"
TMP_INPUT="/tmp/my-wd-input"
TMP_USER_INST="/tmp/my-user-inst-input"

# Check for default file
if [ ! -f /root/my-def-pkgs ]; then
    echo "/root/my-def-pkgs not found; exit 1"
    exit 1
fi
# Clear apk cache directory if it exists
if [ -d /tmp/cache/apk/ ]; then
    rm -rf /tmp/cache/apk/*
fi

# Clean up any previous temporary files
rm -f "$USER_LIST" "$TMP_INST" "$TMP_WD" "$TMP_INPUT" "$TMP_USER_INST"

echo "Gathering installed packages..."
# List installed packages and store them in a temporary file
apk info 2>/dev/null > "$TMP_INST"

echo "Generating reverse dependencies (high-level packages)..."
# For each installed package, generate reverse dependencies (which packages depend on it)
for pkg in $(cat "$TMP_INST" | sort -u); do
    # Get reverse dependencies for each package, excluding the first line
    rdeps=$(apk info --rdepends "$pkg" 2>/dev/null | tail -n +2 | awk '{print $1}')
    cnt=$(echo "$rdeps" | wc -w)
    # Store the count, package name, and reverse dependencies in a temporary file
    printf "%s\t%s\t%s\n" "$cnt" "$pkg" "$rdeps" >> "$TMP_WD"
    printf "."
done
echo " DONE"

# Extract packages with no reverse dependencies
grep "^0" "$TMP_WD" | cut -f2 > "$TMP_USER_INST"

echo "Creating user package list..."
# For each package without reverse dependencies, check if it's not in the default package list
for pkg in $(cat "$TMP_USER_INST"); do
    if ! grep -Fqw "$pkg" /root/my-def-pkgs; then
        # Add the package to the user package list if it's not in the default list
        echo "$pkg" >> "$USER_LIST"
        printf "."
    fi
done

# Final sorting: removed packages first, added packages after
grep "^-*" "$USER_LIST" | sort > /tmp/tmp-rem
grep -v "^-*" "$USER_LIST" | sort > /tmp/tmp-add
# Combine the lists and update the user package list
cat /tmp/tmp-rem /tmp/tmp-add > "$USER_LIST"
rm -f /tmp/tmp-rem /tmp/tmp-add

echo "User package list created at $USER_LIST"

# Clean up temporary files
rm -f "$TMP_WD" "$TMP_INPUT" "$TMP_USER_INST" "$TMP_INST"

where the file "/root/my-def-pkgs" contains the list of packages that you can find with:

1 Like

Try this:

grep -v -F -x -f <(sort /rom/etc/apk/world) <(sort /etc/apk/world)
2 Likes

Refined version:

echo "# Added manually after flashing:"; grep -v -F -x -f <(sort /rom/etc/apk/world) <(sort /etc/apk/world)
echo; echo "# Removed from defaults:"; grep -v -F -x -f <(sort /etc/apk/world) <(sort /rom/etc/apk/world) | sed 's/^/-/'
3 Likes

Attended sysupgrade does not deal with extroot thus the need for this script.

1 Like