Install the virtio driver

更新时间:
复制 MD 格式

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.

image

Procedure

Check kernel support for the virtio driver

  1. 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:

    virtio_driver

  2. Analyze the output.

    Check the values of the CONFIG_VIRTIO_BLK (virtio block device driver) and CONFIG_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.

  1. If both CONFIG_VIRTIO_BLK and CONFIG_VIRTIO_NET are 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

    image

    Debian series

    lsinitramfs /boot/initrd.img-$(uname -r)|grep  virtio

    image

    SUSE series

    lsinitrd /boot/initrd-$(uname -r)|grep virtio

    image

    • As shown in the preceding figures, if the output contains virtio_blk.ko and virtio_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.ko and virtio_net.ko or contains only one of virtio_blk.ko and virtio_net.ko, you need to proceed to the next step to add the virtio driver to the temporary file system.

  2. 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.

    1. Run the following command to open the dracut.conf file.

      vim /etc/dracut.conf
    2. 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.

      Note

      If one of the CONFIG_VIRTIO_BLK and CONFIG_VIRTIO_NET parameters is set to y and the other is set to m, add only the driver for the parameter that is set to m. For example, if the CONFIG_VIRTIO_NET parameter is set to m, add only add_drivers+="virtio_net".

    3. Run the following command to rebuild the initrd.

      dracut -f
      Note

      After 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

    1. Run the following command to open the modules file.

      vim /etc/initramfs-tools/modules
    2. Press i to enter Insert mode and add the following content.

      virtio_blk
      virtio_net

      After you add the content, press Esc, enter :wq, and then press Enter to save the file and exit.

      Note

      If one of the CONFIG_VIRTIO_BLK and CONFIG_VIRTIO_NET parameters is set to y and the other is set to m, add only the driver for the parameter that is set to m. For example, if the CONFIG_VIRTIO_NET parameter is set to m, add only virtio_net.

    3. Run the following command to rebuild the initrd.

      update-initramfs -u
      Note

      After 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 lsinitramfs /boot/initrd.img-$(uname -r)|grep virtio command to verify that the virtio driver is correctly added to the initramfs.

    SUSE series

    • For versions earlier than SUSE 12 SP1 or openSUSE 13

      1. Run the following command to open the kernel file.

        vim /etc/sysconfig/kernel
      2. 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.

        Note

        If one of the CONFIG_VIRTIO_BLK and CONFIG_VIRTIO_NET parameters is set to y and the other is set to m, add only the driver for the parameter that is set to m. For example, if the CONFIG_VIRTIO_NET parameter is set to m, add only INITRD_MODULES="virtio_net".

      3. Run the following command to rebuild the initrd.

        mkinitrd
    • For SUSE 12 SP1, openSUSE 13, or later versions

      1. Run the following command to open the dracut.conf file.

        vim /etc/dracut.conf
      2. 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.

        Note

        If one of the CONFIG_VIRTIO_BLK and CONFIG_VIRTIO_NET parameters is set to y and the other is set to m, add only the driver for the parameter that is set to m. For example, if the CONFIG_VIRTIO_NET parameter is set to m, add only add_drivers+="virtio_net".

      3. Run the following command to rebuild the initrd.

        dracut -f
    Note

    After 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

Note

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.

  1. Run the following command to install the required components for kernel compilation.

    yum install -y ncurses-devel gcc make wget
  2. 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

    kernel_version

  3. 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.

    download

  4. 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
  5. 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

  1. 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 menuconfig
    Note

    If 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
  2. When the following interface appears, configure the virtio-related options.

    Note

    An asterisk (*) indicates that the option is compiled into the kernel. An m indicates that the option is compiled as a module.

    1. Configure Virtualization options.

      1. Use the Spacebar to select Virtualization, and then press Enter to open the details list.Select_Virtualization

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

    2. Configure Processor type and features options.

      1. Return to the main menu, use the arrow keys to select Processor type and features, and then press Enter to open the details list.

      2. Use the Spacebar to select Paravirtualized guest support, and then press Enter to open the details list.guest_support

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

    3. Configure Device Drivers options.

      1. Return to the main menu, use the arrow keys to select Device Drivers, and then press Enter to open the details list.

      2. Use the Spacebar to select Block devices, and then press Enter to open the details list.

      3. Verify that the Virtio block driver option is selected.Virtio_block

      4. Return to the Device Drivers list, use the arrow keys to select Network device support, and then press Enter to open the details list.

      5. Verify that the Virtio network driver option is selected.Virtio_network

    4. Press the Esc key to exit the kernel configuration menu. When prompted, select Yes to save the .config file.

    5. Verify that the virtio-related options are correctly configured.

      For more information, see Check kernel support for the virtio driver.

    6. (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
    7. 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.builtin

      If either command returns a list of virtio-related files, such as virtio_blk, virtio_pci, and virtio_console, the virtio driver is installed correctly.确认结果