view removeKernel @ 174:5da4de3d7ee1

remove files from /var/tmp/genkernel and /lib/modules, too
author Dirk Olmes <dirk.olmes@codedo.de>
date Thu, 09 Apr 2020 11:21:59 +0200
parents 4500455b2375
children 8af0a6219353
line wrap: on
line source

#!/bin/bash

if [ `whoami` != "root" ]; then
    echo "$0 must be run as root!"
    exit 1
fi

if [ $# -lt 3 ]; then
    echo "usage: $0 <major minor micro [revision], e.g. 2 6 21 r4>"
    exit 1
fi

MAJOR=$1
MINOR=$2
MICRO=$3
if [ $# -gt 3 ]; then
    REVISION=$4
fi

ARCH=`uname -m`
if [ "$ARCH" = "i686" ]; then
    ARCH="x86"
fi

LOGFILE=/tmp/removeKernel-$$.log

# unmerge the kernel
cd /usr/src
KERNEL_DIR="linux-${MAJOR}.${MINOR}.${MICRO}-gentoo"
if [ "$REVISION" != "" ]; then
    KERNEL_DIR="${KERNEL_DIR}-${REVISION}"
fi
if [ -d $KERNEL_DIR ]; then
    KERNEL_NAME=gentoo-sources-${MAJOR}.${MINOR}.${MICRO}
    if [ "${REVISION}" != "" ]; then
        KERNEL_NAME="${KERNEL_NAME}-${REVISION}"
    fi

    echo "unmerge kernel $KERNEL_NAME" >> $LOGFILE 2>&1
    emerge --unmerge $KERNEL_NAME >> $LOGFILE 2>&1

    echo "remove $KERNEL_DIR" >> $LOGFILE 2>&1
    rm -r  $KERNEL_DIR >> $LOGFILE 2>&1
fi

# remove modules
cd /lib/modules
LIB_DIR=${MAJOR}.${MINOR}.${MICRO}-gentoo
if [[ "$REVISION" != "" ]]; then
    LIB_DIR="${LIB_DIR}-${REVISION}"
fi
if [[ ! -d $LIB_DIR ]]; then
	LIB_DIR="${LIB_DIR}-${ARCH}"
fi
if [[ -d $LIB_DIR ]]; then
    echo "remove modules from `pwd`/${LIB_DIR}" >> $LOGFILE 2>&1
    rm -r $LIB_DIR >> $LOGFILE 2>&1
fi

# remove the kernel and stuff, check if /boot is mounted first
MOUNTED=`mount | grep /boot`
if [ "$MOUNTED" = "" ]; then
    echo "mount /boot" >> $LOGFILE 2>&1
    mount /boot >> $LOGFILE 2>&1
fi

cd /boot
SUFFIX_V3="genkernel-${ARCH}-${MAJOR}.${MINOR}.${MICRO}"
if [[ "${REVISION}" != "" ]]; then
	SUFFIX_V3="${SUFFIX_V3}-${REVISION}"
fi
SUFFIX_V3="${SUFFIX_V3}-gentoo"

SUFFIX_V4="${MAJOR}.${MINOR}.${MICRO}"
if [ "${REVISION}" != "" ]; then
    SUFFIX_V4="${SUFFIX_V4}-${REVISION}"
fi
SUFFIX_V4="${SUFFIX_V4}-gentoo-${ARCH}"

SYSTEM_MAP="System.map-${SUFFIX_V3}"
if [[ -f $SYSTEM_MAP ]]; then
	rm ${SYSTEM_MAP} >> $LOGFILE 2>&1
fi
SYSTEM_MAP="System.map-${SUFFIX_V4}"
if [[ -f ${SYSTEM_MAP} ]]; then
	rm ${SYSTEM_MAP} >> $LOGFILE 2>&1
fi
if [[ -f ${SYSTEM_MAP}.old ]]; then
	rm ${SYSTEM_MAP}.old >> $LOGFILE 2>&1
fi

INITRAMFS="initramfs-${SUFFIX_V3}"
if [[ -f ${INITRAMFS} ]]; then
	rm ${INITRAMFS} >> $LOGFILE 2>&1
fi
INITRAMFS="initramfs-${SUFFIX_V4}"
if [[ -f ${INITRAMFS} ]]; then
	rm ${INITRAMFS} >> $LOGFILE 2>&1
fi
if [[ -f ${INITRAMFS}.old ]]; then
	rm ${INITRAMFS}.old >> $LOGFILE 2>&1
fi

KERNEL="kernel-${SUFFIX_V3}"
if [[ -f $KERNEL ]]; then
	rm $KERNEL >> $LOGFILE 2>&1
fi
KERNEL="vmlinuz-${SUFFIX_V4}"
if [[ -f $KERNEL ]]; then
	rm $KERNEL >> $LOGFILE 2>&1
fi
if [[ -f $KERNEL.old ]]; then
	rm $KERNEL.old >> $LOGFILE 2>&1
fi

cd /var/tmp/genkernel
INITRAMFS="initramfs-${ARCH}-${SUFFIX_V4}"
if [[ -f ${INITRAMFS} ]]; then
    rm ${INITRAMFS} >> $LOGFILE 2>&1
fi

# if we got until here, just remove the logfile again
rm $LOGFILE