1
0
mirror of https://github.com/krislamo/kernmod.git synced 2024-09-20 05:40:36 +00:00
kernmod/build.sh

102 lines
2.5 KiB
Bash
Raw Normal View History

2021-11-10 06:14:16 +00:00
#!/bin/bash
2022-03-18 06:43:41 +00:00
# Initial variables
2022-02-01 04:04:50 +00:00
PACKAGE="helloworld"
2022-01-14 06:54:13 +00:00
VERSION="0.1"
2022-02-01 04:04:50 +00:00
REVISION="1"
2022-03-18 06:43:41 +00:00
SRCDIR="/vagrant/src"
SCRATCH="/vagrant/scratch"
OUTDIR="$SCRATCH"
TEMPDIR="$(mktemp -d)"
INSTALL=0
BUILDDIR="$TEMPDIR/${PACKAGE}_$VERSION-$REVISION"
2022-01-14 06:54:13 +00:00
2022-04-20 05:10:21 +00:00
# Test for distribution of GNU/Linux
# 0 = unknown
# 1 = debian
# 2 = rocky/centos/rhel
function check_distro {
if [ -f /etc/debian_version ]; then echo 1
elif [ -f /etc/rocky-release ] || [ -f /etc/centos-release ]; then echo 2
else echo 0
fi
}
2022-03-18 06:43:41 +00:00
# Build debian package
function build_deb {
2022-04-20 05:10:21 +00:00
[ $INSTALL -eq 1 ] && debian_headers
2022-03-18 06:43:41 +00:00
mkdir -p "$BUILDDIR/usr/src/${PACKAGE}-$VERSION"
mkdir -p "$BUILDDIR/etc"
mkdir -p "$BUILDDIR/DEBIAN"
cp -r $SRCDIR/usr/src/* "$BUILDDIR/usr/src/${PACKAGE}-$VERSION"
cp -r $SRCDIR/etc/* "$BUILDDIR/etc"
cp -r $SRCDIR/DEBIAN/* "$BUILDDIR/DEBIAN"
cd "$TEMPDIR"
dpkg-deb --build "${PACKAGE}_$VERSION-$REVISION"
}
2022-02-01 04:04:50 +00:00
2022-04-20 05:10:21 +00:00
# Build redhat package
function build_rpm {
[ $INSTALL -eq 1 ] && rhel_headers
}
2022-03-18 06:43:41 +00:00
# Display details on module
function info_mod {
modinfo "$PACKAGE"
cat /proc/modules | grep "$PACKAGE"
rmmod "$PACKAGE"
modprobe "$PACKAGE"
cat /var/log/messages | grep "$PACKAGE"
}
2022-01-14 06:54:13 +00:00
2022-04-20 05:10:21 +00:00
# Install Linux headers for current debian kernel
function debian_headers {
2022-03-18 06:43:41 +00:00
export DEBIAN_FRONTEND=noninteractive
apt-get update
apt-get install -y linux-headers-$(uname -r)
}
2022-04-20 05:10:21 +00:00
# Install Linux headers for current rhel kernel
function rhel_headers {
VERSION="$(uname -r | rev | cut -d '.' -f 2- | rev)"
yum install -y kernel-headers-"$VERSION"
}
# Find GNU/Linux distribution
2022-03-18 06:43:41 +00:00
set -x
2022-04-20 05:10:21 +00:00
DISTRO="$(check_distro)"
if [ $DISTRO -eq 0 ]; then
echo "ERROR: GNU/Linux distribution not detected"
exit -1
fi
# Build and install helloworld module or module(s) in $SCRATCH
2022-03-18 07:13:42 +00:00
if [ ! -z "$(ls -Al $SCRATCH | grep -e ^d)" ]; then
2022-03-18 06:43:41 +00:00
cd "$SCRATCH"
for d in */ ; do
if [ -f "$(basename $d)/override.sh" ]; then
SRCDIR="$(pwd)/$(basename $d)"
. "$(basename $d)/override.sh"
2022-04-20 05:10:21 +00:00
[ $DISTRO -eq 1 ] && build_deb
[ $DISTRO -eq 2 ] && build_rpm
2022-03-18 06:43:41 +00:00
if [ $INSTALL -eq 1 ]; then
apt-get install -y "./${PACKAGE}_$VERSION-$REVISION.deb"
info_mod
fi
cp "./${PACKAGE}_$VERSION-$REVISION.deb" \
"$OUTDIR/${PACKAGE}_$VERSION-$REVISION-$(date +%s).deb"
fi
done
else
INSTALL=1
2022-04-20 05:10:21 +00:00
if [ $DISTRO -eq 1 ]; then
build_deb
apt-get install -y "./${PACKAGE}_$VERSION-$REVISION.deb"
elif [ $DISTRO -eq 2 ]; then
build_rpm
echo "end of debug: exiting..."
exit 0
yum install -y "./${PACKAGE}_$VERSION-$REVISION.rpm"
fi
2022-03-18 06:43:41 +00:00
fi