Linux Raspberry Resize SD Card Image Tech

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

Win32 Disk Imager Saving SD Raspberry Card

Lately I’m engaged on challenge involving Raspberry Pi. A few months in the past I wrote a fast begin information to Raspberry Pi. Now I’ve another matter to cowl 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 working system on SD card, it boots from SD card, it saves knowledge to SD card, and so forth. And if you work with SD playing cards from totally different producers you understand that though playing cards are equal in measurement they really differ with some small bytes. For instance I’ve 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 obtained error saying that there is not sufficient area on disk.

Once I must learn/write a picture from/to SD card on Home windows I exploit Win32 Disk Imager. It is very merely and good to have software. Right here is the error which I obtained from Disk Imager. Listed here are two screenshots – first one displaying file that I chosen and ready for writing and second one is error which it provides again.

Win32 Disk Imager Saving SD Raspberry CardWin32 Disk Imager Saving SD Raspberry Card

Saving Bigger Raspberry SD Card Image ErrorSaving Bigger Raspberry SD Card Image Error

Take note of highlighted numbers in second screenshot these can be found sectors. Precise picture has 15130624 sectors, and SD card the place we’re going to reserve it has 15126528 sectors. In case you pay nearer consideration to error message you will notice that sector measurement is 512 bytes. So let’s calculate distinction

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

With different phrases this is 2M distinction between these 8G SD playing cards – one in every of them AData and second one Tohisba. Ant this 2Mb stops you from writing picture to another SD card. In fact there is an answer which incorporates resizing the picture beneath Linux.

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

howopensource@debian:~/picture$ 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/bodily): 512 bytes / 512 bytes
I/O measurement (minimal/optimum): 512 bytes / 512 bytes
Disk identifier: 0xa6202af7

Gadget 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
howopensource@debian:~/picture$

Raspberry SD card image fdisk InfoRaspberry SD card image fdisk Info

Don’t take note of heads. monitor, and so forth. These are usually not legitimate for SD playing cards. However concentrate for complete sectors – 15130624 and to measurement of picture – 7746 MB. Observe that this picture is expanded. However typically I obtained some unexpanded photographs. You recognize whenever you first boot into Raspbian very first thing that is executed is sudo raspi-config. And from this menu you’ll be able to broaden information system to make use of complete area on SD card. Whenever you obtain and get 4G picture for instance and save in over 8G card you have got 4G free. They don’t seem to be used till you don’t run sudo raspi-config. Listed here are extra information about raspi-config. So right here the way it seems to be one unexpanded picture. Truly is the identical picture, however second partition has a half a blocks (measurement). And as an alternative to finish of 15130623 sector ends 8110079.

howopensource@debian:~/picture$ 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/bodily): 512 bytes / 512 bytes
I/O measurement (minimal/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
howopensource@debian:~/picture$

Why I’m scripting this is as a result of studying another manuals on the internet you could discover that you simply picture doesn’t finish on the final sector, however finish on the half of sectors or elsewhere. Any means hold that in thoughts.

Examine out there area on the right track SD card

Subsequent step is to examine out there area on the right track SD card – the place you will write picture. So insert SD card into SD card reader/dongle after which plug it into USB on the identical Linux machine. Then it’s worthwhile to confirm to which system it maps. For that function run dmesg | tail and verify output.

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

As you see my system is hooked up to /dev/sdb, chances are you’ll be hooked up to sda, sdc, and so on. In the event you take note of these messages you will notice 15126528 512-byte logical blocks: (7.74 GB/7.21 GiB) line. In order that’s the variety of sectors and, block measurement and complete measurement of goal SD card. However you’ll be able to examine it additional with fdisk. Notice that with a view to entry /dev/sdb/ you will have of sudo.

howopensource@debian:~/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/bodily): 512 bytes / 512 bytes
I/O measurement (minimal/optimum): 512 bytes / 512 bytes
Disk identifier: 0xa6202af7

System Boot Begin Finish Blocks Id System
/dev/sdb1 8192 122879 57344 c W95 FAT32 (LBA)
/dev/sdb2 122880 15126527 7501824 83 Linux
howopensource@debian:~/picture$

SD Card Sectors, Size and Other InfoSD Card Sectors, Size and Other Info

Calculate new measurement and shrink picture file

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

Subsequent step is to mount picture file with command – losetup

howopensource@debian:~/picture$ sudo losetup -f –show Raspberry-Image-8G.img
/dev/loop0
howopensource@debian:~/picture$

So picture file is mounted to /dev/loop0. You’ll be able to run fdisk on it and you will notice the identical outcome that from run fdisk on picture file itself.

howopensource@debian:~/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/bodily): 512 bytes / 512 bytes
I/O measurement (minimal/optimum): 512 bytes / 512 bytes
Disk identifier: 0xa6202af7

Gadget Boot Begin Finish Blocks Id System
/dev/loop0p1 8192 122879 57344 c W95 FAT32 (LBA)
/dev/loop0p2 122880 15130623 7503872 83 Linux
howopensource@debian:~/picture$

As you see on this output (and on earlier as properly) we have now two partitions within the file. The primary one is boot one. Which we aren’t going to the touch. The second is knowledge partition – /dev/loop0p2 which we’re going to shrink. For that function we have to mount solely that partition. We’re going to use -o or offset parameter of losetup command. However first we have to calculate offset. Get the beginning 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 have to unmount picture file. First command is to unmount and second to mount solely partition that we need to shrink.

howopensource@debian:~/picture$ sudo losetup -d /dev/loop0
howopensource@debian:~/picture$
howopensource@debian:~/picture$ sudo losetup -f –show -o 62914560 Raspberry-Image-8G.img
/dev/loop0
howopensource@debian:~/picture$

Now we will run some instructions to examine file system for instance e2fsck

howopensource@debian:~/picture$ 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 listing construction
Cross three: Checking listing connectivity
Cross four: Checking reference counts
Move 5: Checking group abstract info
Free blocks rely incorrect (1158894, counted=1158871).
Repair? sure
Free inodes rely incorrect (385389, counted=385394).
Repair? sure

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

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

Subsequent step is to shrink file system. For that functions we’re going to use resize2fs command. It accepts new measurement with bytes, megabytes, gigabytes or sectors. Sectors is straightforward, as a result of we simply know the dimensions of sectors of previous, new one and distinction. So Begin sector is 122880, Finish 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 CompareRaspberry SD Card Image Compare

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

15007743 – 4096 = 15003647

And we have to execute the command.

howopensource@debian:~/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.
howopensource@debian:~/picture$

Now we will verify once more for errors file system.

howopensource@debian:~/picture$ sudo e2fsck -f /dev/loop0
e2fsck 1.42.5 (29-Jul-2012)
Move 1: Checking inodes, blocks, and sizes
Move 2: Checking listing construction
Cross three: Checking listing connectivity
Cross four: Checking reference counts
Cross 5: Checking group abstract info
/dev/loop0: 88814/474208 information (zero.1% non-contiguous), 717097/1875455 blocks
howopensource@debian:~/picture$

Lastly don’t forget to unmount file.

howopensource@debian:~/picture$ sudo losetup -d /dev/loop0

At that time we’re prepared to put in writing picture from Linux. However nonetheless we will be unable to put in writing 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 is going to written and it’ll work. And second drawback is that partition information is not right. It’ll work, however in the event you attempt to edit with gparted for instance it provides you with some unusual errors. So let’s see the way to repair these issues.

Repair partition information

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

howopensource@debian:~/picture$ sudo losetup -f –show Raspberry-Image-8G.img
/dev/loop0
howopensource@debian:~/picture$

Then we have to run command that exhibits partition information.

howopensource@debian:~/picture$ 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= zero, measurement= zero, Id= zero
howopensource@debian:~/picture$

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

howopensource@debian:~/picture$ sudo sfdisk -d /dev/loop0 > partition.txt

Edit partition.txt and exchange 15007744 with 15003648 (which is 15007744 – 4096). So file will seem like this:

howopensource@debian:~/picture$ sudo sfdisk -d /dev/loop0 > partition.txt
howopensource@debian:~/picture$ vi partition.txt
howopensource@debian:~/picture$ extra partition.txt
# partition desk of /dev/loop0
unit: sectors

/dev/loop0p1 : begin= 8192, measurement= 114688, Id= c
/dev/loop0p2 : begin= 122880, measurement= 15003648, Id=83
/dev/loop0p3 : begin= zero, measurement= zero, Id= zero
/dev/loop0p4 : begin= zero, measurement= zero, Id= zero
howopensource@debian:~/picture$

And ultimate command is to import again this partition information with the identical command.

howopensource@debian:~/picture$ 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. However as I stated you’ll be able to skip this step. It should work with out it.

Last step is to unmount file.

howopensource@debian:~/picture$ sudo losetup -d /dev/loop0

Shrink the file measurement

To the second we shrink file system measurement. However measurement of picture file is nonetheless the identical. Making an attempt to write down it to SD card with Win32 Disk Imager won’t be attainable. Nonetheless you possibly can write it to the SD card with Linux dd command. It will provide you with an error, you possibly can ignore it and it’ll work. Right here is the command to put in writing file to SD card and you may see the error. However as you see it copy file as much as measurement that may match to SD card.

howopensource@debian:~/picture$ 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+zero data out
7744782336 bytes (7.7 GB) copied, 1521.76 s, 5.1 MB/s
howopensource@debian:~/picture$ sudo sync

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

howopensource@debian:~/picture$ 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 must be:

7746879488 – 4096 * 512 = 7744782336

And right here is the command that you must execute.

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

As you see picture is truncated to it is new measurement. As I stated this step is not crucial. Nevertheless it provides you with risk to write down picture with Win32 Disk Imager or with dd Linux command with out supplying you with warnings. Format of command is easy. From the place to repeat to the place to repeat and parameter bs specifies block measurement – it is not obligatory. If not specified it can learn and write block by one after the other. Setting it to four megabytes will velocity up just a little bit course of. What we’re executing right here is copy file Raspberry-Image-8G.img to gadget /dev/sdb utilizing block copy. And since measurement of file ought to be the identical as measurement of goal SD card we won’t get error on the finish.

howopensource@debian:~/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 need to run sync command to ensure results of dd command is synced after which you possibly can unplug SD card from Linux field and put it into Raspberry.

howopensource@debian:~/picture$ sudo sync

Write Image to SD Card with Linux dd CommandWrite Image to SD Card with Linux dd Command

Hope this tutorial lets you perceive crucial operation to works with SD playing cards and file methods on Linux. Rules are the identical and you may apply it over another media – flash drive, onerous drive, and so forth.