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

201
README.md
View File

@ -1,48 +1,183 @@
# initramfs
Tiny initramfs for suckless systems
Features
--------
- POSIX compliance
========
- No `local`'s, no bashisms, only POSIX shell
- Easy configuration
- LUKS
- LVM
- mdev, mdevd, eudev
Installation
------------
Requirements:
* busybox
- --install [-s] [DIR]
- mdev
- switch_root
- mount
- umount
- blkid
* kmod
* cryptsetup - LUKS
* lvm2 - LVM
* util-linux - PARTUUID
============
Dependencies
------------
* POSIX shell
* `toybox` OR `busybox` OR `sbase/ubase` OR `coreutils/util-linux`
* `mdev` OR `mdevd` OR `eudev`
* `kmod`
- Not required for monolithic kernel (builtin modules)
* `cryptsetup`
- Required for LUKS support
* `lvm2`
- Required for LVM support
Usage
-----
Download & Setup:
```
git clone https://github.com/illiliti/initramfs
cd initramfs
usage: ./tinyramfs [option]
-o, --output <file> set initramfs image name
-c, --config <file> set config file path
-m, --moddir <dir> set modules directory
-k, --kernel <ver> set kernel version
-F, --files <dir> set files directory
-d, --debug enable debug mode
-f, --force overwrite initramfs image
```
TODO
Configuration
-------------
TODO
=============
```sh
# debug mode
#
debug=0
# overwrite initramfs
#
force=0
# initramfs name
#
# default - /tmp/initramfs-$kernel
# example - output="/tmp/myinitramfs.img.gz"
#
output=""
# monolithic kernel
#
monolith=0
# modules directory
#
# default - /lib/modules
# example - moddir="/mnt/root/lib/modules"
#
moddir=""
# kernel version
#
# default - $(uname -r)
# example - kernel="5.4.18_1"
#
kernel=""
# compression program
#
# default - gzip -9
# example - compress="pigz -9"
#
compress=""
# root
#
# supported - PARTUUID, DEVICE, LABEL, PARTUUID
# example -
# root="/dev/sda1"
# root="PARTUUID=35f923c5-083a-4950-a4da-e611d0778121"
#
root=""
# root type
#
# default - autodetected
# example - root_type="btrfs"
#
root_type=""
# root options
# example - see fstab(5)
#
root_opts=""
# device manager
# supported - udev, mdev, mdevd
#
devmgr=""
# hostonly mode
#
hostonly=0
# additional modules
# example - modules="fat crc32c_generic"
#
modules=""
# exclude modules
# example - modules_exclude="wmi fuse"
#
modules_exclude=""
# additional binaries
# example - binaries="ls cat /path/to/mycustomprog"
#
binaries=""
# LVM support
#
lvm=0
# LVM options
#
# supported - tag, name, group, config, discard
# description -
# tag - trigger lvm by tag
# name - trigger lvm by logical volume name
# group - trigger lvm by volume group name
# config - embed host lvm config
# discard - enable issue_discards
# example -
# lvm_opts="tag=lvm-server"
# lvm_opts="name=lv1,group=vg1"
# lvm_opts="config=1,discard"
# lvm_opts="discard=1"
#
lvm_opts=""
# LUKS support
#
luks=0
# LUKS encrypted root
#
# supported - PARTUUID, DEVICE, LABEL, PARTUUID
# example -
# luks_root="/dev/sda1"
# luks_root="PARTUUID=35f923c5-083a-4950-a4da-e611d0778121"
#
luks_root=""
# LUKS options
#
# supported - key, name, header, discard
# description -
# key - embed key
# name - device mapper name
# header - embed header
# discard - enable allow-discards
# example -
# luks_opts="key=/path/to/keyfile,name=myluksroot,header=/path/to/header,discard"
# luks_opts="discard=1"
#
luks_opts=""
```
TODO document kernel command-line parameters
License
-------
=======
Licensed under GPLv3
Exceptions:
The "mdev.conf" configuration file is modified version from "mdev-like-a-boss" project, Copyright (c) 2012-2020, Piotr Karbowski <piotr.karbowski@gmail.com>.
Please consult the license notice in the file for terms and conditions.
The "storage-device" script is modified version from "mdev-like-a-boss" project, Copyright (c) 2012-2020, Piotr Karbowski <piotr.karbowski@gmail.com>.
Please consult the license notice in the file for terms and conditions.

149
config
View File

@ -3,91 +3,134 @@
#
# debug mode
#debug=0
#
debug=0
# overwrite initramfs
#force=0
# init
#init=""
#
force=0
# initramfs name
#initramfs=""
#
# default - /tmp/initramfs-$kernel
# example - output="/tmp/myinitramfs.img.gz"
#
output=""
# monolithic kernel
#monolith=""
#
monolith=0
# drivers directory
#moddir=""
# modules directory
#
# default - /lib/modules
# example - moddir="/mnt/root/lib/modules"
#
moddir=""
# kernel version
#kernel=""
#
# default - $(uname -r)
# example - kernel="5.4.18_1"
#
kernel=""
# compression program
#compress=""
#
# default - gzip -9
# example - compress="pigz -9"
#
compress=""
# root
#root=""
#
# supported - PARTUUID, DEVICE, LABEL, PARTUUID
# example -
# root="/dev/sda1"
# root="PARTUUID=35f923c5-083a-4950-a4da-e611d0778121"
#
root=""
# root type
#root_type=""
#
# default - autodetected
# example - root_type="btrfs"
#
root_type=""
# root options
#root_opts=""
# util-linux binaries
#util_linux=0
# example - see fstab(5)
#
root_opts=""
# device manager
#devmgr=""
# supported - udev, mdev, mdevd
#
devmgr=""
# hostonly mode
#hostonly=0
#
hostonly=0
# additional drivers
#drivers=""
# additional modules
# example - modules="fat crc32c_generic"
#
modules=""
# exclude drivers
#drivers_exclude=""
# exclude modules
# example - modules_exclude="wmi fuse"
#
modules_exclude=""
# additional binaries
#binaries=""
# example - binaries="ls cat /path/to/mycustomprog"
#
binaries=""
# LVM support
#lvm=0
# LVM logical volume name
#lvm_name=""
# LVM volume group name
#lvm_group=""
# LVM config
#lvm_config=0
# LVM issue_discards
#lvm_discard=0
#
lvm=0
# LVM options
#lvm_args=""
#
# supported - tag, name, group, config, discard
# description -
# tag - trigger lvm by tag
# name - trigger lvm by logical volume name
# group - trigger lvm by volume group name
# config - embed host lvm config
# discard - enable issue_discards
# example -
# lvm_opts="tag=lvm-server"
# lvm_opts="name=lv1,group=vg1"
# lvm_opts="config=1,discard"
# lvm_opts="discard=1"
#
lvm_opts=""
# LUKS support
#luks=0
#
luks=0
# LUKS encrypted root
#luks_root=""
# LUKS mapper name
#luks_name=""
# LUKS detached header
#luks_header="/path/to/header"
# LUKS keyfile
#luks_keyfile="/path/to/keyfile"
# LUKS allow_discards
#luks_discard=0
#
# supported - PARTUUID, DEVICE, LABEL, PARTUUID
# example -
# luks_root="/dev/sda1"
# luks_root="PARTUUID=35f923c5-083a-4950-a4da-e611d0778121"
#
luks_root=""
# LUKS options
#luks_args=""
#
# supported - key, name, header, discard
# description -
# key - embed key
# name - device mapper name
# header - embed header
# discard - enable allow-discards
# example -
# luks_opts="key=/path/to/keyfile,name=myluksroot,header=/path/to/header,discard"
# luks_opts="discard=1"
#
luks_opts=""

592
tinyramfs
View File

@ -1,32 +1,27 @@
#!/bin/sh
#!/bin/sh -ef
#
# tiny initramfs generation tool
# tiny initramfs
msg() {
msg()
{
case "$1" in
info)
printf "info >> %s\n" "$2" >&2
;;
warn)
printf "warning >> %s\n" "$2" \
"are you sure you want to continue?" \
"press enter to continue or ctrl+c to exit" >&2
read -r _
;;
printf "info >> %s\n" "$2"
;;
panic)
printf "panic >> %s\n" "$2" >&2
exit 1
;;
;;
esac
}
usage() {
usage()
{
cat << EOF
usage: $0 [option...]
usage: $0 [option]
-o, --output <file> set initramfs image name
-c, --config <file> set config file path
-m, --moddir <dir> set drivers directory
-m, --moddir <dir> set modules directory
-k, --kernel <ver> set kernel version
-F, --files <dir> set files directory
-d, --debug enable debug mode
@ -35,181 +30,227 @@ usage: $0 [option...]
EOF
}
parse_args() {
parse_args()
{
while [ "$1" ]; do
case "$1" in
-o | --output)
_initramfs="${2:?}"
_output="${2:?}"
shift 2
;;
;;
-c | --config)
_config="${2:?}"
shift 2
;;
;;
-m | --moddir)
_moddir="${2:?}"
shift 2
;;
;;
-k | --kernel)
_kernel="${2:?}"
shift 2
;;
;;
-F | --files)
_filesdir="${2:?}"
shift 2
;;
;;
-d | --debug)
_debug=1
shift 1
;;
;;
-f | --force)
_force=1
shift 1
;;
;;
-h | --help)
usage
exit 0
;;
;;
*)
printf "%s\n\n" "invalid option: '$1'"
printf "invalid option: %s\n\n" "$1"
usage
exit 1
;;
;;
esac
done
}
prepare_environment() {
prepare_environment()
{
msg info "preparing environment"
for _file in $_config /etc/tinyramfs/config ./config; do
[ -f "$_file" ] && . "$_file" && break
[ -f "$_file" ] &&
{
. "$_file"
break
}
done || msg panic "failed to source config"
for _dir in $_filesdir /usr/share/tinyramfs ./usr/share/tinyramfs; do
[ -d "$_dir" ] && filesdir="$_dir" && break
[ -d "$_dir" ] &&
{
filesdir="$_dir"
break
}
done || msg panic "failed to locate required files"
kernel="${_kernel:-${kernel:-$(uname -r)}}"
moddir="${_moddir:-${moddir:-/lib/modules}}"
debug="${_debug:-${debug:-0}}"
force="${_force:-${force:-0}}"
initramfs="${_initramfs:-${initramfs:-/tmp/initramfs-${kernel}}}"
modker="${moddir}/${kernel}"
}
moddir="${_moddir:-${moddir:-/lib/modules}}"
kernel="${_kernel:-${kernel:-$(uname -r)}}"
output="${_output:-${output:-/tmp/initramfs-${kernel}}}"
create_workdir() {
msg info "creating working directory"
workdir="${XDG_CACHE_HOME:-${TMPDIR:-/tmp}}/initramfs.$$"
mkdir -p "$workdir" ||
mkdir -p "${workdir=${XDG_CACHE_HOME:-${TMPDIR:-/tmp}}/initramfs.$$}" ||
msg panic "failed to create working directory"
workdirbin="${workdir}/usr/bin/"
workdirlib="${workdir}/usr/lib/"
modker="${moddir}/${kernel}"
OLD_IFS="$IFS"
}
remove_workdir() {
remove_workdir()
{
msg info "removing working directory"
rm -rf "$workdir"
}
install_requirements() {
install_requirements()
{
msg info "installing requirements"
# install user specified binaries
[ "$binaries" ] &&
for _binary in $binaries; do
install_binary "$_binary"
done
# install util-linux binaries
[ "$util_linux" = 1 ] &&
for _binary in mount blkid; do
install_binary "$_binary"
done
# install required binaries
for _binary in busybox modprobe; do
# install user specified binaries if any
for _binary in $binaries; do
install_binary "$_binary"
done
# install required binaries
for _binary in \[ sh sleep mount printf setsid switch_root; do
install_binary "$_binary"
done
# copy init
install -m755 "${filesdir}/init" "${workdir}/init"
# copy config
printf "%s\n" \
monolith="$monolith" \
root="$root" \
root_type="$root_type" \
root_opts="$root_opts" \
devmgr="$devmgr" \
lvm="$lvm" \
lvm_opts="$lvm_opts" \
luks="$luks" \
luks_root="$luks_root" \
luks_opts="$luks_opts" \
> "${workdir}/etc/config"
}
create_structure() {
create_structure()
{
msg info "creating directory structure"
for _dir in dev tmp var run etc usr/lib usr/bin mnt/root proc root sys; do
for _dir in etc tmp dev sys proc root usr/lib usr/bin mnt/root; do
mkdir -p "${workdir}/${_dir}"
done
}
create_symlinks() {
create_symlinks()
{
msg info "creating symlinks"
ln -s usr/lib "${workdir}/lib"
ln -s usr/lib "${workdir}/lib64"
ln -s usr/bin "${workdir}/bin"
ln -s usr/bin "${workdir}/sbin"
ln -s ../run "${workdir}/var/run"
ln -s bin "${workdir}/usr/sbin"
ln -s lib "${workdir}/usr/lib64"
}
install_devmgr() {
install_devmgr()
{
msg info "installing device manager"
# TODO investigate booting without device manager
case "$devmgr" in
udev)
for _binary in udevd udevadm dmsetup; do
for _binary in udevd udevadm; do
install_binary "$_binary"
done
find /usr/lib/udev \
! -path "*rc_keymaps*" \
! -path "*hwdb.d*" \
-type f |
# exclusively handle requirement
[ "$luks" = 1 ] || [ "$lvm" = 1 ] &&
install_binary dmsetup
cpio -pd "$workdir" > /dev/null 2>&1
;;
mdev)
install -m644 "${filesdir}/mdev.conf" "${workdir}/etc/mdev.conf"
install -Dm755 "${filesdir}/storage-device" "${workdir}/lib/mdev/storage-device"
;;
mdevd)
for _binary in mdevd mdevd-coldplug; do
for _binary in /usr/lib/udev/ata_id /usr/lib/udev/scsi_id; do
install -Dm755 "$_binary" "${workdir}${_binary}"
done
set +f; for _file in /usr/lib/udev/rules.d/*; do
install -Dm644 "$_file" "${workdir}${_file}"
done; set -f
;;
mdev | mdevd)
[ "$devmgr" = mdev ] && install_binary mdev
[ "$devmgr" = mdevd ] &&
for _binary in mdevd mdevd-coldplug; do
install_binary "$_binary"
done
for _binary in ln kill mkdir blkid "${filesdir}/device-helper"; do
install_binary "$_binary"
done
install -m644 "${filesdir}/mdev.conf" "${workdir}/etc/mdev.conf"
install -Dm755 "${filesdir}/storage-device" "${workdir}/lib/mdev/storage-device"
;;
printf "%s\n" \
'SUBSYSTEM=block;.* 0:0 660 @device-helper' \
> "${workdir}/etc/mdev.conf"
[ "$monolith" != 1 ] && printf "%s\n" \
'$MODALIAS=.* 0:0 660 @modprobe "$MODALIAS"' \
>> "${workdir}/etc/mdev.conf"
;;
esac
}
install_lvm() {
install_lvm()
{
msg info "installing LVM"
install_binary lvm
for _binary in lvchange vgchange; do
install_binary "$_binary"
done
# install lvm config
if [ "$lvm_config" = 1 ]; then
mkdir -p "${workdir}/etc/lvm"
{ IFS=,; set -- $lvm_opts; IFS="$OLD_IFS"; }
cp /etc/lvm/*.conf "${workdir}/etc/lvm"
else
mkdir -p "${workdir}/etc/lvm"
for opt; do
case "$opt" in
config | config=1)
install -Dm644 /etc/lvm/lvm.conf "${workdir}/etc/lvm/lvm.conf"
return
;;
esac
done
cat << EOF > "${workdir}/etc/lvm/lvm.conf"
devices {
# block discard support
issue_discards = ${lvm_discard:-0}
mkdir -p "${workdir}/etc/lvm"
printf "%s\n" \
'devices {' \
'write_cache_state = 0' \
'}' \
'backup {' \
'backup = 0' \
'archive = 0' \
'}' \
'global {' \
'use_lvmetad = 0' \
'}' \
> "${workdir}/etc/lvm/lvm.conf"
}
global {
# disable lvmetad
use_lvmetad = 0
}
EOF
fi
}
install_luks() {
install_luks()
{
msg info "installing LUKS"
install_binary cryptsetup
@ -219,61 +260,91 @@ install_luks() {
[ -e /usr/lib/libgcc_s.so.1 ] &&
install_library /usr/lib/libgcc_s.so.1
# copy luks header
[ -f "$luks_header" ] &&
install -m400 "$luks_header" "${workdir}/root/luks_header"
{ IFS=,; set -- $luks_opts; IFS="$OLD_IFS"; }
# copy luks keyfile
[ -f "$luks_keyfile" ] &&
install -m400 "$luks_keyfile" "${workdir}/root/luks_keyfile"
for opt; do
case "${opt%%=*}" in
header)
install -m400 "${opt##*=}" "${workdir}/root/header"
luks_opts=$(printf "%s" "$luks_opts" | sed "s|${opt##*=}|/root/header|")
;;
key)
install -m400 "${opt##*=}" "${workdir}/root/key"
luks_opts=$(printf "%s" "$luks_opts" | sed "s|${opt##*=}|/root/key|")
;;
esac
done
}
install_driver() {
driver="$1"
install_module()
{
module="$1"
modprobe -S "$kernel" -D "$driver" 2> /dev/null |
modprobe -S "$kernel" -D "$module" 2> /dev/null |
while read -r driver; do
while read -r module || [ "$module" ]; do
# strip unneeded stuff
driver="${driver##*builtin*}"
driver="${driver##*net*}"
driver="${driver#insmod }"
module="${module##*builtin*}"
module="${module##*net*}"
module="${module#insmod }"
# exclude user specified drivers
[ "$drivers_exclude" ] &&
for _exclude_driver in $drivers_exclude; do
driver="${driver##*${_exclude_driver}*}"
done
# exclude user specified modules if any
for _exclude_module in $modules_exclude; do
module="${module##*${_exclude_module}*}"
done
# check empty
[ "$driver" ] || continue
# check if driver already installed
[ -e "${workdir}${driver}" ] ||
install -Dm644 "$driver" "${workdir}${driver}"
done
# check if module already installed
[ -e "$module" ] && [ ! -e "${workdir}${module}" ] &&
install -Dm644 "$module" "${workdir}${module}"
done ||:
}
install_hostonly_drivers() {
msg info "installing hostonly drivers"
install_hostonly_modules()
{
msg info "installing hostonly modules"
# perform autodetection of drivers via /sys
# perform autodetection of modules via /sys
find /sys -name modalias -exec sort -u {} + |
while read -r _driver; do
install_driver "$_driver"
done
while read -r _module || [ "$_module" ]; do
install_module "$_module"
done ||:
# install user specified drivers
[ "$drivers" ] &&
for _driver in $drivers; do
install_driver "$_driver"
# install LVM modules
[ "$lvm" = 1 ] &&
for _module in dm-thin-pool dm-multipath dm-snapshot dm-cache dm-log dm-mirror; do
install_module "$_module"
done
# install LUKS modules
[ "$luks" = 1 ] &&
for _module in aes dm-crypt sha256 sha512 wp512 ecb lrw xts twofish serpent; do
install_module "$_module"
done
# install root partition module
if [ "$root_type" ]; then
install_module "$root_type"
else
while read -r _ _dir _type _ _ _; do
[ "$_dir" = / ] &&
{
install_module "$_type"
break
}
done < /proc/mounts
fi
# install user specified modules if any
for _module in $modules; do
install_module "$_module"
done
}
install_all_drivers() {
msg info "installing all drivers"
install_all_modules()
{
msg info "installing all modules"
find \
"${modker}/kernel/arch" \
@ -289,202 +360,153 @@ install_all_drivers() {
"${modker}/kernel/drivers/virtio" \
-type f 2> /dev/null |
while read -r _driver; do
while read -r _module || [ "$_module" ]; do
# strip path and extension
_driver="${_driver##*/}"
_driver="${_driver%%.*}"
_module="${_module##*/}"
_module="${_module%%.*}"
install_driver "$_driver"
done
install_module "$_module"
done ||:
}
generate_depmod() {
msg info "generating drivers list"
cp "${modker}/modules.builtin" \
"${modker}/modules.order" \
"${workdir}${modker}"
depmod -b "$workdir" "$kernel"
}
install_binary() {
install_binary()
{
binary=$(command -v "$1")
realbin="$(dirname "$binary")/$(readlink "$binary")"
fullbin=$(readlink -f "$binary")
workdirbin="${workdir}/usr/bin/"
# check if binary exists
[ "$binary" ] || msg panic "$binary doesn't exists"
# check if binary exist and builtin
case "$binary" in
*/*)
: no operation
;;
"")
msg panic "$1 doesn't exist"
;;
*)
{ IFS=:; set -- $PATH; IFS="$OLD_IFS"; }
for _dir; do
[ -x "${_dir}/${binary}" ] &&
{
binary="${_dir}/${binary}"
break
}
done || msg panic "couldn't find external $1 binary"
;;
esac
# check if binary already installed
[ -e "${workdirbin}${fullbin##*/}" ] &&
return
[ -e "${workdirbin}${binary##*/}" ] && return
# install symlinks if any
[ -h "$binary" ] && {
# iterate throught symlinks and copy them
while [ -h "$binary" ]; do
cp -P "$binary" "$workdirbin"
binary="${binary%/*}/$(readlink "$binary")"
done
# symlink may link to symlink
[ -h "$realbin" ] &&
cp -a "$realbin" "$workdirbin"
install -m755 "$binary" "${workdirbin}${binary##*/}"
strip "${workdirbin}${binary##*/}" > /dev/null 2>&1 ||:
cp -a "$binary" "$workdirbin"
}
# install and strip binary
install -s -m755 "$fullbin" "${workdirbin}${fullbin##*/}"
# check static
ldd "$binary" > /dev/null 2>&1 || return
# check if binary statically linked
ldd "$binary" > /dev/null 2>&1 || return 0
# exract paths to libraries
ldd "$binary" |
while read -r _library; do
while read -r _library || [ "$_library" ]; do
# strip unneeded stuff
_library="${_library##*vdso*}"
_library="${_library#* => }"
_library="${_library% *}"
# check empty
[ "$_library" ] || continue
install_library "$_library"
done
[ -e "$_library" ] && install_library "$_library"
done ||:
}
install_library() {
install_library()
{
library="$1"
reallib="$(dirname "$library")/$(readlink "$library")"
fulllib=$(readlink -f "$library")
workdirlib="${workdir}/usr/lib/"
# check if library already installed
[ -e "${workdirlib}${fulllib##*/}" ] &&
return
[ -e "${workdirlib}${library##*/}" ] && return
# install symlinks if any
[ -h "$library" ] && {
# iterate throught symlinks and copy them
while [ -h "$library" ]; do
cp -P "$library" "$workdirlib"
library="${library%/*}/$(readlink "$library")"
done
# symlink may link to symlink
[ -h "$reallib" ] &&
cp -a "$reallib" "$workdirlib"
cp -a "$library" "$workdirlib"
}
# install library
install -s -m755 "$fulllib" "${workdirlib}${fulllib##*/}"
install -m755 "$library" "${workdirlib}${library##*/}"
strip "${workdirlib}${library##*/}" > /dev/null 2>&1 ||:
}
install_files() {
msg info "installing files"
cat > "${workdir}/config" << EOF
debug="$debug"
init="$init"
root="$root"
root_type="$root_type"
root_opts="$root_opts"
devmgr="$devmgr"
#drivers
lvm="$lvm"
lvm_name="$lvm_name"
lvm_group="$lvm_group"
#lvm_discard
lvm_args="$lvm_args"
luks="$luks"
luks_root="$luks_root"
luks_name="$luks_name"
#luks_header
#luks_keyfile
luks_discard="$luks_discard"
luks_args="$luks_args"
EOF
install -m644 "${filesdir}/passwd" "${workdir}/etc/passwd"
install -m644 "${filesdir}/group" "${workdir}/etc/group"
install -m755 "${filesdir}/init" "${workdir}/init"
}
create_initramfs() {
create_initramfs()
{
msg info "creating initramfs image"
# TODO add uncompressed option
# check if image already exist
[ "$force" = 0 ] && [ -e "$initramfs" ] &&
msg warn "looks like you already have initramfs image"
[ "$force" != 1 ] && [ -e "$output" ] &&
msg panic "initramfs image already exist"
(
cd "$workdir"
find . | cpio -oH newc | ${compress:-gzip -9}
) > "$initramfs" 2> /dev/null ||
) > "$output" 2> /dev/null ||
msg panic "failed to generate initramfs image"
}
# check root
[ "$(id -u)" = 0 ] || msg panic "must be run as root"
# int main()
{
# check root
[ "$(id -u)" = 0 ] || msg panic "must be run as root"
parse_args "$@"
prepare_environment
parse_args "$@"
prepare_environment
# remove workdir on signals
# we are doing unset EXIT signal to avoid endless loop
# because afterwards we execute exit command.
# also some shells (dash,mksh,etc) doesn't exit on INT
# signal. as workaround we manually execute exit command.
# tested bash,dash,mksh,busybox sh
# TODO fix zsh, ref https://www.zsh.org/mla/users/2015/msg00436.html
trap "remove_workdir && trap - EXIT && exit" EXIT INT TERM HUP
[ "$debug" = 1 ] && set -x
[ "$debug" = 1 ] && {
# hacky, but compatible with all posix shells
trap '
ret="$?"
trap - EXIT INT
[ "$debug" = 1 ] || remove_workdir
[ "$ret" = 0 ] || msg panic "something went wrong"
' EXIT INT
# debug shell commands
set -x
create_structure
create_symlinks
# don't remove anything
trap - EXIT INT TERM HUP
[ "$lvm" = 1 ] && install_lvm
[ "$luks" = 1 ] && install_luks
# check monolithic kernel
[ "$monolith" != 1 ] && [ -d "$moddir" ] &&
{
# check hostonly mode
if [ "$hostonly" = 1 ]; then
install_hostonly_modules
else
install_all_modules
fi
for _binary in find sort modprobe; do
install_binary "$_binary"
done
cp "${modker}/modules.builtin" \
"${modker}/modules.order" \
"${workdir}${modker}"
depmod -b "$workdir" "$kernel"
}
install_devmgr
install_requirements
create_initramfs
msg info "done! check out - $output"
}
create_workdir
create_structure
create_symlinks
[ "$lvm" = 1 ] && install_lvm
[ "$luks" = 1 ] && install_luks
# check monotihic kernel
[ "$monolith" != 1 ] && [ -d "$moddir" ] && {
# check hostonly mode
if [ "$hostonly" = 1 ]; then
install_hostonly_drivers
# install lvm drivers
[ "$lvm" = 1 ] &&
for _driver in dm-thin-pool dm-multipath dm-snapshot dm-cache dm-log dm-mirror; do
install_driver "$_driver"
done
# install luks drivers
[ "$luks" = 1 ] &&
for _driver in aes dm-crypt sha256 sha512 wp512 ecb lrw xts twofish serpent; do
install_driver "$_driver"
done
else
install_all_drivers
fi
generate_depmod
}
install_devmgr
install_files
install_requirements
create_initramfs
msg info "done! check out $initramfs"

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