updated mkinitramfs.sh and added colors

This commit is contained in:
wt
2025-01-10 12:42:52 +07:00
parent 049d5cda86
commit 599c22515f
3 changed files with 132 additions and 27 deletions
+1 -1
View File
@@ -1,3 +1,3 @@
mkinitramfs.pkg
mkinitramfs.pkg.gz
initramfs.img
initramfs*.img
+2 -1
View File
@@ -1,4 +1,5 @@
INITRAMFSCOMP="gzip"
BINARIES=""
MODULES=""
FILES=""
FILES=""
colors=true
+129 -25
View File
@@ -1,26 +1,47 @@
#!/bin/sh
if [ $DEBUG ]; then
set -x
fi
#--- default config ---
export PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin
initdir=$(pwd)/initramfs
INITIN="/usr/share/mkinitramfs/init.in"
CONFIG="/etc/mkinitramfs.conf"
BUSYBOX="/usr/share/mkinitramfs/busybox"
BUSYBOX_LIST="/usr/share/mkinitramfs/busybox_list"
version="0.0.1" # script version
initdir=$(pwd)/initramfs # initramfs working directory
INITIN="/usr/share/mkinitramfs/init.in" # init.int file
CONFIG="/etc/mkinitramfs.conf" # mkinitramfs configuration file
BUSYBOX="/usr/share/mkinitramfs/busybox" # busybox binary
BUSYBOX_LIST="/usr/share/mkinitramfs/busybox_list" # busybox_list file
KERNEL="$(uname -r)" # kernel version
INITRAMFS="initramfs-$KERNEL.img" # initramfs filename (cpio archive filename)
#----------------------
#--- colors ---
RESET="\033[0m"
BRED="\033[1;31m"
BGREEN="\033[1;32m"
BYELLOW="\033[1;33m"
BBLUE="\033[1;34m"
#--------------
#--- create default directories function ---
create_dirs() {
mkdir -pv $initdir/{bin,dev,mnt,proc,root,sys,run}
mkdir -p $initdir/{bin,dev,mnt,proc,root,sys,run}
}
#-------------------------------------------
#--- print script version function ---
print_version() {
echo "mkinitramfs - 0.0.1"
echo "mkinitramfs - $version"
}
#-------------------------------------
#--- print help info function ---
usage() {
print_version
cat << EOF
Usage: $(basename $0) [options] [arguments]
Options:
-o, --output <output> - custom output name (default: $INITRAMFS)
-b, --busybox <busybox path> <busybox_list path> - use custom busybox (default: $BUSYBOX), and custom busybox_list (default: $BUSYBOX_LIST)
-c, --config <config> - use custom config (default: $CONFIG)
-i, --init <init> - custom init file (default: $INITIN)
@@ -29,14 +50,18 @@ Options:
-h, --help - print this help message
EOF
}
#--------------------------------
#--- check have argument function ---
needarg() {
if [ ! "$1" ]; then
echo "ERROR: argument is needed for this option!"
echo ":: ERROR: argument is needed for this option!\n"
exit 1
fi
}
#------------------------------------
#--- add compression function ---
add_compression() {
complevel_gzip="${complevel_gzip:--9}"
complevel_xz="${complevel_xz:--9}"
@@ -52,10 +77,15 @@ case "$INITRAMFSCOMP" in
*) comp="gzip" ;;
esac
}
#--------------------------------
#--- parse options function ---
parse_opt() {
while [ $1 ]; do
case $1 in
-o|--output) needarg $2
OUTPUT=$2
shift 1;;
-b|--busybox) needarg $2
needarg $3
BUSYBOX=$2
@@ -78,42 +108,116 @@ parse_opt() {
shift
done
}
#------------------------------
#-- print message function ---
msg() {
[ $QUIET ] && return
if [ "$colors" = "true" ]; then
printf "$BGREEN::$RESET $*\n"
return
fi
echo ":: $*"
}
#-----------------------------
#--- print error message function ---
msgerr() {
[ $QUIET ] && return
if [ "$colors" = "true" ]; then
printf "$BRED:: ERROR:$RESET $*\n"
return
fi
echo "ERROR: $*"
}
#------------------------------------
#--- print warning message function ---
msgwarn() {
[ $QUIET ] && return
if [ "$colors" = "true" ]; then
printf "$BYELLOW:: WARNING:$RESET $*\n"
return
fi
echo "WARNING: $*"
}
#--------------------------------------
#-- cleanup function ---
cleanup() {
rm -rf $initdir
}
#-----------------------
#--- interrupt function ---
interrupted() {
cleanup
exit 1
}
#--------------------------
#--- main function ---
main() {
parse_opt "$@"
if [ "$(id -u)" != "0" ]; then
parse_opt "$@" # parsing options
if [ "$(id -u)" != "0" ]; then # check root access
msgerr "need root access!"
exit 1
fi
CPIO=$(command -v bsdcpio) || CPIO=$(command -v cpio)
CPIO=$(command -v bsdcpio) || CPIO=$(command -v cpio) # check bsdcpio or cpio binary
if [ ! "$CPIO" ]; then
msgerr "neither 'bsdcpio' or 'cpio' found, exiting..."
exit 1
fi
if [ -f "$CONFIG" ]; then
if [ -f "$CONFIG" ]; then # check config file
. "$CONFIG"
else
msgerr "config file '$CONFIG' not exist."
exit 1
fi
create_dirs
cp -v $INITIN $initdir/init
chmod 755 $initdir/init
cp -v $BUSYBOX $initdir/bin/busybox
cd $initdir/bin
for i in $(cat $BUSYBOX_LIST);do ln -sv busybox $i;done
cd $initdir
add_compression
find . | $CPIO -H newc -o | $comp > ../initramfs.img
cd ..
rm -rv $initdir
}
if [ ! -f "$INITIN" ]; then # check init.in file
msgerr "init file '$INITIN' not exist."
exit 1
fi
if [ "$OUTPUT" ]; then # check output path
if [ $(basename $OUTPUT) != "$OUTPUT" ] && [ ! -d $(dirname $OUTPUT) ]; then
msgerr "directory '$(dirname $OUTPUT)' for output '$(basename $OUTPUT)' not exist."
exit 1
elif [ -d "$OUTPUT" ]; then
msgerr "'$OUTPUT' is a directory."
exit 1
fi
INITRAMFS="$OUTPUT"
fi
if [ ! -f $BUSYBOX ]; then # check busybox binary
msgerr "busybox file '$BUSYBOX' not exist."
exit 1
fi
if [ ! -f $BUSYBOX_LIST ]; then # check busybox_list file
msgerr "busybox list file '$BUSYBOX_LIST' not exist."
exit 1
fi
msg "Generating initramfs..."
create_dirs # creating base directories
install -m0755 $INITIN $initdir/init # install init.in to initramfs
install -m4755 $BUSYBOX $initdir/bin/busybox # install busybox to initramfs
for i in $(cat $BUSYBOX_LIST);do ln -s busybox $initdir/bin/$i;done # install busybox to initramfs
add_compression # add compression to initramfs
msg "generating initramfs..."
rm -f "$INITRAMFS" # clean initramfs file
( cd $initdir; find . | $CPIO -H newc -o --quiet | $comp ) > $INITRAMFS # create initramfs file (cpio archive)
cleanup # cleanup working directory
msg "Generating initramfs done: $INITRAMFS ($(du -h $INITRAMFS | awk '{print $1}'))"
}
#---------------------
#--- run main ---
main "$@"
#----------------