updated mkinitramfs.sh and added colors

This commit is contained in:
wt committed 2025-01-10 12:42:52 +07:00
1 parent 049d5cda86
commit 599c22515f
3 files changed
+131 -26

No files matched your search

+1 -1
View File
@@ -1,3 +1,3 @@
mkinitramfs.pkg mkinitramfs.pkg
mkinitramfs.pkg.gz mkinitramfs.pkg.gz
initramfs.img initramfs*.img
+1
View File
@@ -2,3 +2,4 @@ INITRAMFSCOMP="gzip"
BINARIES="" BINARIES=""
MODULES="" MODULES=""
FILES="" FILES=""
colors=true
+129 -25
View File
@@ -1,26 +1,47 @@
#!/bin/sh #!/bin/sh
if [ $DEBUG ]; then
set -x
fi
#--- default config ---
export PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin export PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin
initdir=$(pwd)/initramfs version="0.0.1" # script version
INITIN="/usr/share/mkinitramfs/init.in" initdir=$(pwd)/initramfs # initramfs working directory
CONFIG="/etc/mkinitramfs.conf" INITIN="/usr/share/mkinitramfs/init.in" # init.int file
BUSYBOX="/usr/share/mkinitramfs/busybox" CONFIG="/etc/mkinitramfs.conf" # mkinitramfs configuration file
BUSYBOX_LIST="/usr/share/mkinitramfs/busybox_list" 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() { 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() { print_version() {
echo "mkinitramfs - 0.0.1" echo "mkinitramfs - $version"
} }
#-------------------------------------
#--- print help info function ---
usage() { usage() {
print_version print_version
cat << EOF cat << EOF
Usage: $(basename $0) [options] [arguments] Usage: $(basename $0) [options] [arguments]
Options: 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) -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) -c, --config <config> - use custom config (default: $CONFIG)
-i, --init <init> - custom init file (default: $INITIN) -i, --init <init> - custom init file (default: $INITIN)
@@ -29,14 +50,18 @@ Options:
-h, --help - print this help message -h, --help - print this help message
EOF EOF
} }
#--------------------------------
#--- check have argument function ---
needarg() { needarg() {
if [ ! "$1" ]; then if [ ! "$1" ]; then
echo "ERROR: argument is needed for this option!" echo ":: ERROR: argument is needed for this option!\n"
exit 1 exit 1
fi fi
} }
#------------------------------------
#--- add compression function ---
add_compression() { add_compression() {
complevel_gzip="${complevel_gzip:--9}" complevel_gzip="${complevel_gzip:--9}"
complevel_xz="${complevel_xz:--9}" complevel_xz="${complevel_xz:--9}"
@@ -52,10 +77,15 @@ case "$INITRAMFSCOMP" in
*) comp="gzip" ;; *) comp="gzip" ;;
esac esac
} }
#--------------------------------
#--- parse options function ---
parse_opt() { parse_opt() {
while [ $1 ]; do while [ $1 ]; do
case $1 in case $1 in
-o|--output) needarg $2
OUTPUT=$2
shift 1;;
-b|--busybox) needarg $2 -b|--busybox) needarg $2
needarg $3 needarg $3
BUSYBOX=$2 BUSYBOX=$2
@@ -78,42 +108,116 @@ parse_opt() {
shift shift
done done
} }
#------------------------------
#-- print message function ---
msg() {
[ $QUIET ] && return
if [ "$colors" = "true" ]; then
printf "$BGREEN::$RESET $*\n"
return
fi
echo ":: $*"
}
#-----------------------------
#--- print error message function ---
msgerr() { msgerr() {
[ $QUIET ] && return [ $QUIET ] && return
if [ "$colors" = "true" ]; then
printf "$BRED:: ERROR:$RESET $*\n"
return
fi
echo "ERROR: $*" 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() { main() {
parse_opt "$@" parse_opt "$@" # parsing options
if [ "$(id -u)" != "0" ]; then
if [ "$(id -u)" != "0" ]; then # check root access
msgerr "need root access!" msgerr "need root access!"
exit 1 exit 1
fi 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 if [ ! "$CPIO" ]; then
msgerr "neither 'bsdcpio' or 'cpio' found, exiting..." msgerr "neither 'bsdcpio' or 'cpio' found, exiting..."
exit 1 exit 1
fi fi
if [ -f "$CONFIG" ]; then if [ -f "$CONFIG" ]; then # check config file
. "$CONFIG" . "$CONFIG"
else else
msgerr "config file '$CONFIG' not exist." msgerr "config file '$CONFIG' not exist."
exit 1 exit 1
fi 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 "$@" main "$@"
#----------------