Limit partition size before first boot

Hello,
Question about disk partition today.
I installed DietPi over a 250Gig SSD disk and I want to limit his rootfs to 40Gig before the first boot.

However, I have this error once booted into the system.
Partition 3 does not start on physical sector boundary.

I use balenaEtcher or/and rpi-imager to write the DietPi image.
Then I use parted to do the disk partition stuff.

sudo parted -s -a optimal – /dev/sdb resizepart 2 40GB
sudo parted -s -a optimal – /dev/sdb mkpart primary 40GB 100%
sudo parted /dev/sdb print

Any suggestion ?

1 Like

The root file system size is only ~600 MiB or so on first boot, based on the minimal possible reduction, so you cannot shrink it further before first boot.

On first boot the partition first and the filesystem second, is then maximised to fill the size of the underlying disk automatically. It would be possible to prevent that, but that makes the first run setup break which requires additional space already. So I’d leave that in place.

So you need to shrink the partition and filesystem afterwards, from an external system, as mounted filesystems cannot be shrunken. Start with the filesystem:

resize2fs /dev/sdb2 10485760

Assuming the root partition is /dev/sdb2, using 40 GiB (given in 4k blocks).

Then reduce the partition size

sfdisk --no-reread --no-tell-kernel -fN2 /dev/sdb <<< ',83886080'

With 40 GiB given in 512 byte sectors.

On the other hand, if you’re able to edit the ext4 partition prior to first boot, you can edit the expansion script directly:
/var/lib/dietpi/services/fs_partition_resize.sh

https://github.com/MichaIng/DietPi/blob/master/rootfs/var/lib/dietpi/services/fs_partition_resize.sh#L42
Replace the + with 83886080 as above.

https://github.com/MichaIng/DietPi/blob/master/rootfs/var/lib/dietpi/services/fs_partition_resize.sh#L55
Append 10485760 as above.

1 Like

If you like to use more a graphic interface, you could use GParted tool to resize the partitions after first run completed

Thanks for the suggestions.
I want to keep as much as possible simple my installation process.

The fs_partition_resize.sh modification proposition look nice.
Will give a try and come back with result.

I only did the sfdisk change cause I don’t feel the resize2fs need to be changed. (im maybe wrong)
tested over a 64G MicroSD card.

# Maximize root partition size
sfdisk --no-reread --no-tell-kernel -fN"$ROOT_PART" "$ROOT_DRIVE" <<< ',83886080'



# Maximize root file system
resize2fs "$ROOT_DEV"[/icode]

Result ...
lsblk
[icode]root@DietPi:~# lsblk
NAME        MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
mmcblk0     179:0    0 59.5G  0 disk 
├─mmcblk0p1 179:1    0  256M  0 part /boot
└─mmcblk0p2 179:2    0   40G  0 part /

parted

root@DietPi:~# parted /dev/mmcblk0 print all
Model: SD SN64G (sd/mmc)
Disk /dev/mmcblk0: 63.9GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:

Number  Start   End     Size    Type     File system  Flags
 1      4194kB  273MB   268MB   primary  fat32        lba
 2      273MB   43.2GB  42.9GB  primary  ext4

df

root@DietPi:~# df --block-size=MB | egrep 'boot|root'
/dev/root         42251MB 2287MB   38222MB   6% /
/dev/mmcblk0p1      265MB   52MB     213MB  20% /boot

How do I convert 40 GiB to 83886080 ?

ok its a number of sectors.
Thanks cfdisk

Jep 40 GiB = 83886080 sectors with each 512 bytes = 10485760 blocks (with each 4096 bytes in 95% of cases). Nasty that not every tool simply prints and takes sizes in bytes by default, but that’s how it is :stuck_out_tongue:. At least it assures that only valid sizes can be used.

Hello,
My goal is to combine the disk setup and DietPi-Automation in the simplest procedure for a quick re-installation.
But Im in a chicken and egg situation.

When I create a third partition with cfdisk … fdisk complain about …

Partition 3 does not start on physical sector boundary.

And when I create a third partition with fdisk … cfdisk complain about …

# parted /dev/sda align-check opt
Partition number? 3                                                       
3 not aligned

… any suggestions for the disk setup ?
DietPi-Automation was done without any problem.

When you create the partition, assure that it starts at best at a multiple of 4k (block size). It says sector there, but I’m not even sure if it’s possible to create a partition not at a physical sector start.

How exactly did you create the partition?

here is my successful test over a MicroSD card.

  • Disk info
sudo fdisk -l /dev/sda
Disk /dev/sda: 59.5 GiB, 63864569856 bytes, 124735488 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0xe8af6eb2

Image

PImage=DietPi_RPi-ARMv6-Buster.img
sudo dd if=${PImage} of=/dev/sda bs=4M conv=fsync
sudo sync

Partitions

sudo parted -a optimal /dev/sda resizepart 2 67697654s      # 67697654 sector should bring the partition to 32GiB
sudo resize2fs /dev/sda2
sudo fsck.ext4 -fy /dev/sda2

sudo parted -a optimal /dev/sda mkpart primary 67698688s 124735487s

Validation

sudo fdisk -l /dev/sda
0 error



sudo parted /dev/sda align-check opt
Partition number? 1
1 aligned

sudo parted /dev/sda align-check opt
Partition number? 2
2 aligned

sudo parted /dev/sda align-check opt
Partition number? 3
3 aligned

Then my tests errors over a SSD disk

  • Disk info
sudo fdisk -l /dev/sdb
Disk /dev/sdb: 232.9 GiB, 250059350016 bytes, 488397168 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 4096 bytes / 33553920 bytes                                 <-- is it the problem ???
Disklabel type: dos
Disk identifier: 0x8f4dbd00

Image

PImage=DietPi_RPi-ARMv8-Buster.img
sudo dd if=${PImage} of=/dev/sdb bs=4M conv=fsync
sudo sync

Partitions

sudo parted -a optimal /dev/sdb resizepart 2 67697654s      # 67697654 sector should bring the partition to 32GiB
sudo resize2fs /dev/sdb2
sudo fsck.ext4 -fy /dev/sdb2

sudo parted -a optimal /dev/sdb mkpart primary 67698688s 488397167s
Warning: The resulting partition is not properly aligned for best performance.
Ignore/Cancel

Partitions … also in error…

sudo parted -a optimal /dev/sdb mkpart primary 68877285s 488397167s
sudo parted -a optimal /dev/sdb mkpart primary 67698688s 488397167s
sudo parted -a optimal /dev/sdb mkpart primary 68942820s 488397167s
sudo parted -a optimal /dev/sdb mkpart primary 68943872s 488397167s

SSD disk solution …

sudo fdisk -l /dev/sdb
...
I/O size (minimum/optimal): 4096 bytes / 33553920 bytes             4096 bytes = parted min alignment
                                                                33553920 bytes = parted opt alignment
...

Partitions

sudo parted -a optimal /dev/sdb resizepart 2 68870143s      # 67697654s = 32GiB / 68870143s = 32.6Gib
sudo resize2fs /dev/sdb2
sudo fsck.ext4 -fy /dev/sdb2

sudo parted -a min /dev/sdb mkpart primary 68870144s 488397167s

Validation

sudo fdisk -l /dev/sdb
0 error

sudo parted /dev/sdb align-check min
Partition number? 1
1 aligned

sudo parted /dev/sdb align-check min
Partition number? 2
2 aligned

sudo parted /dev/sdb align-check min
Partition number? 3
3 aligned

Sorry about this thread… learning alone is so boring.

nothing to be sorry about, it’s good learning and might be interesting for others as well :slight_smile:

Interesting actually, thanks for sharing!

Interesting is that for my HDDs minimal and optimal alignment always leads to the same result. Everything is 512 bytes (like on your SD card). Probably my HDDs are too old :smiley:, or it is more common on SSDs.

I wonder how 33553920 bytes is optimal, as it’s 32767.5 KiB or 31.99951171875 MiB :thinking:, at least a multiple of 512 bytes. Obviously this is very disk specific.

And then I wonder how the original boot and root partitions are aligned correctly. The first still starts at 4 MiB and the second at 260 MiB (4 + 256), which both is not a multiple of 33553920 bytes :thinking:. Or did I get something wrong?

fyi … this disk partitioning method
broke the installation over the new DietPi_RPi-ARMv8-Bullseye.7z image.

and the solution is …
Because I made a zfs pool over the /dev/sdb3,
I feel that a wipefs over this disk before I dd the DietPi image help me.

But the real change was to not play anymore with the file system.
These 2 cmd broke the installation just before we get any networking.

sudo resize2fs /dev/sdb2
sudo fsck.ext4 -fy /dev/sdb2

Here is my final cmd to create a third partitions.

  • Clean the disk and install DietPi image.
sudo wipefs --all /dev/sdb
sudo dd if=/tmp/DietPi_RPi-ARMv8-Bullseye.img of=/dev/sdb bs=4M conv=fsync
sudo sync
  • Resize the second partition and create the third one.
sudo parted -a optimal /dev/sdb resizepart 2 68870143s     
sudo parted -a min /dev/sdb mkpart primary 68870144s 488397167s
  • Copy the DietPiAutomation and a Custom_Script.
sudo mkdir -p /mnt/sdb1
sudo mount /dev/sdb1 /mnt/sdb1/
sudo cp ${DietPiAutomation} /mnt/sdb1/dietpi.txt
sudo cp ${Custom_Script} /mnt/sdb1/Automation_Custom_Script.sh
sudo umount /mnt/sdb1/