Create Linux Image

Last Updated At: 2025-11-26 15:01:22

Overview

This document guides you to create a Linux image.

Directions

Preparations

When you export a system disk image, you need to check the following:

Note:

If you export via a data disk image, you can skip this operation.

Check OS Partition and Starting Mode

  1. Execute the following command to check whether the OS partition is a GPT partition.

    sudo parted -l /dev/sda | grep 'Partition Table'
    
    • If the returned result is msdos, it is an MBR partition, then proceed to the next step.
    • If the returned result is gpt, it is a GPT partition. Currently, GPT partitions are not supported for service migration.
  2. Execute the following command to check whether the operating system is started in EFI mode.

    sudo ls /sys/firmware/efi
    
    • If the file exists, the current operating system is started in EFI mode.
    • If the file does not exist, proceed to the next step.

Check System Key Files

The key system files that need to be checked include but are not limited to the following:

Note:

Follow the standards of the relevant distribution to ensure that the location and permissions of key system files are correct and can be read and written normally.

  • /etc/grub/grub.cfg: It is recommended to use uuid to mount root in kernel parameters. Other methods (such as root=/dev/sda) may cause the system to fail to start.
  • /etc/fstab: Do not mount other hard disks. After migration, the system may fail to start due to disk loss.
  • /etc/shadow: The permissions are normal and can be read and written.

Uninstall Software

Uninstall drivers and software that may cause conflicts (including VMware tools, Xen tools, Virtualbox GuestAdditions, and some software that comes with its own underlying drivers).

Check Virtio Driver

For details, see Check Virtio Driver in Linux System.

Install cloud-init

For installation details, see Install cloud-init on Linux System.

Check Other Hardware-related Configurations

Hardware changes after migration to the cloud include but may not be limited to:

  • The graphics card is replaced with Cirrus VGA.
  • The disk is replaced with Virtio Disk, and the device names are vda and vdb.
  • The network interface is replaced with Virtio Nic, which only provides eth0 by default.

Find Partitions and Sizes

Execute the following command to view the partition format of the current operating system and determine the partition to be copied and its size.

mount

Take the following returned results as an example:

proc on /proc type proc (rw,nosuid,nodev,noexec,relatime)
sys on /sys type sysfs (rw,nosuid,nodev,noexec,relatime)
dev on /dev type devtmpfs (rw,nosuid,relatime,size=4080220k,nr_inodes=1020055,mode=755)
run on /run type tmpfs (rw,nosuid,nodev,relatime,mode=755)
/dev/sda1 on / type ext4 (rw,relatime,data=ordered)
securityfs on /sys/kernel/security type securityfs (rw,nosuid,nodev,noexec,relatime)
tmpfs on /dev/shm type tmpfs (rw,nosuid,nodev)
devpts on /dev/pts type devpts (rw,nosuid,noexec,relatime,gid=5,mode=620,ptmxmode=000)
tmpfs on /sys/fs/cgroup type tmpfs (ro,nosuid,nodev,noexec,mode=755)
cgroup on /sys/fs/cgroup/unified type cgroup2 (rw,nosuid,nodev,noexec,relatime,nsdelegate)
cgroup on /sys/fs/cgroup/systemd type cgroup (rw,nosuid,nodev,noexec,relatime,xattr,name=systemd)
pstore on /sys/fs/pstore type pstore (rw,nosuid,nodev,noexec,relatime)
cgroup on /sys/fs/cgroup/cpu,cpuacct type cgroup (rw,nosuid,nodev,noexec,relatime,cpu,cpuacct)
cgroup on /sys/fs/cgroup/cpuset type cgroup (rw,nosuid,nodev,noexec,relatime,cpuset)
cgroup on /sys/fs/cgroup/rdma type cgroup (rw,nosuid,nodev,noexec,relatime,rdma)
cgroup on /sys/fs/cgroup/blkio type cgroup (rw,nosuid,nodev,noexec,relatime,blkio)
cgroup on /sys/fs/cgroup/hugetlb type cgroup (rw,nosuid,nodev,noexec,relatime,hugetlb)
cgroup on /sys/fs/cgroup/memory type cgroup (rw,nosuid,nodev,noexec,relatime,memory)
cgroup on /sys/fs/cgroup/devices type cgroup (rw,nosuid,nodev,noexec,relatime,devices)
cgroup on /sys/fs/cgroup/pids type cgroup (rw,nosuid,nodev,noexec,relatime,pids)
cgroup on /sys/fs/cgroup/freezer type cgroup (rw,nosuid,nodev,noexec,relatime,freezer)
cgroup on /sys/fs/cgroup/net_cls,net_prio type cgroup (rw,nosuid,nodev,noexec,relatime,net_cls,net_prio)
cgroup on /sys/fs/cgroup/perf_event type cgroup (rw,nosuid,nodev,noexec,relatime,perf_event)
systemd-1 on /home/libin/work_doc type autofs (rw,relatime,fd=33,pgrp=1,timeout=0,minproto=5,maxproto=5,direct,pipe_ino=12692)
systemd-1 on /proc/sys/fs/binfmt_misc type autofs (rw,relatime,fd=39,pgrp=1,timeout=0,minproto=5,maxproto=5,direct,pipe_ino=12709)
debugfs on /sys/kernel/debug type debugfs (rw,relatime)
mqueue on /dev/mqueue type mqueue (rw,relatime)
hugetlbfs on /dev/hugepages type hugetlbfs (rw,relatime,pagesize=2M)
tmpfs on /tmp type tmpfs (rw,nosuid,nodev)
configfs on /sys/kernel/config type configfs (rw,relatime)
tmpfs on /run/user/1000 type tmpfs (rw,nosuid,nodev,relatime,size=817176k,mode=700,uid=1000,gid=100)
gvfsd-fuse on /run/user/1000/gvfs type fuse.gvfsd-fuse (rw,nosuid,nodev,relatime,user_id=1000,group_id=100)

It can be seen that the root partition is in /dev/sda1, while there is no independent partition in /boot and /home. And sda1 contains the boot partition, lacks mbr, so we just need to copy the entire sda.

Note:

The exported image must contain at least the root partition and MBR. If the exported image lacks MBR, it will not be able to start. In the current operating system, if /boot and /home are independent partitions, the exported image also needs to contain these two independent partitions.

Export Image

Select different ways to export the image based on actual needs.

  • Use Tool to Export
  • Use Command to Export Image

Use Platform Tools to Export Image

Use the export image tool of the virtualization platform such as VMWare vCenter Convert or Citrix XenConvert. For details, see the export tool documentation for each platform.

Note:

Currently, the image formats supported by service migration are: qcow2, vhd, raw, and vmdk.

Use Command to Export Image

Note:

Using commands to manually export images has a high risk (for example, when IO is busy, the file system metadata may become disordered). It is recommended that you check image is complete after exporting it.

You can export the image by executing the following command:

  • Useqemu-img command
    For example, execute the following command to export /dev/sda to /mnt/sdb/test.qcow2.

    sudo qemu-img convert -f raw -O qcow2 /dev/sda /mnt/sdb/test.qcow2
    

    If you need to convert to other formats, you can modify the parameter values of -O. The following parameter values can be modified:

Among them, /mnt/sdb indicates a new mounted disk or other network storage.

Parameter Value Description
qcow2 qcow2 format
vpc vhd format
vmdk vmdk format
raw No format
  • Usedd command
    For example, execute the following command to export the image in raw format.

    sudo dd if=/dev/sda of=/mnt/sdb/test.imag bs=1K count=$count
    

    Among them, count parameter is the number of partitions to be copied. You can use fdisk command to find out the value. If you need to copy the entire disk, count parameter can be ignored.
    For example, execute the following command to view the number of partitions in /dev/sda.

    fdisk -lu /dev/sda
    

    The result resembles the following is returned:

    Disk /dev/sda: 1495.0 GB, 1494996746240 bytes
    255 heads, 63 sectors/track, 181756 cylinders, total 2919915520 sectors
    Units = sectors of 1 * 512 = 512 bytes
    Sector size (logical/physical): 512 bytes / 4096 bytes
    I/O size (minimum/optimal): 4096 bytes / 4096 bytes
    Disk identifier: 0x0008f290
    
       Device Boot      Start         End      Blocks   Id  System
    /dev/sda1   *        2048    41945087    20971520   83  Linux
    /dev/sda2        41945088    46123007     2088960   82  Linux swap / Solaris
    /dev/sda3        46123008    88066047    20971520   83  Linux
    /dev/sda4        88066048  2919910139  1415922046   8e  Linux LVM
    

    The returned result of the fdisk command shows that the end position of sda1 is 41945087 * 512 bytes, and the count is set to 20481M.

Note:

The image exported by the dd command is in raw format. It is recommended to convert the image to qcow2, vhd, or other image format.

Image Format Conversion

Note:

Currently, the image formats supported by service migration are: qcow2, vhd, vmdk, and raw. It is recommended to use a compressed image format to save transmission and migration time.

Use qemu-img command to convert the image format.
For example, execute the following command to convert an image in raw format to qcow2 format.

sudo qemu-img convert -f raw -O qcow2 test.img test.qcow2
  • -f indicates the format of the source image file.
  • -O indicates the format of the target image file. For supported formats, see the parameter value of -O.

Check Image

Note:

If you create an image without stopping the service or for other reasons, the image file system may be incorrect. Therefore, it is recommended that you check whether the image is correct after creating it.

When the image format is consistent with the format supported by the current platform, you can directly open the image to check the file system. For example, the Windows platform can directly attach an image in vhd format, the Linux platform can use qemu-nbd to open an image in qcow2 format, and the Xen platform can directly enable a vhd file.
Take Linux platform as an example:

modprobe nbd
qemu-nbd -c /dev/nbd0 xxxx.qcow2
mount /dev/nbd0p1 /mnt

If the file system is damaged when the first partition of the qcow2 image is exported, an error will be reported during mount.
In addition, you can also start the Cloud Virtual Machine to test whether the image file can be used before uploading it.