Build and load binder/ashmem kernel modules on service start

As both kernel modules are not part of the standard Ubuntu kernel we
need to build and load them at runtime. We bundle this together with
the container manager start and reuse the classic snap scripts to do
the actual build in a proper environment.
This commit is contained in:
Simon Fels 2016-12-13 13:50:04 +01:00
commit 7f564c55eb
5 changed files with 206 additions and 0 deletions

View file

@ -1,4 +1,5 @@
#!/bin/sh
set -x
# We need to put the rootfs somewhere where we can modify some
# parts of the content on first boot (namely file permissions).
@ -21,6 +22,50 @@ if [ ! -e $SYSTEM_IMG ]; then
exit 1
fi
build_kernel_modules() {
kversion=$1
rm -rf $SNAP_COMMON/kernel-*
$SNAP/bin/classic-create || true
rm -rf $SNAP_COMMON/classic/build
mkdir -p $SNAP_COMMON/classic/build
cp -rav $SNAP/ashmem $SNAP_COMMON/classic/build/
cp -rav $SNAP/binder $SNAP_COMMON/classic/build/
cat<<EOF > $SNAP_COMMON/classic/build/run.sh
#!/bin/sh
set -ex
apt update
apt install -y --force-yes linux-headers-$kversion build-essential
cd /build/ashmem
make
cd /build/binder
make
EOF
chmod +x $SNAP_COMMON/classic/build/run.sh
$SNAP/bin/classic /build/run.sh
mkdir -p $SNAP_COMMON/kernel-$kversion
cp $SNAP_COMMON/classic/build/ashmem/ashmem_linux.ko \
$SNAP_COMMON/kernel-$kversion/
cp $SNAP_COMMON/classic/build/binder/binder_linux.ko \
$SNAP_COMMON/kernel-$kversion/
}
load_kernel_modules() {
kversion=`uname -r`
rmmod ashmem_linux binder_linux || true
echo "Loading kernel modules for version $kversion.."
insmod $SNAP_COMMON/kernel-$kversion/binder_linux.ko
insmod $SNAP_COMMON/kernel-$kversion/ashmem_linux.ko
sleep 0.5
chmod 666 /dev/binder
chmod 666 /dev/ashmem
}
start() {
# Extract ramdisk content instead of trying to bind mount the
# cpio image file to allow modifications.
@ -52,6 +97,13 @@ start() {
# possible. See snapcraft.yaml for further details.
$SNAP/bin/anbox-bridge.sh start
kversion=`uname -r`
if [ ! -e $SNAP_COMMON/kernel-$kversion ]; then
build_kernel_modules $kversion
fi
load_kernel_modules
exec $SNAP/usr/sbin/aa-exec -p unconfined -- $SNAP/bin/anbox-wrapper.sh container-manager
}
@ -63,6 +115,8 @@ stop() {
umount $ROOTFS_PATH
$SNAP/bin/anbox-bridge.sh stop
rmmod ashmem_linux binder_linux || true
}
case "$1" in