User avatar
n3tw0rk5
Posts: 216
Joined: Fri Jan 06, 2012 6:04 pm

Re: Dynamically extend the root partition

Mon May 07, 2012 11:43 pm

Ok folks, is it possible to extend the Raspi debian root partition on the fly from command line?

It's an 8gb sd card so there's plenty of space on there, I know a few peeps seem to be using gparted on laptops, but i'm ssh'ing remotely to the raspi.

I know I could just create new partitions/lvols and just mount /home /tmp, etc separately, but I want to know if it's possible to extend / dynamically.

Thanks in advance.

User avatar
ukscone
Forum Moderator
Forum Moderator
Posts: 3872
Joined: Fri Jul 29, 2011 2:51 pm
Contact: Website

Re: Dynamically extend the root partition

Mon May 07, 2012 11:54 pm

yes it's possible although i'd personally do it on another linux computer rather than on a running system (but that's just me) and it's not really something you should do as a beginner but you have to start somewhere so if you don't mind trashing your sd card and having to redo it then go for it

you need to change the partition size using fdisk and then use resize2fs to shrink/expand the partition.

so something like

sudo fdisk /dev/mmcblk0

delete the partition and then recreate it but with a larger size

resize2fs /dev/mmclk0p2

see http://linux.die.net/man/8/resize2fs

User avatar
n3tw0rk5
Posts: 216
Joined: Fri Jan 06, 2012 6:04 pm

Re: Dynamically extend the root partition

Tue May 08, 2012 12:11 am

I'm used to using lvextend n fsadm on hp-ux to do this, but i'm still fairly green to linux, i'll have a play on a vm and see how it goes.

Thanks for the info.

User avatar
ukscone
Forum Moderator
Forum Moderator
Posts: 3872
Joined: Fri Jul 29, 2011 2:51 pm
Contact: Website

Re: Dynamically extend the root partition

Tue May 08, 2012 12:40 am

n3tw0rk5 said:


I'm used to using lvextend n fsadm on hp-ux to do this


you should have no problem then

NeilNjae
Posts: 16
Joined: Tue Apr 03, 2012 9:44 am
Contact: Website

Re: Dynamically extend the root partition

Tue May 08, 2012 9:50 am

n3tw0rk5 said:


I know I could just create new partitions/lvols and just mount /home /tmp, etc separately, but I want to know if it's possible to extend / dynamically.


Here's what I did (copied from a post on the Debian forum) to manually resize the partiton. I don't know about automatic resizing.

I did this on Ubuntu, with the card as /dev/sdd.

Start with the SD card image written to the card, in a card reader, but with nothing mounted.


$ sudo parted /dev/sdd

(parted) unit chs

(parted) print


(this gave me:)

Model: Generic- SD/MMC (scsi)
Disk /dev/sdd: 121535,3,31
Sector size (logical/physical): 512B/512B
BIOS cylinder,head,sector geometry: 121536,4,32. Each cylinder is 65.5kB.
Partition Table: msdos

Number Start End Type File system Flags
1 16,0,0 1215,3,31 primary fat32 lba
2 1232,0,0 26671,3,31 primary ext4
3 26688,0,0 29743,3,31 primary linux-swap(v1)

(Then carry on…)


(parted) move 3 118479,0,0

(parted) rm 2

(parted) mkpart primary 1232,0,0 118478,3,31

(parted) quit

$ sudo e2fsck -f /dev/sdd2


(allow it to add lost-and-found)


$ sudo resize2fs /dev/sdd2


Then put the card in the RPi and boot. You end up with a 7Gb partition…

$ df -h
Filesystem Size Used Avail Use% Mounted on
tmpfs 94M 4.0K 94M 1% /lib/init/rw
udev 10M 168K 9.9M 2% /dev
tmpfs 94M 0 94M 0% /dev/shm
rootfs 7.1G 1.3G 5.4G 20% /
/dev/mmcblk0p1 75M 28M 48M 37% /boot

Max

Re: Dynamically extend the root partition

Tue May 08, 2012 2:19 pm

NeilNjae said:


Number Start End Type File system Flags
1 16,0,0 1215,3,31 primary fat32 lba
2 1232,0,0 26671,3,31 primary ext4
3 26688,0,0 29743,3,31 primary linux-swap(v1)



Perhaps the swap partition could be moved before the main partition in the next version of the official image?

Might make it more trival to extend the root partition from within the system by users who might not have access to a normal Linux computer to perform the operation.

resize2fs is also known to work fine on mounted live systems.

But with the swap partition behind, swap has to be disabled first and the swap partition moved. Which is potentially dangerous if a novice user has other windows open, is close to the memory limit, and then launches a file system modifying utility that might be killed by the kernel for running low of memory.

MarkyMark
Posts: 23
Joined: Fri Mar 09, 2012 3:50 pm

Re: Dynamically extend the root partition

Tue May 08, 2012 11:23 pm

I've managed to use gparted to move the swap partition out of the way (To the end of the SD card) but what is the command to resize the mounted root partition to the end of unallocated space?

resize2fs just reports there is nothing todo.

Many Thanks

Mark

Max

Re: Dynamically extend the root partition

Wed May 09, 2012 2:15 am

MarkyMark said:

I've managed to use gparted to move the swap partition out of the way (To the end of the SD card) but what is the command to resize the mounted root partition to the end of unallocated space?

gparted may refuse to touch the live root partition, but sfdisk has less moral issues.

If swap is already out of the way:


pi@raspberrypi:~$ sudo sfdisk -N2 --no-reread /dev/sda

Disk /dev/sda: 45013 cylinders, 4 heads, 32 sectors/track
Old situation:
Units = cylinders of 65536 bytes, blocks of 1024 bytes, counting from 0

Device Boot Start     End   #cyls    #blocks   Id  System
/dev/sda1         16    1215    1200      76800    c  W95 FAT32 (LBA)
/dev/sda2       1232   26671   25440    1628160   83  Linux
/dev/sda3      41952   45007    3056     195584   82  Linux swap / Solaris
/dev/sda4          0       -       0          0    0  Empty
Input in the following format; absent fields get a default value.
<start> <size> <type [E,S,L,X,hex]> <bootable [-,*]> <c,h,s> <c,h,s>
Usually you only need to specify <start> and <size> (and perhaps <type>).


>> The main partition /dev/sda2 starts at 1232

>> in my example the moved swap partition starts at 41952. So the maximum size of /dev/sda2 could be 41952 - 1232 = 40720. make sure you do your own calculation

>> Enter the start and the size you calculated separated by spaces.


/dev/sda2 :1232 40720
/dev/sda2       1232   41951   40720    2606080   83  Linux
New situation:
Units = cylinders of 65536 bytes, blocks of 1024 bytes, counting from 0

Device Boot Start     End   #cyls    #blocks   Id  System
/dev/sda1         16    1215    1200      76800    c  W95 FAT32 (LBA)
/dev/sda2       1232   41951   40720    2606080   83  Linux
/dev/sda3      41952   45007    3056     195584   82  Linux swap / Solaris
/dev/sda4          0       -       0          0    0  Empty
Warning: no primary partition is marked bootable (active)
This does not matter for LILO, but the DOS MBR will not boot this disk.
Do you want to write this to disk? [ynq] y
Successfully wrote the new partition table

Re-reading the partition table ...
BLKRRPART: Device or resource busy
The command to re-read the partition table failed.
Run partprobe(8), kpartx(8) or reboot your system now,
before using mkfs

If you created or changed a DOS partition, /dev/foo7, say, then use dd(1)
to zero the first 512 bytes:  dd if=/dev/zero of=/dev/foo7 bs=512 count=1
(See fdisk(8).)

pi@raspberrypi:~$ sudo reboot

pi@raspberrypi:~$ sudo resize2fs /dev/sda2
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/sda2 is mounted on /; on-line resizing required
old desc_blocks = 1, new_desc_blocks = 1
Performing an on-line resize of /dev/sda2 to 651520 (4k) blocks.
The filesystem on /dev/sda2 is now 651520 blocks long.


woodinblack
Posts: 39
Joined: Mon Mar 19, 2012 12:05 am

Re: Dynamically extend the root partition

Wed May 09, 2012 1:09 pm

I have used gparted in X (I can't remember the commands of the terminal, and it is much easier to just slide the swap to the right then grab the right edge of the main and expand it to the end.

However, I always did this while booted from another SD card, with the card I was editing in a USB SD reader.

Can you do this live?

Max

Re: Dynamically extend the root partition

Wed May 09, 2012 11:49 pm

woodinblack said:


I have used gparted in X (I can't remember the commands of the terminal, and it is much easier to just slide the swap to the right then grab the right edge of the main and expand it to the end.

However, I always did this while booted from another SD card, with the card I was editing in a USB SD reader.

Can you do this live?



gparted is in the menu under "Preferences". But it is too protective, shows a lock icon next to the live root file system and will not let you modify it.

sfdisk+resize2fs does allow you that.

woodinblack
Posts: 39
Joined: Mon Mar 19, 2012 12:05 am

Re: Dynamically extend the root partition

Thu May 10, 2012 6:54 am

Max said:


gparted is in the menu under "Preferences". But it is too protective, shows a lock icon next to the live root file system and will not let you modify it.

sfdisk+resize2fs does allow you that.


On mine it is under other, but I start it from the terminal, as it doesn't start from the menu without requiring a password to an account that doesn't exist.

Does sfdisk+resize2fs have a UI then?

I was actually just knocking up an OSX app to expand the partition while writing it anyway.

User avatar
gordon@drogon.net
Posts: 1953
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK
Contact: Website

Re: Dynamically extend the root partition

Thu May 10, 2012 12:35 pm

Bit late to reply but I put some stuff here

https://projects.drogon.net/raspberry-p ... al-setup1/

about repartitioning, etc.

What I've done is to delete the swap partition and replace it with a file - you just need a small swap file, if that - it's really really slow when it swaps, so sort of self-defeating, however a little bit of swap cane free up enough RAM for file/disk IO buffers to make things a little smoother.

Gordon
--
Gordons projects: https://projects.drogon.net/

MarkyMark
Posts: 23
Joined: Fri Mar 09, 2012 3:50 pm

Re: Dynamically extend the root partition

Thu May 10, 2012 8:46 pm

Max/GordonH/All

Many thanks for all that info - worked a treat!! Now go plenty of space. Gordon - your tut is v good

Cheers

Mark

lurgen
Posts: 1
Joined: Fri May 11, 2012 1:32 am

Re: Dynamically extend the root partition

Fri May 11, 2012 2:37 am

Bit late to reply but I put some stuff here

https://projects.drogon.net/raspberry-p ... al-setup1/

about repartitioning, etc.

What I"ve done is to delete the swap partition and replace it with a file - you just need a small swap file, if that - it"s really really slow when it swaps, so sort of self-defeating, however a little bit of swap cane free up enough RAM for file/disk IO buffers to make things a little smoother.

Gordon

A superb set of instructions that should be merged into the official newbie guide. Much appreciated Gordon.

fillan
Posts: 1
Joined: Mon May 14, 2012 8:04 pm

Re: Dynamically extend the root partition

Mon May 14, 2012 8:12 pm

I followed the hints here but when I try resize2fs I get this:
pi@raspberrypi:~$ sudo resize2fs /dev/mmcblk0p2
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/mmcblk0p2 is mounted on /; on-line resizing required
old desc_blocks = 1, new_desc_blocks = 1
resize2fs: Permission denied to resize filesystem

Does anybody know why?

Joe Schmoe
Posts: 4277
Joined: Sun Jan 15, 2012 1:11 pm

Re: Dynamically extend the root partition

Mon May 14, 2012 8:46 pm

I know the focus here is on doing it while the machine is on - i.e., live (insert the car mechanic/surgeon joke here) - but it really is easier to do it "offline" - that is, from a running Linux system other than the Pi.

It occurs to me that you ought to be able to do this all manually, with a lot less risk (and by "risk", I mean the need to blindly follow a bunch of cryptic instructions with the knowledge that any typo could be fatal). I don't mean to imply that my instructions won't be cryptic (to the non-Linux user), but at least the overall scheme makes sense and doesn't feel like walking a tightrope.

So, suppose we have a 16Gb SD card and a 2GB Debian image, and we are running on a Linux system with the SD card at /dev/sdd. So, we use fdisk to partition the SD card, first removing whatever FAT partition was there from the factory. We create 3 partitions, in order, a 75M FAT partition, a 200M Linux swap partition (type 82), and the rest as Linux (type 83). We mount the first partition of the image using loop, and, after mkdosfs'ing the first partition, mkswap'ing the second, and mke2fs'ing the third, we copy all the files from partition of the image to the first partition of the card - and change cmdline.txt to use the 3rd, not second partition as root. Then we mount the second partition of the image using loop and copy everything from there to the 3rd partition on the card.

Then we should be done...
And some folks need to stop being fanboys and see the forest behind the trees.

(One of the best lines I've seen on this board lately)

User avatar
RaspberryPiBeginners
Posts: 218
Joined: Fri May 11, 2012 6:54 am
Location: Soham, Cambridgeshire
Contact: Website

Re: Dynamically extend the root partition

Tue May 15, 2012 8:07 am

http://www.youtube.com/watch?v=R4VovMDnsIE << this Video will show you step by step how to do exactly this.
Click here for my unofficial YouTube Channel > http://goo.gl/oFYBD

SwitchDoctor
Posts: 1
Joined: Tue Jun 12, 2012 3:28 pm

Re: Dynamically extend the root partition

Tue Jun 12, 2012 3:59 pm

Followed the video, worked great apart from re-creating the swap file.

My 'top' shows zeros across the swap line and then 98M cached.

i've also heard a rumour of a utility in the bundle that allows you to do this automatically?

fektom
Posts: 2
Joined: Sat Dec 22, 2012 6:03 pm

Re: Dynamically extend the root partition

Sat Dec 22, 2012 6:12 pm

Hello everybody!

I need help, because your writings didn't really work at my raspi.

First I tried to resize with the first boot config menu. I set to automatically resize the partition in the next boot-up process. I got an error message: "Invalid argument, While trying to add group #25"

Resize2fs continously try to resize my 16Gbyte sd card with every boot-up, with no success.

I tried manually based upon the youtube video, but at the end (with resize2fs), I always get the same error message. Is there anyone, who knows what to do in this case? This situation is quite annoying.

fektom
Posts: 2
Joined: Sat Dec 22, 2012 6:03 pm

Re: Dynamically extend the root partition

Sun Dec 23, 2012 7:36 pm

Hello again!

I've got solution for the problem.
Autoresize from raspi-config sux! Doesn't worked with my 16gig ultrafast sdhc card.

Manually doing the resize I made something wrong. With fdisk, I wrote wrong last sector number for the Linux partition. I used the very last sector indicating the end of the partition table, however, you must to use the last before the very last sector to sign the end of linux partition. That's why resize2fs didn't work for me manually. Hope, you will have much less problem with this.
Good summarizing link for resizing the raspi:
http://elinux.org/RPi_Resize_Flash_Partitions

Good luck!

relnah
Posts: 4
Joined: Thu Dec 27, 2012 1:38 pm

Re: Dynamically extend the root partition

Thu Dec 27, 2012 1:46 pm

Hi!
I get the exact same error from resize2fs, "Invalid argument, While trying to add group #25".

I've tried both raspi-configs option to exapnd and to manually expand with fdisk (https://projects.drogon.net/raspberry-p ... al-setup1/). I used the second last sector as end of the partition, actually hitting enter to let fdisk set it.

I'm running the recommended wheezy on a Kingston 32GB class 10 card.

Any suggestions how to get my partition to expand?

Regards,
Relnah

relnah
Posts: 4
Joined: Thu Dec 27, 2012 1:38 pm

Re: Dynamically extend the root partition

Fri Dec 28, 2012 12:53 pm

Hi!

Just wanted to post what solved my problem.
Re write the image :)

I'm guessing I cut the power before the resize was done the very first time I did it and it could never really recover from that.

I re-wrote the image and ran raspi-config expanding the fs. On reboot I waited until the act led had been off for a minute or so. It was constantly on for somewhere between 30 min and an hour.

Regards,
Relnah

Return to “Beginners”

Who is online

Users browsing this forum: No registered users and 23 guests