Skip to contentSkip to conversation

x86: Build ISO images from x86/*.elf

PATCH Superseded Yi Sun 2 participants 3 messages Jun 6, 2021 – Jun 8, 2021 KVM

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Yi Sun

author
#0 lore raw

PATCH 1/2 x86: Build ISO images from x86/*.elf

Make use of tool grub-mkresure to wrap x86/*.elf in ISO images. VMM could load test cases just like a CD-ROM, which could extend usage of those cases.

Refine Makefile to clean *.iso when running 'make clean'.

lib/grub/grub.cfg new file +7 -0
new file mode 100644
index 0000000..b287cf4
@@ -0,0 +1,7 @@
+set timeout=0
+set default=0
+
+menuentry "my os" {
+    multiboot /boot/kernel.bin
+    boot
+}
x86/Makefile.common +17 -1
index 52bb7aa..62eea51 100644
@@ -50,6 +50,22 @@ FLATLIBS = lib/libcflat.a
 	$(OBJCOPY) -O elf32-i386 $^ $@
 	@chmod a-x $@
 
+grub_cfg := lib/grub/grub.cfg
+
+elf_files := $(wildcard ./x86/*.elf)
+iso_files := $(patsubst %.elf,%.iso,$(elf_files))
+
+%.iso: %.elf
+	@echo "Creating ISO for case: $(notdir $<)"
+	@rm -rf build/isofiles
+	@mkdir -p build/isofiles/boot/grub
+	@cp $< build/isofiles/boot/kernel.bin
+	@cp $(grub_cfg) build/isofiles/boot/grub
+	@grub-mkrescue -o $@ build/isofiles 2> /dev/null
+
+iso: $(iso_files)
+	echo "All ISO created successfully!"
+
 tests-common = $(TEST_DIR)/vmexit.flat $(TEST_DIR)/tsc.flat \
                $(TEST_DIR)/smptest.flat  \
                $(TEST_DIR)/realmode.flat $(TEST_DIR)/msr.flat \
@@ -81,5 +97,5 @@ $(TEST_DIR)/hyperv_stimer.elf: $(TEST_DIR)/hyperv.o
 $(TEST_DIR)/hyperv_connections.elf: $(TEST_DIR)/hyperv.o
 
 arch_clean:
-	$(RM) $(TEST_DIR)/*.o $(TEST_DIR)/*.flat $(TEST_DIR)/*.elf \
+	$(RM) $(TEST_DIR)/*.o $(TEST_DIR)/*.flat $(TEST_DIR)/*.elf $(TEST_DIR)/*.iso \
 	$(TEST_DIR)/.*.d lib/x86/.*.d \
Signature · 1 line
2.27.0
author
#1 lore raw

PATCH v2 2/2 x86: Create ISO images according to unittests.cfg

Create ISO image according to the configure file unittests.cfg, where describes the parameters of each test case.

The grub.cfg in ios/boot/grub/ looks as following, taking case 'vmx_apicv_test' as an example:

set timeout=0
set default=0

menuentry "vmx.elf" {
multiboot /boot/vmx.elf apic_reg_virt_test virt_x2apic_mode_test
module /boot/module
boot
}

[1] All parameters are from '-append xxx' in the unittests.cfg.
[2] The file 'module' is a fixed file that can be passed by paramters of script ./create_iso.sh.

x86/create_iso.sh new file +83 -0
new file mode 100755
index 0000000..e55ee9e
@@ -0,0 +1,83 @@
+#!/bin/bash
+set -e
+config_file=$1
+module_file=$2
+
+opts=
+extra_params=" "
+kernel=
+smp=
+testname=
+
+grub_cfg() {
+	local kernel_elf=$1
+	local kernel_para=$2
+	local module_file=$3
+
+	if [[ "${module_file}" != "" ]]; then
+		module_cmd="module   /boot/${module_file}"
+	fi
+
+	cat << EOF
+set timeout=0
+set default=0
+
+menuentry "${kernel_elf}" {
+    multiboot /boot/${kernel_elf} ${kernel_para}
+    ${module_cmd}
+    boot
+}
+EOF
+}
+
+create_iso() {
+	local case_name=$1
+	local kernel_elf=$2
+	local kernel_params=$3
+	local module_file=$4
+
+	if [ -f $kernel_elf ]; then
+		rm -rf build/isofiles
+		mkdir -p build/isofiles/boot/grub
+
+		cp $kernel_elf build/isofiles/boot/
+		[[ -f $module_file ]] && cp $module_file build/isofiles/boot/
+
+		grub_cfg ${kernel_elf##*/} "${kernel_params}" ${module_file##*/} > build/isofiles/boot/grub/grub.cfg
+
+		rm -rf ${testname}.iso
+		grub-mkrescue -o ${case_name}.iso build/isofiles 2> /dev/null
+		[ $? == 0 ] && echo "Creating ISO for case: ${case_name}"
+	fi
+}
+
+nline=`wc $config_file | cut -d' ' -f 2`
+
+while read -r line; do
+	if [[ "$line" =~ ^\[(.*)\]$ || $nline == 1 ]]; then
+		rematch=${BASH_REMATCH[1]}
+		if [[ "${testname}" != "" ]]; then
+			create_iso $testname ${kernel}.elf "${extra_params}" $module_file
+		fi
+		testname=$rematch
+		extra_params=" "
+		smp=1
+		kernel=""
+		opts=""
+		groups=""
+	elif [[ $line =~ ^file\ *=\ *(.*)\.flat$ ]]; then
+		kernel=${BASH_REMATCH[1]}
+	elif [[ $line =~ ^smp\ *=\ *(.*)$ ]]; then
+		smp=${BASH_REMATCH[1]}
+	elif [[ $line =~ ^extra_params\ *=\ *(.*)$ ]]; then
+		opts=${BASH_REMATCH[1]}
+		if [[ "$opts" =~ .*append\ (.*)$ ]]; then
+			extra_params=${BASH_REMATCH[1]}
+			extra_params=`echo $extra_params | sed 's/\"//g'`
+		fi
+	elif [[ $line =~ ^groups\ *=\ *(.*)$ ]]; then
+		groups=${BASH_REMATCH[1]}
+	fi
+	(( nline -= 1))
+
+done < $config_file
Signature · 1 line
2.27.0
#2 lore raw

Re: [kvm-unit-tests PATCH 2/2 v2] x86: Create ISO images according to unittests.cfg

Quoted 18 lines · Yi Sun

Create ISO image according to the configure file unittests.cfg, where describes the parameters of each test case.

The grub.cfg in ios/boot/grub/ looks as following, taking case 'vmx_apicv_test' as an example:

set timeout=0
set default=0

menuentry "vmx.elf" {
multiboot /boot/vmx.elf apic_reg_virt_test virt_x2apic_mode_test
module /boot/module
boot
}

Quoted 5 lines

[1] All parameters are from '-append xxx' in the unittests.cfg.
[2] The file 'module' is a fixed file that can be passed by paramters of script ./create_iso.sh.

Thanks Yi!

I gave it a spin and it works nicely.

One minor issue that I think worth fixing is silent failures. Initially, the script failed silently since I did not have the xorriso package installed on my Ubuntu (the error message "grub-mkrescue: error: xorriso not found.” was suppressed). It would be nicer to print some error message in such case.

Two general notes regarding patch submission:

1. Please include a cover-letter (using git’s “--cover-letter” option), which would include general description of what the patch-set does. In addition, track changes between the versions.

2. Please use the same version for all patches. You can use git “-v” option for this matter.

Thanks again,
Nadav