diff --git a/qemu-kernel.go b/qemu-kernel.go index 68628b2..8f839df 100644 --- a/qemu-kernel.go +++ b/qemu-kernel.go @@ -280,6 +280,16 @@ func (q *QemuSystem) CopyFile(user, localPath, remotePath string) (err error) { return } +func (q *QemuSystem) CopyAndInsmod(localKoPath string) (output string, err error) { + remoteKoPath := fmt.Sprintf("/tmp/module_%d.ko", rand.Int()) + err = q.CopyFile("root", localKoPath, remoteKoPath) + if err != nil { + return + } + + return q.Command("root", "insmod "+remoteKoPath) +} + // CopyAndRun is copy local file to qemu vm then run it func (q *QemuSystem) CopyAndRun(user, path string) (output string, err error) { remotePath := fmt.Sprintf("/tmp/executable_%d", rand.Int()) diff --git a/qemu-kernel_test.go b/qemu-kernel_test.go index 5c9b4bb..81c3bca 100644 --- a/qemu-kernel_test.go +++ b/qemu-kernel_test.go @@ -213,3 +213,30 @@ func TestQemuSystemCopyAndRun(t *testing.T) { t.Fatal("Wrong output from copyied executable (" + output + "," + randStr + ")") } } + +func TestQemuSystemCopyAndInsmod(t *testing.T) { + qemu, err := startTestQemu(t) + if err != nil { + t.Fatal(err) + } + defer qemu.Stop() + + lsmodBefore, err := qemu.Command("root", "lsmod | wc -l") + if err != nil { + t.Fatal(err) + } + + _, err = qemu.CopyAndInsmod(testConfigSampleKo) + if err != nil { + t.Fatal(err) + } + + lsmodAfter, err := qemu.Command("root", "lsmod | wc -l") + if err != nil { + t.Fatal(err) + } + + if lsmodBefore == lsmodAfter { + t.Fatal("insmod returns ok but there is no new kernel modules") + } +} diff --git a/test.config.go b/test.config.go index 69f0f66..a49693d 100644 --- a/test.config.go +++ b/test.config.go @@ -7,3 +7,4 @@ package qemukernel const testConfigVmlinuz = "tools/qemu-debian-img/vmlinuz-bionic" const testConfigInitrd = "tools/qemu-debian-img/initrd-bionic" const testConfigRootfs = "tools/qemu-debian-img/bionic.img" +const testConfigSampleKo = "tools/qemu-debian-img/sample.ko" diff --git a/tools/qemu-debian-img/.gitignore b/tools/qemu-debian-img/.gitignore index d7d027b..53d6d13 100644 --- a/tools/qemu-debian-img/.gitignore +++ b/tools/qemu-debian-img/.gitignore @@ -2,3 +2,4 @@ *.log vmlinuz* initrd* +*.ko \ No newline at end of file diff --git a/tools/qemu-debian-img/Vagrantfile b/tools/qemu-debian-img/Vagrantfile index 7e84d1e..0942f46 100644 --- a/tools/qemu-debian-img/Vagrantfile +++ b/tools/qemu-debian-img/Vagrantfile @@ -14,5 +14,6 @@ Vagrant.configure("2") do |config| cp bionic.img /vagrant/ cp /boot/vmlinuz-* /vagrant/vmlinuz-bionic cp /boot/initrd-* /vagrant/vmlinuz-bionic + cp /lib/modules/`uname -r`/kernel/lib/test_static_key_base.ko /vagrant/sample.ko SHELL end