Creating Linux Image Based on Local System
Application scenarios
Primarily intended for users to create custom images locally that are compatible with platform requirements.
The file provides instructions on how to create a custom image based on the Ubuntu 16.04 system.
Operation steps
Preparation
Due to restrictions of Baidu AI Cloud on the format, driver and other aspects of imported images, please learn more about Image Import Restrictions and configure the system in advance when necessary. Baidu AI Cloud provides Image Detection Tool to help you quickly verify whether the relevant configurations meet platform requirements.
Locate system disk partition
A complete Linux system image must, at a minimum, include an MBR (Master Boot Record) and a root disk partition (where the / directory is mounted, containing the main file system). Without these, the system will fail to boot.
If included, the boot disk partition (mounting the /boot directory and storing system kernels and other files necessary for booting) and the home disk partition (mounting the /home directory, which contains user directories) must be copied together.
Execute the mount command to check the mounting status:
1 sysfs on /sys type sysfs (rw,nosuid,nodev,noexec,relatime)
2 proc on /proc type proc (rw,nosuid,nodev,noexec,relatime)
3 udev on /dev type devtmpfs (rw,nosuid,relatime,size=1002644k,nr_inodes=250661,mode=755)
4 devpts on /dev/pts type devpts (rw,nosuid,noexec,relatime,gid=5,mode=620,ptmxmode=000)
5 tmpfs on /run type tmpfs (rw,nosuid,noexec,relatime,size=204784k,mode=755)
6 /dev/sda1 on / type ext4 (rw,relatime,data=ordered)
7 securityfs on /sys/kernel/security type securityfs (rw,nosuid,nodev,noexec,relatime)
8 tmpfs on /dev/shm type tmpfs (rw,nosuid,nodev)
9 tmpfs on /run/lock type tmpfs (rw,nosuid,nodev,noexec,relatime,size=5120k)
10 tmpfs on /sys/fs/cgroup type tmpfs (ro,nosuid,nodev,noexec,mode=755)
11 cgroup on /sys/fs/cgroup/systemd type cgroup (rw,nosuid,nodev,noexec,relatime,xattr,release_agent=/lib/systemd/systemd-cgroups-agent,name=systemd)
12 pstore on /sys/fs/pstore type pstore (rw,nosuid,nodev,noexec,relatime)
13 cgroup on /sys/fs/cgroup/cpuset type cgroup (rw,nosuid,nodev,noexec,relatime,cpuset)
14 cgroup on /sys/fs/cgroup/hugetlb type cgroup (rw,nosuid,nodev,noexec,relatime,hugetlb)
15 cgroup on /sys/fs/cgroup/net_cls,net_prio type cgroup (rw,nosuid,nodev,noexec,relatime,net_cls,net_prio)
16 cgroup on /sys/fs/cgroup/devices type cgroup (rw,nosuid,nodev,noexec,relatime,devices)
17 cgroup on /sys/fs/cgroup/cpu,cpuacct type cgroup (rw,nosuid,nodev,noexec,relatime,cpu,cpuacct)
18 cgroup on /sys/fs/cgroup/blkio type cgroup (rw,nosuid,nodev,noexec,relatime,blkio)
19 cgroup on /sys/fs/cgroup/freezer type cgroup (rw,nosuid,nodev,noexec,relatime,freezer)
20 cgroup on /sys/fs/cgroup/memory type cgroup (rw,nosuid,nodev,noexec,relatime,memory)
21 cgroup on /sys/fs/cgroup/perf_event type cgroup (rw,nosuid,nodev,noexec,relatime,perf_event)
22 cgroup on /sys/fs/cgroup/pids type cgroup (rw,nosuid,nodev,noexec,relatime,pids)
23 mqueue on /dev/mqueue type mqueue (rw,relatime)
24 hugetlbfs on /dev/hugepages type hugetlbfs (rw,relatime)
25 debugfs on /sys/kernel/debug type debugfs (rw,relatime)
26 systemd-1 on /proc/sys/fs/binfmt_misc type autofs (rw,relatime,fd=35,pgrp=1,timeout=0,minproto=5,maxproto=5,direct)
27 fusectl on /sys/fs/fuse/connections type fusectl (rw,relatime)
28 lxcfs on /var/lib/lxcfs type fuse.lxcfs (rw,nosuid,nodev,relatime,user_id=0,group_id=0,allow_other)
29 tmpfs on /run/user/0 type tmpfs (rw,nosuid,nodev,relatime,size=204784k,mode=700)
As observed, the boot and home disk partitions are excluded. Only the MBR and the root disk partition need to be copied, specifically /dev/sda.
Create an image
Use an image export tool on a virtualization platform such as VMware vCenter Converter or VirtualBox. For more details, please refer to the export tool documentation for various platforms.
Execute qemu-img or dd command to export image
- Create image with qmeu-img:
If the qemu-img command is not available, run the following command to install it:
1 apt-get update
2 apt-get install qemu
Run the following command to generate an image, where test.qcow2 is the generated image. The image will be saved to a new disk or a network directory like /mnt/test/. The -O option specifies the output image format (you can select the format type as required, such as qcow2, vpc, vmdk, raw, etc. Baidu AI Cloud currently supports only raw and qcow2 formats for image import).
1`qemu-img convert -f raw -O qcow2 /dev/sda /mnt/test/test.qcow2`
The operation takes a few minutes. After completion, execute the following command for compatibility conversion
qemu-img amend -f qcow2 -o compat=0.10 test.qcow2
View image information with qemu-img info as follows:
1 image: test.qcow2
2 file format: qcow2
3 virtual size: 40G (42949672960 bytes)
4 disk size: 3.6G
5 cluster_size: 65536
6 Format specific information:
7 compat: 0.10
8 refcount bits: 16
- Execute dd command to create image:
Execute the following command to copy the disk
dd if=/dev/sda of=/mnt/test/test.img bs=1M
The full disk will be copied after the above command is executed. If only a specific disk partition needs to be copied, you may appoint it with the count parameter (where bs value * count value = size to be copied)
dd if=/dev/sda of=/mnt/test/test.img bs=1M count=1024
It takes a few minutes. Upon completion, view image information with qemu-img info:
1image: test.img
2file format: raw
3virtual size: 40G (42949672960 bytes)
4disk size: 40G
Format conversion
Baidu AI Cloud now supports image import in the formats of raw and qcow2. For other formats such as vmdk or vpc, please refer to Format Conversion for conversion.
Subsequent operations
The generated image can be imported into Baidu AI Cloud to create instances as needed.
