How to create a Linux disk image with multiple partitions without having a physical sample

Hello dear reader of this guide. My name is Andrey, and no, I’m not an alcoholic, I’m a developer of embedded systems based on Linux.

This article is a quick guide to creating a disk image with several partitions, where you could write all sorts of things for transfer to the customer, and later on to production.

What is it for? All processor boards developed by our company have a slot for sdcard and mmc installed on the board. The system is loaded either from sd or from mmc. The sd card is usually a temporary solution, it captivates with its ease of installation. And mmc is a permanent solution where the final software is installed. Switching between boot methods is carried out by jumpers (jumpers on the board).

In the case when we are talking about the customer at the development stage, he needs an independent opportunity to work with the device. To do this, he is provided with the device itself and a link to download the image, with instructions on how to install the image on the sdcard and boot the system from it. Next, the customer initiates either the installation from the sd card, or launches the diagnostic software, but these are already specific details of working with the customer.

In the case when we are talking about the production and release of the device, we know that people who will install the final product on mmc, as a rule, do not know how to connect via the terminal and work with the u-boot or Linux command line, and they simply can don’t have time for that. Their tasks should include the implementation of a simple production cycle, for example:

  1. get a device;

  2. connect to the stand;

  3. install jumpers and sd card;

  4. supply food;

  5. wait for messages about the end of the software installation;

  6. turn off the device;

  7. remove the jumpers and remove the sd card;

  8. supply food;

  9. wait for diagnostic messages;

  10. turn off the device and transfer further …

Of course, the image can be placed, for example, on a server, and rolled over the network, but this option still requires, at a minimum, the presence of a bootloader on mmc. Again, that’s a completely different story.

In order not to suffer with each sd card for each option, you can prepare several images in advance. And then the question arises – how? The assembly can even be automated, but that’s a completely different story.

What is required to understand and implement what is happening:

  • OS GNU/Linux Debian;

  • ability to use the command term at the level of command input;

  • understand what an environment variable is and how to use its value on the command line.

We create an image:

  1. Create a disk image file of the given size:

    sudo dd if=/dev/zero of=${FILENAME} bs=${BYTES} count=${NUMBER_OF_BYTES}
  2. We connect the disk to the system, split and format:

    sudo losetup /dev/loop0 ${FILENAME}
    sudo fdisk   /dev/loop0 # как делить диск и на какое количество разделов, личное дело каждого
    # может потребоваться повторное подключение образа, чтобы ос считала разделы:
    sudo losetup -d /dev/loop0
    sudo losetup /dev/loop0 ${FILENAME}
    # подключаем разделы:
    sudo kpartx -v -a /dev/loop0
    # форматируем разделы, если образ диска включает два раздела, то:
    sudo mkfs.${FSTYPE} /dev/mapper/loop0p1
    sudo mkfs.${FSTYPE} /dev/mapper/loop0p2
  3. We mount partitions and write information:

    sudo mount /dev/mapper/loop0p${N} ${MOUNTPOINT}${N}
    sudo cp -aR /rootfs/* ${MOUNTPOINT}${N}/ # что записываем и куда, каждый решает сам
    # если вам также как и мне требуется на диске u-boot, то не забываем установить:
    sudo dd if=u-boot.img of=/dev/loop0 bs=1k seek=1 conv=fsync
  4. We turn off the disks in the reverse order:

    sudo umount ${MOUNTPOINT}${N}
    sudo kpartx -v -d /dev/loop0
    sudo losetup -d /dev/loop0
  5. We write the resulting image to the appropriate device, if necessary, for example, for verification and testing:

    sudo dd if=${FILENAME} of=${BLKDEV}

    We transfer the received $ {FILENAME} to the customer or production, with instructions on how to use it.

You may not have the kpartx program installed, you can install it with the command:

 sudo apt-get install kpartx

Conclusion

There are two ways to prepare a disk image:

  • making an image from a real physical media using the dd program;

  • by creating initially a completely virtual image that can be installed on any external media using the same dd.

Why doesn’t the first method always work? If you are developing one system, using one sd card, then this method seems to be quite acceptable. But, as soon as you try to install an image from this single sd card to another, similar one, for example, of a different size, you will be disappointed in this approach. I usually have several embedded systems in my work, and keeping several different sd cards for each is simply an unaffordable luxury. The virtual image is always easy to modify and distribute. But there is a drawback – this is its fixed size, it will go to the sd card in the same form, limiting the size of the space used, but for the case when it is necessary to deploy the system, this does not matter.

Similar Posts

Leave a Reply