Limit partition size before first boot Topic is solved

Having issues with your DietPi installation or found a bug? Post it here.
vonloschz
Posts: 34
Joined: Wed Apr 21, 2021 4:30 pm

Limit partition size before first boot

Post by vonloschz »

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 ?
User avatar
MichaIng
Site Admin
Posts: 2917
Joined: Sat Nov 18, 2017 6:21 pm

Re: Limit partition size before first boot

Post by MichaIng »

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:

Code: Select all

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

Then reduce the partition size

Code: Select all

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 ... ize.sh#L42
Replace the + with 83886080 as above.

https://github.com/MichaIng/DietPi/blob ... ize.sh#L55
Append 10485760 as above.
User avatar
Joulinar
Posts: 4504
Joined: Sat Nov 16, 2019 12:49 am

Re: Limit partition size before first boot

Post by Joulinar »

If you like to use more a graphic interface, you could use GParted tool to resize the partitions after first run completed
Pls let us know if a solution is working. This could help others if they hit by similar situation. Your DietPi Team
vonloschz
Posts: 34
Joined: Wed Apr 21, 2021 4:30 pm

Re: Limit partition size before first boot

Post by vonloschz »

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.
vonloschz
Posts: 34
Joined: Wed Apr 21, 2021 4:30 pm

Re: Limit partition size before first boot

Post by vonloschz »

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.

Code: Select all

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

Code: Select all

# 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

Code: Select all

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

Code: Select all

root@DietPi:~# df --block-size=MB | egrep 'boot|root'
/dev/root         42251MB 2287MB   38222MB   6% /
/dev/mmcblk0p1      265MB   52MB     213MB  20% /boot
vonloschz
Posts: 34
Joined: Wed Apr 21, 2021 4:30 pm

Re: Limit partition size before first boot

Post by vonloschz »

How do I convert 40 GiB to 83886080 ?
vonloschz
Posts: 34
Joined: Wed Apr 21, 2021 4:30 pm

Re: Limit partition size before first boot

Post by vonloschz »

ok its a number of sectors.
Thanks cfdisk
User avatar
MichaIng
Site Admin
Posts: 2917
Joined: Sat Nov 18, 2017 6:21 pm

Re: Limit partition size before first boot

Post by MichaIng »

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 :P. At least it assures that only valid sizes can be used.
vonloschz
Posts: 34
Joined: Wed Apr 21, 2021 4:30 pm

Re: Limit partition size before first boot

Post by vonloschz »

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 ...

Code: Select all

Partition 3 does not start on physical sector boundary.
And when I create a third partition with fdisk ... cfdisk complain about ...

Code: Select all

# 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.
User avatar
MichaIng
Site Admin
Posts: 2917
Joined: Sat Nov 18, 2017 6:21 pm

Re: Limit partition size before first boot

Post by MichaIng »

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?
Post Reply