I'm testing a Python script intended for use on Raspberry Pi systems for reformatting and copying partition info and partition data. To get the information from the first device (often a USB memory stick), I use:
sfdisk -d /dev/sda >sda_data.txt
Then, to copy the same table to the destination drive, I use:
sfdisk /dev/sdb <sda_data.txt
Overall, it works as expected, but what if I use it on a smaller disk? For instance, say sda1 is a msdos format boot partition and sda2 is an extfs partition filling the rest of the drive. Say the total data (other than boot commands and data) is about 10GB on /dev/sda2 and /dev/sda is 64GB.
My data is small enough that when I copy the files from the 64GB device to a smaller 32GB device, there will be enough room on the smaller device at /dev/sdb for it to store all the data on a smaller device. So if sda is 64GB and sdb is 32GB, I can still copy all the data I have on sda to sdb.
The problem or question lies with the partition of the smaller sdb.
When I read in the partition information from sda, it includes partition sizes and the 2nd partition, sda2, will be a lot bigger than it's new counterpart, sdb2. When I use the info dump in sda_data.txt on sdb, my experience is that sfdisk allows for the smaller device size on sdb and doesn't try to make too large a partition - it creates a partition on sdb2 that automatically goes to the end of the smaller device.
This is my experience. Is this standard behavior? Will sfdisk always resize that last partition to the smaller device? In other words, can I rely on sfdisk creating that smaller partition on the smaller device? (This is assuming no partition is starting after the end of sdb and would be entirely outside of the space on that device. For the purposes of this discussion, we can assume that the last partition may need to be reduced, but that it will still be able to fit on sdb - it just won't be full size.)
truncate
ordd
, one 2GB in size, one 1GB. Partition the 2GB file similarly to how you partitioned your 64GB device (i.e. one small partition and one larger partition taking up the rest of the space). Then use sfdisk to clone the partition table to the 1GB file. Examine the cloned partition table, to see if sfdisk automatically corrected the partition size or not. If not, it won't be safe to use until you edit it. – cas Jul 19 '23 at 10:51