Skip to main content

Creating a Linux Image Using VirtualBox and an ISO File

VirtualBox is free, open-source virtualization software. It was first offered by InnoTek GmbH from Germany and re-branded as Oracle VM VirtualBox when InnoTek was acquired by Oracle Corporation.

For more information about VirtualBox, visit the Oracle official website. Click here to see the guest OSs that can work with VirtualBox.

You can use a 32-bit or 64-bit Linux guest OS provided by VirtualBox to create an image file in VHD format. The following figure shows how to use VirtualBox to create an image from an ISO file.

Figure 1 Image creation process

Installing VirtualBox​

Prerequisites​

The host where VirtualBox is to be installed must meet the following requirements:

  • A 64-bit Windows OS (recommended).
  • At least 4 GB of memory and a dual-core processor. For example, the host specifications can be 8U16G.
  • At least 20 GB of available disk space.
  • Hardware virtualization (Intel VT-x or AMD-V). For how to enable this, see Configuring Host BIOS CPU Settings.
note

For details about how to install VirtualBox, see the VirtualBox user guide at
https://www.virtualbox.org/manual/UserManual.html.

Configuring Host BIOS CPU Settings​

For an Intel host, perform the following operations to enable hardware virtualization:

warning

The operations may differ depending on the CPU type. You can do it as prompted.

  1. During the host startup, press the BIOS key set by the manufacturer to access the BIOS.

  2. Choose Configuration -> Intel Virtual Technology, and press Enter.

  3. Select Enabled and press Enter. The value of Intel Virtual Technology will become Enabled.

  4. Press F10 to save the settings and exit.

    Figure 1 Enabling hardware virtualization

Installing VirtualBox Binaries​

  1. Download the VirtualBox installation package. VirtualBox-5.2.0 is used as an example.

    Download it from https://www.virtualbox.org/wiki/Downloads.

  2. Decompress the package. Right-click VirtualBox-5.2.0-118431-Win.exe, choose Run as administrator, and click Next.

    Figure 2 Installing VirtualBox

  3. Select the VirtualBox installation path and click Next.

    Figure 3 Selecting an installation path

  4. Personalize the settings and click Next.

    Figure 4 Personalized settings

  5. Click Finish.

Creating a VM and Installing an OS​

Creating an Empty VM​

  1. Open VirtualBox and click New. In the displayed Create Virtual Machine dialog box, enter a VM name, select an OS type and version, and click Next.

    Take Ubuntu as an example. The type must be Linux. Ensure that the selected version is the same as that of the OS you want to install on the VM.

    Figure 1 Creating a VM

  2. In the Memory size dialog box, set a value and click Next.

    You can reference the VM specifications or official OS requirements. The minimum value is 256 MB. You can set the memory size to 512 MB as an example.

    Figure 2 Setting the memory size

  3. In the Hard disk dialog box, select Create a virtual hard disk now and click Create.

    Figure 3 Creating a virtual hard disk

  4. In the Hard disk file type dialog box, select VHD and click Next.

    Figure 4 Setting the hard disk file type

  5. In the Storage on physical hard disk dialog box, select Dynamically allocated and click Next.

    Figure 5 Selecting the disk allocation mode

  6. In the File location and size dialog box, set the disk size and storage location.

    For example, you can set the disk size to 20 GB.

    Figure 6 Setting the disk location and size

  7. Click Create.

Installing a Linux OS on the VM​

The procedure varies depending on the image file you use. This section uses Ubuntu 20.04 as an example to describe how to install a Linux OS on the VM.

note

Make sure you have obtained the ISO image file of your target OS, for example, Ubuntu-20.04-server.iso.

Use the ISO file to install Linux for the empty VM.

  1. In VirtualBox Manager, select the new VM and click Settings.

    Figure 1 Setting the VM

  2. Choose Storage -> Empty, click in the Attributes area, and select the ISO image file Ubuntu-20.04-server.iso.

    Figure 2 Selecting the ISO file to be mounted

    Figure 3 Mounted ISO file

  3. Click OK.

  4. In VirtualBox Manager, select the new VM and click Start.

    Figure 4 Starting the VM

  5. Install the OS.

    1. Select English and press Enter.

    2. Select Continue without updating.

    3. Retain the default settings for the keyboard. Select Done

    4. Retain the default settings for the installation base. Select Done.

    5. Retain the default settings for the network. Select Done.

      The installation program will automatically identify the IP address. If the network cannot be found, the installation program can still continue and you can configure the network again after the installation is complete.

    6. Retain the default settings for the proxy. Select Done.

    7. Retain the default settings for the software source. Select Done.

    8. Retain the default settings for disk partitioning (use an entire disk and set up this disk as an LVM group). Select Done.

      The file system information will be displayed. Check it and select Done.

      Confirm the destructive action and select Continue.

    9. Configure the server name, username, and password. Select Done.

      Your name: It is not a username for logging in to the server. You can consider it as server description.

      Your Server's name: It is a unique server name on the same network. The name cannot contain uppercase letters.

      Pick a username: It is a username for logging in to the server. If you forget it or its password, you will not be allowed to log in to the server.

      Choose a password: It is the password for logging in to the server.

      Confirm your password: Enter your password again.

    10. Install SSH so that you can remotely connect to the Linux server.

      Select Install OpenSSH server. Then, press TAB to select Done.

    11. Select Done to start the OS installation.

    12. After the installation is complete, select Reboot to restart the system.

Configuring the VM​

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

  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
    note

    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

Changing 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"
  3. 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
    }
  4. 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.
  5. Obtain the UUID of the root partition based on root=/dev/xvda1 and information obtained by running the blkid command.

  6. Run the following command to open the grub.cfg file:

    vi /boot/grub/grub.cfg
  7. 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.

  8. Press ESC, enter :wq, and press Enter to save the settings and exit the vi editor.

  9. 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
    }

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

Installing Cloud-Init​

note

For more information on cloud-init check this link.

Configuring Cloud-Init​

note

For more information on cloud-init check this link.

Configuring NetworkManager​

Linux allows you to use NetworkManager to automatically configure the VM network. You are advised to use NetworkManager for new OS versions.

Alternatively, you can use the native network management service of the OS.

  1. Run the following command to install NetworkManager:

    yum install NetworkManager
  2. Delete ifcfg-eth1 to ifcfg-eth11 from the /etc/sysconfig/network-scripts/ directory and retain only ifcfg-eth0.

  3. Run the following command to disable the network:

    service network stop
  4. Run the following command to disable automatic startup of the network:

    chkconfig network off
  5. Run the following commands to restart messagebus and NetworkManager and enable NetworkManager to start automatically at startup:

    service messagebus restart
    service NetworkManager restart
    chkconfig NetworkManager on

Obtaining the Image File​

After the VM is configured, perform the following operations to generate and export a Linux image file:

  1. Open VirtualBox, select the VM, choose Settings -> Storage, and select Linux.vhd.
  2. On the right pane, view the image file location.
  3. Go to the location to obtain the generated Linux.vhd image file.

Registering the Image File as a Private Image​

Upload the image file to an OBS bucket and register it as a private image.

important
  • Only an unencrypted image file or an image file encrypted using SSE-KMS can be uploaded to an OBS bucket.
  • When uploading an image file, you must select an OBS bucket with the storage class of Standard.
  1. Use OBS Browser+ to upload the image file. For details, see OBS Browser+ Best Practices.

    For how to download OBS Browser+, see https://docs.otc.t-systems.com/object-storage-service/tool-guide/downloading_obs_browser.html.

  2. Register the external image file as a private image. For details, see Registering an Image File as a Private Image (Linux).