The virtio driver is a high-performance I/O driver that is essential for virtualized environments. To ensure that your custom Linux image can boot properly on Alibaba Cloud, you must install the virtio driver and include it in the initial RAM filesystem (initramfs). Otherwise, Elastic Compute Service (ECS) instances created from your image may fail to start because they cannot recognize the virtualized devices. This topic shows you how to install the virtio driver on the virtual machine that you use to create a custom Linux image.
Workflow
The following figure shows the workflow for installing the virtio driver on a Linux system.
Procedure
Check kernel support for the virtio driver
-
On the virtual machine that you use to create the image, run the following command to check if the current kernel supports the virtio driver. The result determines whether you need to install the driver or add it to the initramfs.
grep -i virtio /boot/config-$(uname -r)The command returns output similar to the following:

-
Analyze the output.
Check the values of the
CONFIG_VIRTIO_BLK(virtio block device driver) andCONFIG_VIRTIO_NET(virtio network device driver) parameters in the output:-
Both parameters are set to y: The virtio driver is already installed and compiled into the kernel. It loads automatically at boot. You can skip the rest of this topic.
-
Both parameters are set to m, or one is set to y and the other to m: The virtio driver is available as a kernel module. You need to proceed to add the virtio driver to the initramfs.
-
These parameters are not present: The virtio driver is not installed. You must manually install the virtio driver.
-
Add the virtio driver to the initramfs
If the kernel is configured with CONFIG_VIRTIO_BLK=m or CONFIG_VIRTIO_NET=m but these modules are not included in the initramfs, the system may fail to load the virtio driver during boot. As a result, the virtual machine cannot detect the virtio devices.
-
If both
CONFIG_VIRTIO_BLKandCONFIG_VIRTIO_NETare set to m, or one is set to y and the other to m, run the command for your operating system to check if the virtio driver is included in the initramfs.CentOS series
lsinitrd /boot/initramfs-$(uname -r).img | grep virtio
Debian series
lsinitramfs /boot/initrd.img-$(uname -r)|grep virtio
SUSE series
lsinitrd /boot/initrd-$(uname -r)|grep virtio
-
As shown in the preceding figures, if the output contains
virtio_blk.koandvirtio_net.ko, the virtio driver is already included in the initramfs. You can skip the rest of this topic. -
If the output does not contain
virtio_blk.koandvirtio_net.koor contains only one ofvirtio_blk.koandvirtio_net.ko, you need to proceed to the next step to add the virtio driver to the temporary file system.
-
-
Add the virtio driver to the initramfs.
CentOS series
For CentOS 6, Anolis OS 7, AlmaLinux 8, Fedora 33, or later versions with a kernel version later than 2.6.24 (which you can check by running
uname -r), follow these steps to add the virtio driver to the initramfs.-
Run the following command to open the dracut.conf file.
vim /etc/dracut.conf -
Press i to enter Insert mode and add the following line.
add_drivers+="virtio_blk virtio_net"After you add the line, press Esc, enter :wq, and then press Enter to save the file and exit.
NoteIf one of the
CONFIG_VIRTIO_BLKandCONFIG_VIRTIO_NETparameters is set toyand the other is set tom, add only the driver for the parameter that is set tom. For example, if theCONFIG_VIRTIO_NETparameter is set tom, add onlyadd_drivers+="virtio_net". -
Run the following command to rebuild the initrd.
dracut -fNoteAfter the command runs, run the
echo $?command to verify it. A return value of 0 indicates success and that the initrd is rebuilt. You can also run the following command to verify that the virtio driver is correctly added to the initramfs.lsinitrd /boot/initramfs-$(uname -r).img | grep virtio
Debian series
-
Run the following command to open the modules file.
vim /etc/initramfs-tools/modules -
Press i to enter Insert mode and add the following content.
virtio_blk virtio_netAfter you add the content, press Esc, enter :wq, and then press Enter to save the file and exit.
NoteIf one of the
CONFIG_VIRTIO_BLKandCONFIG_VIRTIO_NETparameters is set toyand the other is set tom, add only the driver for the parameter that is set tom. For example, if theCONFIG_VIRTIO_NETparameter is set tom, add onlyvirtio_net. -
Run the following command to rebuild the initrd.
update-initramfs -uNoteAfter the command runs, run the
echo $?command to verify it. A return value of 0 indicates success and that the initrd is rebuilt. You can also run thelsinitramfs /boot/initrd.img-$(uname -r)|grep virtiocommand to verify that the virtio driver is correctly added to the initramfs.
SUSE series
-
For versions earlier than SUSE 12 SP1 or openSUSE 13
-
Run the following command to open the kernel file.
vim /etc/sysconfig/kernel -
Press i to enter Insert mode and add the following line.
INITRD_MODULES="virtio_blk virtio_net"After you add the line, press Esc, enter :wq, and then press Enter to save the file and exit.
NoteIf one of the
CONFIG_VIRTIO_BLKandCONFIG_VIRTIO_NETparameters is set toyand the other is set tom, add only the driver for the parameter that is set tom. For example, if theCONFIG_VIRTIO_NETparameter is set tom, add onlyINITRD_MODULES="virtio_net". -
Run the following command to rebuild the initrd.
mkinitrd
-
-
For SUSE 12 SP1, openSUSE 13, or later versions
-
Run the following command to open the dracut.conf file.
vim /etc/dracut.conf -
Press i to enter Insert mode and add the following line.
add_drivers+="virtio_blk virtio_net"After you add the line, press Esc, enter :wq, and then press Enter to save the file and exit.
NoteIf one of the
CONFIG_VIRTIO_BLKandCONFIG_VIRTIO_NETparameters is set toyand the other is set tom, add only the driver for the parameter that is set tom. For example, if theCONFIG_VIRTIO_NETparameter is set tom, add onlyadd_drivers+="virtio_net". -
Run the following command to rebuild the initrd.
dracut -f
-
NoteAfter the command runs, run the
echo $?command to verify it. A return value of 0 indicates success and that the initrd is rebuilt. You can also run the following command to verify that the virtio driver is correctly added to the initramfs.lsinitrd /boot/initrd-$(uname -r)|grep virtio -
Manually install the virtio driver
Step 1: Download the kernel package
This section uses the download of the linux-4.4.24.tar.gz kernel package on a CentOS operating system as an example. Replace the kernel version with your operating system's kernel version.
-
Run the following command to install the required components for kernel compilation.
yum install -y ncurses-devel gcc make wget -
Run the following command to check the current kernel version of your system. In this example, the version is 4.4.24-2.al7.x86_64.
uname -r
-
Go to the Linux kernel list page to find the download URL for your kernel version source code.
In this example, the download URL for linux-4.4.24.tar.gz is
https://www.kernel.org/pub/linux/kernel/v4.x/linux-4.4.24.tar.gz.
-
Run the following commands to download the installation package.
cd /usr/src/ wget https://www.kernel.org/pub/linux/kernel/v4.x/linux-4.4.24.tar.gz -
Run the following commands to extract the installation package and create a symbolic link.
tar -xzf linux-4.4.24.tar.gz ln -s linux-4.4.24 linux cd /usr/src/linux
Step 2: Compile the kernel
-
Run the following commands in sequence to compile the kernel.
make mrproper symvers_path=$(find /usr/src/ -name "Module.symvers") test -f $symvers_path && cp $symvers_path . cp /boot/config-$(uname -r) ./.config make menuconfigNoteIf an error occurs when you run make menuconfig, fix the issue based on the error message. For example, if the ncurses-devel, bison, or flex components are missing, run the following commands in sequence to install them.
yum install ncurses-devel yum install bison yum install flex -
When the following interface appears, configure the virtio-related options.
NoteAn asterisk (*) indicates that the option is compiled into the kernel. An m indicates that the option is compiled as a module.
-
Configure Virtualization options.
-
Use the Spacebar to select Virtualization, and then press Enter to open the details list.

-
Verify that the KVM (Kernel-based Virtual Machine) support option is selected.

-
-
Configure Processor type and features options.
-
Return to the main menu, use the arrow keys to select Processor type and features, and then press Enter to open the details list.
-
Use the Spacebar to select Paravirtualized guest support, and then press Enter to open the details list.

-
Verify that the KVM paravirtualized clock and KVM Guest support options are selected.

-
-
Configure Device Drivers options.
-
Return to the main menu, use the arrow keys to select Device Drivers, and then press Enter to open the details list.
-
Use the Spacebar to select Block devices, and then press Enter to open the details list.
-
Verify that the Virtio block driver option is selected.

-
Return to the Device Drivers list, use the arrow keys to select Network device support, and then press Enter to open the details list.
-
Verify that the Virtio network driver option is selected.

-
-
Press the Esc key to exit the kernel configuration menu. When prompted, select Yes to save the .config file.
-
Verify that the virtio-related options are correctly configured.
For more information, see Check kernel support for the virtio driver.
-
(Optional) If the virtio-related options are not set, run the following commands in sequence to configure, compile, and install the kernel.
make oldconfig make prepare make scripts make make install -
Run the following commands to verify the virtio driver installation.
find /lib/modules/"$(uname -r)"/ -name "virtio*" | grep -E "virtio*" grep -E "virtio*" < /lib/modules/"$(uname -r)"/modules.builtinIf either command returns a list of virtio-related files, such as virtio_blk, virtio_pci, and virtio_console, the virtio driver is installed correctly.

-








