It is possible to resize the image in Linux using the command line tools dd and parted. First, have a look at the partition table in the image file:
- Code: Select all
user@host:~$ parted Occidentalist.img unit b print
WARNING: You are not superuser. Watch out for permissions.
Model: (file)
Disk /home/andy/Occidentalist.img: 3963617280B
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Number Start End Size Type File system Flags
1 4194304B 62914559B 58720256B primary fat16 lba
2 62914560B 3963617279B 3900702720B primary ext4
user@host:~$
note the start and end of the of second partition, this will be important. Next we want to grab the second partition, as we are going to use it to make a smaller file system.
- Code: Select all
user@host:~$ dd if=Occidentalist.img bs=1M skip=60 of=OccImgPart2.img count=3720
3720+0 records in
3720+0 records out
3900702720 bytes (3.9 GB) copied, 87.1542 s, 44.8 MB/s
user@host:~$
We use a block size of 1 Megabyte (the cap M is important to dd!) , and the EXT4 partition starts right at the 60 megabyte mark and is 3720 megabytes long. If you use a different block size, you will have to recalculate the skip, seek, and count arguments to DD, as they are specified in blocks, not bytes.
Next, we are going to mount the image we just made of the EXT4
- Code: Select all
user@host:~$ mkdir OccFilesystem
user@host:~$ sudo mount OccImagePart2.img OccFilesystem
user@host:~$ sudo mount OccImgPart2.img OccFilesystem
user@host:~$
After this, you should have the Occidentalist image's filesystem mounted in the directory OccFilesystem. Now we need to make a new Filesystem image, but reduced size.
If you know your card's capacity in megabytes, take that number and subtract 60 (the size of the first partition and partition info in the current Occidentalist image). Otherwise use a suitably small number. For this case, I am going to use 3500 megabytes
- Code: Select all
user@host:~$ dd bs=1M if=/dev/zero of=SmallerOccImgPart2.img count=3500
3500+0 records in
3500+0 records out
3670016000 bytes (3.7 GB) copied, 36.3484 s, 101 MB/s
user@host:~$
and format it ext4
- Code: Select all
user@host:~$ mke2fs -T ext4 ./SmallerOccImgPart2.img
mke2fs 1.42 (29-Nov-2011)
./SmallerOccImgPart2.img is not a block special device.
Proceed anyway? (y,n) y
Discarding device blocks: done
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
224000 inodes, 896000 blocks
44800 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=918552576
28 block groups
32768 blocks per group, 32768 fragments per group
8000 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736
Allocating group tables: done
Writing inode tables: done
Creating journal (16384 blocks): done
Writing superblocks and filesystem accounting information: done
user@host:~$
Next, mount the smaller filesystem and copy the files from the Occidentalist filesystem to the smaller filesystem:
- Code: Select all
user@host:~$ mkdir SmallerOccFilesystem/
user@host:~$ sudo mount SmallerOccImgPart2.img SmallerOccFilesystem
user@host:~$ sudo cp -aux OccFilesystem/ SmallerOccFilesystem/
user@host:~$
Time to make a new smaller image file. Remember, we made the smaller filesystem 3500 megabytes. We now add sixty to that number to make the whole image:
- Code: Select all
user@host:~$ dd bs=1M if=/dev/zero of=SmallerOccImg.img count=3560
3560+0 records in
3560+0 records out
3732930560 bytes (3.7 GB) copied, 37.4573 s, 99.7 MB/s
user@host:~$
Add some partitions to that image. The sizes are done in bytes, since parted uses salesman kilobyte / megabyte sizes :
- Code: Select all
user@host:~$ parted SmallerOccImg.img mklabel msdos
WARNING: You are not superuser. Watch out for permissions.
user@host:~$ parted SmallerOccImg.img unit b mkpart primary fat16 4194304 62914559
WARNING: You are not superuser. Watch out for permissions.
user@host:~$ parted SmallerOccImg.img unit b mkpart primary ext4 62914560 3732930559
WARNING: You are not superuser. Watch out for permissions.
user@host:~$
Now you have an empty image with two partitions. Time to copy the data:
- Code: Select all
user@host:~$ dd bs=1M if=Occidentalist.img skip=4 of=SmallerOccImg.img seek=4 count=56 conv=nocreat,notrunc
56+0 records in
56+0 records out
58720256 bytes (59 MB) copied, 0.449081 s, 131 MB/s
user@host:~$ dd bs=1M if=SmallerOccImgPart2.img of=SmallerOccImg.img seek=60 count=3500 conv=nocreat,notrunc
3500+0 records in
3500+0 records out
3670016000 bytes (3.7 GB) copied, 62.812 s, 58.4 MB/s
user@host:~$
We use conv=nocreat,notrunc for DD to prevent DD from wiping out our partitioned image file.
After that, SmallerOccImg.img is ready to be installed on your "4GB" SDCard. Have fun!