Saturday, May 9, 2015

How to Resize Raspbian Image

In this tutorial I am going to explain how te resize a raspbian image so it can be used in QEMU.
I faced the problem of having no space in to install extra packages on raspbian like Qt, so I decided to increase the space of the Raspbian image to 8 GB.
After a lost of search and testings, I discovered the best way to increase the size of raspbian to 8 GB or less or more. You can resize to any size, but make sure it is more than 3 GB; because Raspbian itself requires about 2.5 GB.



Caution: Please make a backup from the Raspbian raw file because we will need it at the end of this tutorial.


Step 1: Resizing the raw image file using QEMU.

# qemu-img resize +
For example:
# qemu-img resize 2015-02-16-raspbian-wheezy.img +5G
Note:
The 5G here will be added to the size of the original raw image. For example, raspbian image is 3 GBs and now it will become * GBs (3 GBs + 5 GBs).

Step 2: Resizing the root partition of Raspbian.

After first step you will notice that the root partition still 3 GBs; this because partition table of the raw image knows nothing about the change, since we only changed the raw file. So we need to do the following steps:

1- Associate the raw file of Raspbian image with a loop device.

# losetup -P
For example:
# losetup -P  /dev/loop0 2015-02-16-raspbian-wheezy-8GB.img

2- Delete the root partition:

Note that the root partition number is 2.
# fdisk /dev/loop0
Now enter "d" and then enter "2"

3- Creating a new partition:

Note: Keep fdisk open and do the following:
1- Enter "p" to show the details of the current partitions. You will see only one small partition.

Note: We need the value inside the box witch is 122879 (It may vary from raspbian version to another)
2- Enter "n" and then enter "p" to select a primapry partition (p is the the default value).

3-  Now enter the number of the partition. Enter "2" witch is the default option, or use the default option.

4-  Now enter the beginning of the partition. We need the End value of the first partition from step 1 witch is in our case 122879. We add 1 to this value and the enter it to fdisk. Now the new partition will begin at 122880.

Note: if you used the default value for the beginning  of the new partition, you will end up with a very small partition; because you will be allocating the free space before the first partition witch is very small and it is devoted for MBR and partition table, so you will also corrupt the raw image.
 
5- Choose the end of the new partition. Just leave the default vale.

6- Now we need to set the type of the new partition. Enter "t"

7- Enter the number of the partition for witch we will change type. In our case it is 2
8- Enter the number of the type of the file system. In our case it is 83.
9- Now enter "w" to write all changes to disk.

4- Update partition table that the kernel is using.

# partprobe

5- Format the newly create partition.

To get the path of the partition in dev folder, we use fdisk to print the partition table of the raw file
# fdisk /dev/loop0
 Now Enter "p"

 Notice the newly created partition is associated with /dev/loop0p2
To format the newly created partition
# mkfs.ext4 /dev/loop0p2 
Now, we have a new partition that has no data inside it. We need o copy all files from the original Raspbian image to the new partition.
Note: We cannot use dd; because if we did, we will end up with a partition that is 3 GBs size and that is the size of the original partition in the Raspbian raw file.

We need to associate the original Raspbian raw file with a loop device
# losetup -P /dev/loop10 2015-02-16-raspbian-wheezy.img
Now mount both the newly created partition and the root partition from the original Raspbian raw file using any mount method that you prefer.
Then copy all folders and file in the rot partition of the original Raspbian raw file to the newly created partition without altering the permissions of files nor their owners.
# cp -rp /*
Foe example
# cp -rp /run/media/eng.mahmoud/f24a4949-f4b2-4cad-a780-a138695079ec/* /run/media/eng.mahmoud/6e001351-2aa9-4344-aa29-5ca8909a91da/


Congratulations, now you have your own resized Raspbian image.



No comments:

Post a Comment