Skip to content

Instantly share code, notes, and snippets.

@geonaumov
Last active September 5, 2024 12:05
An easy way to create .deb packages for BusyBox
#!/bin/bash
# Geo. Naumov
set -e
# OPTIONS
OUTPUTDIR="${HOME}/mypackages" # where your output packages will be placed
STAGINGDIR="${HOME}/mystaging" # where the work is done
PKGARCH="amd64" # edit this, uname -m won't work
PKGDESC="Created with mkdeb.sh" # Under development :)
MAINTAINER="test@example.com" # edit this
# DON'T EDIT BELOW THIS LINE
help_me () {
echo "DPKG CREATOR"
echo "A easy way to create .deb packages"
echo
echo "Usage:"
echo "./mkdeb.sh name version section dir"
echo
echo "Where:"
echo "name - is the name of your package, example: testapp"
echo "version - is the version id of your package, example: 1.1.1"
echo "section - is the section of your package, example: tools"
echo "dir - is the directory containing the files you want to pack"
echo
echo "Notes:"
echo " * All arguments are compulsory."
echo " * There are 3 extra dpkg tags, architecture, description and maintainer. Modify these by editing this script."
echo " * Architecture can be easily determined automatically, but by default Debian requires amd64 instead of x86_64, so this variable had to be hardcoded."
echo
echo "Hardcoded variables and their values:"
echo "Architecture is ${PKGARCH}"
echo "Maintainer is ${MAINTAINER}"
echo "Output directory is ${OUTPUTDIR}"
echo
}
clean_up () {
echo "Cleaning up..."
rm -rf $STAGINGDIR
rm -rf $INSTALLDIR # This command is executed only if you call the function after a successfull run, else the variable is not set yet.
}
if [[ $1 == "clean" ]]; then
clean_up
exit
fi
if [ $# -ne 4 ]; then
help_me
exit 1
fi
PKGNAME=$1
PKGVERSION=$2
PKGTAG=$3
INSTALLDIR=$(realpath $4)
OUTNAME=$OUTPUTDIR/$1-$2-$3-$PKGARCH.deb
mkdir -pv $OUTPUTDIR
mkdir -pv $STAGINGDIR
if [ ! -d $OUTPUTDIR ]; then
echo "Output directory missing or cannot be created!"
exit
fi
if [ ! -d $INSTALLDIR ]; then
echo "Install directory is missing, you are supposed to have an installation there!"
exit
fi
if [ -z "$(ls -A $INSTALLDIR)" ]; then
echo "Your install directory is empty, cannot create an empty package!"
exit
fi
if [ "$(ls -A $STAGINGDIR)" ]; then
echo "Your staging directory is not empty, clean it up or choose a different location!"
exit
fi
cd $INSTALLDIR
echo "Stripping"
# From https://github.com/ChrisSaturn/dpkg-wrapper/blob/master/fixups/50strip.sh
find $INSTALLDIR -type f |
while read f; do
case "$(file --brief $f)" in
ELF*executable*,\ not\ stripped | \
ELF*shared\ object*,\ not\ stripped)
strip --strip-unneeded $f ;;
current\ ar\ archive)
strip --strip-debug $f ;;
esac
done
echo "Creating data archive"
tar zcf $STAGINGDIR/data.tar.gz ./
echo "Creating control file"
mkdir -pv $STAGINGDIR/controldir
cd $STAGINGDIR/controldir
echo "Package: ${PKGNAME}" >> control
echo "Version: ${PKGVERSION}" >> control
echo "Maintainer: ${MAINTAINER}" >> control
echo "Section: ${PKGTAG}" >> control
echo "Architecture: ${PKGARCH}" >> control
echo "Description: ${PKGDESC}" >> control
echo "Creating control archive"
tar zcf $STAGINGDIR/control.tar.gz ./
cd $STAGINGDIR
echo "Creating dpkg version file"
echo 2.0 > debian-binary
ar r $OUTNAME debian-binary control.tar.gz data.tar.gz
echo "Package $OUTNAME created."
clean_up
echo "Done."
Copy link

ghost commented Nov 12, 2018

Awesome

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment