Page 1 of 1

Resize Root partition in .img file

Posted: Mon Oct 29, 2018 11:41 am
by laquica
Hi guys,
First, I am sorry, if topic's location is wrong, let me briefly explain my issue.
I want to resize pi's root partition, and I know I did this with gparted, but I want to do writing img file before SD card, so I mounted raspbian .img files to on my system, but I don't know which files I should change. Thank you for your attetion.

Re: Resize Root partition in .img file

Posted: Mon Oct 29, 2018 8:29 pm
by B.Goode
Welcome to the Raspberry Pi forums.


Are you aware that by default one of the first actions of a newly-installed version of the Raspbian Operating System is to resize the root partition to occupy the largest possible area of the microSD card?

At first sight, therefore, what you are proposing appears pointless. Unless you already know this and plan to take steps to inhibit this default action.

Is there some background to your query that might allow someone to give a more informed reply?

Re: Resize Root partition in .img file

Posted: Tue Oct 30, 2018 6:42 am
by aBUGSworstnightmare
laquica wrote:
Mon Oct 29, 2018 11:41 am
Hi guys,
First, I am sorry, if topic's location is wrong, let me briefly explain my issue.
I want to resize pi's root partition, and I know I did this with gparted, but I want to do writing img file before SD card, so I mounted raspbian .img files to on my system, but I don't know which files I should change. Thank you for your attetion.
Take a plain uSD card, use etcher (https://www.balena.io/etcher) to write the Raspbian image file (https://www.raspberrypi.org/downloads/raspbian/) to it. Insert into your Pi, reboot and - as said already - file size will be expanded as first action. Reboots automatically and once finished you can start working with it.

Re: Resize Root partition in .img file

Posted: Tue Oct 30, 2018 8:45 am
by laquica
B.Goode wrote:
Mon Oct 29, 2018 8:29 pm
Welcome to the Raspberry Pi forums.


Are you aware that by default one of the first actions of a newly-installed version of the Raspbian Operating System is to resize the root partition to occupy the largest possible area of the microSD card?

At first sight, therefore, what you are proposing appears pointless. Unless you already know this and plan to take steps to inhibit this default action.

Is there some background to your query that might allow someone to give a more informed reply?
Yeah, I am aware what I am doing, just maybe I misrepresented, so I just want to know, let say we have 32 GB SD card and I want to root partition have 8 GB area, then other files have different size (configs 3GB etc.) Can we set .img file like this before writing the SD card?

Re: Resize Root partition in .img file

Posted: Tue Oct 30, 2018 9:00 am
by aBUGSworstnightmare
How often do you need to write this 'custom' image file?
Why not make a standard installation, then use another Liinux PC for making the required partitions/resizing.

Re: Resize Root partition in .img file

Posted: Tue Oct 30, 2018 9:14 am
by laquica
aBUGSworstnightmare wrote:
Tue Oct 30, 2018 9:00 am
How often do you need to write this 'custom' image file?
Why not make a standard installation, then use another Liinux PC for making the required partitions/resizing.
If we could make this custom image, this provide us huge save time.

Re: Resize Root partition in .img file

Posted: Tue Oct 30, 2018 9:49 am
by aBUGSworstnightmare
laquica wrote:
aBUGSworstnightmare wrote:
Tue Oct 30, 2018 9:00 am
How often do you need to write this 'custom' image file?
Why not make a standard installation, then use another Liinux PC for making the required partitions/resizing.
I am working for IOT company, I cant tell the details but If we could make this custom image, this provide us huge save time.
No need to disclose any details! Sounds like you need some more, hence do as described by me (standard install, resize, etc) and then take an image of your SD card.
This will be your master image from now.

Note: Keep in mind that uSD card size can differ from brand to brand, batch to batch. Means, not every 32GB card will have exactly the same size.
so better make your image a few MB smaller than the total availale space.

Re: Resize Root partition in .img file

Posted: Tue Oct 30, 2018 11:20 am
by procount
Without disclosing specific details, I feel you would benefit from better responses if you provided a little more information, because I'm reading a lot of assumptions into your request. Maybe considering the following questions would help clarify what you want to do, because I'm a little confused ;)

1. Is this a totally custom image, a plain Raspbian image, or a Raspbian image that you are customising for an IoT project?

2. How many partitions does your image have?

Standard Raspbian has a small FAT32 boot partition plus an ext4 rootfs that is normally expanded on first execution to fill the rest of the card. You mention writing to a 32GB card.
3. Will you always target a 32GB card, or maybe different sized SD cards?
I want to root partition have 8 GB area, then other files have different size (configs 3GB etc.)
4. You mentioned making the root partition a fixed 8GB in size. What will you be doing with the other ~24GB of SD card space? Did you mean to create other partitions (of 3GB etc) for your configs?
Can we set .img file like this before writing the SD card?
5. Yes, But there are several ways you can do this. What is your motivation?
If we could make this custom image, this provide us huge save time.
6. What is taking too long at the moment? Do you mean the expansion of the partition is taking too long?

You could create an SD card with the exact sized partitions you need and then take an image of it (with e.g. DD or win32diskimager). This image will be quite large and will take a long time to write. You could zip it to save space. You can write it back to new SD cards using Etcher (which understands zip files). As others have said, this will restrict you to writing it to an SD card of the same or larger size. Not all 32GB cards are the same size, so you would need to make it a few MB smaller to be more sure it would fit on them all.

OR, you can create an image with minimally sized partitions and use first-boot scripts to expand them to the required sizes (like Raspbian does) so it fits on a wider variety of SD card sizes.

It is not clear to me what your main concerns are that need addressing. Maybe answering these questions will help others provide better suggestions that are more suited to your needs.

Re: Resize Root partition in .img file

Posted: Tue Oct 30, 2018 1:39 pm
by HawaiianPi
Changing the partitions on the default image is not easy, because that data is not in some configuration file, it's written to the partition table of the SD card when the image is written. You could possibly modify the script that does the resizing to create additional partitions, but then you'd need some way to mount them.

Re: Resize Root partition in .img file

Posted: Wed Oct 31, 2018 6:42 am
by TimG
To prevent the automatic re-partitioning from taking place on first boot, edit cmdline.txt to remove the "quiet init=/usr/lib/raspi-config/init_resize.sh".

Once this is done you can re-partition by hand and save the resulting disk image. Or you can alter /usr/lib/raspi-config/init_resize.sh to do what you want (look for $TARGET_END in the script). The first option is easier, but the second is more flexible.

If you also want to automate resizing of the root file-system you'll need to edit /etc/init.d/resize2fs_once. You won't find it on a running RPi because it deletes itself on first boot, but you can restore it from https://github.com/RPi-Distro/pi-gen/bl ... ze2fs_once

Re: Resize Root partition in .img file

Posted: Mon Nov 05, 2018 12:05 pm
by laquica
TimG wrote:
Wed Oct 31, 2018 6:42 am
To prevent the automatic re-partitioning from taking place on first boot, edit cmdline.txt to remove the "quiet init=/usr/lib/raspi-config/init_resize.sh".

Once this is done you can re-partition by hand and save the resulting disk image. Or you can alter /usr/lib/raspi-config/init_resize.sh to do what you want (look for $TARGET_END in the script). The first option is easier, but the second is more flexible.

If you also want to automate resizing of the root file-system you'll need to edit /etc/init.d/resize2fs_once. You won't find it on a running RPi because it deletes itself on first boot, but you can restore it from https://github.com/RPi-Distro/pi-gen/bl ... ze2fs_once
thank you, this looks like useful, thanks.

Re: Resize Root partition in .img file -request-

Posted: Thu Dec 20, 2018 7:59 pm
by dgerman
PLEASE move the init_resize.sh script from the root partition to the boot partition and change the script line:

Code: Select all

sed -i 's| init=/usr/lib/raspi-config/init_resize.sh||' /boot/cmdline.txt
to

Code: Select all

sed --in-place=o 's| init=.*sh||' /boot/cmdline.txt  # once resized do not invoke me again. 


This will allow users running on Operating Systems without linux files system support to modify the script.

Allowing users to decide on how much space to allocate to root and even adding additional partitions like /var with other file systems.

This will NOT change existing operations.

Thank you

Re: Resize Root partition in .img file

Posted: Wed Jan 02, 2019 2:49 am
by scruss
It might be better to raise this as an issue: forum posts carry no weight. But the track record for issues on partition resizing isn't good: Search · org:RPi-Distro resize - all closed, no action.

Editing a shell script on a Windows machine and getting CRLFs everywhere seldom ends as expected.