How can the kernel module script be automatically removed after logging into the operating system

I used the following two methods to log in to the operating system, but neither took effect. However, manually executing sudo sh kvm.sh can take effect. I don’t know where the mistake is.

ENV:Ubuntu budgie 24.04.02 + X11 + 6.14.0-29-generic + oracel virualbox7.1 + kvm4.1

script:

#!/bin/bash
# 
lsmod | grep kvm

# 
rmmod kvm_intel # Intel CPU,AMD CPU:  rmmod kvm_amd 
rmmod kvm

Method One

systemctl daemon-reload

[Unit]
Description=Disable KVM Kernel Modules

After=multi-user.target

Requires=systemd-modules-load.service

[Service]
User=root
ExecStart=/home/ubuntu/Desktop/kvm.sh
Restart=yes

Type=oneshot

RemainAfterExit=yes

[Install]
WantedBy=multi-user.target

Method Two:

sudo crontab -e

@reboot  /home/ubuntu/Desktop/kvm.sh

This isn’t an area I’m familiar with. However you shouldn’t have scripts in your local home folder since the home folder is subject to different permissions and often encrypted.

Put your scripts in something like /usr/local/bin and remember to give it root:root access with executable rights - probably 755 chmod. Test with 777 rights before reducing it to something less security vulnerable.

1 Like

I moved the script to /usr/local/bin, and reboot,but the same error occurred when starting the virtual machine. Only by manually executing the script can the virtual machine start.

ubuntu@master:~$ ls -lhtra /usr/local/bin | grep kvm
-rwxr-xr-x 1 root root  287  9月 14 22:06 kvm.sh
ubuntu@master:~$ 
ubuntu@master:~$ cat /etc/systemd/system/kvm.service 
[Unit]
Description=Disable KVM Kernel Modules

After=multi-user.target

Requires=systemd-modules-load.service

[Service]
User=root
# ExecStart=/home/ubuntu/Desktop/kvm.sh
ExecStart=/usr/local/bin/kvm.sh
Restart=yes

Type=oneshot

RemainAfterExit=yes

[Install]
WantedBy=multi-user.target

error:

VM Name: Win11

VD: Backend 'VBoxIsoMaker' does not support async I/O (VERR_NOT_SUPPORTED).
VirtualBox can't operate in VMX root mode. Please disable the KVM kernel extension, recompile your kernel and reboot (VERR_VMX_IN_VMX_ROOT_MODE).
Result Code:
NS_ERROR_FAILURE (0x80004005)
Component:
ConsoleWrap
Interface:
IConsole {6ac83d89-6ee7-4e33-8ae6-b257b2e81be8}

Hi @Tianyazi!

Why use a script?

Blacklisting the “kvm_intel” module (in your case) is reputed to prevent the incompatible module from loading at boot-time.

It also appears that this has solved the problem for several users.

Worth trying.

1 Like