#!/bin/sh PREFIX= . "$PREFIX/lib/libalpine.sh" usage() { cat <<__EOF__ usage: setup-apkcache [-h] [DIR] Setup apk caching. If DIR is not specified user will be asked for location. options: -h Show this help __EOF__ exit 1 } is_mounted() { awk '{print $2}' /proc/mounts | grep -q "^$1$" } # figure out mount point find_mount_point() { local dir="$1" while ! [ -d "$dir" ]; do dir=${dir%/*} done local fs_id=$(stat -f -c %i "$dir") local parent="${dir%/*}" while [ -n "$parent" ] && [ "$(stat -f -c %i $parent)" = "$fs_id" ]; do dir=$parent parent=${parent%/*} done [ -z "$dir" ] && dir=/ echo $dir } get_mount_opts() { local mnt="$1" awk "\$2 == \"$mnt\" {gsub(/,/, \" \", \$4); print \$4}" /proc/mounts } is_mounted_ro() { local mnt="$1" local opts=$(get_mount_opts $mnt) local opt= for opt in $opts; do if [ "$opt" = "ro" ]; then return 0 fi done return 1 } while getopts "h" opt; do case $opt in h) usage;; esac done shift $(( $OPTIND - 1 )) # try auto detetect what we suggest suggestion= if [ -L /etc/apk/cache ]; then suggestion=$(readlink /etc/apk/cache) fi if [ -z "$suggestion" ] && [ -f /etc/lbu/lbu.conf ]; then . /etc/lbu/lbu.conf if [ -n "$LBU_MEDIA" ]; then suggestion=/media/$LBU_MEDIA/cache fi fi if [ -z "$suggestion" ] && [ -L /dev/usbdisk ]; then suggestion=/media/usb/cache fi if [ -z "$suggestion" ]; then suggestion=none fi cachedir=$1 while [ -z "$cachedir" ]; do echo -n "Where would you like to store apk cache? (or none) [$suggestion] " default_read cachedir $suggestion done if [ "$cachedir" = "none" ]; then exit 0 fi mount=$(find_mount_point $cachedir) cleanup= if ! is_mounted $mount; then mount $mount || exit 1 cleanup="umount" elif is_mounted_ro $mount; then mount -o remount,rw $mount || exit 1 cleanup="remount" fi mkdir -p $cachedir if [ -L /etc/apk/cache ]; then rm -f /etc/apk/cache fi ln -s $cachedir /etc/apk/cache case "$cleanup" in umount) umount $mount;; remount) mount -o remount,ro $mount;; esac