Linux Raspberry Resize SD Card Image

Raspberry Pi SD Card Image is too big for another SD Card

Just lately I am engaged on venture involving Raspberry Pi. A couple of months in the past I wrote a fast start information to Raspberry Pi. Now I’ve another matter to cover that is associated to Raspberry and challenge I’m engaged on. As you already know Raspberry Pi doesn’t have arduous drive. As an alternative is used SD card – it masses operating system on SD card, it boots from SD card, it saves knowledge to SD card, and so forth. And whenever you work with SD cards from totally different producers you understand that although cards are equal in measurement they really differ with some small bytes. For example I have AData SD card which is 8G nevertheless it is barely greater that 8G Toshiba SD card. And when I attempt to save picture produced on AData on Toshiba SD card I acquired error saying that there is not sufficient area on disk.

Once I needs to read/write a picture from/to SD card on Windows I exploit Win32 Disk Imager. It is very merely and nice to have software. Right here is the error which I received from Disk Imager. Listed here are two screenshots – first one displaying file that I selected and prepared for writing and second one is error which it provides back.

Saving Bigger Raspberry SD Card Image Error

Pay attention to highlighted numbers in second screenshot those are available sectors. Actual picture has 15130624 sectors, and SD card where we’re going to reserve it has 15126528 sectors. In the event you pay nearer consideration to error message you will notice that sector measurement is 512 bytes. So let’s calculate difference

15130624 – 15126528 = 4096 sectors
4096 * 512 = 2097152 bytes distinction
2097152 / 1024 = 2048 kilobytes

With different words this is 2M difference between those 8G SD cards – one among them AData and second one Tohisba. Ant this 2Mb stops you from writing image to another SD card. In fact there is an answer which includes resizing the picture underneath Linux.

First step is to repeat that picture into Linux. Then you definitely merely run fdisk -l identify of picture

[email protected]:~/image$ fdisk -l Raspberry-Image-8G.img

Disk Raspberry-Image-8G.img: 7746 MB, 7746879488 bytes
255 heads, 63 sectors/monitor, 941 cylinders, complete 15130624 sectors
Models = sectors of 1 * 512 = 512 bytes
Sector measurement (logical/physical): 512 bytes / 512 bytes
I/O measurement (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xa6202af7

System Boot Begin Finish Blocks Id System
Raspberry-Image-8G.img1 8192 122879 57344 c W95 FAT32 (LBA)
Raspberry-Image-8G.img2 122880 15130623 7503872 83 Linux
[email protected]:~/picture$

Raspberry SD card image fdisk Info

Do not take note of heads. monitor, and so on. Those will not be valid for SD playing cards. However concentrate for complete sectors – 15130624 and to measurement of picture – 7746 MB. Notice that this picture is expanded. But typically I obtained some unexpanded photographs. You realize whenever you first boot into Raspbian first thing that is executed is sudo raspi-config. And from this menu you possibly can increase information system to use whole area on SD card. If you download and get 4G image for example and save in over 8G card you will have 4G free. They don’t seem to be used until you do not run sudo raspi-config. Listed here are more information about raspi-config. So right here how it seems to be one unexpanded image. Truly is the same image, but second partition has a half a blocks (measurement). And as an alternative to end of 15130623 sector ends 8110079.

[email protected]:~/image$ fdisk -l Raspberry-Image-8G-Not-Expanded.img

Disk Raspberry-Image-8G-Not-Expanded.img: 7746 MB, 7746879488 bytes
255 heads, 63 sectors/monitor, 941 cylinders, complete 15130624 sectors
Models = sectors of 1 * 512 = 512 bytes
Sector measurement (logical/physical): 512 bytes / 512 bytes
I/O measurement (minimum/optimum): 512 bytes / 512 bytes
Disk identifier: 0xa6202af7

Gadget Boot Begin Finish Blocks Id System
Raspberry-Image-8G-Not-Expanded.img1 8192 122879 57344 c W95 FAT32 (LBA)
Raspberry-Image-8G-Not-Expanded.img2 122880 8110079 3993600 83 Linux
[email protected]:~/picture$

Why I am scripting this is as a result of studying some other manuals on the internet chances are you’ll find that you simply picture does not end at the last sector, however end on the half of sectors or elsewhere. Any method maintain that in thoughts.

Examine out there area on the right track SD card

Subsequent step is to verify obtainable area on course SD card – where you’ll write image. So insert SD card into SD card reader/dongle and then plug it into USB on the identical Linux machine. Then you should confirm to which system it maps. For that function run dmesg | tail and examine output.

[email protected]:~/picture$ dmesg | tail
[ 8727.215069] usbcore: registered new interface driver usb-storage
[ 8727.215071] USB Mass Storage help registered.
[ 8728.212952] scsi 6:0:zero:0: Direct-Access Myson SD/MMC/MS Reader 1.00 PQ: zero ANSI: 0 CCS
[ 8728.213826] sd 6:0:0:0: Hooked up scsi generic sg2 sort zero
[ 8728.214531] sd 6:0:0:0: [sdb] 15126528 512-byte logical blocks: (7.74 GB/7.21 GiB)
[ 8728.214909] sd 6:0:zero:zero: [sdb] Write Shield is off
[ 8728.214912] sd 6:0:0:zero: [sdb] Mode Sense: 03 00 00 00
[ 8728.215283] sd 6:0:zero:zero: [sdb] Write cache: enabled, read cache: enabled, doesn’t help DPO or FUA
[ 8728.219054] sdb: sdb1 sdb2
[ 8728.221659] sd 6:0:0:zero: [sdb] Hooked up SCSI removable disk
[email protected]:~/picture$

As you see my gadget is hooked up to /dev/sdb, you could be hooked up to sda, sdc, and so on. When you pay attention to these messages you will notice 15126528 512-byte logical blocks: (7.74 GB/7.21 GiB) line. So that’s the variety of sectors and, block measurement and complete measurement of target SD card. But you possibly can examine it additional with fdisk. Notice that as a way to entry /dev/sdb/ you’ll need of sudo.

[email protected]:~/picture$ sudo fdisk -l /dev/sdb

Disk /dev/sdb: 7744 MB, 7744782336 bytes
239 heads, 62 sectors/monitor, 1020 cylinders, complete 15126528 sectors
Models = sectors of 1 * 512 = 512 bytes
Sector measurement (logical/physical): 512 bytes / 512 bytes
I/O measurement (minimum/optimum): 512 bytes / 512 bytes
Disk identifier: 0xa6202af7

Gadget Boot Start End Blocks Id System
/dev/sdb1 8192 122879 57344 c W95 FAT32 (LBA)
/dev/sdb2 122880 15126527 7501824 83 Linux
[email protected]:~/picture$

SD Card Sectors, Size and Other Info

Calculate new measurement and shrink image file

As you see an important info that we focus to the moment is sectors – 15130624 – 15126528 = 4096 sectors. Keep in mind that number as we’re going to use it couple of occasions.

Subsequent step is to mount picture file with command – losetup

[email protected]:~/picture$ sudo losetup -f –show Raspberry-Image-8G.img
/dev/loop0
[email protected]:~/image$

So image file is mounted to /dev/loop0. You possibly can run fdisk on it and you will notice the same end result that from run fdisk on picture file itself.

[email protected]:~/picture$ sudo fdisk -l /dev/loop0

Disk /dev/loop0: 7746 MB, 7746879488 bytes
255 heads, 63 sectors/monitor, 941 cylinders, complete 15130624 sectors
Models = sectors of 1 * 512 = 512 bytes
Sector measurement (logical/physical): 512 bytes / 512 bytes
I/O measurement (minimum/optimum): 512 bytes / 512 bytes
Disk identifier: 0xa6202af7

System Boot Start Finish Blocks Id System
/dev/loop0p1 8192 122879 57344 c W95 FAT32 (LBA)
/dev/loop0p2 122880 15130623 7503872 83 Linux
[email protected]:~/image$

As you see in this output (and on previous as nicely) we now have two partitions within the file. The first one is boot one. Which we aren’t going to the touch. The second one is knowledge partition – /dev/loop0p2 which we’re going to shrink. For that function we have to mount solely that partition. We are going to use -o or offset parameter of losetup command. But first we have to calculate offset. Get the start sector of second partition which in my case is 122880 and multiply it by 512.

122880 * 512 = 62914560

Earlier than to mount second partition we need to unmount picture file. First command is to unmount and second to mount solely partition that we need to shrink.

[email protected]:~/picture$ sudo losetup -d /dev/loop0
[email protected]:~/image$
[email protected]:~/image$ sudo losetup -f –show -o 62914560 Raspberry-Image-8G.img
/dev/loop0
[email protected]:~/picture$

Now we will run some commands to examine file system for example e2fsck

[email protected]:~/image$ sudo e2fsck -f /dev/loop0
e2fsck 1.42.5 (29-Jul-2012)
/dev/loop0: recovering journal
Cross 1: Checking inodes, blocks, and sizes
Cross 2: Checking directory construction
Cross 3: Checking directory connectivity
Move four: Checking reference counts
Cross 5: Checking group abstract info
Free blocks rely fallacious (1158894, counted=1158871).
Fix? sure
Free inodes rely fallacious (385389, counted=385394).
Fix? sure

/dev/loop0: ***** FILE SYSTEM WAS MODIFIED *****
/dev/loop0: 88814/474208 information (0.1% non-contiguous), 717097/1875968 blocks

Once I run this command I obtained some errors. In fact I would like them to be fastened. So when program asks me I affirm.

Next step is to shrink file system. For that functions we are going to use resize2fs command. It accepts new measurement with bytes, megabytes, gigabytes or sectors. Sectors is straightforward, as a result of we just know the dimensions of sectors of previous, new one and difference. So Start sector is 122880, End sector is 15130623. So the dimensions in sector is:

15130623 – 122880 = 15007743

See distinction between picture and SD card in contrast aspect by aspect on subsequent screenshot:

Raspberry SD Card Image Compare

Difference is 4096 sectors. So we have to subtract from measurement that quantity

15007743 – 4096 = 15003647

And we have to execute the command.

[email protected]:~/picture$ sudo resize2fs -p /dev/loop0 15003647s
resize2fs 1.42.5 (29-Jul-2012)
Resizing the filesystem on /dev/loop0 to 1875455 (4k) blocks.
The filesystem on /dev/loop0 is now 1875455 blocks lengthy.
[email protected]:~/image$

Now we will examine again for errors file system.

[email protected]:~/picture$ sudo e2fsck -f /dev/loop0
e2fsck 1.42.5 (29-Jul-2012)
Move 1: Checking inodes, blocks, and sizes
Move 2: Checking directory construction
Cross three: Checking directory connectivity
Move 4: Checking reference counts
Cross 5: Checking group summary info
/dev/loop0: 88814/474208 information (0.1% non-contiguous), 717097/1875455 blocks
[email protected]:~/image$

Finally don’t forget to unmount file.

[email protected]:~/image$ sudo losetup -d /dev/loop0

At that point we are ready to write down picture from Linux. But still we will be unable to write down it with Win32 Disk Imager. Even when we write it from Linux we’ll get error that measurement of picture file is greater that the SD card. However it can written and it’ll work. And second drawback is that partition information is not right. It’s going to work, however for those who try to edit with gparted for example it offers you some strange errors. So let’s see the right way to repair those issues.

Repair partition information

First we need to mount complete file once more, as we did it at first

[email protected]:~/picture$ sudo losetup -f –show Raspberry-Image-8G.img
/dev/loop0
[email protected]:~/image$

Then we need to run command that exhibits partition information.

[email protected]:~/image$ sudo sfdisk -d /dev/loop0
# partition desk of /dev/loop0
unit: sectors

/dev/loop0p1 : begin= 8192, measurement= 114688, Id= c
/dev/loop0p2 : begin= 122880, measurement= 15007744, Id=83
/dev/loop0p3 : begin= zero, measurement= zero, Id= zero
/dev/loop0p4 : begin= 0, measurement= zero, Id= zero
[email protected]:~/picture$

Keep in mind quantity 15007744, so we need to subtract from it once more 4096 and to save lots of back partition information. For that objective output that information into file. Then edit that file and then run command to reserve it again.

[email protected]:~/image$ sudo sfdisk -d /dev/loop0 > partition.txt

Edit partition.txt and substitute 15007744 with 15003648 (which is 15007744 – 4096). So file will appear to be this:

[email protected]:~/picture$ sudo sfdisk -d /dev/loop0 > partition.txt
[email protected]:~/image$ vi partition.txt
[email protected]:~/picture$ more partition.txt
# partition table of /dev/loop0
unit: sectors

/dev/loop0p1 : start= 8192, measurement= 114688, Id= c
/dev/loop0p2 : start= 122880, measurement= 15003648, Id=83
/dev/loop0p3 : begin= zero, measurement= zero, Id= zero
/dev/loop0p4 : begin= 0, measurement= 0, Id= 0
[email protected]:~/picture$

And last command is to import back this partition information with the same command.

[email protected]:~/image$ sudo sfdisk /dev/loop0

So now drawback with partition information is fastened and gparted won’t complain if you wish to edit partitions with it. But as I stated you possibly can skip this step. It should work with out it.

Ultimate step is to unmount file.

[email protected]:~/picture$ sudo losetup -d /dev/loop0

Shrink the file measurement

To the moment we shrink file system measurement. But measurement of picture file is nonetheless the same. Making an attempt to put in writing it to SD card with Win32 Disk Imager won’t be attainable. Still you’ll be able to write it to the SD card with Linux dd command. It provides you with an error, you’ll be able to ignore it and it will work. Here is the command to put in writing file to SD card and you may see the error. But as you see it copy file as much as measurement that can fit to SD card.

[email protected]:~/image$ sudo dd if=Raspberry-Image-8G.img of=/dev/sdb bs=4M
dd: writing `/dev/sdb’: No area left on system
1847+zero data in
1846+0 data out
7744782336 bytes (7.7 GB) copied, 1521.76 s, 5.1 MB/s
[email protected]:~/image$ sudo sync

To keep away from that we need to shrink the file measurement itself. For that objective we are going to use truncate command. Keep in mind 4096 sectors, multiply them by 512 and that is measurement that we have to subtract from current measurement. To verify file measurement sort

[email protected]:~/image$ ls -la
complete 37826592
drwxr-xr-x 2 howopensource howopensource 4096 Mar 19 18:16 .
drwxr-xr-x four howopensource howopensource 4096 Mar 19 18:16 ..
-rw-r–r– 2 howopensource howopensource 7746879488 Mar 19 18:20 Raspberry-Image-8G.img

Image file measurement as you see is 7746879488 bytes. New measurement ought to be:

7746879488 – 4096 * 512 = 7744782336

And right here is the command that you want to execute.

[email protected]:~/image$ truncate -s 7744782336 Raspberry-Image-8G.img
[email protected]:~/picture$ ls -la
complete 37822496
-rw-r–r– 2 howopensource howopensource 7744782336 Mar 19 18:33 Raspberry-Image-8G.img

As you see image is truncated to it’s new measurement. As I stated this step is not mandatory. Nevertheless it will provide you with risk to put in writing picture with Win32 Disk Imager or with dd Linux command with out supplying you with warnings. Format of command is easy. From where to repeat to where to repeat and parameter bs specifies block measurement – it is not obligatory. If not specified it’s going to read and write block by one after the other. Setting it to four megabytes will velocity up slightly bit course of. What we’re executing right here is copy file Raspberry-Image-8G.img to system /dev/sdb using block copy. And because measurement of file must be the identical as measurement of goal SD card we won’t get error at the end.

[email protected]:~/picture$ sudo dd if=Raspberry-Image-8G.img of=/dev/sdb bs=4M
1846+1 data in
1846+1 data out
7744782336 bytes (7.7 GB) copied, 1535.56 s, 5.zero MB/s

On the finish you must run sync command to ensure result of dd command is synced and then you possibly can unplug SD card from Linux field and put it into Raspberry.

[email protected]:~/image$ sudo sync

Write Image to SD Card with Linux dd Command

Hope this tutorial helps you to understand crucial operation to works with SD playing cards and file methods on Linux. Rules are the same and you may apply it over some other media – flash drive, onerous drive, and so on.