Help Center/ Image Management Service/ Best Practices/ Creating a Linux Image Using VirtualBox and an ISO File/ Configuring the VM/ Installing Drivers and Changing the Disk Identifiers to the UUID Format
Updated on 2023-04-27 GMT+08:00

Installing Drivers and Changing the Disk Identifiers to the UUID Format

To ensure that the ECSs created from the image support both Xen and KVM virtualization, install Native Xen and KVM drivers and change the disk identifiers to the UUID format for the VM which is used as the image source.

This section describes how to perform these operations on a Linux VM that runs Ubuntu 20.04. For other OSs, see Optimization Process (Linux).

Install Native Xen and KVM Drivers

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

    vi /etc/initramfs-tools/modules

  2. Press i to enter the editing mode and add the native Xen (xen-pv) and KVM (virtio) drivers to the /etc/initramfs-tools/modules file (the format depends on the OS requirements).
    [root@CTU10000xxxxx ~]#vi /etc/initramfs-tools/modules 
    ...
    # Examples: 
    # 
    # raid1 
    # sd_mOd 
    xen-blkfront 
    xen-netfront 
    virtio_blk 
    virtio_scsi 
    virtio_net 
    virtio_pci 
    virtio_ring 
    virtio  
  3. Press Esc, enter :wq, and press Enter to save the settings and exit the vi editor.
  4. Run the following command to generate initrd again:

    update-initramfs -u

  5. Run the following commands to check whether native Xen and KVM drivers have been installed:

    lsinitramfs /boot/initrd.img-`uname -r` |grep xen

    lsinitramfs /boot/initrd.img-`uname -r` |grep virtio

    [root@ CTU10000xxxxx home]# lsinitramfs /boot/initrd.img-`uname -r` |grep xen 
    lib/modules/3.5.0-23-generic/kernel/drivers/net/ethernet/qlogic/netxen 
    lib/modules/3.5.0-23-generic/kernel/drivers/net/ethernet/qlogic/netxen/netxen_nic.ko 
    lib/modules/3.5.0-23-generic/kernel/drivers/net/xen-netback 
    lib/modules/3.5.0-23-generic/kernel/drivers/net/xen-netback/xen-netback.ko 
    lib/modules/3.5.0-23-generic/kernel/drivers/block/xen-blkback 
    lib/modules/3.5.0-23-generic/kernel/drivers/block/xen-blkback/xen-blkback.ko 
     
    [root@ CTU10000xxxxx home]# lsinitramfs /boot/initrd.img-`uname -r` |grep virtio 
    lib/modules/3.5.0-23-generic/kernel/drivers/scsi/virtio_scsi.ko

    If you add built-in drivers to the initrd or initramfs file, the VM will not be affected. This makes it easy to modify the drivers. However, the drivers cannot be shown by running the lsinitrd command. You can run the following commands to check whether the drivers are built-in ones in the kernel:

    [root@ CTU10000xxxxx home]# cat /boot/config-`uname -r` | grep CONFIG_VIRTIO | grep y
    CONFIG_VIRTIO_BLK=y
    CONFIG_VIRTIO_NET=y
    CONFIG_VIRTIO=y
    CONFIG_VIRTIO_RING=y
    CONFIG_VIRTIO_PCI=y
    CONFIG_VIRTIO_MMIO_CMDLINE_DEVICES=y
    [root@ CTU10000xxxxx home]# cat /boot/config-`uname -r` | grep CONFIG_XEN | grep y
    CONFIG_XEN_BLKDEV_FRONTEND=y
    CONFIG_XEN_NETDEV_FRONTEND=y

Change the Disk Identifier in the GRUB Configuration File to the UUID Format

Take Ubuntu 20.04 as an example. Run blkid to obtain the UUID of the root partition. Modify the /boot/grub/grub.cfg file and use the UUID of the root partition to configure the boot item. If the root partition already uses UUID, no modification is required. The procedure is as follows:
  1. Log in to the newly created VM as user root.
  2. Run the following command to query all types of mounted file systems and their device UUIDs:

    blkid

    The following information is displayed:

    /dev/xvda1: UUID="ec51d860-34bf-4374-ad46-a0c3e337fd34" TYPE="ext3"
    /dev/xvda5: UUID="7a44a9ce-9281-4740-b95f-c8de33ae5c11" TYPE="swap"
  1. Run the following command to query the grub.cfg file:

    cat /boot/grub/grub.cfg

    The following information is displayed:

    ......menuentry 'Ubuntu Linux, with Linux 3.13.0-24-generic' --class ubuntu --class gnu-linux --class gnu --class os --unrestricted $menuentry_id_option 'gnulinux-3.13.0-24-generic-advanced-ec51d860-34bf-4374-ad46-a0c3e337fd34' {
    recordfail
    load_video
    gfxmode $linux_gfx_mode
    insmod gzio
    insmod part_msdos
    insmod ext2
    if [ x$feature_platform_search_hint = xy ]; then
    search --no-floppy --fs-uuid --set=root ec51d860-34bf-4374-ad46-a0c3e337fd34
    else
    search --no-floppy --fs-uuid --set=root ec51d860-34bf-4374-ad46-a0c3e337fd34
    fi
    echo 'Loading Linux 3.13.0-24-generic ...'
    linux /boot/vmlinuz-3.13.0-24-generic root=/dev/xvda1 ro 
    echo 'Loading initial ramdisk ...'
    initrd /boot/initrd.img-3.13.0-24-generic 
    }
  1. Check whether the /boot/grub/grub.cfg configuration file contains root=/dev/xvda1 or root=UUID=ec51d860-34bf-4374-ad46-a0c3e337fd34.
    • If root=UUID=ec51d860-34bf-4374-ad46-a0c3e337fd34 is contained, the root partition is in the UUID format and no further action is required.
    • If root=/dev/xvda1 is contained, the root partition is represented by a device name. Go to step 5.
  1. Obtain the UUID of the root partition based on root=/dev/xvda1 and information obtained by running the blkid command.
  2. Run the following command to open the grub.cfg file:

    vi /boot/grub/grub.cfg

  3. Press i to enter the editing mode. Change the identifier of the root partition to the UUID format. For example, change root=/dev/xvda1 to root=UUID=ec51d860-34bf-4374-ad46-a0c3e337fd34.
  4. Press Esc, enter :wq, and press Enter to save the settings and exit the vi editor.
  5. Run the following command to verify the change:

    cat /boot/grub/grub.cfg

    The change is successful if information similar to the following is displayed:

    ......menuentry 'Ubuntu Linux, with Linux 3.13.0-24-generic' --class ubuntu --class gnu-linux --class gnu --class os --unrestricted $menuentry_id_option 'gnulinux-3.13.0-24-generic-advanced-ec51d860-34bf-4374-ad46-a0c3e337fd34' {
    recordfail
    load_video
    gfxmode $linux_gfx_mode
    insmod gzio
    insmod part_msdos
    insmod ext2
    if [ x$feature_platform_search_hint = xy ]; then
    search --no-floppy --fs-uuid --set=root ec51d860-34bf-4374-ad46-a0c3e337fd34
    else
    search --no-floppy --fs-uuid --set=root ec51d860-34bf-4374-ad46-a0c3e337fd34
    fi
    echo 'Loading Linux 3.13.0-24-generic ...'
    linux /boot/vmlinuz-3.13.0-24-generic root=UUID=ec51d860-34bf-4374-ad46-a0c3e337fd34 ro
    echo 'Loading initial ramdisk ...'
    initrd /boot/initrd.img-3.13.0-24-generic
    }

Change the Disk Identifiers in the fstab File to the UUID Format

Take Ubuntu 20.04 as an example. Run blkid to obtain the UUIDs of all partitions. Modify the /etc/fstab file and use the partition UUIDs to configure automatic partition mounting.

  1. Run the following command to query all types of mounted file systems and their device UUIDs:

    blkid

    The following information is displayed:

    /dev/xvda2: UUID="4eb40294-4c6f-4384-bbb6-b8795bbb1130" TYPE="xfs"  
    /dev/xvda1: UUID="2de37c6b-2648-43b4-a4f5-40162154e135" TYPE="swap"
  2. Run the following command to query the fstab file:

    cat /etc/fstab

    The following information is displayed:

    [root@CTU1000028010 ~]# cat /etc/fstab  
    /dev/xvda2  /       xfs     defaults    0 0 
    /dev/xvda1  swap    swap    defaults    0 0     
  3. Check whether the disk identifiers in the fstab file are device names or UUIDs.
    • If they are UUIDs, no further action is required.
    • If they are device names, go to step 4.
  4. Run the following command to open the fstab file:

    vi /etc/fstab

  5. Press i to enter the editing mode and change the disk identifiers to the UUID format.
  6. Press Esc, enter :wq, and press Enter to save the settings and exit the vi editor.