How to create a multi partition SD disk image without root privileges?

后端 未结 5 790
陌清茗
陌清茗 2021-02-13 11:06

Is it possible to create a complete SD image in linux without having root privileges (that is, no loopback mount)? I\'m looking for a way to automate embedded system image creat

5条回答
  •  面向向阳花
    2021-02-13 11:18

    I'm trying to do the same thing. My first attempt used the loopback block device, but I have found work-arounds to both steps that require loopback.

    Steps with loopback

    Here's what I'm doing ( $1 is image file name, $2 is file size):

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

    The loopback is used because

    • in step 4 & 5, mkfs doesn't have an offset option so losetup is used to solve that problem
    • in step 6, mount allows the use of the operating systems ext4 driver

    Looback workarounds

    Shitty work-around for step 4 & 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

    Work-around solution to step 6:

    • follow all steps for step 5 work around
    • use fuseext2 to mount ./mnt2/Partition1

    Caveat

    Caveat: ext4 support is not advertised in their documentation, and attempts to mount 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 a raw image without the help of xmount, but there is a bug which ignores the RAW option.

    I tracked down and fixed the bug with a patch here:

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

提交回复
热议问题