aports

Custom Alpine Linux aports

git clone git://git.lin.moe/aports.git

  1#!/bin/sh
  2# Copyright (c) 2012-2019, Piotr Karbowski <piotr.karbowski@gmail.com>
  3# All rights reserved.
  4#
  5# Redistribution and use in source and binary forms, with or without modification, are
  6# permitted provided that the following conditions are met:
  7#
  8#    * Redistributions of source code must retain the above copyright notice, this list
  9#      of conditions and the following disclaimer.
 10#    * Redistributions in binary form must reproduce the above copyright notice, this list
 11#      of conditions and the following disclaimer in the documentation and/or other
 12#      materials provided with the distribution.
 13#    * Neither the name of the Piotr Karbowski nor the names of its contributors may be
 14#      used to endorse or promote products derived from this software without specific
 15#      prior written permission.
 16#
 17# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
 18# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 19# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
 20# THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 21# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
 22# OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 23# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 24# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE US
 25# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 26
 27# This script meant to create /dev/disk/by-* and /dev/mapper/* symlinks.
 28# and remove them after storage device is removed.
 29# the /dev/disk/by-* handling based on the idea and proof of concept from BitJam.
 30
 31# debug
 32#exec >> /run/debug-mdev 2>&1
 33#set -x
 34#echo '### ENV:'
 35#env
 36#echo '### CODE:'
 37#
 38
 39umask 077
 40
 41storage_dir="/dev/.mdev-like-a-boss"
 42[ -d "${storage_dir}" ] || mkdir "${storage_dir}"
 43
 44[ "${MDEV}" ] || exit 2
 45
 46create_uuid_label_symlink() {
 47	local target_dir="/dev/disk/by-${1}"
 48	local target_symlink="${target_dir}/${2}"
 49	[ -e "${target_symlink}" ] && return
 50	mkdir -p "${target_dir}"
 51	ln -snf "/dev/${MDEV}" "${target_symlink}"
 52	echo "${target_symlink}" >"${storage_dir}/storage_symlink_${1}_${MDEV}"
 53}
 54
 55add_symlinks() {
 56	# Skip temp cryptsetup nodes.
 57	case "${MDEV}" in
 58		'dm-'[0-9]*)
 59			case "$(cat "/sys/block/${MDEV}/dm/name")" in
 60				'temporary-cryptsetup-'[0-9]*)
 61					return 0
 62				;;
 63			esac
 64		;;
 65	esac
 66
 67	if command -v blkid >/dev/null 2>&1; then
 68		local field name value UUID LABEL TYPE PTTYPE PARTUUID
 69		local blkid_output="$(blkid "/dev/${MDEV}")"
 70		eval "${blkid_output#*: }"
 71
 72		[ "${UUID}" ] && create_uuid_label_symlink 'uuid' "${UUID}"
 73		[ "${LABEL}" ] && create_uuid_label_symlink 'label' "${LABEL}"
 74		[ "${PARTUUID}" ] && create_uuid_label_symlink 'partuuid' "${PARTUUID}"
 75	fi
 76
 77	if [ -f "/sys/block/${MDEV}/dm/name" ]; then
 78		[ -d '/dev/mapper' ] || mkdir '/dev/mapper'
 79		if ! [ -c '/dev/mapper/control' ]; then
 80			awk '$2 == "device-mapper" { foo = system("mknod /dev/mapper/control c 10 " $1); exit foo }' /proc/misc || exit 1
 81		fi
 82		local dmname="$(cat "/sys/block/${MDEV}/dm/name")"
 83		if [ "${dmname}" ]; then
 84			local target_symlink="/dev/mapper/${dmname}"
 85			[ -e "${target_symlink}" ] && return
 86			ln -snf "/dev/${MDEV}" "${target_symlink}"
 87			echo "${target_symlink}" >"${storage_dir}/storage_symlink_mapper_${MDEV}"
 88		fi
 89	fi
 90}
 91
 92set_readahead() {
 93	read_ahead_kb_control="/sys/class/block/${MDEV}/queue/read_ahead_kb"
 94	new_read_ahead_kb="2048"
 95	if [ -f "${read_ahead_kb_control}" ]; then
 96		read_ahead_kb="$(cat "${read_ahead_kb_control}")"
 97		if [ "${read_ahead_kb}" -lt "${new_read_ahead_kb}" ]; then
 98			logger -t mdev "Changing ${MDEV} read_ahead_kb from ${read_ahead_kb} to ${new_read_ahead_kb}"
 99			echo -n "${new_read_ahead_kb}" >"${read_ahead_kb_control}"
100		fi
101	fi
102}
103
104# media change detection
105set_eventpoll() {
106	[ -f /sys/block/$MDEV/events ] || return 0
107	case "$(cat /sys/block/$MDEV/events)" in
108		'media_change')
109			echo 2000 > /sys/block/$MDEV/events_poll_msecs
110			;;
111	esac
112}
113
114drop_symlinks() {
115	local type
116	for type in uuid label mapper; do
117		[ -f "${storage_dir}/storage_symlink_${type}_${MDEV}" ] || continue
118		local target_symlink="$(cat "${storage_dir}/storage_symlink_${type}_${MDEV}" 2>/dev/null)"
119		[ "${target_symlink}" ] || continue
120
121		local target_symlink_device="$(readlink "${target_symlink}")"
122		if [ "${target_symlink_device}" = "/dev/${MDEV}" ]; then
123			rm "${target_symlink}"
124		fi
125		rm "${storage_dir}/storage_symlink_${type}_${MDEV}"
126	done
127}
128
129case "${ACTION}" in
130	'add'|'')
131		add_symlinks
132		set_readahead
133		set_eventpoll
134	;;
135	'remove')
136		drop_symlinks
137	;;
138esac
139