Expand root partition after the installation

Hi,

I installed DietPi on a 4GB SD card, set up everything, working fine. I made an image from the installed system than copied to a 16GB SD card.
I know that the first step of the installation is that the installer expand the root file system to the maximum size of SD card but how can I do this after the migration? In raspbian config there is a setup of this but in dietpi config I haven’t seen.
Is there any method to do this after the installation?

Thanks!

Regards,
nemeth9

Hi Nemeth9,

As per the re-partition and resize ext4 code in DietPi: https://github.com/Fourdee/DietPi/blob/master/dietpi/boot#L44-L72.

You could essentially make the following scripts to complete the above:
Remember to chmod +x before executing them.

Resize Partition (run 1st):

#!/bin/bash
#Stop Services
/DietPi/dietpi/dietpi-services stop

#Resize partition
cat << _EOF_ | fdisk /dev/mmcblk0
p
d
2
n
p
2
$(parted /dev/mmcblk0 -ms unit s p | grep ':ext4::;' | sed 's/:/ /g' | sed 's/s//g' | awk '{ print $2 }')

p
w

_EOF_

Reboot System (run 2nd):

reboot

Resize ext4 filesystem (run 3rd):

#!/bin/bash
# Stop Services
/DietPi/dietpi/dietpi-services stop

# Resize Filesystem to new partition size
resize2fs /dev/mmcblk0p2

# Start services
/DietPi/dietpi/dietpi-services start
1 Like

Thanks for your fast reply!
It’s working!

Regards,
nemeth9

I would like to see an option for MAX_ROOT_PARTITION in dietpi.txt that would allow for partition expansion… but not to the maximum size of the uSD card.

#Resize partition
cat << _EOF_ | fdisk /dev/mmcblk0
p
d
2
n
p
2
$(parted /dev/mmcblk0 -ms unit s p | grep ':ext4::;' | sed 's/:/ /g' | sed 's/s//g' | awk '{ print $2 }')
                          <<<<-------------HERE
p
w

_EOF_

the blank line in the script (that I marked as “HERE”) is essentially taking fdisk’s default value for the ending sector (which is the last sector of the uSD). However, if you change that line to “+2048M” , then you get a 2GB partition… even on a 4-64GB card. By adding a setting in the dietpi.txt file, it can be caught during that initial boot where the resizing occurs.

This would allow the admin to come back in and add additional partitions if desired.

or, just resize to 2GB during initial boot and allow for resize in the dietpi-config script to any arbitrary larger size.

Much simpler, with no I/O redirect or sub-shells…

parted /dev/mmcblk0 -s resizepart 2 100%


(I haven’t tested this yet with a “+500M” type of parameter)

i do that kind of things with gparted on another linux PC

works perfectly

After first boot my partition was expanded, but I didn’t want that.
I wanted to shrink my partion again.
Is there an option to do so at DietPi?
Thanks

There is no build in option to do that. Using gparted on another PC (booting from Live USB Stick) would be best option

For some strange reason a system didn’t expand when it was installed, so I had to find a manual way of doing it

These are the steps I followed

fdisk -l /dev/mmcblk1

Which showed me

Disk /dev/mmcblk1: 14.56 GiB, 15634268160 bytes, 30535680 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: 0x2c2a3e4f

Device         Boot Start      End  Sectors  Size Id Type
/dev/mmcblk1p1       8192 8268096 8259904 3.6G 83 Linux

df -h

Which showed me

Filesystem      Size  Used Avail Use% Mounted on
udev            1.5G     0  1.5G   0% /dev
tmpfs           379M  5.7M  373M   2% /run
/dev/mmcblk1p1  3.9G  3.9G     0 100% /
tmpfs           1.9G     0  1.9G   0% /dev/shm
tmpfs           5.0M  4.0K  5.0M   1% /run/lock
/dev/loop0      2.2M  2.2M     0 100% /snap/avahi/277
tmpfs           1.9G     0  1.9G   0% /tmp
tmpfs            50M  8.0K   50M   1% /var/log
/dev/loop2       49M   49M     0 100% /snap/core18/2289
/dev/loop1       99M   99M     0 100% /snap/core/13253
/dev/loop3       58M   58M     0 100% /snap/core20/1380
/dev/loop4       58M   58M     0 100% /snap/core20/1437
/dev/loop6       49M   49M     0 100% /snap/core18/2406
/dev/loop5       98M   98M     0 100% /snap/core/12837
/dev/loop7       39M   39M     0 100% /snap/snapd/15541
/dev/loop8       39M   39M     0 100% /snap/snapd/15909
/dev/loop9       10M   10M     0 100% /snap/velbus-tcp/140

Enlarge the partition: fdisk -u /dev/mmcblk1.

p to print the partition table, take note of the number, start, end, type of sda1.

Delete it: d:

Recreate it using command n with same number (1), start and type but with a bigger end (taking care not to overlap with other partitions).

  • However, fdisk suggested the maximum value as Default, so I didn’t type any value in and just pressed Enter

if choosing a different end point try to align things on a megabyte boundary that is for end, make it a multiple of 2048 minus 1. Change the type if needed with t (for partitions holding an extX or btrfs filesystem, the default of 83 is fine).

Then w to write and q to quit.

I ran partprobe but nothing seems to change, df -h still showed as only using 3.6GB.

I rebooted, but nothing changed.

this last step did the trick.

(run each command on it’s own)

/boot/dietpi/dietpi-services stop
resize2fs /dev/mmcblk1p1
df -h
/boot/dietpi/dietpi-services start`

I now have the full 14GB available to me

Disk /dev/mmcblk1: 14.56 GiB, 15634268160 bytes, 30535680 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: 0x2c2a3e4f

Device         Boot Start      End  Sectors  Size Id Type
/dev/mmcblk1p1       8192 30535679 30527488 14.6G 83 Linux

You can also do it via dietpi-drive_manager or:

systemctl enable dietpi-fs_partition_resize
reboot

Doing this on reboot is a bit safer, and that service/script assures that the kernel is informed about the partition table change before expanding the filesystem :slight_smile:.

3 Likes

thank you so much, it’s more easier and safer.
https://dietpi.com/docs/dietpi_tools/system_configuration/#dietpi-drive-manager

1 Like

3 posts were split to a new topic: Expanding partition does not work

A post was merged into an existing topic: Expanding partition does not work