Hello,
after more testing in a Linux VM creating and managing .img disks found steps to solve problem:
a) Complete backup of 16 Gb SD Card:
Code:
$ sudo dd bs=4M if=/dev/mmcblk0 of=/media/TOSHIBA/16Gb.img
b) Mount image as a loopback device and shrink second partition to 3 Gbytes.:
Code:
$ sudo losetup -f --show /media/TOSHIBA/16Gb.img
Mounted as loopback device
/dev/loop0
Code:
$ sudo fdisk -l /dev/loop0
Disk /dev/loop0: 2962 MB, 2962227200 bytes
255 heads, 63 sectors/track, 360 cylinders, total 5785600 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0002c262
Device Boot Start End Blocks Id System
/dev/loop0p1 8192 122879 57344 c W95 FAT32 (LBA)
/dev/loop0p2 122880 5785599 2831360 83 Linux
Second partition starts in sector
122880
Byte position 122880 * 512 =
62914560
Code:
$ sudo losetup -f --show -o 62914560 /media/TOSHIBA/16Gb.img
Mounted as loopback device
/dev/loop1
Check Disk :
Code:
$ sudo e2fsck -p -y -f /dev/loop1
Shrink :
Code:
$ sudo resize2fs -p /dev/loop1 3G
Delete loopback devices:
Code:
$ sudo losetup -d /dev/loop0 /dev/loop1
d) We will create one image disk with similar properties (sectors/track and heads) like original Raspbian image, but greater in size. Information of raspbian image disk (fdisk can obtain/manipulate directly info from image file):
Code:
$ sudo fdisk -l /media/TOSHIBA/16Gb.img
Disk /media/TOSHIBA/16Gb.img: 2962 MB, 2962227200 bytes
255 heads, 63 sectors/track, 360 cylinders, total 5785600 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0002c262
Device Boot Start End Blocks Id System
/media/TOSHIBA/16Gb.img1 8192 122879 57344 c W95 FAT32 (LBA)
/media/TOSHIBA/16Gb.img2 122880 5785599 2831360 83 Linux
We will use cylinder number, that contain 255 x 63 =
16.065 sectors each one or 16.065 x 512bytes = 8.225.280 bytes per cylinder.
We will create one image disk with 4 MBytes (at beginning) + 56 MBytes (Fat32 partition) + 3 GBytes (3.072 Mb system partition) that it 3.132 MBytes or 3.284.140.032 bytes, we divide it by bytes per cylinder and obtain number of cylinders to create on disk:
3.284.140.032 / 8.225.280 = 399,27 cylinders
We add 1 and we will use 400 that will be 3.137 MBytes approximately
Code:
$ dd if=/dev/zero of=/media/TOSHIBA/3Gb.img bs=8225280c count=400
e) Using fdisk we create directly to the image the disk structure with correct Cylinders, Sectors and Tracks and create directly partitions in this fdisk session:
Code:
$ fdisk -u -C400 -S63 -H255 /media/TOSHIBA/3Gb.img
We create the first partition in file in same fdisk session and change partition type to W95 FAT32 (LBA):
Code:
Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 1
First sector (2048-204799, default 2048): 8192
Last sector, +sectors or +size{K,M,G} (8192-204799, default 204799): +56M
Command (m for help): p
...
Device Boot Start End Blocks Id System
/dev/loop0p1 8192 122879 57344 83 Linux
Command (m for help): t
Selected partition 1
Hex code (type L to list codes): c
Changed system type of partition 1 to c (W95 FAT32 (LBA))
We create the second partition in file in same fdisk session and change partition type to Linux:
Code:
Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 2
First sector (2048-204799, default 2048): 122880
Last sector, +sectors or +size{K,M,G} (122880-204799, default 204799): +3072M
Command (m for help): t
Partition number (1-4): 2
Hex code (type L to list codes): 83
We see created partitions in same fdisk session:
Code:
Command (m for help): p
Disk /media/TOSHIBA/3Gb.img: 3290 MB, 3290112000 bytes
255 heads, 63 sectors/track, 400 cylinders, total 6426000 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xbfe1ca60
Device Boot Start End Blocks Id System
/media/TOSHIBA/3Gb.img1 8192 122879 57344 c W95 FAT32 (LBA)
/media/TOSHIBA/3Gb.img2 122880 6414335 3145728 83 Linux
Save disk information and exit this fdisk session:
Code:
Command (m for help): w
The partition table has been altered!
WARNING: If you have created or modified any DOS 6.x
partitions, please see the fdisk manual page for additional
information.
Syncing disks.
h) We will use dd to "copy" first partition from 16Gb image file to 3Gb image file
skipping from input file 4 MBytes (8192 sectors of 512 bytes) and
seeking 4 MBytes (8192 sectors of 512 bytes) on output file to copy 56 MBytes (114688 sectors of 512 bytes) of first partition:
Code:
$ dd if=/media/TOSHIBA/16Gb.img of=/media/TOSHIBA/3Gb.img bs=512 count=114688 skip=8192 seek=8192
Mon Dec 16 16:27:09 UTC 2013
114688+0 records in
114688+0 records out
58720256 bytes (59 MB) copied, 13.9752 s, 4.2 MB/s
i) We will use dd to "copy" second partition from 16Gb image file to 3Gb image file
skipping from input file 4 MBytes + 56 MBytes (122880 sectors of 512 bytes, 60 MBytes) and
seeking 4 MBytes + 56 MBytes (122880 sectors of 512 bytes, 60 MBytes) on output file to copy 3072 MBytes (6291456 sectors of 512 bytes) of second partition:
Code:
$ dd if=/media/TOSHIBA/16Gb.img of=/media/TOSHIBA/3Gb.img bs=512 count=6291456 skip=122880 seek=122880
Mon Dec 16 16:28:20 UTC 2013
6291456+0 records in
6291456+0 records out
3221225472 bytes (3.2 GB) copied, 762.765 s, 4.2 MB/s
Mon Dec 16 16:41:02 UTC 2013
j) We compared with hexedit 16Gb and 3Gb file going to start of each partition and end of each partition and all data is equal, backup seems correct.
k) Copied 3Gb file with ftp/scp to windows and used win32diskimager to restore image to 4 Gb SD Card, Raspbian boots and works perfect!