From e283886c03271f4ba1a2680db98c0d66932bc96c Mon Sep 17 00:00:00 2001 From: wangchuanguo Date: Sat, 2 Sep 2023 14:54:53 +0800 Subject: [PATCH] add tool of creating initrd image that run as ramfs Signed-off-by: wangchuanguo --- create-initrd-ramos-from-cur-os.conf | 26 +++++ create-initrd-ramos-from-cur-os.sh | 154 +++++++++++++++++++++++++++ 2 files changed, 180 insertions(+) create mode 100755 create-initrd-ramos-from-cur-os.conf create mode 100755 create-initrd-ramos-from-cur-os.sh diff --git a/create-initrd-ramos-from-cur-os.conf b/create-initrd-ramos-from-cur-os.conf new file mode 100755 index 0000000..996c4d7 --- /dev/null +++ b/create-initrd-ramos-from-cur-os.conf @@ -0,0 +1,26 @@ +#! /bin/bash +RESULT_DIR="initrd_result" + +#if adapting to other cores, change to your version +#example: "4.18.0-240.el8.x86_64" +KERNEL_VERSION=`uname -r` +#all kernel drivers that should be included or should not be included, split with a space +#example: "module1 module2" +ADD_DRIVERS="" +OMIT_DRIVERS="" + +#all excutable programs that should be installed, split with a space, +#example: "strace fsck.ext3 ssh" +INSTALL_PROG="" + +# INCLUDE_FILE_DIRECTORY1="src_path dst_path" +# 1 file or directory should be copy from 'src_path' at current os filesystem to 'dst_path' at initrd's file system +# for multi-files, multiple INCLUDE_FILE_DIRECTORY* are needed +# example: "./my.conf /etc/my2.conf" or "./mydir /etc/mydir2" +INCLUDE_FILE_DIRECTORY1="" +#INCLUDE_FILE_DIRECTORY2="" + +#set a cmd to execute when the system starts +#Example: echo 123 >> /var/log/syslog3 +PRODUCT_CMD="" + diff --git a/create-initrd-ramos-from-cur-os.sh b/create-initrd-ramos-from-cur-os.sh new file mode 100755 index 0000000..c1781c7 --- /dev/null +++ b/create-initrd-ramos-from-cur-os.sh @@ -0,0 +1,154 @@ +#! /bin/bash +#########################begin##################################### +source ./create-initrd-ramos-from-cur-os.conf +#########################begin##################################### +if test -z "$(rpm -qa dracut-live)"; then + echo "Info: install dracut-live" + yum install dracut-live -y + if test -z "$(rpm -qa dracut-live)"; then + echo "Error: dracut-live has not been installed, and yum installation failed!" + exit 1 + fi +fi +if test -z "$(rpm -qa anaconda-dracut)"; then + echo "Info: install anaconda-dracut" + yum install anaconda-dracut -y + if test -z "$(rpm -qa anaconda-dracut)"; then + echo "Error: anaconda-dracut has not been installed, and yum installation failed!" + exit 1 + fi +fi +if test -z "$(rpm -qa nfs-utils)"; then + echo "Info: install nfs-utils" + yum install nfs-utils -y + if test -z "$(rpm -qa nfs-utils)"; then + echo "Error: nfs-utils has not been installed, and yum installation failed!" + exit 1 + fi +fi + +if [ ! -f "/.buildstamp" ]; then + cp ./buildstamp /.buildstamp +fi + +valid_char_for_param='^[a-zA-Z0-9 _!@#\/\-]\+$' +if [ ! -z "$ADD_DRIVERS" ]; then + if echo "$ADD_DRIVERS" | grep "$valid_char_for_param"; then + ADD_DRIVERS_TMP="" + for d in ${ADD_DRIVERS[@]} + do + modprobe $d + if [ $? != 0 ]; then + echo "warning: driver[$d] in ADD_DRIVERS is not found!" + else + ADD_DRIVERS_TMP+="$d " + fi + done + ADD_DRIVERS=$ADD_DRIVERS_TMP + echo "Info: ADD_DRIVERS[$ADD_DRIVERS] checked" + else + echo "Error: ADD_DRIVERS contains incorrect data" + exit 2 + fi +fi +if [ ! -z "$OMIT_DRIVERS" ]; then + if echo "$OMIT_DRIVERS" | grep "$valid_char_for_param"; then + echo "Info: OMIT_DRIVERS checked" + else + echo "Error: OMIT_DRIVERS contains incorrect data" + exit 2 + fi +fi +if [ ! -z "$INSTALL_PROG" ]; then + if echo "$INSTALL_PROG" | grep "$valid_char_for_param"; then + echo "Info: INSTALL_PROG checked" + else + echo "Error: INSTALL_PROG contains incorrect data" + exit 2 + fi +fi + +cmd_options="" +if [ ! -z "$ADD_DRIVERS" ]; then + cmd_options=" --add-drivers '"$ADD_DRIVERS"' " +fi +if [ ! -z "$OMIT_DRIVERS" ]; then + cmd_options=$cmd_options" --omit-drivers '"$OMIT_DRIVERS"' " +fi +if [ ! -z "$INSTALL_PROG" ]; then + cmd_options=$cmd_options" --install '"$INSTALL_PROG"' " +fi +#there would not be more than 100 repos +for ((i=1; i<=100; i++)) +do + tmp="INCLUDE_FILE_DIRECTORY$i" + if [ -z "${!tmp}" ]; then + continue + fi + if echo "${!tmp}" | grep "$valid_char_for_param"; then + num_empty=`echo "${!tmp}" |tr -cd ' ' |wc -c` + if [ "X$num_empty" == "X1" ]; then + echo "Info: $tmp checked" + else + echo "Error: $tmp must contains both source and target" + exit 2 + fi + else + echo "Error: $tmp contains incorrect data" + exit 2 + fi + cmd_options=$cmd_options" --include "${!tmp}" " +done + +mkdir -p $RESULT_DIR +CMD="dracut --nomdadmconf --nolvmconf --xz --install /.buildstamp --add \"fips anaconda pollcdrom qemu qemu-net prefixdevname-tools\" $cmd_options --no-hostonly --no-early-microcode --force $RESULT_DIR/initrd-$KERNEL_VERSION.img $KERNEL_VERSION " +echo "run cmd:$CMD " +eval $CMD + +let ret_dracut=$? +if [ $ret_dracut -ne 0 ]; then + echo "dracut fail!" + exit $ret_dracut +fi + +echo "unpack the initrd.img => initrd-dir ..." +rm -rf initrd-dir +mkdir initrd-dir +xz -dc $RESULT_DIR/initrd-$KERNEL_VERSION.img | cpio -idm -D initrd-dir +cp -r /etc/systemd/system/getty.target.wants initrd-dir/etc/systemd/system/ +cp /usr/lib/systemd/system/serial-getty\@.service initrd-dir/usr/lib/systemd/system/ +cp /usr/lib/systemd/system/getty* initrd-dir/usr/lib/systemd/system/ +cd initrd-dir/usr/lib/systemd/system/ && ln -s serial-getty\@.service serial-getty@tty1.service && cd - +cp /usr/lib/systemd/system-generators/systemd-getty-generator initrd-dir/usr/lib/systemd/system-generators/ + +cat> initrd-dir/usr/lib/systemd/system/getty.sh < /var/log/syslog1 +systemctl restart serial-getty@tty1.service > /var/log/syslog2 +echo "wcg-test" > /var/log/syslog3 +$PRODUCT_CMD +EOF + +chmod a+x initrd-dir/usr/lib/systemd/system/getty.sh +sed -i "s/Type=oneshot/#Type=oneshot/g" initrd-dir/usr/lib/systemd/system/initrd-switch-root.service +sed -i "s,ExecStart=/usr/bin/systemctl --no-block switch-root /sysroot,#ExecStart=/usr/bin/systemctl --no-block switch-root /sysroot,g" initrd-dir/usr/lib/systemd/system/initrd-switch-root.service +echo "ExecStart=/usr/lib/systemd/system/getty.sh RemainAfterExit=yes" >> initrd-dir/usr/lib/systemd/system/initrd-switch-root.service + +sed -i "s,ExecStart=-/sbin/agetty -o '-p -- \\\\\\\u' --noclear %I \$TERM,ExecStart=-/sbin/agetty --autologin root --noclear -n -l /bin/bash -o '--login' %I 38400,g" initrd-dir/usr/lib/systemd/system/getty\@.service +cd initrd-dir/usr/lib/systemd/system/ && ln -s getty\@.service getty\@tty1.service && cd - +echo "Wants=getty@tty1.service" >> initrd-dir/usr/lib/systemd/system/getty.target + +sed -i "s/dev-%i.device/dev-%i/g" initrd-dir/usr/lib/systemd/system/serial-getty\@.service +sed -i "s#ExecStart=-/sbin/agetty -o '-p -- \\\\\\\u' --keep-baud 115200,38400,9600 %I \$TERM#ExecStart=-/sbin/agetty --autologin root --noclear -n -l /bin/bash -o '--login' %I 38400#g" initrd-dir/usr/lib/systemd/system/serial-getty\@.service + +cp /sbin/agetty initrd-dir/sbin/ +rm -rf initrd-dir/usr/lib/systemd/system/dracut-* +##### +echo "pack and generate initrd-new.img ..." +cd initrd-dir/ && find . | cpio -c -o | xz -9 --format=xz --check=crc32 > ../initrd-new.img && cd - +rm -rf $RESULT_DIR/initrd-$KERNEL_VERSION.img +rm -rf initrd-dir/ +mv initrd-new.img $RESULT_DIR/initrd-$KERNEL_VERSION.img +echo "initrd image[$RESULT_DIR/initrd-$KERNEL_VERSION.img] is created successfully!" + + -- Gitee