Quantcast
Channel: Debian User Forums
Viewing all articles
Browse latest Browse all 3260

System and Network configuration • apt-get, archives and file protocol

$
0
0
I am using apt-get for installing packages.
I never remove packages from /var/cache/apt/archives in order to be able to reinstall then with dpkg.
I spend many hours because I could not set apt-get to store my packages in archives directory automatically.
Stating my ignorance, I tried to ask an AI.
After a few questions, I realized the problem was the protocol. I store on disk the images of CDROMs from debian installation (the machine is not connected to any network).
So I do not use http protocol for downloading packages.
Then after a few propositions the AI gave me a solution and authorized me to to share it with you.
Use a custom /usr/local/bin/apt-get-local shell partly created by the AI:

Code:

#!/bin/bash# Generate a timestamp with millisecondsTIMESTAMP=$(date +%Y%m%d%H%M%S.%3N)# Define temporary directoriesTMP_DIR="/tmp/apt-get-local-$TIMESTAMP"WORK_DIR="/tmp/apt-get-local-repack-$TIMESTAMP"# Create the temporary directoriesmkdir -p "$TMP_DIR"mkdir -p "$WORK_DIR"# Ensure the temporary directories are cleaned up on exittrap 'rm -rf "$TMP_DIR" "$WORK_DIR"' EXIT# Capture the original commandCOMMAND="apt-get $@"# Execute the original commandsudo $COMMAND# Function to handle repacking of packageshandle_local_packages() {    PACKAGE=$1    if dpkg -s $PACKAGE >/dev/null 2>&1; then        echo "Repacking installed package: $PACKAGE"                # Perform repacking in a known writable directory        (cd "$WORK_DIR" && dpkg-repack $PACKAGE)        # Move the .deb file to the temporary directory        DEB_FILE=$(find "$WORK_DIR" -maxdepth 1 -name "${PACKAGE}_*.deb")        if [ -n "$DEB_FILE" ]; then            mv "$DEB_FILE" "$TMP_DIR/"            echo "Package $PACKAGE repacked to $TMP_DIR/"        fi    fi}# Process arguments to identify package namesif [[ "$@" == *"install"* ]] || [[ "$@" == *"upgrade"* ]] || [[ "$@" == *"dist-upgrade"* ]]; then    for arg in "$@"; do        case $arg in            -*|--*) # Skip options                ;;            install|upgrade|dist-upgrade) # Skip the commands                ;;            *) # Assume anything else is a package name                handle_local_packages $arg                ;;        esac    done    # Move any repacked packages from the temporary directory to the archives    if [ -d "$TMP_DIR" ]; then        mv "$TMP_DIR"/*.deb /var/cache/apt/archives/        echo "Moved repacked packages to /var/cache/apt/archives/"    fifi
What do you think of this solution?
It uses dpkg-repack and copies the .deb to archives even if the protocol is "file".
This script is intended to be run by root (the 'sudo' can be removed.).

Statistics: Posted by hagi-yaki — 2024-08-03 16:53 — Replies 0 — Views 3



Viewing all articles
Browse latest Browse all 3260

Trending Articles