This topic describes how to use VirtualBox to create a virtual machine, install a Linux operating system on the virtual machine, and then generate an image file in the VHD format.

Prerequisites

  • VirtualBox is installed. If VirtualBox is not installed, click VirtualBox to download VirtualBox.
  • An ISO image file is prepared. Example: CentOS-7-x86_64-Minimal-2009.iso. You can visit the Alibaba Cloud Official Image Site to download ISO image files.
  • The network connection is stable.

Background information

The procedure on how to install an operating system may vary based on the operating system. In this topic, VirtualBox 7.0.2 is used to create a virtual machine and install CentOS 7.9. You can perform the following steps to create a Linux image file:
  1. Step 1: Create a virtual machine
  2. Step 2: Install a Linux operating system

Step 1: Create a virtual machine

  1. Start Oracle VM VirtualBox Manager and click New to create a virtual machine.
  2. In the Create Virtual Machine wizard, configure parameters and click Create.
    • In the Name field, enter a name for the virtual machine. Example: centos VM.
    • Set the Machine Folder, Type, and Version parameters.
    • In the Memory size section, select a memory size based on the configurations of the virtual machine that you want to create and the requirements of the operating system that you want to install. The default memory size is 1,024 MB. In this example, 1,024 MB is used.
    • Select Create a virtual hard disk now for Hard disk.
  3. In the Create Virtual Hard Disk wizard, configure parameters and click Create.
    • Specify a hard disk size and a directory in which to store files. In this example, 20 GB is specified as the hard disk size.
    • Select VHD (Virtual Hard Disk) for Hard disk file type.
      Note Only image files in the RAW, VHD, and QCOW2 formats can be imported to Elastic Compute Service (ECS). If you select another format here, you must convert the created image to a supported format before you can import it to ECS. For more information, see Convert the format of an image.
    • Select Dynamically allocated for Storage on physical hard disk.

Step 2: Install a Linux operating system

Method 1: Install a Linux operating system in BIOS mode

  1. Start Oracle VM VirtualBox Manager and select the new virtual machine. Example: centos VM.
  2. Click Settings.
  3. In the Settings window, select Storage in the left-side navigation pane. In the storage device list, select Empty. Then, in the Attributes section on the right side, click the Disk icon and select Choose a disk file. Select the CentOS-7-x86_64-Minimal-2009.iso file and click OK.
  4. Select the new virtual machine and click Start to start the virtual machine.
  5. Go to the operating system installation page and install the operating system as instructed.
    1. Select a system language and click Continue.
    2. On the INSTALLATION SUMMAR page, click INSTALLATION DESTINATION.
    3. On the INSTALLATION DESTINATION page, select Local Standard Disks for Device Selection, select I will configure partitioning for Partitioning, and then click Done.
    4. On the MANUAL PARTITIONING page, click the Create icon icon, set the Mount Point, Device Type, and File System parameters, and then click Done.
      Important The system disk can have only the root partition. If the system disk has other partitions, the instance cannot be started or the disk cannot be resized. Therefore, you must mount the root partition at last.
      1. Mount the /boot/efi partition, select Standard Partition for Device Type, and then select BIOS Boot for File System.
      2. Mount the / root partition, select Standard Partition for Device Type, and then select ext4 for File System.
    5. On the INSTALLATION SUMMARY page, click NETWORK & HOST NAME.
    6. On the NETWORK & HOST NAME page, select Ethernet, click the On button on the right side, and then click Done.
    7. On the INSTALLATION SUMMARY page, click Begin Installation.
    8. After the operating system is installed, set a password for the root user.
      You must use the password for authentication when you log on to the virtual machine. Keep the password confidential.
  6. After the operating system is installed, log on to the virtual machine and run the following command to make sure that the root partition is mounted at last. If the root partition is not mounted at last, the instance cannot start or the disk cannot be resized.
     lsblk
    A command output similar to the following one is displayed. Check whether the partitions are correct

Method 2: Install a Windows operating system in Unified Extensible Firmware Interface (UEFI) mode

  1. Start Oracle VM VirtualBox Manager and select the new virtual machine. Example: centos VM.
  2. Click Settings.
  3. In the Settings window, select Storage in the left-side navigation pane. In the storage device list, select Empty. Then, in the Attributes section on the right side, click the Disk icon and select Choose a disk file. Select the CentOS-7-x86_64-Minimal-2009.iso file and click OK.
  4. Click Settings.
  5. In the Settings window, select System in the left-side navigation pane. On the Motherboard tab, select Enable EFI (special OSes only) for Extended Features and click OK.
  6. Select the new virtual machine and click Start to start the virtual machine.
  7. Go to the operating system installation page and install the operating system as instructed.
    For more information, see Step 5 in the "Method 1: Install a Windows operating system in BIOS mode" section.
    When you configure partitions, take note of the following items:
    • The system disk can have only the root partition. If the system disk has other partitions, the instance cannot be started or the disk cannot be resized. Therefore, you must mount the root partition at last.
    • You must first mount the /boot/efi partition, select Standard Partition for Device Type, and then select EFI System Partition for File System.
  8. After the operating system is installed, log on to the virtual machine and run the following command to make sure that the root partition is mounted at last. If the root partition is not mounted at last, the instance cannot start or the disk cannot be resized.
     lsblk
    A command output similar to the following one is displayed. Check whether the partitions are correct