Browse Source

First version of scripts

Yann Weber 4 weeks ago
parent
commit
6c971a658e
8 changed files with 870 additions and 0 deletions
  1. 161
    0
      Makefile
  2. 90
    0
      README
  3. 16
    0
      config.mk.inc
  4. 228
    0
      scripts/checkconfig.sh
  5. 60
    0
      scripts/test_debvm.expect
  6. 53
    0
      scripts/test_libvirt.expect
  7. 227
    0
      scripts/testlib.expect
  8. 35
    0
      scripts/upgrade.sh

+ 161
- 0
Makefile View File

@@ -0,0 +1,161 @@
1
+include config.mk
2
+
3
+SRC_DIR=linux-source
4
+VERSION=$(shell $(MAKE) -s -C $(SRC_DIR) kernelversion)
5
+BZIMAGE=archives/bzImage.$(VERSION)
6
+CONFIG_REPO=kernel-config
7
+KERNEL.CONFIG=$(shell realpath "$(CONFIG_REPO)/kernel.config")
8
+TESTED_HASH=.bzImage_tested.sha512
9
+CHECKCONFIG_SCRIPT=./scripts/checkconfig.sh
10
+VIRSH_TEST_SCRIPT=./scripts/test_libvirt.expect
11
+DEBVM_TEST_SCRIPT=./scripts/test_debvm.expect
12
+DEBVM_IMG_DIR=/tmp
13
+DEBVM_DISK=$(DEBVM_IMG_DIR)/vz5_kernel_test.ext4
14
+DEBVM_SWAPFILE=$(DEBVM_IMG_DIR)/vz5_kernel_test.swapfile
15
+DEBVM_SWAPLABEL=testswap
16
+UPGRADE_SCRIPT=./scripts/upgrade.sh
17
+HASH=sha512sum
18
+
19
+CRED=$(shell tput setaf 1)
20
+CGRN=$(shell tput setaf 2)
21
+CRAZ=$(shell tput sgr0)
22
+
23
+
24
+ARCHIVE=archives/install_$(shell date "+%Y_%m_%dT%H_%M_%S")_$(VERSION)
25
+
26
+all: status
27
+
28
+status:
29
+	@echo "Images :\n========\n";
30
+	@[ -f "bzImage.latest" ] && echo 'Latest    : $(shell [ -f "bzImage.latest" ] && file -L bzImage.latest | cut -d ',' -f2)' || echo "Latest : $(CRED)None built$(CRAZ)";
31
+	@[ -f "$(BZIMAGE_TEST)" ] && echo 'In test   : $(shell [ -f "$(BZIMAGE_TEST)" ] && file -L $(BZIMAGE_TEST) | cut -d ',' -f2)' || echo "In test : $(CRED)None in test$(CRAZ)";
32
+	@[ -f "$(BZIMAGE_INSTALL)" ] && echo 'Installed : $(shell [ -f "$(BZIMAGE_INSTALL)" ] && file -L $(BZIMAGE_INSTALL) | cut -d ',' -f2)' || echo "Installed : $(CRED)None installed$(CRAZ)";
33
+	@echo "\nLatest tested :\n----------";
34
+	@[ -f "bzImage.latest" ] && $(MAKE) -s tested || true;
35
+	@echo "\nSources :\n=========";
36
+	@sh $(UPGRADE_SCRIPT) check >/dev/null \
37
+		&& echo "$(CGRN)Kernel sources are up to date$(CRAZ)" \
38
+		|| echo "$(CRED)Kernel sources can be upgraded$(CRAZ)";
39
+	@echo "\nKernel config git repo :\n========================"
40
+	@git -C $(CONFIG_REPO) status
41
+
42
+
43
+$(SRC_DIR)/.config: $(KERNEL.CONFIG)
44
+	cp -v "$<" "$@"
45
+
46
+$(KERNEL.CONFIG): $(SRC_DIR)/.config
47
+	cp -v "$<" "$@"
48
+
49
+$(CONFIG_REPO):
50
+	mkdir $(CONFIG_REPO) && git -C $(CONFIG_REPO) init;
51
+
52
+menuconfig: $(SRC_DIR)/.config
53
+	$(MAKE) -C $(SRC_DIR) menuconfig
54
+	sh "$(CHECKCONFIG_SCRIPT)" -v "$(SRC_DIR)/.config"
55
+
56
+$(BZIMAGE): $(SRC_DIR)/.config
57
+	$(MAKE) -C $(SRC_DIR);
58
+	mkdir -p archives/;
59
+	cp "$(SRC_DIR)/arch/x86_64/boot/bzImage" "$@";
60
+	ln -vfs "$@" bzImage.latest
61
+	ls -lah "$@"
62
+
63
+upgrade_src:
64
+	sh $(UPGRADE_SCRIPT)
65
+
66
+can_upgrade:
67
+	@sh $(UPGRADE_SCRIPT) check || echo "$(CRED)Kernel sources can be ugraded$(CRAZ)";
68
+
69
+kernel: can_upgrade $(BZIMAGE)
70
+
71
+commit-config: tested $(KERNEL.CONFIG) can_upgrade checkconfig
72
+	cp -v "$(SRC_DIR)/.config" "$(KERNEL.CONFIG)";
73
+	git -C $(CONFIG_REPO) add "$(KERNEL.CONFIG)";
74
+	git -C $(CONFIG_REPO) commit -m "Update config for kernel $(VERSION)";
75
+	git -C $(CONFIG_REPO) tag -fa "$(VERSION)" -m "$(shell date -Is) Config for kernel $(VERSION)";
76
+
77
+archives/:
78
+	mkdir -p archives;
79
+
80
+archive:
81
+	mkdir -p $(ARCHIVE);
82
+	cp -v $(BZIMAGE) $(ARCHIVE)/bzImage.new;
83
+	cp -v $(BZIMAGE_INSTALL) $(ARCHIVE)/bzImage.old;
84
+
85
+
86
+clean:
87
+	-rm -v $(BZIMAGE) bzImage.latest "$(DEBVM_SWAPFILE)" "$(DEBVM_DISK)" "$(TESTED_HASH)";
88
+	$(MAKE) -C $(SRC_DIR) clean;
89
+
90
+distclean:
91
+	-rm -R "$(shell realpath $(SRC_DIR))" "$(SRC_DIR)"
92
+
93
+.PHONY: can_upgrade distclean clean $(BZIMAGE_TEST) $(BZIMAGE_INSTALL) _test tested install checkconfig
94
+
95
+checkconfig: $(KERNEL.CONFIG)
96
+	-sh $(CHECKCONFIG_SCRIPT) -v $(KERNEL.CONFIG)
97
+
98
+tested:
99
+	@$(HASH) -c "$(TESTED_HASH)" 2>/dev/null || {\
100
+		echo "$(CRED)$(BZIMAGE) is not tested.";\
101
+		echo "run make test as root.$(CRAZ)";\
102
+		false;\
103
+	};
104
+
105
+_test:
106
+	if $(MAKE) is_root >/dev/null 2>/dev/null; then \
107
+	     	$(MAKE) _virsh_test;\
108
+	else\
109
+		$(MAKE) _debvm_test;\
110
+	fi
111
+
112
+test: _test
113
+	$(HASH) "$(BZIMAGE)" > "$(TESTED_HASH)";
114
+
115
+# non-privileged user tests
116
+
117
+$(DEBVM_DISK): $(BZIMAGE)
118
+	@debvm-create -r stable -o "$@" -h "$(TEST_VM_HOSTNAME)" \
119
+		--skip autologin --skip kernel --skip systemdnetwork -- \
120
+		--customize-hook="echo \"LABEL=$(DEBVM_SWAPLABEL) none swap sw 0 0\" >> \$$1/etc/fstab"\
121
+		--customize-hook="mkdir -p \$$1/etc/network; echo \"auto enp0s2\niface enp0s2 inet dhcp\" > \$$1/etc/network/interfaces" \
122
+		--include="udev" \
123
+		--include="iputils-ping iproute2 ifupdown isc-dhcp-client" \
124
+		--include="ssh qemu-guest-agent tzdata initscripts ntpdate ca-certificates console-setup console-common console-data locales acpid vim screen bash-completion rsyslog" \
125
+		|| { rm "$@"; false;}
126
+
127
+$(DEBVM_SWAPFILE):
128
+	dd if=/dev/zero "of=$@" bs=4096 count=32768 status=progress &&\
129
+		chmod 0600 "$@" &&\
130
+		/usr/sbin/mkswap -L "$(DEBVM_SWAPLABEL)" "$@"
131
+
132
+_debvm_test: $(DEBVM_DISK) $(DEBVM_SWAPFILE)
133
+	@$(DEBVM_TEST_SCRIPT) "$(TEST_VM_HOSTNAME)" "$(VERSION)" "$(DEBVM_DISK)" "$(BZIMAGE)" "$(DEBVM_SWAPFILE)" "$(DEBVM_SWAPLABEL)" \
134
+		|| { echo "$(CRED)Kernel $(VERSION) [Error]$(CRAZ): Check /tmp/test_vz5_debvm_expect.log" >&2; false; } \
135
+		&& echo "$(CGRN)Kernel $(VERSION) [OK]$(CRAZ): console logs in /tmp/test_debvm_expect.log" >&2;
136
+
137
+# superuser designed targets
138
+
139
+is_root:
140
+	@[ "$(shell whoami)" = "root" ] || {\
141
+		echo "$(CRED)ERROR :$(CRAZ) You need to be root to run this target" >&2;\
142
+		false;\
143
+	};
144
+
145
+$(BZIMAGE_TEST): can_upgrade is_root
146
+	cp "$(BZIMAGE)" "$@"
147
+
148
+_virsh_test: $(BZIMAGE_TEST) can_upgrade is_root
149
+	@$(VIRSH_TEST_SCRIPT) "$(TEST_VM_HOSTNAME)" "$(VERSION)" "$(TEST_VM_NAME)" "$(TEST_VM_IFNAME)" "$(TEST_VM_IPV4)" "$(TEST_VM_IPV6)" || {\
150
+		echo "$(CRED)Kernel $(VERSION) [Error]$(CRAZ): Check /tmp/test_vz5_expect.log" >&2;\
151
+		false;\
152
+	} && echo "$(CGRN)Kernel $(VERSION) [OK]$(CRAZ): console logs in /tmp/test_vz5_expect.log" >&2;
153
+
154
+
155
+
156
+$(BZIMAGE_INSTALL): tested can_upgrade is_root
157
+	-cp -v $(BZIMAGE_INSTALL) $(BZIMAGE_INSTALL).old;
158
+	cp -v $(BZIMAGE) $(BZIMAGE_INSTALL);
159
+
160
+install: $(BZIMAGE_INSTALL) archive is_root
161
+

+ 90
- 0
README View File

@@ -0,0 +1,90 @@
1
+Linux VZ5 :
2
+===========
3
+
4
+Compile test & install kernel image for vz5's VM.
5
+
6
+Dependencies :
7
+==============
8
+
9
+- expect
10
+- qemu/kvm
11
+- `apt build-dep linux-image-amd64`
12
+- `apt install linux-source`
13
+
14
+Optionnal dependencies :
15
+------------------------
16
+- virsh
17
+- debvm
18
+
19
+Install & config :
20
+==================
21
+
22
+Copy sample config `cp config.mk.inc config.mk`
23
+Edit configuration file to meet your needs `editor config.mk`
24
+
25
+Then you will need kernel sources & kernel configuration.
26
+
27
+Kernel sources  :
28
+-----------------
29
+
30
+* Automatic method (debian based) :
31
+
32
+Run `make upgrade`
33
+Will fetch the last kernel source in /usr/src
34
+
35
+* Manual method :
36
+
37
+Decompress kernel sources and create a symlink `linux-source` pointing to it.
38
+
39
+Kernel config :
40
+---------------
41
+
42
+Kernel configuration are versionned in a separetd git repository in kernel-config/ directory.
43
+In order to seed config file with yours run (see Notes about kernel config) :
44
+```
45
+mkdir kernel-config
46
+git -C kernel-config/ init
47
+cp /path/to/.config kernel-config/kernel.config
48
+git -C kernel-config add kernel.config
49
+git -C kernel-config commit -m 'Initial kernel config'
50
+```
51
+
52
+Usage :
53
+=======
54
+
55
+Get current status with `make`
56
+
57
+Compile a kernel `make kernel`
58
+
59
+Test a kernel on virsh vm `make test`
60
+	If make test is run with root, we will try to start TEST_VM_NAME with virsh
61
+	If make test is run without privileges, a disk image will be built using debvm
62
+	and the tests will be run using qemu.
63
+	NOTE : the user needs access to /dev/kvm (adduser username kvm)
64
+
65
+Install kernel `make install`
66
+
67
+Commit tested kernel config `make commit-config`
68
+
69
+Notes :
70
+=======
71
+
72
+All scripts & tests are designed for monolitic kernel without initrd.
73
+
74
+Kernel config should contains :
75
+```
76
+# CONFIG_BLK_DEV_INITRD is not set
77
+# CONFIG_MODULES is not set
78
+```
79
+
80
+KVM has a documentation about kernel tuning for guest that can be found here :
81
+https://www.linux-kvm.org/page/Tuning_Kernel#Kernel_for_guest_with_paravirtualization
82
+
83
+The script `./scripts/checkconfig.sh` check that kernel config fits basic needs.
84
+
85
+You can use the script to generate a config file with most mandatory options set
86
+`./scripts/checkconfig.sh -g > linux-source/.config`
87
+and then run
88
+`make menuconfig`
89
+or to prompt for a y/n choice for each config
90
+`make -C linux-source config`

+ 16
- 0
config.mk.inc View File

@@ -0,0 +1,16 @@
1
+# Kernel installation path
2
+BZIMAGE_INSTALL=/srv/tftpd/boot/bzImage
3
+# Test VM hostname (for both virsh & debvm tests)
4
+TEST_VM_HOSTNAME=test
5
+
6
+# virsh test specific config
7
+
8
+# Test kernel installation path
9
+BZIMAGE_TEST=/srv/tftpd/boot/bzImageTest
10
+# virsh test vm name
11
+TEST_VM_NAME=test
12
+# test vm ifname
13
+TEST_VM_IFNAME=enp1s0
14
+# test vm ips
15
+TEST_VM_IPV4=10.0.2.42
16
+TEST_VM_IPV6=fe80::42

+ 228
- 0
scripts/checkconfig.sh View File

@@ -0,0 +1,228 @@
1
+#!/bin/sh
2
+set -e
3
+
4
+TEMP=$(getopt -s 'sh' -o 'vg' -l 'verbose,generate' -n 'checkconfig.sh' -- "$@")
5
+[ $? -ne 0 ] && exit 1
6
+
7
+eval set -- "$TEMP"
8
+unset TEMP
9
+
10
+verbose=0
11
+generate=0
12
+while true; do
13
+	case "$1" in
14
+		'-v'|'--verbose')
15
+			verbose=1
16
+			shift
17
+			continue
18
+		;;
19
+		'-g'|'--generate')
20
+			generate=1
21
+			shift
22
+			continue
23
+		;;
24
+		'--')
25
+			shift
26
+			break
27
+		;;
28
+		*)
29
+			echo 'getopt error...' >&2
30
+			exit 1
31
+		;;
32
+	esac
33
+done
34
+
35
+if [ "$generate" -eq "0" ] && [ "$#" -lt 1 ]
36
+then
37
+	echo "Usage : $0 [-v] [-g] configfile" 2>/dev/null
38
+	exit 1
39
+fi
40
+
41
+config=$1
42
+err=0
43
+
44
+if [ "$generate" -eq "0" ]
45
+then
46
+	conf=$(cat "$config")
47
+fi
48
+
49
+while read -r rule
50
+do
51
+	opt="$(echo "$rule" | cut -d ":" -f 1)"
52
+	comment="$(echo "$rule" | cut -d ":" -f 2-)"
53
+	if [ "$generate" -ne "0" ]
54
+	then
55
+		echo "$opt"
56
+		continue
57
+	fi
58
+	ok=0
59
+	if echo "$conf" | grep "$opt" >/dev/null
60
+	then
61
+		ok=1
62
+	else
63
+		if echo "$opt" | grep "^#" >/dev/null
64
+		then
65
+			if echo "$conf" | grep -v "$(echo "$opt" | sed -e 's/^# //' -e 's/ is not set.*$//')" >/dev/null
66
+			then
67
+				ok=1
68
+			fi
69
+		fi
70
+	fi
71
+	if [ "$ok" -eq 1 ]
72
+	then
73
+		if [ "$verbose" -ne 0 ]
74
+		then
75
+			printf "%-50s" "$opt"
76
+			echo " [$(tput setaf 2)OK$(tput sgr0)]"
77
+		fi
78
+	else
79
+		err=1
80
+		printf "%-50s" "$opt"
81
+		echo "[$(tput setaf 1)FAIL$(tput sgr0)] $comment"
82
+	fi
83
+
84
+done << __EOF__
85
+# CONFIG_BLK_DEV_INITRD is not set:Initrd should be disabled
86
+# CONFIG_MODULES is not set:Kernel should be monolitic (without module support)
87
+# CONFIG_VIRTUALIZATION is not set:Do you want to run guest on guest ?
88
+# CONFIG_VIRT_DRIVERS is not set:Do you want to run guest on guest ?
89
+# CONFIG_VHOST_MENU is not set:Do you want to run guest on guest ?
90
+CONFIG_HYPERVISOR_GUEST=y:Run as guest
91
+CONFIG_PREEMPT_NONE_BUILD=y:Disable preemption for server
92
+# CONFIG_PREEMPT_DYNAMIC is not set:Disable dynamic preemption
93
+CONFIG_PREEMPT_NONE=y:Disable preemption for server
94
+CONFIG_VIRTIO=y:Needed by VIRTIO_PCI and VIRTIO_BALOON
95
+CONFIG_VIRTIO_BALLOON=y:Support for hot memory amount change for KVM guest
96
+CONFIG_VIRTIO_BLK=y:The virtio block device driver
97
+CONFIG_VIRTIO_CONSOLE=y:Support for virtio serial console
98
+CONFIG_VIRTIO_INPUT=y:The virtio input (kbd, mice) driver
99
+CONFIG_VIRTIO_IOMMU=y:Virtio IOMMU support
100
+CONFIG_VIRTIO_MEM=y:The virtio memory driver
101
+CONFIG_VIRTIO_MENU=y:The virtio drivers
102
+CONFIG_VIRTIO_MMIO=y:Support for memory mapped virtio driver
103
+CONFIG_VIRTIO_NET=y:The virtio network driver
104
+CONFIG_VIRTIO_PCI=y:Support for virtio PCI devices
105
+CONFIG_VIRTIO_VDPA=y:Virtio data path acceleration support
106
+CONFIG_HW_RANDOM_VIRTIO=y:Virtio random number generator support
107
+CONFIG_VP_VDPA=y:Bridges virtio PCI to vDPA
108
+CONFIG_SCSI_VIRTIO=y:Virtual HBA driver for virtio
109
+CONFIG_PCI_MSI=y:Allows driver to enable Message Signaled Interrupts
110
+CONFIG_KVM_GUEST=y:Optimization for KVM guest
111
+CONFIG_PARAVIRT=y:Optimization for linux guest
112
+CONFIG_PARAVIRT_CLOCK=y:Optimization for linux guest
113
+CONFIG_PARAVIRT_SPINLOCKS=y:Optimization for linux spinlocks on guest
114
+CONFIG_MEMORY_HOTPLUG=y:Needed by VIRTIO_BALLOON
115
+CONFIG_MEMORY_HOTREMOVE=y:Needed by VIRTIO_BALLOON
116
+CONFIG_SERIAL_8250_CONSOLE=y:Needed by serial console
117
+CONFIG_TMPFS=y:Needed for various pseudo-filesystems
118
+CONFIG_DEVTMPFS=y:/dev pseudofilesystem
119
+CONFIG_EXT4_FS=y:Needed by tests with debvm
120
+CONFIG_SWAP=y:Swap support
121
+CONFIG_STANDALONE=y:Select only drivers that don't need compile-time external firmware
122
+CONFIG_PREVENT_FIRMWARE_BUILD=y:Disable drivers features which enable custom firmware building
123
+# CONFIG_FW_CFG_SYSFS is not set:Disable qemu firmware controlling interface
124
+# CONFIG_RPMSG_VIRTIO is not set:Not needed
125
+CONFIG_CGROUPS=y:Cgroups support
126
+CONFIG_UNIX=y:Unix socket support
127
+CONFIG_PACKET=y:Packet socket support
128
+CONFIG_CRYPTO_USER_API_RNG=y:Userspace interface for RNG
129
+CONFIG_PAGE_TABLE_CHECK=y:Security hardening
130
+CONFIG_SHUFFLE_PAGE_ALLOCATOR=y:Security hardening
131
+CONFIG_SECURITY_DMESG_RESTRICT=y:Security hardening
132
+CONFIG_SECURITY_PERF_EVENTS_RESTRICT=y:Security hardening
133
+CONFIG_HARDENED_USERCOPY=y:Security hardening
134
+CONFIG_FORTIFY_SOURCE=y:Security hardening
135
+CONFIG_STATIC_USERMODEHELPER=y:Security hardening
136
+CONFIG_STRICT_KERNEL_RWX=y:Security hardening
137
+# CONFIG_DEBUG_FS is not set:Security hardening
138
+CONFIG_STACKPROTECTOR=y:Security hardening
139
+CONFIG_STACKPROTECTOR_STRONG=y:Security hardening
140
+# CONFIG_DEVMEM is not set:Security hardening
141
+CONFIG_SCHED_STACK_END_CHECK=y:Security hardening
142
+CONFIG_HARDENED_USERCOPY=y:Security hardening
143
+CONFIG_VMAP_STACK=y:Security hardening
144
+CONFIG_FORTIFY_SOURCE=y:Security hardening
145
+# CONFIG_PROC_KCORE is not set:Security hardening
146
+CONFIG_SECURITY_DMESG_RESTRICT=y:Security hardening
147
+CONFIG_RETPOLINE=y:Security hardening
148
+CONFIG_LEGACY_VSYSCALL_NONE=y:Security hardening
149
+# CONFIG_LEGACY_VSYSCALL_XONLY is not set:Security hardening
150
+CONFIG_DEBUG_CREDENTIALS=y:Security hardening
151
+CONFIG_DEBUG_NOTIFIERS=y:Security hardening
152
+CONFIG_DEBUG_LIST=y:Security hardening
153
+CONFIG_DEBUG_SG=y:Security hardening
154
+CONFIG_BUG_ON_DATA_CORRUPTION=y:Security hardening
155
+CONFIG_SLAB_FREELIST_RANDOM=y:Security hardening
156
+CONFIG_SLUB=y:Security hardening
157
+CONFIG_SLAB_FREELIST_HARDENED=y:Security hardening
158
+# CONFIG_SLAB_MERGE_DEFAULT is not set:Security hardening
159
+CONFIG_PAGE_POISONING=y:Security hardening
160
+# CONFIG_COMPAT_BRK is not set:Security hardening
161
+CONFIG_BUG=y:Security hardening
162
+CONFIG_PANIC_ON_OOPS=y:Security hardening
163
+CONFIG_PANIC_TIMEOUT=-1:Security hardening
164
+CONFIG_SECURITY_YAMA=y:Security hardening
165
+CONFIG_SECCOMP=y:Security hardening
166
+CONFIG_SECCOMP_FILTER=y:Security hardening
167
+CONFIG_SYN_COOKIES=y:Security hardening
168
+# CONFIG_KEXEC is not set:Security hardening
169
+# CONFIG_HIBERNATION is not set:Security hardening
170
+# CONFIG_BINFMT_MISC is not set:Security hardening
171
+# CONFIG_LEGACY_PTYS is not set:Security hardening
172
+# CONFIG_ACPI_CUSTOM_METHOD is not set
173
+# CONFIG_COMPAT_VDSO is not set
174
+# CONFIG_LEGACY_VSYSCALL_EMULATE is not set
175
+# CONFIG_X86_VSYSCALL_EMULATION is not set
176
+# CONFIG_SECURITY_WRITABLE_HOOKS is not set
177
+CONFIG_DEFAULT_MMAP_MIN_ADDR=65536:Security hardening
178
+CONFIG_RANDOMIZE_BASE=y:Security hardening
179
+CONFIG_RANDOMIZE_MEMORY=y:Security hardening
180
+CONFIG_PAGE_TABLE_ISOLATION=y:Security hardening
181
+# CONFIG_IA32_EMULATION is not set:Security hardening
182
+# CONFIG_MODIFY_LDT_SYSCALL is not set:Security hardening
183
+CONFIG_GCC_PLUGINS=y:Security hardening
184
+CONFIG_GCC_PLUGIN_LATENT_ENTROPY=y:Security hardening
185
+CONFIG_GCC_PLUGIN_STACKLEAK=y:Security hardening
186
+CONFIG_RANDSTRUCT=y:Security hardening
187
+CONFIG_RANDSTRUCT_FULL=y:Security hardening
188
+__EOF__
189
+
190
+if [ "$generate" -ne 0 ]
191
+then
192
+	echo "\
193
+CONFIG_X86_PLATFORM_DEVICES=n
194
+CONFIG_IOMMU_SUPPORT=y
195
+# Needed by paravirt spinlocks
196
+CONFIG_SMP=y
197
+# Needed by virtio scsi
198
+CONFIG_SCSI=y
199
+CONFIG_SCSI_LOWLEVEL=y
200
+CONFIG_BLK_DEV=y
201
+# Needed by virtio net
202
+CONFIG_NET=y
203
+CONFIG_NETDEVICES=y
204
+CONFIG_NET_CORE=y
205
+CONFIG_INET=y
206
+# PCI support
207
+CONFIG_PCI=y
208
+#Needed by serial console
209
+CONFIG_SERIAL_8250=y
210
+# Needed by virtio rng
211
+CONFIG_HW_RANDOM=y
212
+# Needed by virtio vdpa
213
+CONFIG_VDPA=y
214
+# Security hardening dependencies
215
+CONFIG_SECURITY=y
216
+CONFIG_SLUB_DEBUG_ON=y
217
+CONFIG_ARCH_OPTIONAL_KERNEL_RWX=y
218
+CONFIG_ARCH_HAS_STRICT_KERNEL_RWX=y
219
+CONFIG_DEBUG_KERNEL=y
220
+# Security hardening
221
+CONFIG_RELOCATABLE=y
222
+CONFIG_MODIFY_LDT_SYSCALL=n
223
+CONFIG_EXPERT=y
224
+"
225
+fi
226
+
227
+
228
+exit $err

+ 60
- 0
scripts/test_debvm.expect View File

@@ -0,0 +1,60 @@
1
+#!/usr/bin/expect -f
2
+
3
+log_file -noappend "/tmp/test_debvm_expect.log"
4
+
5
+namespace eval VZ5 {
6
+	variable HOSTNAME "[lindex $argv 0]"
7
+	variable KVER "[lindex $argv 1]"
8
+}
9
+set DISK_IMG_PATH "[lindex $argv 2]"
10
+set KERNEL_PATH "[lindex $argv 3]"
11
+set SWAP_PATH "[lindex $argv 4]"
12
+set SWAP_UUID "[lindex $argv 5]"
13
+
14
+set where [file dirname [info script]]
15
+source [file join $where testlib.expect]
16
+
17
+proc VZ5::destroy {} {
18
+	send_log "\nERROR EXIT DESTROY\n"
19
+	system "kill [exp_pid]"
20
+	exit 1
21
+}
22
+
23
+proc VZ5::abort {} {
24
+	send_log "\nERROR ABORT\n"
25
+	set timeout 1
26
+	send "\r"
27
+	expect "[prompt]" { send "shutdown -h now\r"; } timeout destroy
28
+	set timeout 15
29
+	expect eof {} timeout destroy
30
+	exit 1
31
+}
32
+
33
+spawn qemu-system-x86_64 \
34
+	-name $::VZ5::HOSTNAME \
35
+	-m 1024\
36
+	-smp 8 -cpu host -machine type=q35,accel=kvm:tcg,kernel-irqchip=split -no-user-config\
37
+	-netdev "user,id=net0,domainname=testvm"\
38
+	-device "virtio-net-pci,netdev=net0,disable-legacy=on,iommu_platform=on"\
39
+	-device "virtio-rng-pci,rng=rng0"\
40
+	-device intel-iommu,intremap=on\
41
+	-object "rng-random,filename=/dev/urandom,id=rng0"\
42
+	-drive "media=disk,format=raw,discard=unmap,file=$DISK_IMG_PATH,if=virtio,cache=unsafe"\
43
+	-drive "media=disk,format=raw,discard=unmap,file=$SWAP_PATH,if=virtio,cache=unsafe"\
44
+	-kernel $KERNEL_PATH -append "root=/dev/vda rw console=ttyS0,38400n8"\
45
+	-nographic -snapshot
46
+
47
+
48
+login
49
+
50
+test_base
51
+test_kernel_version $::VZ5::KVER
52
+test_swap
53
+test_ssh
54
+test_vda_rw
55
+test_apt
56
+test_reboot
57
+
58
+shutdown
59
+
60
+exit 0

+ 53
- 0
scripts/test_libvirt.expect View File

@@ -0,0 +1,53 @@
1
+#!/usr/bin/expect -f
2
+
3
+log_file -noappend "/tmp/test_libvirt_expect.log"
4
+namespace eval VZ5 {
5
+	variable HOSTNAME "[lindex $argv 0]"
6
+	variable KVER "[lindex $argv 1]"
7
+}
8
+variable VMNAME "[lindex $argv 2]"
9
+variable VM_IFNAME "[lindex $argv 3]"
10
+variable VM_IPV4 "[lindex $argv 4]"
11
+variable VM_IPV6 "[lindex $argv 5]"
12
+
13
+set where [file dirname [info script]]
14
+source [file join $where testlib.expect]
15
+
16
+
17
+proc VZ5::destroy {} {
18
+	system virsh destroy $::VMNAME
19
+	send_log "ERROR EXIT DESTROY\n"
20
+	system reset
21
+	exit 1
22
+}
23
+proc VZ5::abort {} {
24
+	set timeout 5
25
+	send_log "\nERROR ABORT\n"
26
+	send "\r"
27
+	expect "root@test:~#" {} timeout VZ5::destroy
28
+	send "shutdown -h now\r"
29
+	expect eof {} timeout VZ5::destroy
30
+	send_log "ERROR EXIT"
31
+	system reset
32
+	exit 1
33
+}
34
+
35
+spawn virsh start --console $VMNAME
36
+
37
+login
38
+
39
+test_base
40
+test_kernel_version $::VZ5::KVER
41
+test_swap
42
+test_ssh
43
+test_netconfig_ipv4 "$VM_IFNAME" "$VM_IPV4"
44
+test_netconfig_ipv6 "$VM_IFNAME" "$VM_IPV6"
45
+test_ping
46
+test_vda_rw
47
+test_apt
48
+test_reboot
49
+test_virsh_reboot
50
+
51
+shutdown
52
+
53
+exit 0

+ 227
- 0
scripts/testlib.expect View File

@@ -0,0 +1,227 @@
1
+# Common VZ5 kernel test procedures
2
+# This file is designed to be sourced from expect script
3
+
4
+namespace eval VZ5 {
5
+	variable rows [stty rows]
6
+	variable cols [stty columns]
7
+}
8
+
9
+
10
+# Procedure called on timeout
11
+# The expect main script must define VZ5::abort ordering the VM to stop
12
+proc abort {} {
13
+	VZ5::abort
14
+}
15
+
16
+# Procedure called when abort fails to stop the VM
17
+# The expect main script must define VZ5::destroy
18
+proc destroy {} {
19
+	VZ5::destroy
20
+}
21
+
22
+# 
23
+proc debug_interact {} {
24
+	expect "[prompt]" {
25
+		send " echo H4sIAAAAAAACA+OSjjY2zFXGB7iUFaSjc/mlo01zXVydQt0VPP1CXIMcnUM8w1wVgl2Dgz39/aAqQEZxETbM2Cg3oCi1uFghuaQoRztRIb9IIS5WoSRfITUvRQFmCkRdSUaqQjFQaWZ+HlhCARUQsA3kKi4uALzdLXnkAAAA|base64 -d|gzip -d\r"
26
+	}
27
+	interact {
28
+		\001 { abort; }
29
+		\x1d { abort; }
30
+	}
31
+}
32
+
33
+proc vm_stty {} {
34
+	expect "[prompt]" {send "stty -brkint -imaxbel iutf8 rows $::VZ5::rows cols $::VZ5::cols\r"} timeout destroy
35
+}
36
+
37
+proc termconfig {} {
38
+	# tput smam : set autowrap
39
+	send_tty "\033\[?7h"
40
+	# set smooth scrolling
41
+	send_tty "\033\[?4h"
42
+	# tput sgr0 : turn off character attributes
43
+	send_tty "\033\[0m"
44
+}
45
+
46
+
47
+####################
48
+# Tests procedures #
49
+####################
50
+
51
+
52
+# Wait for login prompt, and login as root without password
53
+# Calls stty to inform VM of terminal size
54
+proc login {} {
55
+	send_log "\nPID : '[exp_pid]'\n"
56
+	if { [exp_pid] == 0 } { destroy }
57
+	set timeout 15
58
+	expect "Console: " { termconfig } timeout destroy
59
+	if {[catch {
60
+		expect {
61
+			"$::VZ5::HOSTNAME login:" {}
62
+			"FAIL" {
63
+				expect "\r"
64
+				expect "\r" {
65
+					sleep 2;
66
+					expect "login:" {
67
+						send "root\r";
68
+						vm_stty;
69
+						debug_interact;
70
+					} timeout destroy
71
+				} timeout destroy
72
+			}
73
+			timeout destroy
74
+		}
75
+	} err]} {
76
+		destroy
77
+	}
78
+	set timeout 5
79
+	if {[catch {send "root\r"} err]} {
80
+		destroy
81
+	}
82
+	termconfig
83
+	vm_stty
84
+}
85
+
86
+# Basics tests
87
+# - Checks date & time
88
+# - Check uname status code
89
+# - Checks /proc/cpuinfo
90
+# - Checks kernel clocksource kvm-clock support
91
+proc test_base {} {
92
+	set timeout 2
93
+	expect "[prompt]" { send "date -Im\r" } timeout abort
94
+	expect [system date -Im] {} timeout abort
95
+	expect "[prompt]" { send "date\r" } timeout abort
96
+	check_ret
97
+	expect "[prompt]" { send "uname -a\r" } timeout abort
98
+	check_ret
99
+	expect {
100
+		"[prompt]" { send "cat /proc/cpuinfo\r" } timeout abort
101
+		"processor " {} timeout abort
102
+	}
103
+	expect "[prompt]" { send "cat /sys/devices/system/clocksource/clocksource0/current_clocksource\r" } timeout abort
104
+	expect "kvm-clock" {} timeout abort
105
+	expect "[prompt]" { send "cat /sys/class/misc/hw_random/rng_available\r" } timeout abort
106
+	expect "virtio" {} timeout abort
107
+	expect "[prompt]" { send "cat /sys/class/misc/hw_random/rng_current\r" } timeout abort
108
+	expect "virtio" {} timeout abort
109
+}
110
+
111
+# Tests uname kernel version
112
+# Arguments :
113
+#  - version : expected kernel version
114
+proc test_kernel_version {version} {
115
+	set timeout 3
116
+	expect "[prompt]" { send "uname -r\r" } timeout abort
117
+	expect "$version" {} timeout abort
118
+}
119
+
120
+# Testing swap support
121
+proc test_swap {} {
122
+	set timeout 3
123
+	expect "[prompt]" { send "cat /proc/swaps | grep partition\r" } timeout abort
124
+	expect "/dev/vd*" {} timeout abort
125
+
126
+	expect {
127
+		"[prompt]" { send "free -h\r" } timeout abort
128
+		"Mem: " {} timeout abort
129
+		"Swap: " {} timeout abort
130
+	}
131
+
132
+}
133
+
134
+# Testing ssh daemon is running
135
+proc test_ssh {} {
136
+	set timeout 3
137
+	expect {
138
+		"[prompt]" { send "ps aux|grep sshd\r" }
139
+		expect "*/usr/sbin/sshd -D \[listener\] *"
140
+		timeout abort
141
+	}
142
+
143
+}
144
+
145
+# Check that given ifname has given IP (prefix, 10.0.2. will match 10.0.2.42)
146
+proc test_netconfig_ipv4 {ifname ip_prefix} {
147
+	set timeout 2
148
+	expect "[prompt]" { send "ip addr show $ifname\r" } timeout abort
149
+	expect ": $ifname: <BROADCAST,MULTICAST,UP,LOWER_UP>" {} timeout abort
150
+	expect "inet $ip_prefix" {} timeout abort
151
+}
152
+
153
+# Check that given ifname has given Ip (prefix, fe80:: will match fe80::1312)
154
+proc test_netconfig_ipv6 {ifname ip6_prefix} {
155
+	set timeout 5
156
+	expect "[prompt]" { send "ip a show $ifname\r" } timeout abort
157
+	expect ": $ifname: <BROADCAST,MULTICAST,UP,LOWER_UP>" {} timeout abort
158
+	expect "inet6 $ip6_prefix" {} timeout abort
159
+}
160
+
161
+# Testing that apt runs
162
+proc test_apt {} {
163
+	set timeout 30
164
+	expect "[prompt]" { send "apt update\r" } timeout abort
165
+	check_ret
166
+	expect "[prompt]" { send "apt -y dist-upgrade\r" } timeout abort
167
+	check_ret
168
+}
169
+
170
+# Testing ping
171
+proc test_ping {} {
172
+	set timeout 10
173
+	expect "[prompt]" { send "ping -c2 gnu.org\r" } timeout abort
174
+	check_ret
175
+	expect "[prompt]" { send "ping -4 -c2 gnu.org\r" } timeout abort
176
+	check_ret
177
+}
178
+
179
+# Testing that vda is mounted rw
180
+proc test_vda_rw {} {
181
+	set timeout 3
182
+	expect {
183
+		"[prompt]" { send "grep '^rw' /proc/fs/ext4/vda/options\r" } timeout abort
184
+		"rw" {} timeout abort
185
+	}
186
+}
187
+
188
+
189
+# Reboot using systemctl and reboot
190
+proc test_reboot {} {
191
+	expect "[prompt]" { send "# Rebooting in 2 seconds\r" }
192
+	set timeout 30
193
+	expect "[prompt]" { sleep 2; send "systemctl reboot\r" }
194
+	login
195
+	expect "[prompt]" { send "reboot\r" }
196
+	login
197
+}
198
+
199
+
200
+proc test_virsh_reboot {} {
201
+	system virsh reboot test
202
+	login
203
+}
204
+
205
+proc shutdown {} {
206
+	set timeout 2
207
+	expect "[prompt]" { send "shutdown -h now\r" }
208
+	set timeout 15
209
+	expect eof {} timeout destroy
210
+}
211
+
212
+
213
+
214
+# Return the expected prompt in order to expect it
215
+proc prompt {} {
216
+	return "root@${::VZ5::HOSTNAME}:~#"
217
+}
218
+
219
+# Check that a command return a 0 status code
220
+proc check_ret {} {
221
+	expect "[prompt]" {
222
+		send "echo \"ret='$?'\"\r";
223
+		expect -timeout 1 "ret='0'" {} timeout abort
224
+	} timeout abort
225
+}
226
+
227
+

+ 35
- 0
scripts/upgrade.sh View File

@@ -0,0 +1,35 @@
1
+#!/bin/sh
2
+set -e
3
+
4
+src_tarball=$(find /usr/src/linux-source-* |sort -V | tail  -n1)
5
+version=$(basename "$src_tarball" |sed -e 's/^linux-source-//' -e 's/\.tar\.xz//')
6
+tarball_hash=.debian_src_tarball.sha512
7
+
8
+src_dir=$(realpath linux-source)
9
+
10
+if [ -d "$src_dir" ] && [ -L "linux-source" ]
11
+then
12
+	if sha512sum -c "$tarball_hash"
13
+	then
14
+		exit 0
15
+	fi
16
+
17
+	if [ "$1" = "check" ]
18
+	then
19
+		exit 1
20
+	fi
21
+
22
+	make commit-config || true
23
+	rm -Rf "$src_dir" || true
24
+	rm "linux-source" || true
25
+
26
+elif [ "$1" = "check" ]
27
+then
28
+	exit 1
29
+fi
30
+
31
+tar -xvf "$src_tarball"
32
+
33
+ln -vs "linux-source-${version}" "linux-source"
34
+
35
+sha512sum "$src_tarball" > "$tarball_hash"

Loading…
Cancel
Save