dd
sudo dd if=/dev/mmblahblah of=name_of_image.img bs=4M
gzip name_of_image.img
The image can be used as a loop device and then mounted as a normal disk to allow access to the files etc.
To access the image as a loop device you let losetup
find the partitions automatically and convert them to to loop partitions loop0p1
(the /boot
partition) and loop0p2
the root
partition. You can then mount whichever one you need.
gm4slv@laptop:~/piimg $ sudo losetup -Pf igate.img gm4slv@laptop:~/piimg $ lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT loop0 7:0 0 4.3G 0 loop ├─loop0p1 259:0 0 256M 0 part └─loop0p2 259:1 0 4G 0 part sda 8:0 0 298.1G 0 disk ├─sda1 8:1 0 235.1G 0 part / ├─sda2 8:2 0 1K 0 part ├─sda3 8:3 0 50.9G 0 part └─sda5 8:5 0 12.1G 0 part [SWAP] sr0 11:0 1 1024M 0 rom gm4slv@laptop:~/piimg $ sudo mount /dev/loop0p2 /mnt gm4slv@laptop:~/piimg $ cd /mnt gm4slv@laptop:/mnt $ ls bin boot dev etc home lib lost+found media mnt opt proc root run sbin share srv sys tmp usr var gm4slv@laptop:/mnt $ cd etc/ gm4slv@laptop:/mnt/etc $ cat hostname igate gm4slv@laptop:/mnt/etc $
This allows you to change files (one example is to set a fixed IP address in /etc/dhcpcd.conf
) as required.
If the OS had been expanded after install to fill the whole SD Card space there is now a lot of pointless wasted space in the image that
The image can be shrunk to a more appropriate size.
The gist of it is here
sudo umount /mnt
gparted
(need to be at a machine with a graphical session) to shrink the partition to a more suitable size, leaving some room for safetyloop
device sudo losetup -d loop0
truncate
fdisk
to inspect the image file to see where the partitions start and end gm4slv@laptop:~/piimg $ sudo fdisk -l igate.img Disk igate.img: 4.25 GiB, 4563402752 bytes, 8912896 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: 0x38ea48c4 Device Boot Start End Sectors Size Id Type igate.img1 8192 532479 524288 256M c W95 FAT32 (LBA) igate.img2 532480 8912895 8380416 4G 83 Linux
sudo truncate --size=$[(8912895+1)*512] name_of_image.img
Voila, a smaller image on file which uses just enough space for the already known to be working OS
Page Updated: 20/05/22 16:41 BST