This commit is contained in:
illiliti
2020-04-11 22:31:02 +03:00
parent de8603e784
commit b9137bf486
11 changed files with 789 additions and 1004 deletions

View File

@@ -1,179 +0,0 @@
.TH tinyramfs 5 "March 2020" tinyramfs
.SH NAME
tinyramfs \- configuration options
.SH GENERAL
.TP
.BR \fBdebug=\fR"\&\fI {0|1} \fR\&"
Enable shell debugging
This option also drop you to shell after root partition being mounted in initramfs
.TP
.TP
.BR \fBforce=\fR"\&\fI {0|1} \fR\&"
Disable warning about exists initramfs image
.TP
.TP
.BR \fBinit=\fR"\&\fI /path/to/file \fR\&"
Specify init program
.TP
.TP
.BR \fBinitramfs=\fR"\&\fI /path/to/file \fR\&"
Specify initramfs output name
.TP
.TP
.BR \fBmonolith=\fR"\&\fI {0|1} \fR\&"
Enable monolithic kernel support
This option required if you using monolithic kernel (builtin drivers)
.TP
.TP
.BR \fBmoddir=\fR"\&\fI /path/to/dir \fR\&"
Specify directory of drivers
.TP
.TP
.BR \fBkernel=\fR"\&\fI ... \fR\&"
Specify kernel version
.TP
.TP
.BR \fBcompress=\fR"\&\fI ... \fR\&"
Specify compression program
.TP
.TP
.BR \fBroot=\fR"\&\fI ... \fR\&"
Specify root partition [PARTUUID, UUID, LABEL, DEVICE]
This option is required
.TP
.TP
.BR \fBroot_type=\fR"\&\fI ... \fR\&"
Specify root partition type
.TP
.TP
.BR \fBroot_opts=\fR"\&\fI ... \fR\&"
Specify root partition mount options (comma separated)
.TP
.TP
.BR \fButil_linux=\fR"\&\fI {0|1} \fR\&"
Include util-linux binaries
This option required if you need PARTUUID support
.TP
.TP
.BR \fBdevmgr=\fR"\&\fI ... \fR\&"
Specify device manager [udev, mdev, mdevd]
This option is required
.TP
.TP
.BR \fBhostonly=\fR"\&\fI {0|1} \fR\&"
Enable hostonly mode
.TP
.TP
.BR \fBdrivers=\fR"\&\fI ... \fR\&"
Include additional drivers (space separated)
If host only mode enabled you need to specify your root partition driver
.TP
.TP
.BR \fBdrivers_exclude=\fR"\&\fI ... \fR\&"
Exclude specified drivers (space separated)
.TP
.TP
.BR \fBbinaries=\fR"\&\fI ... \fR\&"
Include additional binaries (space separated)
.TP
.SH LVM
.TP
.BR \fBlvm=\fR"\&\fI {0|1} \fR\&"
Enable LVM support
This option required if you need LVM support
.TP
.TP
.BR \fBlvm_name=\fR"\&\fI ... \fR\&"
Specify logical volume name
.TP
.TP
.BR \fBlvm_group=\fR"\&\fI ... \fR\&"
Specify volume group name
.TP
.TP
.BR \fBlvm_config=\fR"\&\fI {0|1} \fR\&"
Include config from /etc/lvm
This option overwrite lvm_discard option
.TP
.TP
.BR \fBlvm_discard=\fR"\&\fI {0|1} \fR\&"
Enable TRIM requests
.TP
.TP
.BR \fBlvm_args=\fR"\&\fI ... \fR\&"
Pass additional options to lvm
.TP
.SH LUKS
.TP
.BR \fBluks=\fR"\&\fI {0|1} \fR\&"
Enable LUKS support
This option required if you need LUKS support
.TP
.TP
.BR \fBluks_root=\fR"\&\fI ... \fR\&"
Specify encrypted root partition [PARTUUID, UUID, LABEL, DEVICE]
This option required if you need LUKS support
.TP
.TP
.BR \fBluks_name=\fR"\&\fI ... \fR\&"
Specify device mapper name
.TP
.TP
.BR \fBluks_header=\fR"\&\fI /path/to/file \fR\&"
Embed header into initramfs
.TP
.TP
.BR \fBluks_keyfile=\fR"\&\fI /path/to/file \fR\&"
Embed key into initramfs
.TP
.TP
.BR \fBluks_discard=\fR"\&\fI {0|1} \fR\&"
Enable TRIM requests
.TP
.TP
.BR \fBluks_args=\fR"\&\fI ... \fR\&"
Pass additional options to cryptsetup
.TP

View File

@@ -1,102 +0,0 @@
.TH tinyramfs 7 "March 2020" tinyramfs
.SH NAME
tinyramfs \- kernel command line options
.SH GENERAL
.TP
.BR \fBdebug=\fR"\&\fI {0|1} \fR\&"
Enable shell debugging
This option also drop you to shell after root partition being mounted
.TP
.TP
.BR \fBinit=\fR"\&\fI /path/to/file \fR\&"
Specify init program
.TP
.TP
.BR \fBroot=\fR"\&\fI ... \fR\&"
Specify root partition [PARTUUID, UUID, LABEL, DEVICE]
.TP
.TP
.BR \fBroot.type=\fR"\&\fI ... \fR\&"
Specify root partition type
.TP
.TP
.BR \fBroot.opts=\fR"\&\fI ... \fR\&"
Specify root partition mount options (comma separated)
.TP
.SH LVM
.TP
.BR \fBlvm=\fR"\&\fI {0|1} \fR\&"
Enable LVM support
.TP
.TP
.BR \fBlvm.name=\fR"\&\fI ... \fR\&"
Specify logical volume name
.TP
.TP
.BR \fBlvm.group=\fR"\&\fI ... \fR\&"
Specify volume group name
.TP
.TP
.BR \fBlvm.config=\fR"\&\fI {0|1} \fR\&"
Not implemented
.TP
.TP
.BR \fBlvm.discard=\fR"\&\fI {0|1} \fR\&"
Not implemented
.TP
.TP
.BR \fBlvm.args=\fR"\&\fI ... \fR\&"
Pass additional options to lvm
.TP
.SH LUKS
.TP
.BR \fBluks=\fR"\&\fI {0|1} \fR\&"
Enable LUKS support
.TP
.TP
.BR \fBluks.root=\fR"\&\fI ... \fR\&"
Specify encrypted root partition [PARTUUID, UUID, LABEL, DEVICE]
.TP
.TP
.BR \fBluks.name=\fR"\&\fI ... \fR\&"
Specify device mapper name
.TP
.TP
.BR \fBluks.header=\fR"\&\fI /path/to/file \fR\&"
Not implemented
.TP
.TP
.BR \fBluks.keyfile=\fR"\&\fI /path/to/file \fR\&"
Not implemented
.TP
.TP
.BR \fBluks.discard=\fR"\&\fI {0|1} \fR\&"
Enable TRIM requests
.TP
.TP
.BR \fBluks.args=\fR"\&\fI ... \fR\&"
Pass additional options to cryptsetup
.TP

View File

@@ -0,0 +1,43 @@
#!/bin/sh -f
#
# create /dev/disk/by-* and /dev/mapper/* symlinks
create_symlink()
{
sym="$1"
sym="${sym%\"}"
sym="${sym#\"}"
sym="${dir}${sym}"
mkdir -p "$dir" 2> /dev/null
ln -s "/dev/${MDEV}" "$sym" 2> /dev/null
}
# int main()
{
[ "$MDEV" ] || exit 1
for line in $(blkid "$MDEV"); do
case "${line%%=*}" in
UUID)
dir="/dev/disk/by-uuid/"
create_symlink "${line##*=}"
;;
LABEL)
dir="/dev/disk/by-label/"
create_symlink "${line##*=}"
;;
PARTUUID)
dir="/dev/disk/by-partuuid/"
create_symlink "${line##*=}"
;;
esac
done
[ -e "/sys/block/${MDEV}/dm/name" ] &&
{
mkdir -p /dev/mapper 2> /dev/null
read -r name < "/sys/block/${MDEV}/dm/name"
ln -s "/dev/${MDEV}" "/dev/mapper/${name}" 2> /dev/null
}
}

View File

@@ -1,14 +0,0 @@
root:x:0:
tty:x:5:
dialout:x:11:
uucp:x:14:
kmem:x:3:
input:x:25:
video:x:13:
audio:x:12:
lp:x:10:
disk:x:9:
cdrom:x:16:
tape:x:6:
kvm:x:24:
floppy:x:8:

282
usr/share/tinyramfs/init Normal file → Executable file
View File

@@ -1,168 +1,236 @@
#!/sbin/busybox sh
#!/bin/sh -ef
#
# tiny init script
# tiny init
panic() {
panic()
{
printf "panic >> %s\n" "$1"
# TODO fix job control
sh
# see https://busybox.net/FAQ.html#job_control
setsid sh -c "exec sh <> /dev/${console:-console} 2>&1" || sh
}
parse_cmdline() {
read -r cmdline < /proc/cmdline
set -f && set +f -- $cmdline
findfs()
{
value=0; device=
for line in "$@"; do
value="${line##*=}"
case "${1%%=*}" in
/dev/*)
device="$1"
;;
UUID)
device="/dev/disk/by-uuid/${1##*=}"
;;
LABEL)
device="/dev/disk/by-label/${1##*=}"
;;
PARTUUID)
device="/dev/disk/by-partuuid/${1##*=}"
;;
esac
case "${line%%=*}" in
debug) debug="$value" ;;
init) init="$value" ;;
root) root="$value" ;;
root.type) root_type="$value" ;;
root.opts) root_opts="$value" ;;
lvm) lvm="$value" ;;
lvm.name) lvm_name="$value" ;;
lvm.group) lvm_group="$value" ;;
lvm.args) lvm_args="$value" ;;
luks) luks="$value" ;;
luks.root) luks_root="$value" ;;
luks.name) luks_name="$value" ;;
luks.discard) luks_discard="$value" ;;
luks.args) luks_args="$value" ;;
# TODO implement
#lvm.discard) ;;
#lvm.config) ;;
#luks.header) ;;
#luks.keyfile) ;;
esac
# avoid race condition
while [ ! -e "$device" ]; do
value=$(( value + 1 ))
[ "$value" = 15 ] && panic "failed to lookup partition"
sleep 1
done
}
prepare_environment() {
/sbin/busybox --install -s
prepare_environment()
{
. /etc/config
. /config || panic "failed to source config"
export \
SHELL=/bin/sh \
TERM=linux \
HOME=/root \
PATH=/bin:/sbin:/usr/bin:/usr/sbin \
PS1="# " \
LC_ALL=C \
LANG=C \
OLD_IFS="$IFS"
# fix for ubase mount
:> /etc/fstab
mount -t proc -o nosuid,noexec,nodev proc /proc
mount -t sysfs -o nosuid,noexec,nodev sys /sys
mount -t tmpfs -o mode=0755,nosuid,nodev run /run
mount -t devtmpfs -o mode=0755,noexec,nosuid dev /dev
mkdir -pm 0755 /run/cryptsetup /run/lvm /dev/pts /dev/shm
mount -t devpts -o gid=5,mode=620,noexec,nosuid devpts /dev/pts
mount -t tmpfs -o mode=1777,noexec,nosuid,nodev shm /dev/shm
mount -t devtmpfs -o nosuid,noexec,mode=0755 dev /dev
ln -s /proc/self/fd /dev/fd
ln -s /proc/self/fd/0 /dev/stdin
ln -s /proc/self/fd/1 /dev/stdout
ln -s /proc/self/fd/2 /dev/stderr
trap 'panic "something went wrong"' EXIT
}
setup_devmgr() {
parse_cmdline()
{
[ "$break" = cmdline ] && panic "break before parse cmdline"
read -r cmdline < /proc/cmdline
for line in $cmdline; do
case "$line" in
debug | debug=1)
set -x
;;
*.*)
# TODO implement backward compatibilty with dracut, mkinitcpio, etc
: no operation
;;
*=*)
export "$line"
;;
*)
export "${line}=1"
;;
esac
done
}
setup_devmgr()
{
[ "$break" = devmgr ] && panic "break before setup device manager"
case "$devmgr" in
udev)
udevd -d -N never
udevadm trigger -c add -t subsystems
udevadm trigger -c add -t devices
udevadm settle
;;
;;
mdev)
mdev -df &
mdev -df 2> /dev/null & mdev_pid="$!"
find /sys -name modalias -type f -exec sort -u {} + |
xargs modprobe -ba
;;
[ "$monolith" != 1 ] &&
{
set -- $(find /sys -name modalias -type f -exec sort -u {} +)
modprobe -a "$@" 2> /dev/null
}
;;
mdevd)
mdevd &
mdevd 2> /dev/null & mdevd_pid="$!"
mdevd-coldplug
;;
;;
esac
}
findfs_sh() {
value="${1##*=}"
unlock_luks()
{
[ "$break" = luks ] && panic "break before unlock LUKS"
case "${1%%=*}" in
LABEL) device="/dev/disk/by-label/${value}" ;;
UUID) device="/dev/disk/by-uuid/${value}" ;;
PARTUUID) device="/dev/disk/by-partuuid/${value}" ;;
/dev/*) device="$1" ;;
esac
{ IFS=,; set -- $luks_opts; IFS="$OLD_IFS"; }
# avoid race condition
while [ ! -e "$device" ]; do
sleep 0.5
[ "$increment" ] || increment=0
increment=$(( increment + 1 ))
[ "$increment" = 10 ] && panic "failed to lookup partition"
for opt; do
case "$opt" in
discard | discard=1)
luks_discard="--allow-discards"
;;
header=*)
luks_header="--${opt}"
;;
name=*)
luks_name="${opt##*=}"
;;
key=*)
luks_key="-d ${opt##*=}"
;;
esac
done
printf "%s\n" "$device"
findfs "$luks_root"
set -- "--disable-locks" "$luks_key" "$luks_discard" "$luks_header" "$device" "${luks_name:-luks-${device##*/}}"
cryptsetup open $@ || panic "failed to unlock LUKS"
}
unlock_luks() {
[ "$luks_discard" = 1 ] && luks_args="--allow-discards $luks_args"
[ -f /root/luks_header ] && luks_args="--header=/root/luks_header $luks_args"
[ -f /root/luks_keyfile ] && luks_args="--key-file=/root/luks_keyfile $luks_args"
trigger_lvm()
{
[ "$break" = lvm ] && panic "break before trigger LVM"
cryptsetup $luks_args \
luksOpen \
$(findfs_sh "$luks_root") \
${luks_name:-luks_root} ||
panic "failed to unlock luks container"
}
{ IFS=,; set -- $lvm_opts; IFS="$OLD_IFS"; }
for opt; do
case "$opt" in
discard | discard=1)
lvm_discard="--config=devices{issue_discards=1}"
;;
config=0)
:> /etc/lvm/lvm.conf
;;
group=*)
lvm_group="${opt##*=}"
;;
name=*)
lvm_name="/${opt##*=}"
;;
tag=*)
lvm_tag="@${opt##*=}"
;;
esac
done
set -- "--sysinit" "-qq" "-ay" "$lvm_discard"
trigger_lvm() {
if [ "$lvm_group" ] && [ "$lvm_name" ]; then
lvm lvchange $lvm_args --sysinit -q -a y "${lvm_group}/${lvm_name}" > /dev/null
lvchange $@ "${lvm_group}${lvm_name}"
elif [ "$lvm_group" ]; then
lvm vgchange $lvm_args --sysinit -q -a y "$lvm_group" > /dev/null
vgchange $@ "$lvm_group"
elif [ "$lvm_tag" ]; then
lvchange $@ "$lvm_tag"
else
lvm vgchange $lvm_args --sysinit -q -a y > /dev/null
vgchange $@
fi
}
mount_rootfs() {
mount ${root_type:+-t $root_type} \
${root_opts:+-o $root_opts} \
$(findfs_sh "$root") \
/mnt/root ||
panic "failed to mount rootfs"
mount_root()
{
[ "$break" = root ] && panic "break before mount root"
findfs "$root"
set -- "${root_type:+-t $root_type}" "${root_opts:+-o $root_opts}" "$device" "/mnt/root"
mount $@ || panic "failed to mount root"
}
cleanup() {
cleanup()
{
[ "$break" = cleanup ] && panic "break before cleanup"
case "$devmgr" in
udev) udevadm control -e ;;
mdev) killall mdev ;;
mdevd) killall mdevd ;;
mdev) kill "$mdev_pid" ;;
mdevd) kill "$mdevd_pid" ;;
esac
umount /dev /sys /proc
# temporary workaround until util-linux mount implements 'mount -o move'
# see https://github.com/karelzak/util-linux/issues/997
for dir in dev sys proc; do
mount -o move "$dir" "/mnt/root/${dir}" || mount --move "$dir" "/mnt/root/${dir}"
done
}
boot_system() {
exec switch_root \
/mnt/root \
${init:-/sbin/init} ||
panic "failed to boot system"
boot_system()
{
[ "$break" = boot ] && panic "break before boot system"
set -- "/mnt/root" "${init:-/sbin/init}"
exec switch_root $@ 2> /dev/null || panic "failed to boot system"
}
prepare_environment
parse_cmdline
[ "$debug" = 1 ] && set -x
setup_devmgr
# int main()
{
prepare_environment
parse_cmdline
setup_devmgr
# TODO handle situations when LUKS on LVM
[ "$luks" = 1 ] &&
command -v cryptsetup > /dev/null 2>&1 && unlock_luks
[ "$luks" = 1 ] && unlock_luks
[ "$lvm" = 1 ] && trigger_lvm
[ "$lvm" = 1 ] &&
command -v lvm > /dev/null 2>&1 && trigger_lvm
mount_rootfs
[ "$debug" = 1 ] && panic "dropping to shell"
cleanup
boot_system
mount_root
cleanup
boot_system
}

View File

@@ -1,96 +0,0 @@
# Copyright (c) 2012-2019, Piotr Karbowski <piotr.karbowski@gmail.com>
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without modification, are
# permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice, this list
# of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright notice, this list
# of conditions and the following disclaimer in the documentation and/or other
# materials provided with the distribution.
# * Neither the name of the Piotr Karbowski nor the names of its contributors may be
# used to endorse or promote products derived from this software without specific
# prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
# THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
# OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE US
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# mdev-like-a-boss
# Syntax:
# [-]devicename_regex user:group mode [=path]|[>path]|[!] [@|$|*cmd args...]
# [-]$ENVVAR=regex user:group mode [=path]|[>path]|[!] [@|$|*cmd args...]
# [-]@maj,min[-min2] user:group mode [=path]|[>path]|[!] [@|$|*cmd args...]
#
# [-]: do not stop on this match, continue reading mdev.conf
# =: move, >: move and create a symlink
# !: do not create device node
# @|$|*: run cmd if $ACTION=remove, @cmd if $ACTION=add, *cmd in all cases
# support module loading on hotplug
$MODALIAS=.* 0:0 660 @modprobe -b "$MODALIAS"
# null may already exist; therefore ownership has to be changed with command
null 0:0 666 @chmod 666 $MDEV
zero 0:0 666
full 0:0 666
random 0:0 444
urandom 0:0 444
hwrandom 0:0 444
grsec 0:0 660
kmem 0:0 640
mem 0:0 640
port 0:0 640
# console may already exist; therefore ownership has to be changed with command
console 0:5 600 @chmod 600 $MDEV
ptmx 0:5 666
pty.* 0:5 660
# typical devices
tty 0:5 666
tty[0-9]* 0:5 660
vcsa*[0-9]* 0:5 660
ttyS[0-9]* 0:14 660
# block devices
ram([0-9]*) 0:9 660 >rd/%1
loop([0-9]+) 0:9 660 >loop/%1
sr[0-9]* 0:16 660 @ln -sf $MDEV cdrom
fd[0-9]* 0:8 660
SUBSYSTEM=block;.* 0:9 660 */lib/mdev/storage-device
# raid controllers
cciss!(.*) 0:9 660 =cciss/%1
ida!(.*) 0:9 660 =ida/%1
rd!(.*) 0:9 660 =rd/%1
# graphics
card[0-9] 0:13 660 =dri/
agpgart 0:0 660 >misc/
psaux 0:0 660 >misc/
rtc 0:0 664 >misc/
# input stuff
SUBSYSTEM=input;.* 0:25 660
# drm etc
dri/.* 0:13 660
# don't create old usbdev* devices.
usbdev[0-9].[0-9]* 0:0 660 !
# stop creating x:x:x:x which looks like /dev/dm-*
[0-9]+\:[0-9]+\:[0-9]+\:[0-9]+ 0:0 660 !
# /dev/cpu support.
microcode 0:0 600 =cpu/
cpu([0-9]+) 0:0 600 =cpu/%1/cpuid
msr([0-9]+) 0:0 600 =cpu/%1/msr

View File

@@ -1,2 +0,0 @@
root:x:0:0::/root:/bin/sh
nobody:x:99:99::/:/bin/false

View File

@@ -1,133 +0,0 @@
#!/bin/sh
# Copyright (c) 2012-2019, Piotr Karbowski <piotr.karbowski@gmail.com>
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without modification, are
# permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice, this list
# of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright notice, this list
# of conditions and the following disclaimer in the documentation and/or other
# materials provided with the distribution.
# * Neither the name of the Piotr Karbowski nor the names of its contributors may be
# used to endorse or promote products derived from this software without specific
# prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
# THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
# OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE US
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# This script meant to create /dev/disk/by-* and /dev/mapper/* symlinks.
# and remove them after storage device is removed.
# the /dev/disk/by-* handling based on the idea and proof of concept from BitJam.
# debug
#exec >> /run/debug-mdev 2>&1
#set -x
#echo '### ENV:'
#env
#echo '### CODE:'
#
umask 077
storage_dir="/tmp/.mdev-like-a-boss"
[ -d "$storage_dir" ] || mkdir "$storage_dir"
[ "$MDEV" ] || exit 2
create_uuid_label_symlink() {
target_dir="/dev/disk/by-${1}"
target_symlink="${target_dir}/${2}"
[ -e "$target_symlink" ] && return
mkdir -p "$target_dir"
ln -sf "/dev/${MDEV}" "$target_symlink"
printf "%s\n" "$target_symlink" > "${storage_dir}/storage_symlink_${1}_${MDEV}"
}
add_symlinks() {
# Skip temp cryptsetup nodes.
case "$MDEV" in
dm-[0-9]*)
case $(cat "/sys/block/${MDEV}/dm/name") in
temporary-cryptsetup-[0-9]*)
return 0
;;
esac
;;
esac
if command -v blkid > /dev/null 2>&1; then
blkid_output=$(blkid "/dev/${MDEV}")
eval "${blkid_output#*: }"
[ "$UUID" ] && create_uuid_label_symlink uuid "$UUID"
[ "$LABEL" ] && create_uuid_label_symlink label "$LABEL"
[ "$PARTUUID" ] && create_uuid_label_symlink partuuid "$PARTUUID"
fi
if [ -f "/sys/block/${MDEV}/dm/name" ]; then
[ -d /dev/mapper ] || mkdir /dev/mapper
if ! [ -c /dev/mapper/control ]; then
while read -r dm; do
[ "${dm#* }" = device-mapper ] && {
mknod /dev/mapper/control c 10 "${dm% *}" || exit 1
break
}
done < /proc/misc
fi
dmname=$(cat "/sys/block/${MDEV}/dm/name")
if [ "$dmname" ]; then
target_symlink="/dev/mapper/${dmname}"
[ -e "$target_symlink" ] && return
ln -sf "/dev/${MDEV}" "$target_symlink"
printf "%s\n" "$target_symlink" > "${storage_dir}/storage_symlink_mapper_${MDEV}"
fi
fi
}
set_readahead() {
read_ahead_kb_control="/sys/class/block/${MDEV}/queue/read_ahead_kb"
new_read_ahead_kb="2048"
if [ -f "$read_ahead_kb_control" ]; then
read_ahead_kb=$(cat "$read_ahead_kb_control")
if [ "$read_ahead_kb" -lt "$new_read_ahead_kb" ]; then
logger -t mdev "Changing $MDEV read_ahead_kb from $read_ahead_kb to $new_read_ahead_kb"
printf "%s" "$new_read_ahead_kb" > "$read_ahead_kb_control"
fi
fi
}
drop_symlinks() {
for type in uuid label mapper; do
[ -f "${storage_dir}/storage_symlink_${type}_${MDEV}" ] || continue
target_symlink=$(cat "${storage_dir}/storage_symlink_${type}_${MDEV}" 2> /dev/null)
[ "$target_symlink" ] || continue
target_symlink_device=$(readlink "$target_symlink")
if [ "$target_symlink_device" = "/dev/${MDEV}" ]; then
rm "$target_symlink"
fi
rm "${storage_dir}/storage_symlink_${type}_${MDEV}"
done
}
case "$ACTION" in
add | "")
add_symlinks
set_readahead
;;
remove)
drop_symlinks
;;
esac