[raspberry-vi] Re: My raspberry pi image

  • From: "Glenn K0LNY" <glennervin@xxxxxxxxxxxx>
  • To: <raspberry-vi@xxxxxxxxxxxxx>
  • Date: Sun, 3 Jan 2021 21:45:20 -0600

Kelly,
I'm still hoping to get my 16GB image uploaded for you to shrink for me.
Although I have unlimited Open Drive space, I found out that if I go over 10 
TB, my upload to Open Drive drops to a snail's pace.
I was letting a friend store some stuff in my public folder, which put me 
over that limit.
I had him go in today to clean out some stuff, but it may not be until 
tomorrow when I can upload my image.
Then I'll send you a download link for it.
Glenn
----- Original Message ----- 
From: "Kelly Prescott" <kprescott@xxxxxxxxxx>
To: <raspberry-vi@xxxxxxxxxxxxx>
Sent: Thursday, December 24, 2020 12:07 AM
Subject: [raspberry-vi] Re: My raspberry pi image


Here are my shrink instructions:
Remember, I asume you have some Linux sysadmin knowledge.
If you brake it, you own all the parts.
We are using the note_taker.img.gz
make yourself root by what ever means and create yourself a working 
directory somewhere you have space and change to it.
copy your image to it.

First, we will start with the original image.

ls -l
-rwxr-xr-x 1 root root 5291197973 Dec 23 22:28 note_taker.img.gz

now unzip the image
gunzip note_taker.img.gz
after a while, you will get a prompt back.
now look at the directory again
ls -l
-rwxr-xr-x 1 root root 31914983424 Dec 23 22:28 note_taker.img

as you can see, the size has increased and we have lost the .gz extension.

now we need to setup the file to a device so we can work with it.
losetup -fP --show note_taker.img
/dev/loop0
/dev/loop0 is the raw disk device.

now, show partitions on the image:
fdisk -l /dev/loop0
Disk /dev/loop0: 29.72 GiB, 31914983424 bytes, 62333952 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
Disklabel type: dos
Disk identifier: 0xe1100a71

Device       Boot  Start      End  Sectors  Size Id Type
/dev/loop0p1        8192   532479   524288  256M  c W95 FAT32 (LBA)
/dev/loop0p2      532480 31116287 30583808 14.6G 83 Linux

some things to note are that the sectors are 512 bytes long.
This will be important later.
/dev/loop0p2 is the partition we want to resize.

first, run e2fsck on it to make sure it is healthy.

e2fsck -fy /dev/loop0p2
e2fsck 1.45.6 (20-Mar-2020)
rootfs: recovering journal
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
Free blocks count wrong (2104318, counted=2125720).
Fix? yes

Free inodes count wrong (716355, counted=716580).
Fix? yes


rootfs: ***** FILE SYSTEM WAS MODIFIED *****
rootfs: 240012/956592 files (0.1% non-contiguous), 1697256/3822976 blocks

We can see that the used blocks are 1697256 and the total blocks are 
3822976.
We have lots of extra blocks.
Let's find out how much we can shrink the partition:
resize2fs -P /dev/loop0p2
resize2fs 1.45.6 (20-Mar-2020)
Estimated minimum size of the filesystem: 1738330

The block count is 4-k, so if we multiply 1738330*4 we get 6953320
Not quite 7 gig.

Let's make the filesystem 7G, and it should easily fit on a 8G card.

resize2fs /dev/loop0p2 7G
resize2fs 1.45.6 (20-Mar-2020)
Resizing the filesystem on /dev/loop0p2 to 1835008 (4k) blocks.
The filesystem on /dev/loop0p2 is now 1835008 (4k) blocks long.

now, re-check the filesystem:
e2fsck -fy /dev/loop0p2
e2fsck 1.45.6 (20-Mar-2020)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
rootfs: 240012/457856 files (0.7% non-contiguous), 1665861/1835008 blocks

you can see that the used/total blocks are different: 1665861/1835008

Now, we need to write zeros to the unused space on the partitions so the 
image will shrink better.

lets make a scratch directory to mount each partition on.

mkdir scratch

now we are going to mount the boot partition:
mount /dev/loop0p1 scratch

we will make a file of zeros to fill up the free space on the disk:

dd if=/dev/zero of=scratch/filler.img
dd: writing to 'scratch/filler.img': No space left on device
405853+0 records in
405852+0 records out
207796224 bytes (208 MB, 198 MiB) copied, 1.29029 s, 161 MB/s
You will notice the error as the device was filled up.
We need to remove the filler.img file
rm scratch/filler.img

unmount the partition:
umount scratch

Let's do the same to the second partition:
mount /dev/loop0p2 scratch
root@io:/machines/tests/pimage# dd if=/dev/zero of=scratch/filler.img
dd: writing to 'scratch/filler.img': No space left on device
1320401+0 records in
1320400+0 records out
676044800 bytes (676 MB, 645 MiB) copied, 2.78866 s, 242 MB/s
root@io:/machines/tests/pimage# rm scratch/filler.img
root@io:/machines/tests/pimage# umount scratch

you can remove the scratch directory:
rmdir scratch

Now it is time to shrink the partition to match the filesystem.

earlier we saw the size of the filesystem was 1835008 blocks.  These are 4-k 
blocks, so we need to multiply by 8 to get the number of sectors 1835008*8 
14680064 sectors

Now, run fdisk
fdisk /dev/loop0
Welcome to fdisk (util-linux 2.36.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help): p
Disk /dev/loop0: 29.72 GiB, 31914983424 bytes, 62333952 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
Disklabel type: dos
Disk identifier: 0xe1100a71

Device       Boot  Start      End  Sectors  Size Id Type
/dev/loop0p1        8192   532479   524288  256M  c W95 FAT32 (LBA)
/dev/loop0p2      532480 31116287 30583808 14.6G 83 Linux

Command (m for help):

take note of the start for /dev/loop0p2 which is 532480
We are going to remove it and replace it with the correctly sized entry.
Command (m for help): d
Partition number (1,2, default 2): 2

Partition 2 has been deleted.

Command (m for help):

now we need to recreate the partition, here is how to calculate it:
take the number of sectors we got earlier which was 14680064, subtract 1 and 
you get 14680063
so your start which we already took note of is 532480 and the end is 
+14680063
There is a plus sign before that number, so your input to fdisk looks like 
this:

Command (m for help): n
Partition type
    p   primary (1 primary, 0 extended, 3 free)
    e   extended (container for logical partitions)
Select (default p): p
Partition number (2-4, default 2): 2
First sector (2048-62333951, default 2048): 532480
Last sector, +/-sectors or +/-size{K,M,G,T,P} (532480-62333951, default 
62333951): +14680063

Created a new partition 2 of type 'Linux' and of size 7 GiB.
Partition #2 contains a ext4 signature.

Do you want to remove the signature? [Y]es/[N]o: no

Command (m for help): p

Disk /dev/loop0: 29.72 GiB, 31914983424 bytes, 62333952 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
Disklabel type: dos
Disk identifier: 0xe1100a71

Device       Boot  Start      End  Sectors  Size Id Type
/dev/loop0p1        8192   532479   524288  256M  c W95 FAT32 (LBA)
/dev/loop0p2      532480 15212543 14680064    7G 83 Linux

Command (m for help):
You will notice that the size of partition 2 is now exactly 14680064 divide 
that by 8 1835008
That equals the number of blocks when we shrunk the filesystem.
Now we write the partition table back to the image:
Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

now, lets remove the device and partition maps:
root@io:/machines/tests/pimage# partx -d /dev/loop0p2
root@io:/machines/tests/pimage# partx -d /dev/loop0p1
root@io:/machines/tests/pimage# losetup -d /dev/loop0

finally only 2 steps remain:
first, we need to truncate the file to the correct size, then we need to 
compress it.

truncate -s 7606272K note_taker.img
-rwxr-xr-x 1 root root 7788822528 Dec 24 00:24 note_taker.img

gzip -9 note_taker.img
-rwxr-xr-x 1 root root 2807014399 Dec 24 00:24 note_taker.img.gz

Now it is ready to go.
Happy shrinking.



On Wed, 23 Dec 2020, Kelly Prescott wrote:

I will post my shrink howto sometime to night after work.



On Mon, 21 Dec 2020, Pranav Lal wrote:

Hi Kelly,

Many thanks for shrinking the image and testing it.

How did you shrink it? I read your messages on it but could you give a 
setoff detailed steps for future reference?

Pranav


===========================================================
The raspberry-vi mailing list
Archives: //www.freelists.org/archives/raspberry-vi
Administrative contact: <mike.ray@xxxxxxxxxxxxxx>
-----------------------------------------------------------
Raspberry Pi and the Raspberry Pi logo are trademarks of the Raspberry Pi 
Foundation.

This list is not affiliated to the Raspberry Pi Foundation and the views 
and attitudes expressed by the subscribers to this list do not reflect 
those of the Foundation.

Mike Ray, list creator, January 2013


===========================================================
The raspberry-vi mailing list
Archives: //www.freelists.org/archives/raspberry-vi
Administrative contact: <mike.ray@xxxxxxxxxxxxxx>
-----------------------------------------------------------
Raspberry Pi and the Raspberry Pi logo are trademarks of the Raspberry Pi 
Foundation.

This list is not affiliated to the Raspberry Pi Foundation and the views 
and attitudes expressed by the subscribers to this list do not reflect 
those of the Foundation.

Mike Ray, list creator, January 2013


===========================================================
The raspberry-vi mailing list
Archives: //www.freelists.org/archives/raspberry-vi
Administrative contact: <mike.ray@xxxxxxxxxxxxxx>
-----------------------------------------------------------
Raspberry Pi and the Raspberry Pi logo are trademarks of the Raspberry Pi 
Foundation.

This list is not affiliated to the Raspberry Pi Foundation and the views and 
attitudes expressed by the subscribers to this list do not reflect those of 
the Foundation.

Mike Ray, list creator, January 2013

=========================================================== 
The raspberry-vi mailing list 
Archives: //www.freelists.org/archives/raspberry-vi
Administrative contact: <mike.ray@xxxxxxxxxxxxxx>
-----------------------------------------------------------
Raspberry Pi and the Raspberry Pi logo are trademarks of the Raspberry Pi 
Foundation.

This list is not affiliated to the Raspberry Pi Foundation and the views and 
attitudes expressed by the subscribers to this list do not reflect those of the 
Foundation.

Mike Ray, list creator, January 2013

Other related posts: