reorganization
This commit is contained in:
165
files/init
Normal file
165
files/init
Normal file
@@ -0,0 +1,165 @@
|
||||
#!/sbin/busybox sh
|
||||
#
|
||||
# tiny init script
|
||||
|
||||
panic() {
|
||||
printf "panic >> %s\n" "$1"
|
||||
|
||||
# TODO fix job control
|
||||
sh -l
|
||||
}
|
||||
|
||||
parse_cmdline() {
|
||||
# turn output into list
|
||||
set -- $(cat /proc/cmdline)
|
||||
|
||||
# parse line by line
|
||||
for line in "$@"; do
|
||||
|
||||
# parse options
|
||||
case "${line%%=*}" in
|
||||
debug) debug="${line##*=}" ;;
|
||||
init) init="${line##*=}" ;;
|
||||
root) root="${line##*=}" ;;
|
||||
root.type) root_type="${line##*=}" ;;
|
||||
root.opts) root_opts="${line##*=}" ;;
|
||||
lvm) lvm="${line##*=}" ;;
|
||||
lvm.name) lvm_name="${line##*=}" ;;
|
||||
lvm.group) lvm_group="${line##*=}" ;;
|
||||
lvm.args) lvm_args="${line##*=}" ;;
|
||||
luks) luks="${line##*=}" ;;
|
||||
luks.root) luks_root="${line##*=}" ;;
|
||||
luks.name) luks_name="${line##*=}" ;;
|
||||
luks.discard) luks_discard="${line##*=}" ;;
|
||||
luks.args) luks_args="${line##*=}" ;;
|
||||
# TODO implement
|
||||
#lvm.discard) ;;
|
||||
#lvm.conf) ;;
|
||||
#luks_header) ;;
|
||||
#luks_keyfile) ;;
|
||||
esac
|
||||
done
|
||||
}
|
||||
|
||||
mnt_pseudofs() {
|
||||
mount -t proc none /proc
|
||||
mount -t sysfs none /sys
|
||||
mount -t devtmpfs none /dev
|
||||
mount -t tmpfs none /tmp
|
||||
}
|
||||
|
||||
setup_mdev() {
|
||||
# setup hotplugger
|
||||
if [ -e /proc/sys/kernel/hotplug ]; then
|
||||
printf /sbin/mdev > /proc/sys/kernel/hotplug
|
||||
else
|
||||
uevent mdev &
|
||||
fi
|
||||
|
||||
# trigger mdev
|
||||
mdev -s
|
||||
|
||||
# trigger uevent for usb devices
|
||||
for device in /sys/bus/usb/devices/*; do
|
||||
case "${device##*/}" in
|
||||
[0-9]*-[0-9]*) printf add > "${device}/uevent" ;;
|
||||
esac
|
||||
done
|
||||
|
||||
# load drivers
|
||||
find /sys -name modalias -type f -exec sort -u "{}" "+" | xargs modprobe -qba
|
||||
}
|
||||
|
||||
setup_mdevd() {
|
||||
# setup daemon
|
||||
mdevd &
|
||||
|
||||
# trigger uevents
|
||||
mdevd-coldplug
|
||||
}
|
||||
|
||||
setup_udev() {
|
||||
udevd --daemon
|
||||
udevadm trigger --action=add --type=subsystems
|
||||
udevadm trigger --action=add --type=devices
|
||||
udevadm settle
|
||||
}
|
||||
|
||||
findfs_sh() {
|
||||
case "${1%%=*}" in
|
||||
LABEL) device="/dev/disk/by-label/${1##*=}" ;;
|
||||
UUID) device="/dev/disk/by-uuid/${1##*=}" ;;
|
||||
PARTUUID) device="/dev/disk/by-partuuid/${1##*=}" ;;
|
||||
/dev/*) device="$1" ;;
|
||||
*) panic "findfs option broken" ;;
|
||||
esac
|
||||
|
||||
# avoid race condition
|
||||
while [ ! -e "$device" ]; do
|
||||
sleep 0.5
|
||||
[ "$increment" ] || increment=0
|
||||
increment=$(( increment + 1 ))
|
||||
[ "$increment" = 10 ] && panic "failed to lookup partition"
|
||||
done
|
||||
|
||||
printf "%s\n" "$device"
|
||||
}
|
||||
|
||||
unlock_luks() {
|
||||
[ "$luks_discard" = 1 ] && luks_args="--allow-discards $luks_args"
|
||||
cryptsetup $luks_args luksOpen $(findfs_sh "$luks_root") ${luks_name:-luks_root} || panic "failed to unlock luks container"
|
||||
}
|
||||
|
||||
trigger_lvm() {
|
||||
if [ "$lvm_group" ] && [ "$lvm_name" ]; then
|
||||
lvm lvchange $lvm_args --quiet --sysinit -a y "${lvm_group}/${lvm_name}" > /dev/null
|
||||
elif [ "$lvm_group" ]; then
|
||||
lvm vgchange $lvm_args --quiet --sysinit -a y "$lvm_group" > /dev/null
|
||||
else
|
||||
lvm vgchange $lvm_args --quiet --sysinit -a y > /dev/null
|
||||
fi
|
||||
}
|
||||
|
||||
mnt_rootfs() {
|
||||
mount ${root_type:+-t $root_type} ${root_opts:+-o $root_opts} $(findfs_sh "$root") /mnt/root || panic "failed to mount rootfs"
|
||||
}
|
||||
|
||||
cleanup() {
|
||||
case "$devmgr" in
|
||||
mdev) { printf "" > /proc/sys/kernel/hotplug || killall uevent; } > /dev/null 2>&1 ;;
|
||||
mdevd) killall mdevd ;;
|
||||
udev) udevadm control --exit ;;
|
||||
esac
|
||||
|
||||
# unmount pseudofs's
|
||||
umount /dev /sys /proc /tmp
|
||||
}
|
||||
|
||||
boot_system() {
|
||||
exec switch_root /mnt/root ${init:-/sbin/init} || panic "failed to boot system"
|
||||
}
|
||||
|
||||
/sbin/busybox --install -s
|
||||
|
||||
. /config || panic "failed to source config"
|
||||
|
||||
mnt_pseudofs
|
||||
parse_cmdline
|
||||
|
||||
# debug mode
|
||||
[ "$debug" = 1 ] && set -x
|
||||
|
||||
case "$devmgr" in
|
||||
mdev) setup_mdev ;;
|
||||
mdevd) setup_mdevd ;;
|
||||
udev) setup_udev ;;
|
||||
*) panic "devmgr option broken" ;;
|
||||
esac
|
||||
|
||||
# TODO handle situations when LUKS on LVM
|
||||
[ "$luks" = 1 ] && [ -x "$(command -v cryptsetup)" ] && unlock_luks
|
||||
[ "$lvm" = 1 ] && [ -x "$(command -v lvm)" ] && trigger_lvm
|
||||
mnt_rootfs
|
||||
[ "$debug" = 1 ] && panic "dropping to shell"
|
||||
cleanup
|
||||
boot_system
|
||||
136
files/mdev.conf
Normal file
136
files/mdev.conf
Normal file
@@ -0,0 +1,136 @@
|
||||
# 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
|
||||
|
||||
# Kernel-based Virtual Machine.
|
||||
#kvm root:kvm 660
|
||||
|
||||
# vhost-net, to be used with kvm.
|
||||
#vhost-net root:kvm 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
|
||||
|
||||
# Run settle-nics every time new NIC appear.
|
||||
# If you don't want to auto-populate /etc/mactab with NICs, run 'settle-nis' without '--write-mactab' param.
|
||||
#-SUBSYSTEM=net;DEVPATH=.*/net/.*;.* 0:0 600 @/lib/mdev/settle-nics --write-mactab
|
||||
|
||||
#net/tun[0-9]* root:kvm 660
|
||||
#net/tap[0-9]* 0:0 600
|
||||
|
||||
# alsa sound devices and audio stuff
|
||||
#SUBSYSTEM=sound;.* root:audio 660 @/lib/mdev/sound-control
|
||||
|
||||
#adsp root:audio 660 >sound/
|
||||
#audio root:audio 660 >sound/
|
||||
#dsp root:audio 660 >sound/
|
||||
#mixer root:audio 660 >sound/
|
||||
#sequencer.* root:audio 660 >sound/
|
||||
|
||||
|
||||
# raid controllers
|
||||
cciss!(.*) 0:9 660 =cciss/%1
|
||||
ida!(.*) 0:9 660 =ida/%1
|
||||
rd!(.*) 0:9 660 =rd/%1
|
||||
|
||||
|
||||
#fuse 0:0 666
|
||||
|
||||
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
|
||||
|
||||
# v4l stuff
|
||||
#vbi[0-9] 0:13 660 >v4l/
|
||||
#video[0-9] 0:13 660 >v4l/
|
||||
|
||||
# dvb stuff
|
||||
#dvb.* 0:13 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
|
||||
|
||||
# Populate /dev/bus/usb.
|
||||
#SUBSYSTEM=usb;DEVTYPE=usb_device;.* 0:0 660 */lib/mdev/dev-bus-usb
|
||||
|
||||
# Catch-all other devices, Right now useful only for debuging.
|
||||
#.* 0:0 660 */opt/mdev/helpers/catch-all
|
||||
133
files/storage-device
Executable file
133
files/storage-device
Executable file
@@ -0,0 +1,133 @@
|
||||
#!/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
|
||||
Reference in New Issue
Block a user