3.8 KiB
3.8 KiB
#!/bin/sh
# Functions library :: for Linux Live Kit scripts
# Author: Tomas M. <http://www.linux-live.org>
#
# =================================================================
# debug and output functions
# =================================================================
# global variable
DEBUG_IS_ENABLED=$(cat /proc/cmdline 2>/dev/null | grep debug)
debug_log()
{
if [ "$DEBUG_IS_ENABLED" ]; then
echo "- debug: $*" >&2
log "- debug: $*"
fi
}
# header
# $1 = text to show
#
header()
{
echo """$@"""
}
# echogreen will echo $@ in green color
# $1 = text
#
echogreen()
{
echo -ne """$@"""
}
# log - store given text in /var/log/livedbg
log()
{
echo "$@" 2>/dev/null >>/var/log/livedbg
}
# show information about the debug shell
show_debug_banner()
{
echo
echo "====="
echo ": Debugging started. Here is the root shell for you."
echo ": Type your desired commands or hit Ctrl+D to continue booting."
echo
}
# debug_shell
# executed when debug boot parameter is present
#
debug_shell()
{
if [ "$DEBUG_IS_ENABLED" ]; then
show_debug_banner
bash < /dev/console
echo
fi
}
fatal()
{
echolog
header "Fatal error occured - $1"
echolog "Something went wrong and we can't continue. This should never happen."
echolog "Please reboot your computer with Ctrl+Alt+Delete ..."
echolog
bash < /dev/console
}
# test if the script is started by root user. If not, exit
#
allow_only_root()
{
if [ "0$UID" -ne 0 ]; then
echo "Only root can run $(basename $0)"; exit 1
fi
}
# Create bundle
# call mksquashfs with apropriate arguments
# $1 = directory which will be compressed to squashfs bundle
# $2 = output file
# $3..$9 = optional arguments like -keep-as-directory or -b 123456789
#
create_bundle()
{
debug_log "create_module" "$*"
rm -f "$2" # overwrite, never append to existing file
mksquashfs "$1" "$2" -comp xz -b 512K $3 $4 $5 $6 $7 $8 $9>/dev/null
}
# mount virtual filesystems like proc etc
#
init_proc()
{
debug_log "vfs_mount_init"
mount -n -t proc proc /proc
echo "0" >/proc/sys/kernel/printk
mount -n -t sysfs sysfs /sys
mount -n -o remount,rw rootfs /
ln -sf /proc/mounts /etc/mtab
}
# make sure some devices are there
init_devs()
{
if [ ! -e /dev/console ]; then mknod dev/console c 5 1; fi
if [ ! -e /dev/null ]; then mknod dev/null c 1 3; fi
if [ ! -e /dev/zram0 ]; then mknod dev/zram0 b 252 0; fi
}
# Activate zram (auto-compression of RAM)
# Compressed RAM consumes 1/2 or even 1/4 of original size
# Setup static size of 500MB
#
init_zram()
{
debug_log "init_zram"
echo "Setting dynamic RAM compression using ZRAM"
modprobe zram
echo 536870912 > /sys/block/zram0/disksize # 512MB
mkswap /dev/zram0
swapon /dev/zram0 -p 32767
echo 100 > /proc/sys/vm/swappiness
}
# load the AUFS kernel module if needed
#
init_aufs()
{
debug_log "init_aufs"
# TODO maybe check here if aufs support is working at all
# and procude useful error message if user has no aufs
modprobe aufs 2>/dev/null
}
# Setup empty union
# $1 = memory directory (tmpfs will be mounted there)
# $2 = union directory where to mount the union
#
init_union()
{
debug_log "init_union"
mkdir -p "$1"
mkdir -p "$2"
mount -t ramfs ramfs "$1"
mount -t aufs -o xino=$1/xino,br=$1 none "$2"
}
# Find LIVEKIT data by mounting all devices
# If found, retain mounted disk
find_data()
{
debug_log "find_data"
local DEVICE FS MNT
MNT=/mnt
blkid -o device | while read DEVICE; do
FS=$(blkid -s TYPE -o value $DEVICE)
# TODO: if fs = ntfs then mount ntfs-3g, if fs=dos then mount vfat
mount -o ro "$DEVICE" $MNT -t "$FS" 2>/dev/null
if [ -d "$MNT/$LIVEKITNAME" ]; then
echo $MNT
return
fi
umount $MNT 2>/dev/null
done
}
mount_device()
{
echo "A"
}
union_append_bundles()
{
echo "B"
}
# Functions library :: for Linux Live Kit scripts
# Author: Tomas M. <http://www.linux-live.org>
#
# =================================================================
# debug and output functions
# =================================================================
# global variable
DEBUG_IS_ENABLED=$(cat /proc/cmdline 2>/dev/null | grep debug)
debug_log()
{
if [ "$DEBUG_IS_ENABLED" ]; then
echo "- debug: $*" >&2
log "- debug: $*"
fi
}
# header
# $1 = text to show
#
header()
{
echo """$@"""
}
# echogreen will echo $@ in green color
# $1 = text
#
echogreen()
{
echo -ne """$@"""
}
# log - store given text in /var/log/livedbg
log()
{
echo "$@" 2>/dev/null >>/var/log/livedbg
}
# show information about the debug shell
show_debug_banner()
{
echo
echo "====="
echo ": Debugging started. Here is the root shell for you."
echo ": Type your desired commands or hit Ctrl+D to continue booting."
echo
}
# debug_shell
# executed when debug boot parameter is present
#
debug_shell()
{
if [ "$DEBUG_IS_ENABLED" ]; then
show_debug_banner
bash < /dev/console
echo
fi
}
fatal()
{
echolog
header "Fatal error occured - $1"
echolog "Something went wrong and we can't continue. This should never happen."
echolog "Please reboot your computer with Ctrl+Alt+Delete ..."
echolog
bash < /dev/console
}
# test if the script is started by root user. If not, exit
#
allow_only_root()
{
if [ "0$UID" -ne 0 ]; then
echo "Only root can run $(basename $0)"; exit 1
fi
}
# Create bundle
# call mksquashfs with apropriate arguments
# $1 = directory which will be compressed to squashfs bundle
# $2 = output file
# $3..$9 = optional arguments like -keep-as-directory or -b 123456789
#
create_bundle()
{
debug_log "create_module" "$*"
rm -f "$2" # overwrite, never append to existing file
mksquashfs "$1" "$2" -comp xz -b 512K $3 $4 $5 $6 $7 $8 $9>/dev/null
}
# mount virtual filesystems like proc etc
#
init_proc()
{
debug_log "vfs_mount_init"
mount -n -t proc proc /proc
echo "0" >/proc/sys/kernel/printk
mount -n -t sysfs sysfs /sys
mount -n -o remount,rw rootfs /
ln -sf /proc/mounts /etc/mtab
}
# make sure some devices are there
init_devs()
{
if [ ! -e /dev/console ]; then mknod dev/console c 5 1; fi
if [ ! -e /dev/null ]; then mknod dev/null c 1 3; fi
if [ ! -e /dev/zram0 ]; then mknod dev/zram0 b 252 0; fi
}
# Activate zram (auto-compression of RAM)
# Compressed RAM consumes 1/2 or even 1/4 of original size
# Setup static size of 500MB
#
init_zram()
{
debug_log "init_zram"
echo "Setting dynamic RAM compression using ZRAM"
modprobe zram
echo 536870912 > /sys/block/zram0/disksize # 512MB
mkswap /dev/zram0
swapon /dev/zram0 -p 32767
echo 100 > /proc/sys/vm/swappiness
}
# load the AUFS kernel module if needed
#
init_aufs()
{
debug_log "init_aufs"
# TODO maybe check here if aufs support is working at all
# and procude useful error message if user has no aufs
modprobe aufs 2>/dev/null
}
# Setup empty union
# $1 = memory directory (tmpfs will be mounted there)
# $2 = union directory where to mount the union
#
init_union()
{
debug_log "init_union"
mkdir -p "$1"
mkdir -p "$2"
mount -t ramfs ramfs "$1"
mount -t aufs -o xino=$1/xino,br=$1 none "$2"
}
# Find LIVEKIT data by mounting all devices
# If found, retain mounted disk
find_data()
{
debug_log "find_data"
local DEVICE FS MNT
MNT=/mnt
blkid -o device | while read DEVICE; do
FS=$(blkid -s TYPE -o value $DEVICE)
# TODO: if fs = ntfs then mount ntfs-3g, if fs=dos then mount vfat
mount -o ro "$DEVICE" $MNT -t "$FS" 2>/dev/null
if [ -d "$MNT/$LIVEKITNAME" ]; then
echo $MNT
return
fi
umount $MNT 2>/dev/null
done
}
mount_device()
{
echo "A"
}
union_append_bundles()
{
echo "B"
}