Linux Image Creation

Last updated: 2020-02-25 10:31:02

PDF

Scenario

This document guides you through the production of Linux Image.

Directions

Preparations

When making the system disk Image and Export, the following checks need to be carried out:

If you are through the data disk Image Export, you can skip this operation.

Check OS partition and Launch method

  1. Execute the following command to check that the OS partition is a GPT partition.
sudo parted -l /dev/sda | grep 'Partition Table'
  • If the returned result is msdos, it is represented as a MBR partition, please perform the next step.
  • If the returned result is gpt, it is represented as GPT partition. GPT partition is not supported for service migration. Please Submit a ticket Feedback.
  1. Execute the following command to check if the operating system is Launch in EFI mode.
sudo ls /sys/firmware/efi
  • If you need to use the custom image feature, Submit a ticket .
  • If no, proceed to the next step.

Check the key files of the system

System key files to be checked include, but are not limited to, the following files:

Follow the standards of relevant distributions to ensure that the locations and permissions of the system-critical files are correct and the files can be read and written normally.

In the kernel parameter, uuid is recommended for mounting root. Other methods (such as root=/dev/sda) may cause the failure in starting the system.

  • / etc/fstab: Do not mount other disks. After migration, the system may fail to start due to disk missing.
  • / etc/shadow: It has normal permissions and can be read and written.

Unmount software

Unmount the drivers and software that produce conflicts (including VMware tools, Xen tools, Virtualbox GuestAdditions and other software that comes with underlying drivers).

Check the virtio driver

Check the virtio driver: Please see Check virtio Driver in Linux System .

Installing cloud-init

Changes to the hardware on the cloud include but not limited to:

  • Replacing the graphics card with cirrus vga.
  • Replacing the disk with virtio disk. Device name is vda, vdb, and so on.
  • Replacing the ENI with virtio nic. By default, only eth0 is available.

Find Partition and size

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

## mount

Take the returned result 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)

As you can see, the root partition is in /dev/sda1 Medium, /boot and /home There are no separate partitions, sda1 contains boot partitions, missing mbr, we just need to copy the entire sda.

At least the root partition and mbr should be included in Export's Image. If Export's Image lacks mbr, he will not be able to Launch.
In the current operating system, if /boot and /home For independent partitions, Export's Image also needs to include these two independent partitions.

Export Image

According to the actual needs, choose different ways Export Image.

Use the platform tool Export Image

For more information on how to use image export tools of VMWare vCenter Convert, Citrix XenConvert and other virtualization platforms, please see the relevant document of each platform.
The image formats supported by Tencent Cloud Service Migration include qcow2, vhd, raw, and vmdk.

Use orders Export Image

Due to the use of commands, the risk of manual Export Image is relatively large (for example, it may cause metadata confusion of the file system when IO is busy, etc.). It is suggested that after Export and Image, Check Image Intact.

You can execute the following order Export Image:

Use qemu-img command

For example, execute the following command to set the /dev/sda Export to /mnt/sdb/test.qcow2 .

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

among, /mnt/sdb For mounted new disks or other network storage.
If you need to convert to another format, please modify -O Gets or sets the parameter value of the The parameter values that can be modified are as follows:

ParameterContent
Qcow2Qcow2 format
VPCVhd format
VmdkVmdk format
RawNo format
# Use dd command for example, execute the following command, Image in Export raw format. Parameter `count` is the number of partitions to be copied, which can be found by `fdisk` command. If you need to copy it completely, the parameter `count` can be ignored. For example, execute the following command to check the number of partitions for `/ dev/ sda`. `Disk-lu / dev/ sda`A message similar to the one below is returned: `disk / dev/sda: 1495.0 GB, 1494996746240 bytes255 heads, 63 sectors/track, 181756 cylinders, total 2919915520 sectorsUnits = sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 4096 bytesI/O size (minimum/optimal): 4096 bytes / 4096 bytesDisk 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

As you can see from the return result of `fdisk` command, the end position of sda1 is 41945087\ * 512 bytes, and `count` can be set to 20481m.
> The image exported through the `dd` command is in raw format. It is recommended to [convert it to qcow2,vhd or other image format](#ImageFormatConversion)。
>

<span id="ImageFormatConversion"></span>
### Image format conversion
> - At present, service migration supports image formats of qcow2, vpc, vmdk, and raw. A compressed image format is recommended to shorten the transmission and migration.
> 
Convert the image format using the qemu-img command:
For example, execute the following command to convert the image in raw format to qcow2 format.

Sudo qemu-img convert-f raw-O qcow2 test.img test.qcow2

`-f` is the source image file format.
- `-O` For the destination mirror file format, please refer to the supported format[`-Parameter value of O`](#-OParameterValue)。

<span id="CheckMirror"></span>
### Check the mirror image
> When you create an image directly 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 the image.
>
When the image format is consistent with the format supported by the current platform, you can directly turn on the image to check the file system. For example, Windows platform can directly attach vhd format image, Linux platform can use qemu-nbd to open qcow2 format image, and Xen platform can directly enable vhd file.
Take the Linux platform as an example:

Modprobe nbd
Qemu-nbd-c / dev/nbd0 xxxx.qcow2
Mount / dev/nbd0p1 / mnt

If the file system is corrupted when the first partition of the qcow2 image is exported, an error will occur when using the mount command.
In addition, you can start the CVM to check whether the image file works before uploading the image.