diff --git a/src/.config b/src/.config index 480f46cc..7a878772 100644 --- a/src/.config +++ b/src/.config @@ -11,19 +11,19 @@ # # https://kernel.org # -KERNEL_SOURCE_URL=https://kernel.org/pub/linux/kernel/v5.x/linux-5.18.3.tar.xz +KERNEL_SOURCE_URL=https://mirrors.edge.kernel.org/pub/linux/kernel/v$(echo $KERNEL_VERSION | cut -d '.' -f 1).x/linux-$KERNEL_VERSION.tar.xz # You can find the latest GNU C library source bundles here: # # https://gnu.org/software/libc # -GLIBC_SOURCE_URL=https://ftp.gnu.org/gnu/glibc/glibc-2.35.tar.bz2 +GLIBC_SOURCE_URL=https://ftp.gnu.org/gnu/glibc/glibc-$GLIBC_VERSION.tar.bz2 # You can find the latest Busybox source bundles here: # # https://busybox.net # -BUSYBOX_SOURCE_URL=https://busybox.net/downloads/busybox-1.34.1.tar.bz2 +BUSYBOX_SOURCE_URL=https://busybox.net/downloads/busybox-$BUSYBOX_VERSION.tar.bz2 # You can find the latest Syslinux source bundles here: # @@ -65,7 +65,7 @@ FIRMWARE_TYPE=bios # # minimal_config/kernel.config # -USE_PREDEFINED_KERNEL_CONFIG=false +USE_PREDEFINED_KERNEL_CONFIG=true # Build kernel modules and make them available in the overlayfs / rootfs in the # /lib/modules directory. The modules can be loaded via 'insmod' to support @@ -80,7 +80,7 @@ BUILD_KERNEL_MODULES=false # # minimal_config/busybox.config # -USE_PREDEFINED_BUSYBOX_CONFIG=false +USE_PREDEFINED_BUSYBOX_CONFIG=true # Define the overlay type to use. Possible values are 'sparse' and 'folder'. # You can use any other value, no value, or comment the property in order to @@ -190,7 +190,7 @@ OVERLAY_TYPE=folder # * mll_logo # * mll_source # -OVERLAY_BUNDLES=dhcp,mll_hello,mll_logo,mll_source +OVERLAY_BUNDLES=$ADD_BUNDLES # The location where the overlay bundle software will be stored. # @@ -208,7 +208,7 @@ OVERLAY_LOCATION=iso # sources and have no internet connection, or if you want to share your version # of 'Minimal Linux Live' with predefined sources. # -USE_LOCAL_SOURCE=false +USE_LOCAL_SOURCE=true # This property enables the standard penguin boot logo in the upper left corner # of the screen. The property is used in 'xx_build_kernel.sh'. The default diff --git a/src/02_build_kernel.sh b/src/02_build_kernel.sh index 4e0db8d7..63044f3c 100755 --- a/src/02_build_kernel.sh +++ b/src/02_build_kernel.sh @@ -4,11 +4,12 @@ set -e # Load common properties and functions in the current script. . ./common.sh +. ./settings echo "*** BUILD KERNEL BEGIN ***" # Change to the kernel source directory which ls finds, e.g. 'linux-4.4.6'. -cd `ls -d $WORK_DIR/kernel/linux-*` +cd `ls -d $WORK_DIR/kernel/linux-$KERNEL_VERSION` # Cleans up the kernel sources, including configuration files. echo "Preparing kernel work area." @@ -23,87 +24,89 @@ if [ "$USE_PREDEFINED_KERNEL_CONFIG" = "true" -a ! -f $SRC_DIR/minimal_config/ke USE_PREDEFINED_KERNEL_CONFIG=false fi -if [ "$USE_PREDEFINED_KERNEL_CONFIG" = "true" ] ; then - # Use predefined configuration file for the kernel. - echo "Using config file '$SRC_DIR/minimal_config/kernel.config'." - cp -f $SRC_DIR/minimal_config/kernel.config .config -else - # Create default configuration file for the kernel. - make defconfig -j $NUM_JOBS - echo "Generated default kernel configuration." +# Create default configuration file for the kernel. +yes | make defconfig -j $NUM_JOBS - # Changes the name of the system to 'minimal'. - sed -i "s/.*CONFIG_DEFAULT_HOSTNAME.*/CONFIG_DEFAULT_HOSTNAME=\"minimal\"/" .config +echo "Generated default kernel configuration." - # OVERLAYFS - BEGIN - most features are disabled (you don't really need them) +# Changes the name of the system to 'minimal'. +sed -i "s/.*CONFIG_DEFAULT_HOSTNAME.*/CONFIG_DEFAULT_HOSTNAME=\"minimal\"/" .config - # Enable overlay support, e.g. merge ro and rw directories (3.18+). - sed -i "s/.*CONFIG_OVERLAY_FS.*/CONFIG_OVERLAY_FS=y/" .config +# OVERLAYFS - BEGIN - most features are disabled (you don't really need them) - # Turn on redirect dir feature by default (4.10+). - echo "# CONFIG_OVERLAY_FS_REDIRECT_DIR is not set" >> .config +# Enable overlay support, e.g. merge ro and rw directories (3.18+). +sed -i "s/.*CONFIG_OVERLAY_FS.*/CONFIG_OVERLAY_FS=y/" .config - # Turn on inodes index feature by default (4.13+). - echo "# CONFIG_OVERLAY_FS_INDEX is not set" >> .config +# Turn on redirect dir feature by default (4.10+). +echo "# CONFIG_OVERLAY_FS_REDIRECT_DIR is not set" >> .config - # Follow redirects even if redirects are turned off (4.15+). - echo "CONFIG_OVERLAY_FS_REDIRECT_ALWAYS_FOLLOW=y" >> .config +# Turn on inodes index feature by default (4.13+). +echo "# CONFIG_OVERLAY_FS_INDEX is not set" >> .config - # Turn on NFS export feature by default (4.16+). - echo "# CONFIG_OVERLAY_FS_NFS_EXPORT is not set" >> .config +# Follow redirects even if redirects are turned off (4.15+). +echo "CONFIG_OVERLAY_FS_REDIRECT_ALWAYS_FOLLOW=y" >> .config - # Auto enable inode number mapping (4.17+). - echo "# CONFIG_OVERLAY_FS_XINO_AUTO is not set" >> .config +# Turn on NFS export feature by default (4.16+). +echo "# CONFIG_OVERLAY_FS_NFS_EXPORT is not set" >> .config - # Тurn on metadata only copy up feature by default (4.19+). - echo "# CONFIG_OVERLAY_FS_METACOPY is not set" >> .config +# Auto enable inode number mapping (4.17+). +echo "# CONFIG_OVERLAY_FS_XINO_AUTO is not set" >> .config - # OVERLAYFS - END +# Тurn on metadata only copy up feature by default (4.19+). +echo "# CONFIG_OVERLAY_FS_METACOPY is not set" >> .config - # Step 1 - disable all active kernel compression options (should be only one). - sed -i "s/.*\\(CONFIG_KERNEL_.*\\)=y/\\#\\ \\1 is not set/" .config +# OVERLAYFS - END - # Step 2 - enable the 'xz' compression option. - sed -i "s/.*CONFIG_KERNEL_XZ.*/CONFIG_KERNEL_XZ=y/" .config +# Step 1 - disable all active kernel compression options (should be only one). +sed -i "s/.*\\(CONFIG_KERNEL_.*\\)=y/\\#\\ \\1 is not set/" .config - # Enable the VESA framebuffer for graphics support. - sed -i "s/.*CONFIG_FB_VESA.*/CONFIG_FB_VESA=y/" .config +# Step 2 - enable the 'xz' compression option. +sed -i "s/.*CONFIG_KERNEL_XZ.*/CONFIG_KERNEL_XZ=y/" .config - # Read the 'USE_BOOT_LOGO' property from '.config' - USE_BOOT_LOGO=`read_property USE_BOOT_LOGO` +# Enable the VESA framebuffer for graphics support. +sed -i "s/.*CONFIG_FB_VESA.*/CONFIG_FB_VESA=y/" .config + +# Read the 'USE_BOOT_LOGO' property from '.config' +USE_BOOT_LOGO=`read_property USE_BOOT_LOGO` + +if [ "$USE_BOOT_LOGO" = "true" ] ; then +sed -i "s/.*CONFIG_LOGO_LINUX_CLUT224.*/CONFIG_LOGO_LINUX_CLUT224=y/" .config +echo "Boot logo is enabled." +else +sed -i "s/.*CONFIG_LOGO_LINUX_CLUT224.*/\\# CONFIG_LOGO_LINUX_CLUT224 is not set/" .config +echo "Boot logo is disabled." +fi - if [ "$USE_BOOT_LOGO" = "true" ] ; then - sed -i "s/.*CONFIG_LOGO_LINUX_CLUT224.*/CONFIG_LOGO_LINUX_CLUT224=y/" .config - echo "Boot logo is enabled." - else - sed -i "s/.*CONFIG_LOGO_LINUX_CLUT224.*/\\# CONFIG_LOGO_LINUX_CLUT224 is not set/" .config - echo "Boot logo is disabled." - fi +# Disable debug symbols in kernel => smaller kernel binary. +sed -i "s/^CONFIG_DEBUG_KERNEL.*/\\# CONFIG_DEBUG_KERNEL is not set/" .config - # Disable debug symbols in kernel => smaller kernel binary. - sed -i "s/^CONFIG_DEBUG_KERNEL.*/\\# CONFIG_DEBUG_KERNEL is not set/" .config +# Enable the EFI stub +sed -i "s/.*CONFIG_EFI_STUB.*/CONFIG_EFI_STUB=y/" .config - # Enable the EFI stub - sed -i "s/.*CONFIG_EFI_STUB.*/CONFIG_EFI_STUB=y/" .config +# Request that the firmware clear the contents of RAM after reboot (4.14+). +echo "CONFIG_RESET_ATTACK_MITIGATION=y" >> .config - # Request that the firmware clear the contents of RAM after reboot (4.14+). - echo "CONFIG_RESET_ATTACK_MITIGATION=y" >> .config +# Disable Apple Properties (Useful for Macs but useless in general) +echo "CONFIG_APPLE_PROPERTIES=n" >> .config - # Disable Apple Properties (Useful for Macs but useless in general) - echo "CONFIG_APPLE_PROPERTIES=n" >> .config +# Check if we are building 64-bit kernel. +if [ "`grep "CONFIG_X86_64=y" .config`" = "CONFIG_X86_64=y" ] ; then +# Enable the mixed EFI mode when building 64-bit kernel. +echo "CONFIG_EFI_MIXED=y" >> .config +fi - # Check if we are building 64-bit kernel. - if [ "`grep "CONFIG_X86_64=y" .config`" = "CONFIG_X86_64=y" ] ; then - # Enable the mixed EFI mode when building 64-bit kernel. - echo "CONFIG_EFI_MIXED=y" >> .config - fi +if [ "$USE_PREDEFINED_KERNEL_CONFIG" = "true" ] ; then + # Use predefined configuration file for the kernel. + echo "Using config file '$SRC_DIR/minimal_config/kernel.config' to update defaults." + $SRC_DIR/./update_config.sh .config $SRC_DIR/minimal_config/kernel.config fi # Compile the kernel with optimization for 'parallel jobs' = 'number of processors'. # Good explanation of the different kernels: # http://unix.stackexchange.com/questions/5518/what-is-the-difference-between-the-following-kernel-makefile-terms-vmlinux-vmlinux echo "Building kernel." -make \ + +yes | make \ CFLAGS="$CFLAGS" \ bzImage -j $NUM_JOBS diff --git a/src/04_build_glibc.sh b/src/04_build_glibc.sh index b76c5c46..224dd521 100755 --- a/src/04_build_glibc.sh +++ b/src/04_build_glibc.sh @@ -4,6 +4,7 @@ set -e # Load common properties and functions in the current script. . ./common.sh +. ./settings echo "*** BUILD GLIBC BEGIN ***" @@ -18,7 +19,7 @@ rm -rf $GLIBC_INSTALLED mkdir $GLIBC_INSTALLED # Find the glibc source directory, e.g. 'glibc-2.23' and remember it. -GLIBC_SRC=`ls -d $WORK_DIR/glibc/glibc-*` +GLIBC_SRC=`ls -d $WORK_DIR/glibc/glibc-$GLIBC_VERSION` # All glibc work is done from the working area. cd $GLIBC_OBJECTS diff --git a/src/07_build_busybox.sh b/src/07_build_busybox.sh index e76c1875..f514603f 100755 --- a/src/07_build_busybox.sh +++ b/src/07_build_busybox.sh @@ -4,6 +4,7 @@ set -e # Load common properties and functions in the current script. . ./common.sh +. ./settings echo "*** BUILD BUSYBOX BEGIN ***" @@ -12,7 +13,7 @@ echo "Removing old Busybox artifacts. This may take a while." rm -rf $BUSYBOX_INSTALLED # Change to the source directory ls finds, e.g. 'busybox-1.24.2'. -cd `ls -d $WORK_DIR/busybox/busybox-*` +cd `ls -d $WORK_DIR/busybox/busybox-$BUSYBOX_VERSION` # Remove previously generated artifacts. echo "Preparing Busybox work area. This may take a while." @@ -26,14 +27,14 @@ if [ "$USE_PREDEFINED_BUSYBOX_CONFIG" = "true" -a ! -f $SRC_DIR/minimal_config/b USE_PREDEFINED_BUSYBOX_CONFIG="false" fi +# Create default configuration file. +echo "Generating default Busybox configuration." +yes | make defconfig -j $NUM_JOBS + if [ "$USE_PREDEFINED_BUSYBOX_CONFIG" = "true" ] ; then # Use predefined configuration file for Busybox. - echo "Using config file $SRC_DIR/minimal_config/busybox.config" - cp -f $SRC_DIR/minimal_config/busybox.config .config -else - # Create default configuration file. - echo "Generating default Busybox configuration." - make defconfig -j $NUM_JOBS + echo "Using config file $SRC_DIR/minimal_config/busybox.config to update defaults." + $SRC_DIR/./update_config.sh .config $SRC_DIR/minimal_config/busybox.config fi # Now we tell Busybox to use the sysroot area. @@ -44,7 +45,7 @@ sed -i "s|.*CONFIG_EXTRA_CFLAGS.*|CONFIG_EXTRA_CFLAGS=\"$CFLAGS -L$SYSROOT/lib\" # Compile busybox with optimization for "parallel jobs" = "number of processors". echo "Building Busybox." -make \ +yes | make \ busybox -j $NUM_JOBS # Create the symlinks for busybox. The file 'busybox.links' is used for this. diff --git a/src/08_prepare_bundles.sh b/src/08_prepare_bundles.sh index f1ef4d6f..50ca1298 100755 --- a/src/08_prepare_bundles.sh +++ b/src/08_prepare_bundles.sh @@ -4,6 +4,7 @@ set -e # Load common properties and functions in the current script. . ./common.sh +. ./settings echo "*** PREPARE OVERLAY BEGIN ***" diff --git a/src/16_cleanup.sh b/src/16_cleanup.sh index f3dbf088..36a3f6bf 100755 --- a/src/16_cleanup.sh +++ b/src/16_cleanup.sh @@ -9,14 +9,19 @@ set -e echo "*** CLEANUP BEGIN ***" if [ "$(id -u)" = "0" ] ; then - echo "Applying original ownership to all affected files. This may take a while." - # Find the original user. Note that this may not always be correct. ORIG_USER=`who | head -n 1 | awk '{print \$1}'` - echo "Original user is '$ORIG_USER'." - # Apply ownership back to original owner for all affected files. - chown -R $ORIG_USER:$ORIG_USER * + if [ -n "$var" ]; then + echo "Applying original ownership to all affected files. This may take a while." + + echo "Original user is '$ORIG_USER'." + + # Apply ownership back to original owner for all affected files. + chown -R $ORIG_USER:$ORIG_USER * + else + echo "Original user unknown" + fi else echo "No need to perform cleanup." fi diff --git a/src/common.sh b/src/common.sh index bbbb724d..9534d7ed 100755 --- a/src/common.sh +++ b/src/common.sh @@ -2,6 +2,8 @@ set -e +. ./settings + SRC_DIR=$PWD CONFIG=$SRC_DIR/.config SOURCE_DIR=$SRC_DIR/source @@ -32,7 +34,8 @@ read_property() ( if [ ! "$prop_name" = "" ] ; then # Search in the main '.config' file. - prop_value=`grep -i ^${prop_name}= $CONFIG | cut -f2- -d'=' | xargs` + value_def=`grep -i ^${prop_name}= $CONFIG | cut -f2- -d'=' | xargs` + prop_value=`eval echo $value_def` fi echo $prop_value diff --git a/src/minimal_overlay/bundles/e2fsprogs/.config b/src/minimal_overlay/bundles/e2fsprogs/.config new file mode 100644 index 00000000..74b87e8a --- /dev/null +++ b/src/minimal_overlay/bundles/e2fsprogs/.config @@ -0,0 +1 @@ +E2FSPROGS_SOURCE_URL=https://git.kernel.org/pub/scm/fs/ext2/e2fsprogs.git/snapshot/e2fsprogs-$E2FSPROGS_VERSION.tar.gz diff --git a/src/minimal_overlay/bundles/e2fsprogs/01_get.sh b/src/minimal_overlay/bundles/e2fsprogs/01_get.sh new file mode 100755 index 00000000..8ba9f365 --- /dev/null +++ b/src/minimal_overlay/bundles/e2fsprogs/01_get.sh @@ -0,0 +1,39 @@ +#!/bin/sh + +set -e + +. ../../common.sh +. ../../../settings + +# Read the common configuration properties. +DOWNLOAD_URL=`read_property E2FSPROGS_SOURCE_URL` +USE_LOCAL_SOURCE=`read_property USE_LOCAL_SOURCE` + +# Grab everything after the last '/' character. +ARCHIVE_FILE=${DOWNLOAD_URL##*/} + +if [ "$USE_LOCAL_SOURCE" = "true" -a ! -f $MAIN_SRC_DIR/source/overlay/$ARCHIVE_FILE ] ; then + echo "Source bundle $MAIN_SRC_DIR/source/overlay/$ARCHIVE_FILE is missing and will be downloaded." + USE_LOCAL_SOURCE="false" +fi + +cd $MAIN_SRC_DIR/source/overlay + +if [ ! "$USE_LOCAL_SOURCE" = "true" ] ; then + # Downloading util-linux source bundle file. The '-c' option allows the download to resume. + echo "Downloading jq source bundle from $DOWNLOAD_URL" + wget -c $DOWNLOAD_URL +else + echo "Using local jq source bundle $MAIN_SRC_DIR/source/overlay/$ARCHIVE_FILE" +fi + +# Delete folder with previously extracted jq. +echo "Removing util-linux work area. This may take a while." +rm -rf $WORK_DIR/overlay/$BUNDLE_NAME +mkdir $WORK_DIR/overlay/$BUNDLE_NAME + +# Extract util-linux to folder 'work/overlay/util_linux'. +# Full path will be something like 'work/overlay/util_linux/util-linux-2.31'. +tar -xvf $ARCHIVE_FILE -C $WORK_DIR/overlay/$BUNDLE_NAME + +cd $SRC_DIR diff --git a/src/minimal_overlay/bundles/e2fsprogs/02_build.sh b/src/minimal_overlay/bundles/e2fsprogs/02_build.sh new file mode 100755 index 00000000..f5bd6c86 --- /dev/null +++ b/src/minimal_overlay/bundles/e2fsprogs/02_build.sh @@ -0,0 +1,67 @@ +#!/bin/sh + +set -e + +. ../../common.sh +. ../../../settings + +cd $WORK_DIR/overlay/$BUNDLE_NAME + +# Change to the util-linux source directory which ls finds, e.g. 'util-linux-2.34'. +cd $(ls -d e2fsprogs-$E2FSPROGS_VERSION) + +if [ -f Makefile ] ; then + echo "Preparing '$BUNDLE_NAME' work area. This may take a while." + make -j $NUM_JOBS clean +else + echo "The clean phase for '$BUNDLE_NAME' has been skipped." +fi + +rm -rf $DEST_DIR +mkdir -p $DEST_DIR/bin +mkdir -p $DEST_DIR/lib + +echo "Configuring '$BUNDLE_NAME'." + +CFLAGS="$CFLAGS" ./configure --prefix=/usr \ + --bindir=/bin \ + --sysconfdir=/etc \ + --enable-elf-shlibs \ + #--disable-libblkid \ + #--disable-libuuid \ + #--disable-uuidd \ + #--disable-fsck + +echo "Building '$BUNDLE_NAME'." +make -j $NUM_JOBS + +echo "Installing '$BUNDLE_NAME'." +make check +#make -j $NUM_JOBS install + +cp -r misc/mke2fs $DEST_DIR/bin/mke2fs +cp -r lib/libext2fs.so.2.4 $DEST_DIR/lib/libext2fs.so.2.4 +cp -r lib/libext2fs.so.2 $DEST_DIR/lib/libext2fs.so.2 +cp -r lib/libext2fs.so $DEST_DIR/lib/libext2fs.so +cp -r lib/libcom_err.so.2.1 $DEST_DIR/lib/libcom_err.so.2.1 +cp -r lib/libcom_err.so.2 $DEST_DIR/lib/libcom_err.so.2 +cp -r lib/libcom_err.so $DEST_DIR/lib/libcom_err.so +cp -r lib/libe2p.so.2.3 $DEST_DIR/lib/libe2p.so.2.3 +cp -r lib/libe2p.so.2 $DEST_DIR/lib/libe2p.so.2 +cp -r lib/libe2p.so $DEST_DIR/lib/libe2p.so +cd $DEST_DIR/bin +ln mke2fs mkfs.ext2 +ln mke2fs mkfs.ext3 +ln mke2fs mkfs.ext4 +cd $(ls -d e2fsprogs-$E2FSPROGS_VERSION) + +echo "Reducing '$BUNDLE_NAME' size." +reduce_size $DEST_DIR + +install_to_overlay bin +install_to_overlay lib + +echo "Bundle '$BUNDLE_NAME' has been installed." + +cd $SRC_DIR + diff --git a/src/minimal_overlay/bundles/e2fsprogs/bundle.sh b/src/minimal_overlay/bundles/e2fsprogs/bundle.sh new file mode 100755 index 00000000..e132bc25 --- /dev/null +++ b/src/minimal_overlay/bundles/e2fsprogs/bundle.sh @@ -0,0 +1,10 @@ +#!/bin/sh + +set -e + +. ../../common.sh + +./01_get.sh +./02_build.sh + +cd $SRC_DIR diff --git a/src/minimal_overlay/bundles/jq/.config b/src/minimal_overlay/bundles/jq/.config new file mode 100644 index 00000000..454fc855 --- /dev/null +++ b/src/minimal_overlay/bundles/jq/.config @@ -0,0 +1,9 @@ +# Some of the functionality depends on third party +# libraries, e.g. 'ncurses' and 'termcap'. Also note +# that the build process requires root permissions. +# +# You can find the latest util_linux archives here: +# +# https://www.kernel.org/pub/linux/utils/util-linux/ +# +JQ_SOURCE_URL=https://github.com/jqlang/jq/releases/download/jq-$JQ_VERSION/jq-$JQ_VERSION.tar.gz diff --git a/src/minimal_overlay/bundles/jq/01_get.sh b/src/minimal_overlay/bundles/jq/01_get.sh new file mode 100755 index 00000000..b4a7aec8 --- /dev/null +++ b/src/minimal_overlay/bundles/jq/01_get.sh @@ -0,0 +1,39 @@ +#!/bin/sh + +set -e + +. ../../common.sh +. ../../../settings + +# Read the common configuration properties. +DOWNLOAD_URL=`read_property JQ_SOURCE_URL` +USE_LOCAL_SOURCE=`read_property USE_LOCAL_SOURCE` + +# Grab everything after the last '/' character. +ARCHIVE_FILE=${DOWNLOAD_URL##*/} + +if [ "$USE_LOCAL_SOURCE" = "true" -a ! -f $MAIN_SRC_DIR/source/overlay/$ARCHIVE_FILE ] ; then + echo "Source bundle $MAIN_SRC_DIR/source/overlay/$ARCHIVE_FILE is missing and will be downloaded." + USE_LOCAL_SOURCE="false" +fi + +cd $MAIN_SRC_DIR/source/overlay + +if [ ! "$USE_LOCAL_SOURCE" = "true" ] ; then + # Downloading util-linux source bundle file. The '-c' option allows the download to resume. + echo "Downloading jq source bundle from $DOWNLOAD_URL" + wget -c $DOWNLOAD_URL +else + echo "Using local jq source bundle $MAIN_SRC_DIR/source/overlay/$ARCHIVE_FILE" +fi + +# Delete folder with previously extracted jq. +echo "Removing util-linux work area. This may take a while." +rm -rf $WORK_DIR/overlay/$BUNDLE_NAME +mkdir $WORK_DIR/overlay/$BUNDLE_NAME + +# Extract util-linux to folder 'work/overlay/util_linux'. +# Full path will be something like 'work/overlay/util_linux/util-linux-2.31'. +tar -xvf $ARCHIVE_FILE -C $WORK_DIR/overlay/$BUNDLE_NAME + +cd $SRC_DIR diff --git a/src/minimal_overlay/bundles/jq/02_build.sh b/src/minimal_overlay/bundles/jq/02_build.sh new file mode 100755 index 00000000..8a98a887 --- /dev/null +++ b/src/minimal_overlay/bundles/jq/02_build.sh @@ -0,0 +1,45 @@ +#!/bin/sh + +set -e + +. ../../common.sh +. ../../../settings + +cd $WORK_DIR/overlay/$BUNDLE_NAME + +# Change to the util-linux source directory which ls finds, e.g. 'util-linux-2.34'. +cd $(ls -d jq-$JQ_VERSION) + +if [ -f Makefile ] ; then + echo "Preparing '$BUNDLE_NAME' work area. This may take a while." + make -j $NUM_JOBS clean +else + echo "The clean phase for '$BUNDLE_NAME' has been skipped." +fi + +rm -rf $DEST_DIR +mkdir -p $DEST_DIR/bin + +echo "Configuring '$BUNDLE_NAME'." + +CFLAGS="$CFLAGS" ./configure \ + --with-oniguruma=builtin + +echo "Building '$BUNDLE_NAME'." +make -j $NUM_JOBS + +echo "Installing '$BUNDLE_NAME'." +make check +make -j $NUM_JOBS install + +cp -r jq $DEST_DIR/bin/jq + +echo "Reducing '$BUNDLE_NAME' size." +reduce_size $DEST_DIR + +install_to_overlay + +echo "Bundle '$BUNDLE_NAME' has been installed." + +cd $SRC_DIR + diff --git a/src/minimal_overlay/bundles/jq/bundle.sh b/src/minimal_overlay/bundles/jq/bundle.sh new file mode 100755 index 00000000..e132bc25 --- /dev/null +++ b/src/minimal_overlay/bundles/jq/bundle.sh @@ -0,0 +1,10 @@ +#!/bin/sh + +set -e + +. ../../common.sh + +./01_get.sh +./02_build.sh + +cd $SRC_DIR diff --git a/src/minimal_overlay/bundles/kernel_modules/bundle.sh b/src/minimal_overlay/bundles/kernel_modules/bundle.sh index 657a0503..55c54962 100755 --- a/src/minimal_overlay/bundles/kernel_modules/bundle.sh +++ b/src/minimal_overlay/bundles/kernel_modules/bundle.sh @@ -3,9 +3,10 @@ set -e . ../../common.sh +. ../../settings -if [ ! -d $WORK_DIR/kernel/linux-* ] ; then +if [ ! -d $WORK_DIR/kernel/linux-$KERNEL_VERSION ] ; then echo "Kernel source directory is missing. Cannot continue." exit 1 else @@ -21,7 +22,7 @@ fi rm -rf $DEST_DIR -cd $WORK_DIR/kernel/linux-* +cd $WORK_DIR/kernel/linux-$KERNEL_VERSION echo "Building kernel modules." make_target modules diff --git a/src/minimal_overlay/bundles/mll_logo/bundle.sh b/src/minimal_overlay/bundles/mll_logo/bundle.sh index 8303f460..c487e5c7 100755 --- a/src/minimal_overlay/bundles/mll_logo/bundle.sh +++ b/src/minimal_overlay/bundles/mll_logo/bundle.sh @@ -3,6 +3,7 @@ set -e . ../../common.sh +. ../../../settings # Uncomment this to regenerate the MLL logo. The file MLL_LOGO # has to be existing in advance and it is your responsibility @@ -28,7 +29,7 @@ if [ ! "$USE_BOOT_LOGO" = "true" ] ; then exit 0 fi -if [ ! -f $WORK_DIR/kernel/linux-*/.config ] ; then +if [ ! -f $WORK_DIR/kernel/linux-$KERNEL_VERSION/.config ] ; then echo "Kernel configuration does not exist. Cannot continue." exit 1 fi diff --git a/src/minimal_overlay/bundles/ncurses/.config b/src/minimal_overlay/bundles/ncurses/.config index f148c8c6..d022b4dc 100644 --- a/src/minimal_overlay/bundles/ncurses/.config +++ b/src/minimal_overlay/bundles/ncurses/.config @@ -10,5 +10,5 @@ # # https://ftp.gnu.org/pub/gnu/ncurses/ # -NCURSES_SOURCE_URL=https://ftp.gnu.org/pub/gnu/ncurses/ncurses-6.1.tar.gz +NCURSES_SOURCE_URL=https://ftp.gnu.org/pub/gnu/ncurses/ncurses-$NCURSES_VERSION.tar.gz diff --git a/src/minimal_overlay/bundles/ncurses/01_get.sh b/src/minimal_overlay/bundles/ncurses/01_get.sh index 0a29bcab..d341b299 100755 --- a/src/minimal_overlay/bundles/ncurses/01_get.sh +++ b/src/minimal_overlay/bundles/ncurses/01_get.sh @@ -3,6 +3,7 @@ set -e . ../../common.sh +. ../../../settings # Read the common configuration properties. DOWNLOAD_URL=`read_property NCURSES_SOURCE_URL` diff --git a/src/minimal_overlay/bundles/ncurses/02_build.sh b/src/minimal_overlay/bundles/ncurses/02_build.sh index 6db40118..b372d9cf 100755 --- a/src/minimal_overlay/bundles/ncurses/02_build.sh +++ b/src/minimal_overlay/bundles/ncurses/02_build.sh @@ -3,11 +3,12 @@ set -e . ../../common.sh +. ../../../settings cd $WORK_DIR/overlay/$BUNDLE_NAME # Change to the ncurses source directory which ls finds, e.g. 'ncurses-6.0'. -cd $(ls -d ncurses-*) +cd $(ls -d ncurses-$NCURSES_VERSION) if [ -f Makefile ] ; then echo "Preparing '$BUNDLE_NAME' work area. This may take a while." diff --git a/src/minimal_overlay/bundles/pkg_config/.config b/src/minimal_overlay/bundles/pkg_config/.config new file mode 100644 index 00000000..7677a640 --- /dev/null +++ b/src/minimal_overlay/bundles/pkg_config/.config @@ -0,0 +1 @@ +PKG_CONFIG_SOURCE_URL=https://pkgconfig.freedesktop.org/releases/pkg-config-$PKG_CONFIG_VERSION.tar.gz diff --git a/src/minimal_overlay/bundles/pkg_config/01_get.sh b/src/minimal_overlay/bundles/pkg_config/01_get.sh new file mode 100755 index 00000000..fcba0b1a --- /dev/null +++ b/src/minimal_overlay/bundles/pkg_config/01_get.sh @@ -0,0 +1,20 @@ +#!/bin/sh + +set -e + +. ../../common.sh +. ../../../settings + +# Read the 'pkg_config' download URL from '.config'. +DOWNLOAD_URL=`read_property PKG_CONFIG_SOURCE_URL` + +# Grab everything after the last '/' character. +ARCHIVE_FILE=${DOWNLOAD_URL##*/} + +# Download 'pkg_config' source archive in the 'source/overlay' directory. +download_source $DOWNLOAD_URL $OVERLAY_SOURCE_DIR/$ARCHIVE_FILE + +# Extract all 'coreutils' sources in the 'work/overlay/pkg_config' directory. +extract_source $OVERLAY_SOURCE_DIR/$ARCHIVE_FILE pkg_config + +cd $SRC_DIR diff --git a/src/minimal_overlay/bundles/pkg_config/02_build.sh b/src/minimal_overlay/bundles/pkg_config/02_build.sh new file mode 100755 index 00000000..20d47928 --- /dev/null +++ b/src/minimal_overlay/bundles/pkg_config/02_build.sh @@ -0,0 +1,32 @@ +#!/bin/sh + +set -e + +. ../../common.sh +. ../../../settings + +cd `ls -d $OVERLAY_WORK_DIR/$BUNDLE_NAME/pkg-config-$PKG_CONFIG_VERSION` + +make_clean + +rm -rf $DEST_DIR + +echo "Configuring '$BUNDLE_NAME'." +CFLAGS="$CFLAGS" ./configure \ + --prefix=/usr \ + --with-internal-glib + +echo "Building '$BUNDLE_NAME'." +make_target + +echo "Installing '$BUNDLE_NAME'." +make_target install DESTDIR=$DEST_DIR + +echo "Reducing '$BUNDLE_NAME' size." +reduce_size $DEST_DIR/usr/bin + +install_to_overlay + +echo "Bundle '$BUNDLE_NAME' has been installed." + +cd $SRC_DIR diff --git a/src/minimal_overlay/bundles/pkg_config/bundle.sh b/src/minimal_overlay/bundles/pkg_config/bundle.sh new file mode 100755 index 00000000..e132bc25 --- /dev/null +++ b/src/minimal_overlay/bundles/pkg_config/bundle.sh @@ -0,0 +1,10 @@ +#!/bin/sh + +set -e + +. ../../common.sh + +./01_get.sh +./02_build.sh + +cd $SRC_DIR diff --git a/src/minimal_overlay/bundles/util_linux/.config b/src/minimal_overlay/bundles/util_linux/.config index c083d982..c25bd00c 100644 --- a/src/minimal_overlay/bundles/util_linux/.config +++ b/src/minimal_overlay/bundles/util_linux/.config @@ -6,4 +6,4 @@ # # https://www.kernel.org/pub/linux/utils/util-linux/ # -UTIL_LINUX_SOURCE_URL=https://kernel.org/pub/linux/utils/util-linux/v2.34/util-linux-2.34.tar.xz +UTIL_LINUX_SOURCE_URL=https://mirrors.edge.kernel.org/pub/linux/utils/util-linux/v$(echo $UTIL_LINUX_VERSION | cut -d '-' -f 1 | cut -d '.' -f 1-2)/util-linux-$UTIL_LINUX_VERSION.tar.xz diff --git a/src/minimal_overlay/bundles/util_linux/01_get.sh b/src/minimal_overlay/bundles/util_linux/01_get.sh index 67bd4ee6..f005f0e4 100755 --- a/src/minimal_overlay/bundles/util_linux/01_get.sh +++ b/src/minimal_overlay/bundles/util_linux/01_get.sh @@ -3,6 +3,7 @@ set -e . ../../common.sh +. ../../../settings # Read the common configuration properties. DOWNLOAD_URL=`read_property UTIL_LINUX_SOURCE_URL` diff --git a/src/minimal_overlay/bundles/util_linux/02_build.sh b/src/minimal_overlay/bundles/util_linux/02_build.sh index 4c61dfad..59712c59 100755 --- a/src/minimal_overlay/bundles/util_linux/02_build.sh +++ b/src/minimal_overlay/bundles/util_linux/02_build.sh @@ -3,11 +3,12 @@ set -e . ../../common.sh +. ../../../settings cd $WORK_DIR/overlay/$BUNDLE_NAME # Change to the util-linux source directory which ls finds, e.g. 'util-linux-2.34'. -cd $(ls -d util-linux-*) +cd $(ls -d util-linux-$UTIL_LINUX_VERSION) if [ -f Makefile ] ; then echo "Preparing '$BUNDLE_NAME' work area. This may take a while." @@ -21,6 +22,7 @@ mkdir -p $DEST_DIR/usr/share/doc/util-linux mkdir -p $DEST_DIR/bin echo "Configuring '$BUNDLE_NAME'." +./autogen.sh && \ CFLAGS="$CFLAGS" ./configure \ ADJTIME_PATH=/var/lib/hwclock/adjtime \ --prefix=$DEST_DIR \ @@ -34,9 +36,14 @@ CFLAGS="$CFLAGS" ./configure \ --disable-pylibmount \ --disable-static \ --disable-makeinstall-chown \ - --without-python \ - --without-systemd \ - --without-systemdsystemunitdir + --enable-sfdisk \ + --enable-mkfs + #--enable-libblkid \ + #--enable-libuuid \ + #--enable-libsmartcols \ + #--without-python \ + #--without-systemd \ + #--without-systemdsystemunitdir echo "Building '$BUNDLE_NAME'." make -j $NUM_JOBS @@ -44,10 +51,20 @@ make -j $NUM_JOBS echo "Installing '$BUNDLE_NAME'." make -j $NUM_JOBS install +cp -r /lib/x86_64-linux-gnu/libtinfo.so.6.3 $DEST_DIR/lib +cd $DEST_DIR/lib +ln -s libtinfo.so.6.3 libtinfo.so.6 +cd $(ls -d util-linux-$UTIL_LINUX_VERSION) + + echo "Reducing '$BUNDLE_NAME' size." reduce_size $DEST_DIR/bin +reduce_size $DEST_DIR/sbin +reduce_size $DEST_DIR/lib install_to_overlay bin +install_to_overlay sbin +install_to_overlay lib echo "Bundle '$BUNDLE_NAME' has been installed." diff --git a/src/minimal_overlay/common.sh b/src/minimal_overlay/common.sh index e252adb9..40fe0a37 100755 --- a/src/minimal_overlay/common.sh +++ b/src/minimal_overlay/common.sh @@ -36,11 +36,13 @@ read_property() ( if [ ! "$prop_name" = "" ] ; then # Search in the main '.config' file. - prop_value="`grep -i ^${prop_name}= $CONFIG | cut -f2- -d'=' | xargs`" + value_def=`grep -i ^${prop_name}= $CONFIG | cut -f2- -d'=' | xargs` + prop_value=`eval echo $value_def` if [ -f $SRC_DIR/.config ] ; then # Search in the local '.config' file. - prop_value_local="`grep -i ^${prop_name}= $SRC_DIR/.config | cut -f2- -d'=' | xargs`" + value_def_local=`grep -i ^${prop_name}= $SRC_DIR/.config | cut -f2- -d'=' | xargs` + prop_value_local=`eval echo $value_def_local` if [ ! "$prop_value_local" = "" ] ; then # Override the original value with the local value. @@ -49,7 +51,7 @@ read_property() ( fi fi - echo "$prop_value" + echo $prop_value ) # Read commonly used configuration properties. diff --git a/src/minimal_overlay/overlay_build.sh b/src/minimal_overlay/overlay_build.sh index 599c94bd..03d38f89 100755 --- a/src/minimal_overlay/overlay_build.sh +++ b/src/minimal_overlay/overlay_build.sh @@ -2,6 +2,8 @@ set -e +. ../settings + SRC_DIR=$(pwd) # Find the main source directory @@ -16,7 +18,8 @@ fi if [ "$1" = "" ] ; then # Read the 'OVERLAY_BUNDLES' property from '.config' - OVERLAY_BUNDLES="$(grep -i ^OVERLAY_BUNDLES $MAIN_SRC_DIR/.config | cut -f2 -d'=')" + OVERLAY_BUNDLES_VALUE_DEF="$(grep -i ^OVERLAY_BUNDLES $MAIN_SRC_DIR/.config | cut -f2 -d'=')" + OVERLAY_BUNDLES=`eval echo $OVERLAY_BUNDLES_VALUE_DEF` else OVERLAY_BUNDLES=$1 fi diff --git a/src/minimal_rootfs/etc/01_prepare.sh b/src/minimal_rootfs/etc/01_prepare.sh index c8eda6d8..6faf00e4 100755 --- a/src/minimal_rootfs/etc/01_prepare.sh +++ b/src/minimal_rootfs/etc/01_prepare.sh @@ -36,5 +36,5 @@ mkdir -p /dev/pts mount -t devpts none /dev/pts -echo "Mounted all core filesystems. Ready to continue." +echo "Mounted all core filesystems." diff --git a/src/minimal_rootfs/etc/03_init.sh b/src/minimal_rootfs/etc/03_init.sh index 42e0b3f1..0180c06d 100755 --- a/src/minimal_rootfs/etc/03_init.sh +++ b/src/minimal_rootfs/etc/03_init.sh @@ -35,29 +35,9 @@ # # exec /sbin/init -# Print first message on screen. -cat /etc/msg/03_init_01.txt - -# Wait 5 second or until any ~keyboard key is pressed. -read -t 5 -n1 -s key - -if [ "$key" = "" ] ; then - # Use default initialization logic based on configuration in '/etc/inittab'. - echo -e "Executing \\e[32m/sbin/init\\e[0m as PID 1." - exec /sbin/init -else - # Print second message on screen. - cat /etc/msg/03_init_02.txt - - if [ "$PID1_SHELL" = "true" ] ; then - # PID1_SHELL flag is set which means we have controlling terminal. - unset PID1_SHELL - exec sh - else - # Interactive shell with controlling tty as PID 1. - exec setsid cttyhack sh - fi -fi +# Use default initialization logic based on configuration in '/etc/inittab'. +echo -e "Executing \\e[32m/sbin/init\\e[0m as PID 1." +exec /sbin/init echo "(/etc/03_init.sh) - there is a serious bug." diff --git a/src/minimal_rootfs/etc/autorun/50_log_something_on_disk.sh b/src/minimal_rootfs/etc/autorun/50_log_something_on_disk.sh new file mode 100755 index 00000000..61d5b642 --- /dev/null +++ b/src/minimal_rootfs/etc/autorun/50_log_something_on_disk.sh @@ -0,0 +1,13 @@ +#!/bin/sh + +UNFORMATTED=$(lsblk --fs --json | jq -r '.blockdevices[] | select(.children == null and .fstype == null) | .name'| grep -i "vd") +if [ -n "$UNFORMATTED" ]; then + sfdisk /dev/$UNFORMATTED <> /mnt/virtdrive/log diff --git a/src/minimal_rootfs/init b/src/minimal_rootfs/init index ce9e6483..4fdb8d2a 100755 --- a/src/minimal_rootfs/init +++ b/src/minimal_rootfs/init @@ -29,22 +29,6 @@ echo -e "Welcome to \\e[1mMinimal \\e[32mLinux \\e[31mLive\\e[0m (/init)" # Let's mount all core file systems. /etc/01_prepare.sh -# Print first message on screen. -cat /etc/msg/init_01.txt - -# Wait 5 second or until any ~keyboard key is pressed. -read -t 5 -n1 -s key - -if [ ! "$key" = "" ] ; then - # Print second message on screen. - cat /etc/msg/init_02.txt - - # Set flag which indicates that we have obtained controlling terminal. - export PID1_SHELL=true - - # Interactive shell with controlling tty as PID 1. - exec setsid cttyhack sh -fi # Create new mountpoint in RAM, make it our new root location and overlay it # with our storage area (if overlay area exists at all). This operation invokes diff --git a/src/qemu-bios-console.sh b/src/qemu-bios-console.sh index d30be8fc..33670434 100755 --- a/src/qemu-bios-console.sh +++ b/src/qemu-bios-console.sh @@ -1,4 +1,6 @@ -#!/bin/sh +#!/bin/bash + +. ./argparse.sh # Use this script without arguments to run the generated ISO image with QEMU. # If you pass '-hdd' or '-h' the virtual hard disk 'hdd.img' will be attached. @@ -33,12 +35,33 @@ else ARCH="i386" fi -cmd="qemu-system-$ARCH -m 128M -cdrom minimal_linux_live.iso -boot d -nographic" +cmd="qemu-system-$ARCH -m 128M -cdrom minimal_linux_live.iso -boot d -nodefaults -display none -serial $(tty)" -if [ "$1" = "-hdd" -o "$1" = "-h" ] ; then - echo "Starting QEMU with attached ISO image and hard disk." - echo 'console' | $cmd -hda hdd.img -else - echo "Starting QEMU with attached ISO image." - echo 'console' | $cmd +define_arg "hdd" "false" "Add hdd" "store_true" "false" +define_arg "hdd_img" "" "hdd image specified" "string" "false" +define_arg "virtfs" "false" "Add virtfs" "store_true" "false" +define_arg "virt_img" "" "virtfs image specified" "string" "false" + +parse_args "$@" + +if [[ $hdd == "true" ]]; then + if [[ $hdd_img == "" ]]; then + rm hdd-empty.qcow2 + qemu-img create -f qcow2 hdd-empty.qcow2 100M + cmd="${cmd} -hda hdd-empty.qcow2" + else + cmd="${cmd} -hda $hdd_img" + fi fi + +if [[ $virtfs == "true" ]]; then + if [[ $virt_img == "" ]]; then + rm virtfs-empty.qcow2 + qemu-img create -f qcow2 virtfs-empty.qcow2 100M + cmd="${cmd} -device virtio-blk-pci,drive=drive0,id=virtblk0,num-queues=4 -drive file=virtfs-empty.qcow2,if=none,id=drive0" + else + cmd="${cmd} -device virtio-blk-pci,drive=drive0,id=virtblk0,num-queues=4 -drive file=${virt_img},if=none,id=drive0" + fi +fi + +`$cmd` diff --git a/src/settings b/src/settings new file mode 100644 index 00000000..73ec3dfe --- /dev/null +++ b/src/settings @@ -0,0 +1,9 @@ +KERNEL_VERSION=6.7.5 +GLIBC_VERSION=2.39 +BUSYBOX_VERSION=1.36.1 +ADD_BUNDLES=dhcp,util_linux,passwd,jq,e2fsprogs +UTIL_LINUX_VERSION=2.39.3 +NCURSES_VERSION=6.3 +PKG_CONFIG_VERSION=0.29.2 +JQ_VERSION=1.7.1 +E2FSPROGS_VERSION=1.47.0 diff --git a/src/test_qemu.sh b/src/test_qemu.sh index 824917c5..e8753265 100755 --- a/src/test_qemu.sh +++ b/src/test_qemu.sh @@ -14,6 +14,8 @@ echo " Minimal Linux Live will shut down in 10 seconds." CEOF chmod +x minimal_overlay/rootfs/etc/autorun/99_autoshutdown.sh +cp -f minimal_boot/bios/boot/syslinux/syslinux.cfg original_syslinux.cfg + cat < minimal_boot/bios/boot/syslinux/syslinux.cfg SERIAL 0 DEFAULT operatingsystem @@ -25,10 +27,11 @@ LABEL operatingsystem CEOF ./repackage.sh -qemu-system-x86_64 -m 256M -cdrom minimal_linux_live.iso -boot d -localtime -nographic & +qemu-system-x86_64 -m 128M -cdrom minimal_linux_live.iso -boot d -nographic & +QEMU_PID=$! sleep 5 -if [ "`ps -ef | grep -i [q]emu-system`" = "" ] ; then +if [ "`ps -ef | grep $QEMU_PID | grep -v grep | wc -l`" -eq 0 ] ; then echo "`date` | !!! FAILURE !!! Minimal Linux Live is not running in QEMU." exit 1 else @@ -38,17 +41,18 @@ fi RETRY=10 while [ ! "$RETRY" = "0" ] ; do echo "`date` | Countdown: $RETRY" - if [ "`ps -ef | grep -i [q]emu-system`" = "" ] ; then + if [ "`ps -ef | grep $QEMU_PID | grep -v grep | wc -l`" -eq 0 ] ; then break fi sleep 30 RETRY=$(($RETRY - 1)) done -if [ "`ps -ef | grep -i [q]emu-system`" = "" ] ; then +if [ "`ps -ef | grep $QEMU_PID | grep -v grep | wc -l`" -eq 0 ] ; then echo "`date` | Minimal Linux Live is not running in QEMU." else echo "`date` | !!! FAILURE !!! Minimal Linux Live is still running in QEMU." + kill $QEMU_PID exit 1 fi @@ -56,12 +60,16 @@ cat << CEOF ################################################################## # # - # QEMU test passed. Clean manually the affected MLL artifacts. # + # QEMU test passed. Cleaning up . # # # ################################################################## CEOF +rm -rf minimal_overlay/rootfs/etc/autorun/99_autoshutdown.sh + +cp -f original_syslinux.cfg minimal_boot/bios/boot/syslinux/syslinux.cfg + echo "`date` | *** MLL QEMU test - END ***" set +e diff --git a/src/update_config.sh b/src/update_config.sh new file mode 100755 index 00000000..5aaacf27 --- /dev/null +++ b/src/update_config.sh @@ -0,0 +1,95 @@ +#!/bin/sh + +cat > update_config.py << EOF +import sys +from pathlib import Path +import os + +if len(sys.argv) != 3: + print("ERROR: Missing arguments") + print("update_config.py usage:") + print("update_config.py config_file patches_file") + sys.exit() + +config_to_update = sys.argv[1] +patch = sys.argv[2] + +if not(Path(config_to_update).is_file()): + print(f"ERROR: File [{config_to_update}] not found!") + sys.exit() + +if not(Path(patch).is_file()): + print(f"ERROR: File [{patch}] not found!") + sys.exit() + +with open(config_to_update) as f: + config_list = f.readlines() +config_list = [x.strip() for x in config_list] + +with open(patch) as f: + patch_list = f.readlines() +patch_list = [x.strip() for x in patch_list] + +class Comment(): + def __init__(self, comment): + self.comment = comment + def __repr__(self): + return f"{self.comment}" +class NotSet(): + def __init__(self, key): + self.key = key + def __repr__(self): + return f"# {self.key} is not set" +class KeyValue(): + def __init__(self, key, value): + self.key = key + self.value = value + def __repr__(self): + return f"{self.key}={self.value}" + +def transformToObjects(line): + if line.startswith("# ") and line.endswith(" is not set"): + return NotSet(line[2:][:-11]) + elif not(line.startswith("#")) and "=" in line: + return KeyValue(line.split("=")[0], line.split("=")[1]) + else: + return Comment(line) + +config_objs=list(map(transformToObjects, config_list)) +patch_objs=set(map(transformToObjects, patch_list)) +results=[] +patch_objs_to_remove=[] + +for c in config_objs: + match c: + case Comment(): + results.append(c) + case NotSet(): + toAdd=c + for p in patch_objs: + if hasattr(p, 'key') and p.key == c.key: + toAdd=p + patch_objs_to_remove.append(p) + results.append(toAdd) + case KeyValue(): + toAdd=c + for p in patch_objs: + if hasattr(p, 'key') and p.key == c.key: + toAdd=p + patch_objs_to_remove.append(p) + results.append(toAdd) + +for r in patch_objs_to_remove: + patch_objs.remove(r) + +os.truncate(config_to_update, 0) + +with open(config_to_update, "a") as myfile: + for r in results: + myfile.write(f"{r.__repr__()}\n") + for p in patch_objs: + myfile.write(f"{p.__repr__()}\n") + +EOF + +python3 update_config.py $1 $2