Expand root partition after the installation

Suggestions for features and software you would like to see in DietPi, goes here.
Post Reply
nemeth9
Posts: 4
Joined: Wed Jan 20, 2016 12:04 pm

Expand root partition after the installation

Post by nemeth9 »

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
User avatar
Fourdee
Site Admin
Posts: 2788
Joined: Tue Feb 06, 2007 1:36 pm

Re: Expand root partition after the installation

Post by Fourdee »

nemeth9 wrote: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/ ... ot#L44-L72.

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

Resize Partition (run 1st):

Code: Select all

#!/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):

Code: Select all

reboot
Resize ext4 filesystem (run 3rd):

Code: Select all

#!/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
If you find our project or support useful, then we’d really appreciate it if you’d consider contributing to the project however you can.
Donating is the easiest – you can use PayPal or become a DietPi patron.
nemeth9
Posts: 4
Joined: Wed Jan 20, 2016 12:04 pm

Re: Expand root partition after the installation

Post by nemeth9 »

Fourdee wrote:
nemeth9 wrote: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/ ... ot#L44-L72.

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

Resize Partition (run 1st):

Code: Select all

#!/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):

Code: Select all

reboot
Resize ext4 filesystem (run 3rd):

Code: Select all

#!/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
Thanks for your fast reply!
It's working!

Regards,
nemeth9
rhkean@gmail.com

Re: Expand root partition after the installation

Post by rhkean@gmail.com »

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.

Code: Select all

#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.
rhkean@gmail.com

Re: Expand root partition after the installation

Post by rhkean@gmail.com »

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)
perlian
Posts: 33
Joined: Tue May 17, 2016 12:13 pm
Location: Germany
Contact:

Re: Expand root partition after the installation

Post by perlian »

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

works perfectly
markfree
Posts: 3
Joined: Wed May 06, 2020 1:53 am

Re: Expand root partition after the installation

Post by markfree »

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
User avatar
Joulinar
Posts: 2072
Joined: Sat Nov 16, 2019 12:49 am

Re: Expand root partition after the installation

Post by Joulinar »

There is no build in option to do that. Using gparted on another PC (booting from Live USB Stick) would be best option
Pls let us know if a solution is working. This could help others if they hit by similar situation. Your DietPi Team
Post Reply