Difference between revisions of "RPi Resize Flash Partitions"

From eLinux.org
Jump to: navigation, search
(Added section about resizing with GParted)
(I addded a few things to help people out.)
 
(15 intermediate revisions by 13 users not shown)
Line 1: Line 1:
[[Category:RaspberryPi]]
+
This page is a guide to modifying partitions on the Raspberry Pi (and Pi clones, such as the Banana Pi) for Linux-based operating systems, such as Raspian Linux, CentOS, Arch, and Linux distributions. Incorrectly using these instructions is likely to corrupt your system.
 +
 
 +
The prepared images for the Raspberry Pi are created for SD cards usually 2 GB in size. If you install it on a larger SD card than what it was designed for, you can resize or restructure the system, once it is installed, to use the full size of the SD card.
 +
 
 +
== Raspi-config ==
 +
 
 +
If using the official Raspian images released by the Raspberry Pi Foundation, you can use the <tt>raspi-config</tt> utility to resize the main partition to fill the SD card. Instructions are on the [[RPi raspi-config]] page. Read on if you want to know if you should use it.
 +
 
 +
The <tt>raspi-config</tt> tool is fully automatic. All you have to do is launch it, select the option <strong>expand_rootfs</strong> in the <tt>raspi-config</tt> menu, and reboot the Raspberry Pi. It takes some time for the changes to be made. Once it's finished, the Pi returns to a command-line or graphical login prompt.
 +
 
 +
Alternatively, you can just run <tt>raspi-config --expand-rootfs</tt> to make it a completely non-interactive process.
 +
 
 +
== Explanation ==
 +
 
 +
Storage devices need some structure that allows the operating system to locate existing files and create new ones. This is done using partitions and file systems. For a simplistic explanation, see [[Partition (basics)]], which applies to all systems that use partitioning.
 +
 
 +
A partition is a section of a storage device, which is formatted with a file system, in which the operating system creates a directory structure. The Linux system has a single directory structure starting at the root directory ("/"). Partitions are ''mounted'' at points in the directory structure, but the file system remains a single structure. Generally, users do not need to know about, or see, how partitions are used. This is different from Windows, where each partition becomes a separate drive, referenced by a letter such as <tt>C:</tt>, <tt>D:</tt>, and so on.
 +
 
 +
A storage device can have a single partition, or several partitions. Changing a partition structure might be seen as a difficult operation to perform without losing data, so that structure should be considered carefully before putting data onto a device.
 +
 
 +
If you skipped it, the [[Partition (basics)]] page gives more details.
  
 
==Manually resizing the SD card on Linux==
 
==Manually resizing the SD card on Linux==
  
Tutorial video here: http://www.youtube.com/watch?v=R4VovMDnsIE
+
A tutorial video: http://www.youtube.com/watch?v=R4VovMDnsIE
 +
 
 +
The first step is to discover where your system accesses your SD card. How the SD card shows up on your system depends largely on whether you have attached it directly into an SD card slot in your computer or into an external card reader plugged into a USB port of your computer.
 +
 
 +
The best way to discover the storage devices connected to your computer is the <tt>lsblk</tt> command. Here is the sample output:
 +
 
 +
<pre>
 +
$ lsblk
 +
NAME        MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
 +
sda          8:0    0 149.1G  0 disk
 +
├─sda1        8:1    0  48.5G  0 part
 +
└─sda2        8:2    0  96.3G  0 part
 +
mmcblk0    179:0    0  7.5G  0 disk
 +
├─mmcblk0p1 179:1    0  50MB  0 part
 +
└─mmcblk0p2 179:2    0    2G  0 part
 +
</pre>
 +
 
 +
In this sample outpet, the <tt>sda</tt> device is the computer's internal hard drive, and the <tt>mmtblk0</tt> device is an SD card that has been inserted into the computer's SD card reader.
 +
 
 +
Here is an example of an SD card in an external card reader that is plugged into a computer's USB port:
 +
 
 +
<pre>
 +
sda          8:0    0 149.1G  0 disk
 +
├─sda1        8:1    0  48.5G  0 part
 +
└─sda2        8:2    0  96.3G  0 part
 +
sdb          8:16  0 465.9G  0 disk
 +
├─sdb1        8:17  0  50MB  0 part /boot
 +
└─sdb2        8:18  0    2G  0 part /
 +
</pre>
 +
 
 +
If you are not sure which device represents your SD card, just remove your SD card and run <tt>lsblk</tt> again. The device that disappears was your SD card, so insert your card again and run <tt>lsblk</tt> and take note of the new device in the list.
 +
 
 +
===Backup===
 +
 
 +
If you have any data other than a fresh image on the SD card, <strong>backup your SD card before resizing</strong> partitions. Windows users may use the [http://hddguru.com/software/HDD-Raw-Copy-Tool/ HDD Raw Copy Tool].
 +
 
 +
Linux users can just use <tt>dd</tt> (the same command you probably used to get the image onto the SD card in the first place) or the dcfldd command (which shows the progress of the operation unlike dd). For example, to copy all contents off the device that represents your SD card into an <tt>.img</tt> file in your home directory:
 +
 
 +
You should find out the block size of your sd card first (using an improper block size can lead to issues later on... TRUST)
 +
<pre>
 +
$ sudo fdisk -l /dev/mmcblk0
 +
</pre>
 +
 
 +
Now, look at this line from the output of the previous command. It will determine what we should use as the bs (block size, not bullsh!t):
 +
<pre>
 +
Units: sectors of 1 * 512 = 512 bytes
 +
</pre>
 +
So, bs=512 would be the proper syntax in this instance. Other examples could be: bs=2MB (for block size = 2 Megabytes), or bs=4MB (for block size = 4 Megabytes), without specification, dd/dcfldd default to the bytes value.
 +
 
 +
And finally, to copy all contents off the device that represents your SD card into an <tt>.img</tt> file in your home directory:
 +
<pre>
 +
$ sudo dd if=/dev/mmcblk0 of=$HOME/sdbackup.img bs=512
 +
</pre>
 +
 
 +
===Resizing===
 +
 
 +
With the SD card in your computer or card reader, make sure it's unmounted. Use the correct device designation. In this example, I use <tt>mmcblk0</tt> but yours may differ:
 +
 
 +
<pre>
 +
$ sudo umount /dev/mmcblk0
 +
</pre>
 +
 
 +
Use the <code>parted</code> (partition editor) tool to resize the partitions.
 +
 
 +
Use <tt>parted</tt> to examine the card:
  
Following on from the instructions above, keep the newly-written SD card in the card reader, but unmounted. We'll use the <code>parted</code> (partition editor) tool to resize the partitions.
+
<pre>
* Use parted to examine the card
+
$ sudo parted /dev/mmcblk0
$ sudo parted /dev/sdd
 
 
  (parted) unit chs
 
  (parted) unit chs
 
  (parted) print
 
  (parted) print
  Disk /dev/sdd: 121535,3,31
+
  Disk /dev/mmcblk0: 121535,3,31
 
  Sector size (logical/physical): 512B/512B
 
  Sector size (logical/physical): 512B/512B
 
  BIOS cylinder,head,sector geometry: 121536,4,32.  Each cylinder is 65.5kB.
 
  BIOS cylinder,head,sector geometry: 121536,4,32.  Each cylinder is 65.5kB.
Line 19: Line 102:
 
   2      1232,0,0  26671,3,31  primary  ext4
 
   2      1232,0,0  26671,3,31  primary  ext4
 
   3      26688,0,0  29743,3,31  primary  linux-swap(v1)
 
   3      26688,0,0  29743,3,31  primary  linux-swap(v1)
: This shows how my SD card was formatted after writing the image. Notice that nothing uses the card from end of 'cylinder' 29743 to the card's maximum at 121535.  
+
</pre>
: Partition 1 is the boot partition: we'll leave that alone. Partition 2 is the root partition, which we'll grow to fill most of the card. Partition 3 is the swap space, which needs to be moved to the end of the card. Note that on some other versions of linux (and some other versions of hardware) use /sde not /sdd.
+
 
* Move the swap partition (you'll have to adjust the numbers so that the end of partition 3 is at the end cylinder/head/sector of the card)
+
Nothing uses the card from end of 'cylinder' 29743 to the card's maximum at 121535.
* to calculate the number to use in the following command do:- <code>(Maximum - (Partation 3 End - Partation 3 Start) ) - 1 = Partition 3 New Start</code> so in this example <code>(121535 - ( 29743 - 26688)) -1 = 118479 </code>
+
(parted) move 3 118479,0,0
+
Partition 1 is the boot partition. Leave that one alone. Partition 2 is the root partition, which can afford to grow to fill most of the card. If your distribution has a Partition 3 for swap space, then you need to move that to the end of the card. If your card does not have a swap partition, or if its swap partition is Partition 2, then you can skip the swap step.
* Now grow the root partition. This involves removing the partition, re-creating it, then using <code>resize2fs</code> to grow the filesystem to fill the partition. It won't destroy any data.
+
 
(parted) rm 2
+
===Swap===
(parted) mkpart primary 1232,0,0 118478,3,31
+
 
(parted) quit
+
To move the swap partition (if it exists between your root partition and the end of your SD card), first calculate how many cylinders your swap partition needs to move. To calculate the number to use: <code>(Maximum - (Partition 3 End - Partition 3 Start) ) - 1 = Partition 3 New Start</code>.
: Note that the starting address of the new partition is identical to its original value, and the ending address is immediately before the start of the swap partition.
+
 
* Now clean and resize the root partition. As before, some users may need to use /sde2 instead.
+
In this example: <code>(121535 - ( 29743 - 26688)) -1 = 118479 </code>
$ sudo e2fsck -f /dev/sdd2
+
 
: (allow it to add lost-and-found)
+
Assuming you are using <tt>parted</tt> version 2.4 or greater:
$ sudo resize2fs /dev/sdd2
+
 
* Then put the card in the RPi and boot. You end up with a 7Gb partition to use.
+
<pre>
pi@raspberrypi:~$ df -h
+
(parted) move 3 118479,0,0
 +
</pre>
 +
 
 +
===Expand the partition===
 +
 
 +
To grow the root partition, you must first remove the partition boundaries, then recreate the partition as a larger container, and then resize the file system. As scary as that sounds, it doesn't destroy data, it just redefines the area "around" that data. Even so, you should make sure that you backup any important data before attempting this!
 +
 
 +
To remove and then recreate your root partition, assuming that your root partition is numbered 2 (it may not be, if you have a swap partition in the 2 slot, so use <tt>print</tt> in parted to double check):
 +
 
 +
<pre>
 +
(parted) rm 2
 +
(parted) mkpart primary 1232,0,0 118478,3,31
 +
(parted) quit
 +
</pre>
 +
 
 +
In this example, the starting address of the new partition is identical to its original value, and the ending address is the end of the SD card (or the ''start'' of the swap partition, if you had a swap partition that you had to move).
 +
 
 +
===Clean and grow the file system===
 +
 
 +
Now that you have room for a bigger file system, you must clean and then grow the existing file system that has been displaced by the changing boundaries of its partition. Remember to use the correct device designation. In this example, I use <tt>mmcblk0p2</tt> to represent Partition 2 on an internal SD card slot, but yours may differ.
 +
 
 +
<pre>
 +
$ sudo e2fsck -f /dev/mmcblk0p2
 +
</pre>
 +
 
 +
If <tt>e2fsck</tt> asks to create a Lost+Found directory, it's OK to allow it.
 +
 
 +
Then resize:
 +
 
 +
<pre>
 +
$ sudo resize2fs /dev/mmcblk0p2
 +
</pre>
 +
 
 +
==Boot==
 +
 
 +
Your system now occupies all available space on your SD card. Put the card in your Pi and boot.  
 +
 
 +
Once booted, verify your hard work with <tt>df</tt>:
 +
 
 +
<pre>
 +
pi@raspberrypi:~$ df -h
 
  Filesystem            Size  Used Avail Use% Mounted on
 
  Filesystem            Size  Used Avail Use% Mounted on
 
  tmpfs                  94M  4.0K  94M  1% /lib/init/rw
 
  tmpfs                  94M  4.0K  94M  1% /lib/init/rw
Line 41: Line 164:
 
  rootfs                7.1G  1.3G  5.4G  20% /
 
  rootfs                7.1G  1.3G  5.4G  20% /
 
  /dev/mmcblk0p1        75M  28M  48M  37% /boot
 
  /dev/mmcblk0p1        75M  28M  48M  37% /boot
 +
</pre>
 +
 +
 +
==Manually extracting partitions from the image on Linux==
 +
 +
Get the information about offsets and sizes from the SD-Card-Image:
 +
 +
$ parted -s SD-Card-Image unit KiB print
 +
 +
Here, the -s option directs parted to go into scripting mode and the commands <tt>unit KiB print</tt> tells parted to display its results in blocks of 1024 byte (KiB, see also [http://www.gnu.org/software/coreutils/manual/html_node/Block-size.html#Block-size block size in GNU Coreutils docu]) and print the partition table.
 +
 +
You will get some information like the following (this is created with the Raspbian Wheezy image dated 15-Jul-2012):
 +
 +
Disk SD-Card-Image: 1894400kiB
 +
Sector size (logical/physical): 512B/512B
 +
Partition Table: msdos
 +
 +
Number  Start    End        Size        Type    File system  Flags
 +
  1      4096kiB  61440kiB    57344kiB    primary  fat16        lba
 +
  2      61440kiB  1894400kiB  1832960kiB  primary  ext4
 +
 +
Now you can extract the partitions with
 +
 +
  $ dd if=SD-Card-Image of=Part1 bs=1024 skip=4096 count=57344
 +
  $ dd if=SD-Card-Image of=Part2 bs=1024 skip=61440 count=1832960
  
 +
Fill in the skip and count parameters with the numbers for start and size, which you got from the parted command above.
  
==Manually resizing the SD card using a GUI on Linux==
+
==Manually resizing the SD card using a GUI with GParted==
  
If you are using a PC with a linux distribution to resize the partitions, you can run GParted to resize the partitions using a GUI. This method is tested on Ubuntu 10.10 using the Gnome desktop.
+
If you are using a PC with a linux distribution to resize the partitions, you can run GParted to resize the partitions using a GUI. This method is tested on Ubuntu 10.10 using the Gnome desktop.  Versions of GParted differ slightly in their GUIs. For Windows users, it is also possible to follow the same procedure (less the installation) using the live version of GParted ([http://gparted.org/livecd.php GParted Live CD/USB/PXE/HD]).
  
 
GParted can be installed using:   
 
GParted can be installed using:   
Line 52: Line 201:
 
* Start GParted (on my system it is <code>[System]->[Administration]->[GParted Partition editor]</code>).
 
* Start GParted (on my system it is <code>[System]->[Administration]->[GParted Partition editor]</code>).
 
* Select the drive corresponding to your SD card (was <code>/dev/sdh/</code> on my system). You now see the partitions mentioned above (with some tiny unallocated areas in between and a large one after).
 
* Select the drive corresponding to your SD card (was <code>/dev/sdh/</code> on my system). You now see the partitions mentioned above (with some tiny unallocated areas in between and a large one after).
* Select the swap partition by clicking on it. Select the menu option <code>[Partition]->[Resize/Move]</code> and drag the partition to the right (click/drag in the middle).
+
* Select the swap partition by clicking on it.
* Select the ext4 partition and then the <code>[Resize/Move]</code> menu option. Drag the right edge of the partition all the way to the right (click/drag the right edge).
+
* If the Resize/Move toolbar icon or <code>[Resize/Move]</code> menu option is disabled, go to Partition / Unmount.
* When you are satisfied with the changes, click on the green check mark to execute these changes.
+
* Select the menu option <code>[Partition]->[Resize/Move]</code> and drag the partition to the right (click/drag in the middle).
 +
* Select the ext4 partition.
 +
* If the Resize/Move toolbar icon or <code>[Resize/Move]</code> menu option is disabled, go to Partition / Unmount.
 +
* Resize the partition by dragging the right edge of the partition all the way to the right (click/drag the right edge).
 +
* When you are satisfied with the changes, click on the green check mark, "Return" arrow, or other "apply" control to execute these changes.
 
    
 
    
 
You're done!
 
You're done!
 
  
 
==Manually resizing the SD card on Raspberry Pi==
 
==Manually resizing the SD card on Raspberry Pi==
Line 67: Line 219:
 
Start fdisk:
 
Start fdisk:
  
  sudo fdisk -cu /dev/mmcblk0
+
  sudo fdisk /dev/mmcblk0
  
 
Then delete partitions with ''d'' and create a new with ''n''. You can view the existing table with ''p''.
 
Then delete partitions with ''d'' and create a new with ''n''. You can view the existing table with ''p''.
Line 81: Line 233:
 
   sudo shutdown -r now
 
   sudo shutdown -r now
  
After the reboot you need to resize the actual partition. The <code>resize2fs</code> will resize your main partition to the new size from the changed partition table.
+
After the reboot you need to resize the filesystem on the partition. The <code>resize2fs</code> command will resize your filesystem to the new size from the changed partition table.
  
 
  sudo resize2fs /dev/mmcblk0p2
 
  sudo resize2fs /dev/mmcblk0p2
Line 90: Line 242:
  
 
  df -h
 
  df -h
 +
 +
[[Category:RPi Linux]]

Latest revision as of 17:08, 2 March 2017

This page is a guide to modifying partitions on the Raspberry Pi (and Pi clones, such as the Banana Pi) for Linux-based operating systems, such as Raspian Linux, CentOS, Arch, and Linux distributions. Incorrectly using these instructions is likely to corrupt your system.

The prepared images for the Raspberry Pi are created for SD cards usually 2 GB in size. If you install it on a larger SD card than what it was designed for, you can resize or restructure the system, once it is installed, to use the full size of the SD card.

Raspi-config

If using the official Raspian images released by the Raspberry Pi Foundation, you can use the raspi-config utility to resize the main partition to fill the SD card. Instructions are on the RPi raspi-config page. Read on if you want to know if you should use it.

The raspi-config tool is fully automatic. All you have to do is launch it, select the option expand_rootfs in the raspi-config menu, and reboot the Raspberry Pi. It takes some time for the changes to be made. Once it's finished, the Pi returns to a command-line or graphical login prompt.

Alternatively, you can just run raspi-config --expand-rootfs to make it a completely non-interactive process.

Explanation

Storage devices need some structure that allows the operating system to locate existing files and create new ones. This is done using partitions and file systems. For a simplistic explanation, see Partition (basics), which applies to all systems that use partitioning.

A partition is a section of a storage device, which is formatted with a file system, in which the operating system creates a directory structure. The Linux system has a single directory structure starting at the root directory ("/"). Partitions are mounted at points in the directory structure, but the file system remains a single structure. Generally, users do not need to know about, or see, how partitions are used. This is different from Windows, where each partition becomes a separate drive, referenced by a letter such as C:, D:, and so on.

A storage device can have a single partition, or several partitions. Changing a partition structure might be seen as a difficult operation to perform without losing data, so that structure should be considered carefully before putting data onto a device.

If you skipped it, the Partition (basics) page gives more details.

Manually resizing the SD card on Linux

A tutorial video: http://www.youtube.com/watch?v=R4VovMDnsIE

The first step is to discover where your system accesses your SD card. How the SD card shows up on your system depends largely on whether you have attached it directly into an SD card slot in your computer or into an external card reader plugged into a USB port of your computer.

The best way to discover the storage devices connected to your computer is the lsblk command. Here is the sample output:

$ lsblk
NAME        MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sda           8:0    0 149.1G  0 disk 
├─sda1        8:1    0  48.5G  0 part 
└─sda2        8:2    0  96.3G  0 part 
mmcblk0     179:0    0   7.5G  0 disk 
├─mmcblk0p1 179:1    0   50MB  0 part 
└─mmcblk0p2 179:2    0     2G  0 part

In this sample outpet, the sda device is the computer's internal hard drive, and the mmtblk0 device is an SD card that has been inserted into the computer's SD card reader.

Here is an example of an SD card in an external card reader that is plugged into a computer's USB port:

sda           8:0    0 149.1G  0 disk 
├─sda1        8:1    0  48.5G  0 part 
└─sda2        8:2    0  96.3G  0 part 
sdb           8:16   0 465.9G  0 disk 
├─sdb1        8:17   0   50MB  0 part /boot
└─sdb2        8:18   0     2G  0 part /

If you are not sure which device represents your SD card, just remove your SD card and run lsblk again. The device that disappears was your SD card, so insert your card again and run lsblk and take note of the new device in the list.

Backup

If you have any data other than a fresh image on the SD card, backup your SD card before resizing partitions. Windows users may use the HDD Raw Copy Tool.

Linux users can just use dd (the same command you probably used to get the image onto the SD card in the first place) or the dcfldd command (which shows the progress of the operation unlike dd). For example, to copy all contents off the device that represents your SD card into an .img file in your home directory:

You should find out the block size of your sd card first (using an improper block size can lead to issues later on... TRUST)

$ sudo fdisk -l /dev/mmcblk0

Now, look at this line from the output of the previous command. It will determine what we should use as the bs (block size, not bullsh!t):

Units: sectors of 1 * 512 = 512 bytes

So, bs=512 would be the proper syntax in this instance. Other examples could be: bs=2MB (for block size = 2 Megabytes), or bs=4MB (for block size = 4 Megabytes), without specification, dd/dcfldd default to the bytes value.

And finally, to copy all contents off the device that represents your SD card into an .img file in your home directory:

$ sudo dd if=/dev/mmcblk0 of=$HOME/sdbackup.img bs=512

Resizing

With the SD card in your computer or card reader, make sure it's unmounted. Use the correct device designation. In this example, I use mmcblk0 but yours may differ:

$ sudo umount /dev/mmcblk0

Use the parted (partition editor) tool to resize the partitions.

Use parted to examine the card:

$ sudo parted /dev/mmcblk0
 (parted) unit chs
 (parted) print
 Disk /dev/mmcblk0: 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)

Nothing uses the card from end of 'cylinder' 29743 to the card's maximum at 121535.

Partition 1 is the boot partition. Leave that one alone. Partition 2 is the root partition, which can afford to grow to fill most of the card. If your distribution has a Partition 3 for swap space, then you need to move that to the end of the card. If your card does not have a swap partition, or if its swap partition is Partition 2, then you can skip the swap step.

Swap

To move the swap partition (if it exists between your root partition and the end of your SD card), first calculate how many cylinders your swap partition needs to move. To calculate the number to use: (Maximum - (Partition 3 End - Partition 3 Start) ) - 1 = Partition 3 New Start.

In this example: (121535 - ( 29743 - 26688)) -1 = 118479

Assuming you are using parted version 2.4 or greater:

(parted) move 3 118479,0,0

Expand the partition

To grow the root partition, you must first remove the partition boundaries, then recreate the partition as a larger container, and then resize the file system. As scary as that sounds, it doesn't destroy data, it just redefines the area "around" that data. Even so, you should make sure that you backup any important data before attempting this!

To remove and then recreate your root partition, assuming that your root partition is numbered 2 (it may not be, if you have a swap partition in the 2 slot, so use print in parted to double check):

(parted) rm 2
(parted) mkpart primary 1232,0,0 118478,3,31
(parted) quit

In this example, the starting address of the new partition is identical to its original value, and the ending address is the end of the SD card (or the start of the swap partition, if you had a swap partition that you had to move).

Clean and grow the file system

Now that you have room for a bigger file system, you must clean and then grow the existing file system that has been displaced by the changing boundaries of its partition. Remember to use the correct device designation. In this example, I use mmcblk0p2 to represent Partition 2 on an internal SD card slot, but yours may differ.

$ sudo e2fsck -f /dev/mmcblk0p2

If e2fsck asks to create a Lost+Found directory, it's OK to allow it.

Then resize:

$ sudo resize2fs /dev/mmcblk0p2

Boot

Your system now occupies all available space on your SD card. Put the card in your Pi and boot.

Once booted, verify your hard work with df:

pi@raspberrypi:~$ 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


Manually extracting partitions from the image on Linux

Get the information about offsets and sizes from the SD-Card-Image:

$ parted -s SD-Card-Image unit KiB print

Here, the -s option directs parted to go into scripting mode and the commands unit KiB print tells parted to display its results in blocks of 1024 byte (KiB, see also block size in GNU Coreutils docu) and print the partition table.

You will get some information like the following (this is created with the Raspbian Wheezy image dated 15-Jul-2012):

Disk SD-Card-Image: 1894400kiB
Sector size (logical/physical): 512B/512B
Partition Table: msdos

Number  Start     End         Size        Type     File system  Flags
 1      4096kiB   61440kiB    57344kiB    primary  fat16        lba
 2      61440kiB  1894400kiB  1832960kiB  primary  ext4

Now you can extract the partitions with

 $ dd if=SD-Card-Image of=Part1 bs=1024 skip=4096 count=57344
 $ dd if=SD-Card-Image of=Part2 bs=1024 skip=61440 count=1832960

Fill in the skip and count parameters with the numbers for start and size, which you got from the parted command above.

Manually resizing the SD card using a GUI with GParted

If you are using a PC with a linux distribution to resize the partitions, you can run GParted to resize the partitions using a GUI. This method is tested on Ubuntu 10.10 using the Gnome desktop. Versions of GParted differ slightly in their GUIs. For Windows users, it is also possible to follow the same procedure (less the installation) using the live version of GParted (GParted Live CD/USB/PXE/HD).

GParted can be installed using:

  • sudo apt-get install gparted

Note: I had to physically remove and re-insert the SD card from the card reader after writing the image before the partitions were recognised properly and the following could be done.

  • Start GParted (on my system it is [System]->[Administration]->[GParted Partition editor]).
  • Select the drive corresponding to your SD card (was /dev/sdh/ on my system). You now see the partitions mentioned above (with some tiny unallocated areas in between and a large one after).
  • Select the swap partition by clicking on it.
  • If the Resize/Move toolbar icon or [Resize/Move] menu option is disabled, go to Partition / Unmount.
  • Select the menu option [Partition]->[Resize/Move] and drag the partition to the right (click/drag in the middle).
  • Select the ext4 partition.
  • If the Resize/Move toolbar icon or [Resize/Move] menu option is disabled, go to Partition / Unmount.
  • Resize the partition by dragging the right edge of the partition all the way to the right (click/drag the right edge).
  • When you are satisfied with the changes, click on the green check mark, "Return" arrow, or other "apply" control to execute these changes.

You're done!

Manually resizing the SD card on Raspberry Pi

You can also resize the partitions of the SD card that your Pi is running on.

First you need to change the partition table with fdisk. You need to remove the existing partition entries and then create a single new partition than takes the whole free space of the disk. This will only change the partition table, not the partitions data on disk. The start of the new partition needs to be aligned with the old partition!

Start fdisk:

sudo fdisk /dev/mmcblk0

Then delete partitions with d and create a new with n. You can view the existing table with p.

  • p to see the current start of the main partition
  • d, 3 to delete the swap partition
  • d, 2 to delete the main partition
  • n p 2 to create a new primary partition, next you need to enter the start of the old main partition and then the size (enter for complete SD card). The main partition on the Debian image from 2012-04-19 starts at 157696, but the start of your partition might be different. Check the p output!
  • w write the new partition table

Now you need to reboot:

 sudo shutdown -r now

After the reboot you need to resize the filesystem on the partition. The resize2fs command will resize your filesystem to the new size from the changed partition table.

sudo resize2fs /dev/mmcblk0p2

This will take a few minutes, depending on the size and speed of your SD card.

When it is done, you can check the new size with:

df -h