Score:1

Automatize creating, moving and resizing a persistent data partition in a dual boot scenario

pe flag

Device: Ubuntu 20.04.5 on Raspberry Pi 4B

I am willing to automatize the process of having a persistent data partition in a Azure Device Update scenario. For that (dual boot), I need a persistent data partition (so my partition table is <boot/rootfs1/rootfs2/data>). The image is created in an Azure Pipeline process, so the image only contains a partition table of few gigabytes. When the SD card on the Raspberry Pi 4B is flashed, a script runs at first boot to resize the partitions as followed:

Retrieve the free space (on the base image, all four partition goes to approx. 10Gb, but SD card is 32Gb minimum) Calculate all the sectors to resize the partitions Move them to their final offsets Resize the partition Resize file system All of them are ext4 file systems, and in the image creation process everything goes well, folders are created in the persistent data creation, all of them indicate ext4 and everyone's happy. But during the resize process, and moving partitions with sfdisk, partitions lose their ext4 fstype, as if they were corrupted. Here is my command:

echo "+$data_sectors_to_move," | sfdisk /dev/mmcblk0 -N 4 -f --no-reread

I used to add the --move-data parameter but for the past few days it has been giving me the error

sfdisk: /dev/mmcblk0p4: failed to move data: Not a directory

Now where I don't move the data, it just looses the ext4 format.

I tried to execute mkfs.ext4, but after the automatic process execution and device's reboot, still no ext4 indication when executing parted /dev/mmcblk0 --script print.

No error is given in the logs. Please note that i cannot post my code, but i'll be happy to answer your questions.

Was expecting partitions to be moved and resized, keeping the data on them and their fs type, but none happened.

David avatar
cn flag
What version of Ubuntu are you using?
lalannou avatar
pe flag
Ubuntu 20.04.5, on Raspberry pi 4B
Score:0
br flag

I used to add the --move-data parameter ...

This seems likely to be your problem. Partitioning tools operate on the partition tables only. They generally don't touch the underlying data on the disk, with the exception being sfdisk when using the --move-data flag.

Unless you pass --move-data, the old data won't follow the new partition table. This effectively results in an incorrect partition table because the data is still in the old location. In a data recovery scenario, you'd want to rewrite the partition table again to match the old start & end locations for each data partition. (Unless anything else already overwrote them, you can generally recover data from a disk this way.)

Unless the new partitions are created in free unused disk space, data loss is always a possibility when using tools such as sfdisk, sgdisk, gdisk, and even older tools like fdisk, parted, gparted, etc...

Note that the term "data loss" in this case doesn't mean that you've corrupted the underlying data, but rather that you've lost easy access to it because the partition table entries now point to different locations. When the system tries to read filesystem data at those new sector offset locations, it appears corrupt because it's not starting at that point. A metaphor might be that you lost the table of contents for a book's chapters & starting pages, then tried to create a new table of contents so you could insert your own chapter but forgot to actually move the other chapters and page numbers. Then if you started reading according to the newly incorrect ToC, you'd likely end up starting in the middle of a chapter and the book wouldn't make chronological sense.

I'd suggest tracking down why your original error occurred:

sfdisk: /dev/mmcblk0p4: failed to move data: Not a directory

It's likely that this original error message is happening for another reason, which is hard to say why without seeing your script code or digging deeper into what's going on in the system running the command. My first guess is that if this is happening on boot this could be due to the /dev/mmcblk0p4 device file not actually existing yet due to race conditions common with SystemD and system startup in general. If that's the case, ensure that your script is running after the /dev/mmcblk0p4 device is created and ready. If you're using SystemD, it's as simple as waiting until the dev-mmcblk0p4.device unit is ready. Just add a dependency on dev-mmcblk0p4.device for your startup script. If you're chaining other .service files together as dependencies of each other, make sure you are using systemd-notify as a post-command (or sd_notify() for compiled programs) to signal when each service step is completed. A simple Wants=, Before= or After= unit dependency can still suffer from race conditions because SystemD tries to launch everything in parallel as fast as it can without regard to whether those other units actually completed all their work.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.