Resize the root filesystem


7 posts
by truehl » Fri Jul 13, 2012 9:37 pm
Hi,
I'm searching for a solution (a script) to resize the root partition /dev/mmcblk0p2 to the full space of an SD card, like the script in the new wheezy image. I tried to extract the resize mechanism from the wheezy script without luck.

Anyone an idea?
truehl
http://www.squeezeplug.de
User avatar
Posts: 619
Joined: Sun Mar 04, 2012 6:47 pm
by aaa801 » Sat Jul 14, 2012 1:44 am
sudo fdisk -cu /dev/mmcblk0 < /bin/fdisk.input
shutdown -r now

then

resize2fs /dev/mmcblk0p2
df -h

Should do it

fdisk.input
Code: Select all
p
d
3
n
p
3


w
Posts: 427
Joined: Mon Jun 04, 2012 9:06 pm
Location: Berkshire
by truehl » Sat Jul 14, 2012 7:05 am
aaa801 wrote:sudo fdisk -cu /dev/mmcblk0 < /bin/fdisk.input
shutdown -r now

then

resize2fs /dev/mmcblk0p2
df -h

Should do it

fdisk.input
Code: Select all
p
d
3
n
p
3


w

Thanks for that, I have not tried it but are you sure it will work? From what I can see the script deletes the swap partition and write it new. Then reboot an resize the root partition. I don't understand this. The other thing is, that the wheezy start script changes the boot behaivor, so that the resizing will be executed automatically once.

Greetings, Thomas
truehl
http://www.squeezeplug.de
User avatar
Posts: 619
Joined: Sun Mar 04, 2012 6:47 pm
by Joe Schmoe » Sat Jul 14, 2012 7:15 am
Needless to say, I'd be really careful about doing something like this, blind, based on something some guy posted to a forum. Looking at what he wrote, it looks like it is missing a step or two.

As I recall, the basic method is:

1) Delete the swap partition (3).
2) Delete the main partition (2).
3) Re-create the main partition.

And here's the thing - in previous go-arounds with this, there was some reason why you had to know some magic number (Check the YouTube video for confirmation of this), so it wasn't possible to do it "blind". But I don't remember off-hand what that reason was. I'm sure someone else will supply this information by-and-by.
Never answer the question you are asked. Rather, answer the question you wish you had been asked.

- Robert S. McNamara - quoted in "Fog of War" -
Posts: 2731
Joined: Sun Jan 15, 2012 1:11 pm
by truehl » Sat Jul 14, 2012 7:56 am
This is the code from the Wheezy script that should be used for ridizing:
Code: Select all
  # Get the starting offset of the root partition
  PART_START=$(parted /dev/mmcblk0 -ms unit s p | grep "^2" | cut -f 2 -d:)
  [ "$PART_START" ] || return 1
  # Return value will likely be error for fdisk as it fails to reload the
  # partition table because the root fs is mounted
  fdisk /dev/mmcblk0 <<EOF
p
d
2
n
p
2
$PART_START

p
w
EOF

  # now set up an init.d script
cat <<\EOF > /etc/init.d/resize2fs_once &&
#!/bin/sh
### BEGIN INIT INFO
# Provides:          resize2fs_once
# Required-Start:
# Required-Stop:
# Default-Start: 2 3 4 5 S
# Default-Stop:
# Short-Description: Resize the root filesystem to fill partition
# Description:
### END INIT INFO

. /lib/lsb/init-functions

case "$1" in
  start)
    log_daemon_msg "Starting resize2fs_once" &&
    resize2fs /dev/mmcblk0p2 &&
    rm /etc/init.d/resize2fs_once &&
    update-rc.d resize2fs_once remove &&
    log_end_msg $?
    ;;
  *)
    echo "Usage: $0 start" >&2
    exit 3
    ;;
esac
EOF
  chmod +x /etc/init.d/resize2fs_once &&
  update-rc.d resize2fs_once defaults &&
  whiptail --msgbox "Root partition has been resized.\n\
The filesystem will be enlarged upon the next reboot" 20 60 2

I thinks there is a problem with the runlevels not matching to Squeeze ?! Maybe someone can help and change the script so it fits to the "official" Debian distro?!

Greetings,
Thomas
truehl
http://www.squeezeplug.de
User avatar
Posts: 619
Joined: Sun Mar 04, 2012 6:47 pm
by asb » Sat Jul 14, 2012 9:13 am
truehl: one problem is the parted bit assumes the partition layout I use in the wheezy images, which just has the FAT partition and an ext4 partition. The squeeze images had 3 partitions (they had a separate swap, while I just use a swap file).
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 799
Joined: Fri Sep 16, 2011 7:16 pm
by truehl » Sat Jul 14, 2012 2:54 pm
asb wrote:truehl: one problem is the parted bit assumes the partition layout I use in the wheezy images, which just has the FAT partition and an ext4 partition. The squeeze images had 3 partitions (they had a separate swap, while I just use a swap file).

That's not a problem for me, I like to adapt the partition layout of the Wheezy image. Swap in a File would be fine! Does this make things easier?
truehl
http://www.squeezeplug.de
User avatar
Posts: 619
Joined: Sun Mar 04, 2012 6:47 pm