Creating a non-root SD image

Is it possible to create a full SD image in linux without root privileges (that is, there is no loopback)? I am looking for a way to automate the creation of images of an embedded system. The image must contain a specific partition structure and partitions formatted in FAT and ext2, filled with files from the build system.

+6
source share
2 answers

I am trying to do the same. My first attempt used a loopback locking device, but I found workarounds for both phases requiring a loopback.

Loop steps

Here is what I am doing ($ 1 is the image file name, $ 2 is the file size):

  • create an image file with a zero disk with dd if=/dev/zero of=$1 bs=512 count=$(($2/512))
  • create a partition table with parted -s $1 mklabel msdos
  • create a partition with parted -s $1 "mkpart primary 0% 100%"
  • attach section to sudo losetup --find $1 --offset $OFFSET_TO_PARTITION_BYTES
  • create a file system using mkfs.ext4 with mkfs.ext4 -I 128 -L BOOT -b 2048 -O ^has_journal /dev/loop0 $SIZE_IN_2048_BLOCKS
  • mount / dev / loop0

The loop is used because

  • In steps 4 and 5, mkfs does not have an offset option, so losetup is used to solve this problem.
  • in step 6, mount allows you to use the ext4 driver of the operating system

Looback workarounds

Workaround for steps 4 and 5:

  • xmount --in dd --out vdi disk.img mnt /
  • vdfuse -f mnt / disk.vdi -r./mnt2
  • ./mnt2 will now have two files: EntireDisk and Partition1
  • point mkfs.ext4 at./mnt2/Partition1

Solution for work in step 6:

  • follow all steps for step 5
  • use fuseext2 to install. / mnt 2 / Partition1

Caveat

Caution: ext4 support is not announced in their documentation, and mount attempts come with a warning:

 This is experimental code, opening rw a real file system could be dangerous for your data. Please add "-o ro" if you want to open the file system image in read-only mode, or "-o rw+" if you accept the risk to test this module 

Update

vdfuse should be able to mount the raw image without xmount, but there is an error that ignores the RAW parameter.

I tracked and fixed the bug with the patch here:

https://bugs.launchpad.net/ubuntu/+source/virtualbox-ose/+bug/1019075

+4
source

you can look at genextfs , which creates the ext2 file system in a regular file without any installation.

+4
source

Source: https://habr.com/ru/post/917631/


All Articles