Sfoglia il codice sorgente

First version of scripts

Yann Weber 1 mese fa
parent
commit
f604dddd17
7 ha cambiato i file con 564 aggiunte e 0 eliminazioni
  1. 155
    0
      Makefile
  2. 54
    0
      README
  3. 4
    0
      config.mk.inc
  4. 59
    0
      scripts/test_debvm.expect
  5. 49
    0
      scripts/test_libvirt.expect
  6. 208
    0
      scripts/testlib.expect
  7. 35
    0
      scripts/upgrade.sh

+ 155
- 0
Makefile Vedi File

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

+ 54
- 0
README Vedi File

@@ -0,0 +1,54 @@
1
+Linux VZ5 :
2
+===========
3
+
4
+Compile test & install kernel image for vz5's VM.
5
+
6
+Dependencies :
7
+==============
8
+
9
+- virsh
10
+- expect
11
+- `apt build-dep linux-image-amd64`
12
+- `apt install linux-source`
13
+
14
+Install & config :
15
+==================
16
+
17
+Copy sample config `cp config.mk.inc config.mk`
18
+Edit configuration file to meet your needs `editor config.mk`
19
+
20
+Kernel sources  :
21
+-----------------
22
+
23
+* Automatic method (debian based) :
24
+
25
+Run `make upgrade`
26
+Will fetch the last kernel source in /usr/src
27
+
28
+* Manual method :
29
+
30
+Decompress kernel sources and create a symlink `linux-source` pointing to it.
31
+
32
+Kernel config :
33
+---------------
34
+
35
+Kernel configuration are versionned in a separetd git repository in kernel-config/ directory.
36
+In order to seed config file with yours run :
37
+```
38
+mkdir kernel-config
39
+git -C kernel-config/ init
40
+cp /path/to/.config kernel-config/kernel.config
41
+git -C kernel-config add kernel.config
42
+git -C kernel-config commit -m 'Initial kernel config'
43
+```
44
+
45
+Usage :
46
+=======
47
+
48
+Compile a kernel `make kernel`
49
+
50
+Test a kernel on virsh vm `make test`
51
+
52
+Install kernel `make install`
53
+
54
+Commit tested kernel config `make commit-config`

+ 4
- 0
config.mk.inc Vedi File

@@ -0,0 +1,4 @@
1
+BZIMAGE_TEST=/srv/tftpd/boot/bzImageTest
2
+BZIMAGE_INSTALL=/srv/tftpd/boot/bzImage
3
+TEST_VM_NAME=test
4
+TEST_VM_HOSTNAME=test

+ 59
- 0
scripts/test_debvm.expect Vedi File

@@ -0,0 +1,59 @@
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 -no-user-config\
37
+	-netdev "user,id=net0,domainname=testvm"\
38
+	-device "virtio-net-pci,netdev=net0"\
39
+	-device "virtio-rng-pci,rng=rng0"\
40
+	-object "rng-random,filename=/dev/urandom,id=rng0"\
41
+	-drive "media=disk,format=raw,discard=unmap,file=$DISK_IMG_PATH,if=virtio,cache=unsafe"\
42
+	-drive "media=disk,format=raw,discard=unmap,file=$SWAP_PATH,if=virtio,cache=unsafe"\
43
+	-kernel $KERNEL_PATH -append "root=/dev/vda rw console=ttyS0,38400n8"\
44
+	-nographic -snapshot
45
+
46
+
47
+login
48
+
49
+test_base
50
+test_kernel_version $::VZ5::KVER
51
+test_swap
52
+test_ssh
53
+test_vda_rw
54
+test_apt
55
+test_reboot
56
+
57
+shutdown
58
+
59
+exit 0

+ 49
- 0
scripts/test_libvirt.expect Vedi File

@@ -0,0 +1,49 @@
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
+
10
+set where [file dirname [info script]]
11
+source [file join $where testlib.expect]
12
+
13
+
14
+proc VZ5::destroy {} {
15
+	system virsh destroy $::VMNAME
16
+	send_log "ERROR EXIT DESTROY\n"
17
+	system reset
18
+	exit 1
19
+}
20
+proc VZ5::abort {} {
21
+	set timeout 5
22
+	send_log "\nERROR ABORT\n"
23
+	send "\r"
24
+	expect "root@test:~#" {} timeout VZ5::destroy
25
+	send "shutdown -h now\r"
26
+	expect eof {} timeout VZ5::destroy
27
+	send_log "ERROR EXIT"
28
+	system reset
29
+	exit 1
30
+}
31
+
32
+spawn virsh start --console $VMNAME
33
+
34
+login
35
+
36
+test_base
37
+test_kernel_version $::VZ5::KVER
38
+test_swap
39
+test_ssh
40
+test_vz5_netconfig
41
+test_ping
42
+test_vda_rw
43
+test_apt
44
+test_reboot
45
+test_virsh_reboot
46
+
47
+shutdown
48
+
49
+exit 0

+ 208
- 0
scripts/testlib.expect Vedi File

@@ -0,0 +1,208 @@
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
+	send_tty "\033\[?7h\033\[?4h"
39
+}
40
+
41
+
42
+####################
43
+# Tests procedures #
44
+####################
45
+
46
+
47
+# Wait for login prompt, and login as root without password
48
+# Calls stty to inform VM of terminal size
49
+proc login {} {
50
+	send_log "\nPID : '[exp_pid]'\n"
51
+	if { [exp_pid] == 0 } { destroy }
52
+	set timeout 15
53
+	expect "Console: " { termconfig } timeout destroy
54
+	expect {
55
+		"$::VZ5::HOSTNAME login:" {}
56
+		"FAIL" {
57
+			expect "\r"
58
+			expect "\r" {
59
+				sleep 2;
60
+				expect "login:" {
61
+					send "root\r";
62
+					vm_stty;
63
+					debug_interact;
64
+				} timeout destroy
65
+			} timeout destroy
66
+		}
67
+		timeout destroy
68
+	}
69
+	set timeout 5
70
+	if {[catch {send "root\r"} err]} {
71
+		destroy
72
+	}
73
+	vm_stty
74
+}
75
+
76
+# Basics tests
77
+# - Checks date & time
78
+# - Check uname status code
79
+# - Checks /proc/cpuinfo
80
+# - Checks kernel clocksource kvm-clock support
81
+proc test_base {} {
82
+	set timeout 2
83
+	expect "[prompt]" { send "date -Im\r" } timeout abort
84
+	expect [system date -Im] {} timeout abort
85
+	expect "[prompt]" { send "date\r" } timeout abort
86
+	check_ret
87
+	expect "[prompt]" { send "uname -a\r" } timeout abort
88
+	check_ret
89
+	expect {
90
+		"[prompt]" { send "cat /proc/cpuinfo\r" } timeout abort
91
+		"processor " {} timeout abort
92
+	}
93
+	expect "[prompt]" { send "cat /sys/devices/system/clocksource/clocksource0/current_clocksource\r" } timeout abort
94
+	expect "kvm-clock" {} timeout abort
95
+}
96
+
97
+# Tests uname kernel version
98
+# Arguments :
99
+#  - version : expected kernel version
100
+proc test_kernel_version {version} {
101
+	set timeout 3
102
+	expect "[prompt]" { send "uname -r\r" } timeout abort
103
+	expect "$version-kvmguest" {} timeout abort
104
+}
105
+
106
+# Testing swap support
107
+proc test_swap {} {
108
+	set timeout 3
109
+	expect "[prompt]" { send "cat /proc/swaps | grep partition\r" } timeout abort
110
+	expect "/dev/vd*" {} timeout abort
111
+
112
+	expect {
113
+		"[prompt]" { send "free -h\r" } timeout abort
114
+		"Mem: " {} timeout abort
115
+		"Swap: " {} timeout abort
116
+	}
117
+
118
+}
119
+
120
+# Testing ssh daemon is running
121
+proc test_ssh {} {
122
+	set timeout 3
123
+	expect {
124
+		"[prompt]" { send "ps aux|grep sshd\r" }
125
+		expect "*/usr/sbin/sshd -D \[listener\] *"
126
+		timeout abort
127
+	}
128
+
129
+}
130
+
131
+# Check for vz5 network address configuration
132
+proc test_vz5_netconfig {} {
133
+	set timeout 3
134
+	expect {
135
+		"[prompt]" { send "ip a\r" }
136
+		"2: enp1s0: <BROADCAST,MULTICAST,UP,LOWER_UP>" {} timeout abort
137
+		"inet 192.168.242." {} timeout abort
138
+		"inet6 2001:41d0:203:10ae:1312::" {} timeout abort
139
+	}
140
+}
141
+
142
+# Testing that apt runs
143
+proc test_apt {} {
144
+	set timeout 30
145
+	expect "[prompt]" { send "apt update\r" } timeout abort
146
+	check_ret
147
+	expect "[prompt]" { send "apt -y dist-upgrade\r" } timeout abort
148
+	check_ret
149
+}
150
+
151
+# Testing ping
152
+proc test_ping {} {
153
+	set timeout 10
154
+	expect "[prompt]" { send "ping -c2 gnu.org\r" } timeout abort
155
+	check_ret
156
+	expect "[prompt]" { send "ping -4 -c2 gnu.org\r" } timeout abort
157
+	check_ret
158
+}
159
+
160
+# Testing that vda is mounted rw
161
+proc test_vda_rw {} {
162
+	set timeout 3
163
+	expect {
164
+		"[prompt]" { send "grep '^rw' /proc/fs/ext4/vda/options\r" } timeout abort
165
+		"rw" {} timeout abort
166
+	}
167
+}
168
+
169
+
170
+# Reboot using systemctl and reboot
171
+proc test_reboot {} {
172
+	expect "[prompt]" { send "# Rebooting in 2 seconds\r" }
173
+	set timeout 30
174
+	expect "[prompt]" { sleep 2; send "systemctl reboot\r" }
175
+	login
176
+	expect "[prompt]" { send "reboot\r" }
177
+	login
178
+}
179
+
180
+
181
+proc test_virsh_reboot {} {
182
+	system virsh reboot test
183
+	login
184
+}
185
+
186
+proc shutdown {} {
187
+	set timeout 2
188
+	expect "[prompt]" { send "shutdown -h now\r" }
189
+	set timeout 15
190
+	expect eof {} timeout destroy
191
+}
192
+
193
+
194
+
195
+# Return the expected prompt in order to expect it
196
+proc prompt {} {
197
+	return "root@${::VZ5::HOSTNAME}:~#"
198
+}
199
+
200
+# Check that a command return a 0 status code
201
+proc check_ret {} {
202
+	expect "[prompt]" {
203
+		send "echo \"ret='$?'\"\r";
204
+		expect -timeout 1 "ret='0'" {} timeout abort
205
+	} timeout abort
206
+}
207
+
208
+

+ 35
- 0
scripts/upgrade.sh Vedi File

@@ -0,0 +1,35 @@
1
+#!/bin/sh
2
+set -e
3
+
4
+src_tarball=$(ls /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" -a -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…
Annulla
Salva