drewsif / pishrink Goto Github PK
View Code? Open in Web Editor NEWMake your pi images smaller!
License: MIT License
Make your pi images smaller!
License: MIT License
Hi,
I get the following output, when I start your script with this one-liner:
sudo find /mnt/MyBookI/backup/raspberrypi -name "raspberrypi-dd-backup-*.img" -exec pishrink.sh {} \;
Creating new /etc/rc.local
e2fsck 1.43.3 (04-Sep-2016)
Durchgang 1: Inodes, Blöcke und Größen werden geprüft
Inode 768087 Erweiterung tree (at level 1) could be narrower. Reparieren<j>? ja
Inode 769139 Erweiterung tree (at level 1) could be narrower. Reparieren<j>? ja
Inode 773688 Erweiterung tree (at level 1) could be narrower. Reparieren<j>? ja
Inode 773693 Erweiterung tree (at level 1) could be narrower. Reparieren<j>? ja
Inode 773694 Erweiterung tree (at level 1) could be narrower. Reparieren<j>? ja
Inode 773695 Erweiterung tree (at level 1) could be narrower. Reparieren<j>? ja
Inode 773699 Erweiterung tree (at level 1) could be narrower. Reparieren<j>? ja
Inode 773701 Erweiterung tree (at level 1) could be narrower. Reparieren<j>? ja
Inode 773705 Erweiterung tree (at level 1) could be narrower. Reparieren<j>? ja
Inode 773706 Erweiterung tree (at level 1) could be narrower. Reparieren ('a' enables 'yes' to all) <j>? ja
Inode 773707 Erweiterung tree (at level 1) could be narrower. Reparieren ('a' enables 'yes' to all) <j>? yes to all
Pass 1E: Optimizing Erweiterung trees
Durchgang 2: Verzeichnisstruktur wird geprüft
Durchgang 3: Verzeichnisverknüpfungen werden geprüft
Durchgang 4: Referenzzähler werden überprüft
Der Referenzzähler von Inode 768966 ist 1, sollte aber 2 sein. Reparieren? ja
Nicht verbundener Inode 769038
Nach /lost+found verbinden? ja
Der Referenzzähler von Inode 769038 ist 2, sollte aber 1 sein. Reparieren? ja
Nicht verbundener Inode 769044
Nach /lost+found verbinden? ja
Der Referenzzähler von Inode 769044 ist 2, sollte aber 1 sein. Reparieren? ja
Der Referenzzähler von Inode 769045 ist 1, sollte aber 2 sein. Reparieren? ja
Durchgang 5: Zusammengefasste Gruppeninformation wird geprüft
Unterschiede in der Block-Bitmap: +(3235862--3235866) +3235869 -(3236519--3236520) +3236546 -(3244516--3244518) -(3244942--3244944) -3244946 +3244992 +3244994 -(3250620--3250621) +3250638 +3250642
Reparieren? ja
Die Anzahl freier Blöcke in Gruppe #98 ist falsch (25767, gezählt=25762).
Reparieren? ja
Die Anzahl freier Blöcke in Gruppe #99 ist falsch (12119, gezählt=12124).
Reparieren? ja
/dev/loop0: ***** DATEISYSTEM WURDE VERÄNDERT *****
/dev/loop0: 171522/1904000 Dateien (0.3% nicht zusammenhängend), 3242046/7774592 Blöcke
resize2fs 1.43.3 (04-Sep-2016)
resize2fs 1.43.3 (04-Sep-2016)
Die Größe des Dateisystems auf /dev/loop0 wird auf 3546426 (4k) Blöcke geändert.
Start von Durchgang 2 (max = 812584)
Blöcke werden verschoben XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Start von Durchgang 3 (max = 238)
Die Inode-Tabelle wird gelesenXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Start von Durchgang 4 (max = 15577)
Die Inode-Referenzen werden aktualisiertXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Das Dateisystem auf /dev/loop0 is nun 3546426 (4k) Blöcke lang.
Shrunk /mnt/MyBookI/backup/raspberrypi/raspberrypi-dd-backup-20170601-010001/raspberrypi-dd-backup-20170601-010001.img from 30G to 14G
Creating new /etc/rc.local
e2fsck 1.43.3 (04-Sep-2016)
Durchgang 1: Inodes, Blöcke und Größen werden geprüft
Inode 532845, das Ende der Erweiterung ist außerhalb des gültigen Bereichs
(logischer Block 7, physischer Block 442372, Länge 1)
Bereinigen<j>? ja
Durchgang 2: Verzeichnisstruktur wird geprüft
Durchgang 3: Verzeichnisverknüpfungen werden geprüft
Durchgang 4: Referenzzähler werden überprüft
Durchgang 5: Zusammengefasste Gruppeninformation wird geprüft
/dev/loop0: 183905/1870848 Dateien (0.6% nicht zusammenhängend), 4785872/7577600 Blöcke
resize2fs 1.43.3 (04-Sep-2016)
resize2fs 1.43.3 (04-Sep-2016)
Die Größe des Dateisystems auf /dev/loop0 wird auf 5081596 (4k) Blöcke geändert.
Start von Durchgang 2 (max = 854601)
Blöcke werden verschoben XXXX------------------------------------
I think, it is a problem of the images which may be corrupted, but I'm not sure. What can I do to prevent this problem? Is it correct to answer to all questions with y(es)?
(Is it allowed to run my one-liner?)
My goal is to shrink all of my daily raspbian-backups for example once in a week automatically, if this is possible.
Thanks for help.
Uwe
If I have a 128G sd, and dd it every time I want a new backup, this will imply:
If, sync can be implemented, it would only check changed data from a synced dd .img to the already existing shrink .img.
Add a test option, that does everything and outputs the new size of the .img file, but doesn't overwrite the original file
For compatibility with testing files on read only filesystems and for paranoid users
If pishrink does not leave properly, the loop device remains open. Memory is blocked until the loop device is deleted. A trap handler could solve this Problem.
function cleanup() {
losetup -d "$loopback"
}
trap cleanup SIGINT SIGTERM EXIT
Please check and implement if necessary.
Purely for good practice, maybe an idea to reformulate the call to:
./pishrink original.img shrunk.img
I only back up a partition from an SSD (sda2) to an img file on a FAT32 partition with 'dd if=/dev/sda2 of=/media/fat32/file.img bs=1M'. This works flawlessly.
Then I start: 'sudo /usr/local/bin/pishrink. sh /media/fat32/file.img'
There I get the following output until the script gets stuck with the error:
Creating new /etc/rc.local
e2fsck 1.43.3 (04-Sep-2016)
Durchgang 1: Inodes, Blöcke und Größen werden geprüft
Durchgang 2: Verzeichnisstruktur wird geprüft
Durchgang 3: Verzeichnisverknüpfungen werden geprüft
Durchgang 4: Referenzzähler werden überprüft
Durchgang 5: Zusammengefasste Gruppeninformation wird geprüft
/dev/loop0: 76785/229440 Dateien (0.3% nicht zusammenhängend), 587124/971264 Blöcke
resize2fs 1.43.3 (04-Sep-2016)
resize2fs 1.43.3 (04-Sep-2016)
Die Größe des Dateisystems auf /dev/loop0 wird auf 754009 (4k) Blöcke geändert.
Start von Durchgang 2 (max = 46859)
Blöcke werden verschoben XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Start von Durchgang 3 (max = 30)
Die Inode-Tabelle wird gelesenXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Das Dateisystem auf /dev/loop0 is nun 753664 (4k) Blöcke lang.
parted: invalid token: primary
What's wrong here?
pishrink hangs up, if an imagefile is corrupt. Why? parted tries to interact with the user.
You can run parted, without user intervention:
man parted
-s, --script
never prompts for user intervention
--- screenshot 1 -- pishrink with corrupt image
./pishrink2.sh /mnt/usb/corrupt.img /mnt/usb/corrupt-shrink.img
Copying /mnt/usb/corrupt.img to /mnt/usb/corrupt-shrink.img...
Error: Can't have a partition outside the disk!
^C
<=== interaction, I aborted pishrink with Ctrl-C / Strg-C
--- screenshot 2 -- parted with corrupt image
parted -m /mnt/usb/pishrink/corrupt-shrink.img unit B print
Error: Can't have a partition outside the disk!
Ignore/Cancel? Cancel
<=== interaction, I entered "Cancel"
BYT;
/mnt/usb/corrupt-shrink.img:4021420032B:file:512:512:unknown::;
--- screenshot 3 -- parted ... with option '-s'
parted -s -m /mnt/ntfs/corrupt-shrink.img unit B print
Error: Can't have a partition outside the disk!
BYT;
/mnt/ntfs/corrupt-shrink.img:4021420032B:file:512:512:unknown::;
sorry to bother you with something so basic, but how do I install PiShrink on my RPi3? (running Raspian Stretch)
tune2fs supports only ext2/3/4 filesystems.
It would also be cool to somehow support other filesystems, like F2FS.
I tried to use the program on ubuntu 17.04 with a modified version of retropie and got this message. Internet search looks like If It has something to do partered seeing the partition have more sectors and appending a sector should fit it.
Hi there, sorry to be a bit dense here. Can I run this on my Mac? The Micro SD for the Pi is 119 Gigs (I hate that they advertise as 128 Gigs and measure at 1000 Mb) and my image file is just under 128 Gigs. So having 2 exist on the Pi wouldn't work. Any suggestions are appreciated. Thanks!
The rc.local
script assumes that the Raspberry Pi is booted from SD card. If a Raspberry Pi is booted from a USB disk, then the boot/root FS are on /dev/sda1
and sda2
, and not on /dev/mmcblk0p1
/ 2
.
It would be nice to support auto-expansion of an image that got burned to a USB disk instead of to an SD card.
Raspbian itself no longer has this hardcoded in /boot/cmdline.txt
and /etc/fstab
, so a Raspbian image boots from both an SD card and a USB disk. I don't know how they do it, unfortunately.
Edit: I did find something. This commit shows how the root partition is found: ROOT_DEV=$(findmnt / -o source -n)
gives the root partition. This could be used instead of the current hardcoded values to perform the resize.
log:
mark@mark-Inspiron-1525 /temp $ sudo pishrink.sh stretch.img
Creating new /etc/rc.local
rootfs: 69461/465392 files (0.1% non-contiguous), 551275/1928192 blocks
resize2fs 1.42.13 (17-May-2015)
resize2fs 1.42.13 (17-May-2015)
Resizing the filesystem on /dev/loop4 to 770932 (4k) blocks.
Begin pass 2 (max = 27468)
Relocating blocks XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Begin pass 3 (max = 59)
Scanning inode table XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Begin pass 4 (max = 6792)
Updating inode references XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
The filesystem on /dev/loop4 is now 770932 (4k) blocks long.
parted: invalid token: primary
Error: Expecting a file system type.
Shrunk stretch.img from 7.4G to 0
I created the image with the line:
sudo dd if=/dev/mmcblk0p2 bs=4M of=stretch_3.5.img
after unmounting the two partitions of the sd card using gparted.
EDIT SORRY, realized my mistake after reviewing my post. The above line copies a single partition, not the entire drive and I just happened to have grabbed the large partition which tricked me temporarily.
I changed my copy command to this and then pishrink.sh works! - thanks
sudo dd if=/dev/mmcblk0 bs=4M of=stretch_3.5.img
Win32DiskImager reads SD-Card-Images to File. The image file is as large as the SD-Card.
History: I installed some packages and forgot to expand the filesystem.
pishrink reports error 'Image already shrunk to smallest size' but the file size is 8GB.
Problem: the image size is smaller than the file size.
pishrink runs on host raspberrypi5, raspi model RPi 3B.
SD-Card of the image is plugged in host raspberrypi4, raspi model 2B.
pishrink console output:
root@raspberrypi5:~# ./pishrink.sh /mnt/ntfs/pishrink/2018-02-06-jessie-3.5-adafruit-peppy--2016-11-08-pitft-35r.img /mnt/ntfs/pishrink/2018-02-06-jessie-3.5-adafruit-peppy--2016-11-08-pitft-35r-pishrink.img
Copying /mnt/ntfs/pishrink/2018-02-06-jessie-3.5-adafruit-peppy--2016-11-08-pitft-35r.img to /mnt/ntfs/pishrink/2018-02-06-jessie-3.5-adafruit-peppy--2016-11-08-pitft-35r-pishrink.img...
Creating new /etc/rc.local
/dev/loop0: 143409/391680 files (0.1% non-contiguous), 1125611/1552816 blocks
resize2fs 1.42.12 (29-Aug-2014)
ERROR: Image already shrunk to smallest size
but file size is larger than the image size:
root@raspberrypi5:~# ls -l /mnt/ntfs/pishrink/*img
-rwxrwxrwx 2 pi pi 7994972160 Feb 6 11:48 /mnt/ntfs/pishrink/2018-02-06-jessie-3.5-adafruit-peppy--2016-11-08-pitft-35r.img
-rwxrwxrwx 1 pi pi 7994972160 Feb 7 14:03 /mnt/ntfs/pishrink/2018-02-06-jessie-3.5-adafruit-peppy--2016-11-08-pitft-35r-pishrink.img
Linux command parted reports:
root@raspberrypi5:~# parted -s /mnt/ntfs/pishrink/2018-02-06-jessie-3.5-adafruit-peppy--2016-11-08-pitft-35r-pishrink.img print
Model: (file)
Disk /mnt/ntfs/pishrink/2018-02-06-jessie-3.5-adafruit-peppy--2016-11-08-pitft-35r-pishrink.img: 7995MB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:
Number Start End Size Type File system Flags
1 4194kB 70.3MB 66.1MB primary fat16 lba
2 70.3MB 6431MB 6360MB primary ext4
Disk file size is 7995MB
Partition End is 6431MB
When running PiShrink on Debian 9, I get a lot of *2fs and mount errors:
$ sudo ./pishrink.sh ~/Desktop/shared/FULL_raspbian+RetroPie.img
tune2fs: Input/output error while trying to open /dev/loop0
Couldn't find valid filesystem superblock.
tune2fs: Input/output error while trying to open /dev/loop0
Couldn't find valid filesystem superblock.
mount: /dev/loop0 is write-protected, mounting read-only
mount: wrong fs type, bad option, bad superblock on /dev/loop0,
missing codepage or helper program, or other error
In some cases useful info is found in syslog - try
dmesg | tail or so.
md5sum: /tmp/tmp.o0QqAkNH7P/etc/rc.local: No such file or directory
./pishrink.sh: line 63: [: !=: unary operator expected
umount: /tmp/tmp.o0QqAkNH7P: not mounted
e2fsck 1.43.4 (31-Jan-2017)
e2fsck: Operation not permitted while trying to open /dev/loop0
You must have r/w access to the filesystem or be root
resize2fs 1.43.4 (31-Jan-2017)
resize2fs: Operation not permitted while trying to open /dev/loop0
./pishrink.sh: line 136: [[: Couldn'tfindvalidfilesystemsuperblock.: syntax error: invalid arithmetic operator (error token is "'tfindvalidfilesystemsuperblock.")
expr: syntax error
expr: syntax error
expr: syntax error
resize2fs 1.43.4 (31-Jan-2017)
resize2fs: Operation not permitted while trying to open /dev/loop0
Couldn't find valid filesystem superblock.
ERROR: resize2fs failed...
mount: /dev/loop0 is write-protected, mounting read-only
mount: wrong fs type, bad option, bad superblock on /dev/loop0,
missing codepage or helper program, or other error
In some cases useful info is found in syslog - try
dmesg | tail or so.
mv: cannot stat '/tmp/tmp.o0QqAkNH7P/etc/rc.local.bak': No such file or directory
umount: /tmp/tmp.o0QqAkNH7P: not mounted`
$ sudo dmesg | tail
[ 1636.561900] UDF-fs: error (device loop0): udf_read_tagged: read failed, block=5911639, location=5911639
[ 1636.561904] UDF-fs: error (device loop0): udf_read_tagged: read failed, block=5911747, location=5911747
[ 1636.561909] UDF-fs: error (device loop0): udf_read_tagged: read failed, block=5911491, location=5911491
[ 1636.561913] UDF-fs: error (device loop0): udf_read_tagged: read failed, block=5911745, location=5911745
[ 1636.561917] UDF-fs: error (device loop0): udf_read_tagged: read failed, block=5911489, location=5911489
[ 1636.561921] UDF-fs: error (device loop0): udf_read_tagged: read failed, block=512, location=512
[ 1636.561925] UDF-fs: error (device loop0): udf_read_tagged: read failed, block=256, location=256
[ 1636.561928] UDF-fs: error (device loop0): udf_read_tagged: read failed, block=512, location=512
[ 1636.561930] UDF-fs: warning (device loop0): udf_fill_super: No partition found (1)
[ 1636.571291] isofs_fill_super: bread failed, dev=loop0, iso_blknum=16, block=32
Can anybody tell me what the hell is going on? The errors seem to have to do with no permissions to me, but I'm sure I'm running the script as root.
P.S.: Please don't look at my formatting :)
The error I got running pishrink.sh was
md5sum: /tmp/tmp.3KNSjKk0bS/etc/rc.local: No such file or directory
This led me to discover that pishrink.sh is looking for the last entry "tail -n 1
" in the partition table. In my case, with a pre-1.5 NOOBS install, this found the NOOBS partition (3) instead of the root partition of Raspbian (6). See NOOBS Partitioning Explained. Pishrink continued running, but at best the wrong partition was shrunk. More likely it didn't work at all.
I tried adding support for a -p flag ("use_this_partition"), so I could specify "-p 6
" and it would use grep "^6:"
instead of tail
which seemed to work, but then produced this error
Error: Can't have overlapping partitions.
which leads me to believe I am in over my head. Maybe pishrink doesn't work unless it's working on the last partition, or maybe it doesn't work on non-primary logical partitions, or maybe I just did something wrong to mess up the math. In any case, I think it would be cool if this could be fixed, but I think the right answer for me is to use a newer NOOBS, or more likely get rid of NOOBS and just run Raspbian as described here.
Thanks for your attention.
I had the issue wiht the title "pishrink is hanged in parted" and I have see the issue "pause at "part2=parted $img unit B mkpart primary $partstart $newpartend
"
Both have probably the same cause. The command parted expects an Input (i or c).
The -s option can prevents the hang.
when I delet this "IF" block:
if (( EUID != 0 )); then
echo "ERROR: You need to be running as root."
exit -3
fi
I get the error: ERROR: parted is not installed
I am at my whits end. I used to be able to use Pishrink religiously without issue however I am now facing a random issue where the pishrink operation will complete however upon inspection of the newly shrunk .img file there is 2+GB of free space still in the .img.
osboxes@osboxes:/media/sf_PiShrinkFiles$ sudo ./pishrink.sh My-Image-1c.img
Creating new /etc/rc.local
rootfs: 123054/417792 files (0.7% non-contiguous), 1121550/1644000 blocks
resize2fs 1.42.13 (17-May-2015)
resize2fs 1.42.13 (17-May-2015)
Resizing the filesystem on /dev/loop0 to 1639575 (4k) blocks.
The filesystem on /dev/loop0 is now 1639575 (4k) blocks long.
Shrunk My-Image-1c.img from 15G to 6.4G
now when I boot on a fresh card it shows this for the filesystem:
/dev/mmcblk0p2 Size 7.2GB Used 4.4GB Avail 2.3G
Here is also another capture of a similar operation that I had happen recently.
This is the case no matter which size card I write to be it 8, 16, or 32 with a direct right with no expansion having taken place.
Would you have any suggestions or clues?
Hi,
This script is ace. The only issue I encountered was Ubuntu MATE 16.04 image doesn't have raspi-config installed. Perhaps add apt-get install raspi-config -y
in the rc.local?
Thanks, PLA
Is it possible to use PiShrink using a bootable SD card as the source, or do I have to start from an .img file?
I've been using the commands
sudo dd if=/dev/mmcblk0 of=<pathname>raspi.img
sudo pishrink.sh <pathname>raspi.img
to copy the image to a USB flashdrive connected to my R-Pi first, then running PiShrink against the result. It's a it time consuming process if I have a large SD card in the Pi. (Not complaining. PiShrink has saved me a lot of hassle trying to get large (mostly empty) images onto smaller cards.
Is it possible to do
pishrink.sh /dev/mmcblk0 <pathname>raspi.img
and eliminate an extra step?
After shrinking a couple of images successfully (and getting progress reports similar to what is shown on the "Example" on your code page), I'm not getting an abbreviated printout:
Copying /mnt/pi-usbdrive/raspi-stretch2.img to /mnt/pi-usbdrive/raspi-str2-sml.img...
Creating new /etc/rc.local
rootfs: 137443/957712 files (0.2% non-contiguous), 1150083/3877760 blocks
resize2fs 1.43.4 (31-Jan-2017)
resize2fs 1.43.4 (31-Jan-2017)
Resizing the filesystem on /dev/loop0 to 1574368 (4k) blocks.
Begin pass 3 (max = 119)
Scanning inode table XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
The filesystem on /dev/loop0 is now 1574368 (4k) blocks long.
Shrunk /mnt/pi-usbdrive/raspi-str2-sml.img from 15G to 6.1G
No error reports, but seems odd that I am not getting the rest of the status report (pass 1, pass 2, pass 3... etc.)
Is this anything to worry about?
If you're getting the error:
[...]
has unsupported feature(s): metadata_csum.
[...]
This means that you're running on a too old version of e2fsprogs.
For instance, in order to run this on Ubuntu 16.04 (and earlier versions), you will need to compile e2fsprogs by hand and update the references in the script. (Ubuntu comes with e2fsprogs version 1.42.13, but I was only able to get this working with 1.43.4.)
Autoresize not working with Retropie 4.4 image
The usage example in your README.md file reads:
Usage: ./pishrink [-s] imagefile.img [newimagefile.img]
Should it read:
Usage: ./pishrink.sh [-s] imagefile.img [newimagefile.img]
(note added .sh)
Also, should the first line of your example read:
... sudo ./pishrink.sh pi.img
(note added "pi" before shrink)
Does the script not support Kali for RaspberryPi? I shrink Kali image, but not success.
Hi,
by line 115 part2=parted $img unit B mkpart primary $partstart $newpartend
is script hanging with
Warning: The resulting partition is not properly aligned for best performance.
the script expected hidden a input (i = ignore or c = cancel).
Press I i or c the new Image is unusable
My System is a Raspberry Pi 2 tasted with JESSIE and WHEEZY. The Images is from WHEEZY.
Sorry for my bad English
Best regards, Klaus
Not working on Raspbian image
sudo ./pishrink.sh 2016-05-27-raspbian-jessie.img new.img
Copying 2016-05-27-raspbian-jessie.img to new.img...
Creating new /etc/rc.local
e2fsck 1.42.13 (17-May-2015)
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
/dev/loop0: 129993/241440 files (0.1% non-contiguous), 856512/964096 blocks
resize2fs 1.42.13 (17-May-2015)
resize2fs 1.42.13 (17-May-2015)
The containing partition (or device) is only 964096 (4k) blocks.
You requested a new size of 984096 blocks.
ERROR: resize2fs failed...
Raspbian download:
https://www.raspberrypi.org/downloads/raspbian/
Shrink doesn't complete correctly
command line:
sudo ./pishrink.sh /media/sf_condivisione/tjbot_2.img immagine.jmg
Copying /media/sf_condivisione/tjbot_2.img to immagine.jmg...
Creating new /etc/rc.local
e2fsck 1.42.13 (17-May-2015)
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
root: 166841/863264 files (0.3% non-contiguous), 1371367/3451264 blocks
resize2fs 1.42.13 (17-May-2015)
resize2fs 1.42.13 (17-May-2015)
Resizing the filesystem on /dev/loop0 to 1701696 (4k) blocks.
Begin pass 2 (max = 405323)
Relocating blocks XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Begin pass 3 (max = 106)
Scanning inode table XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Begin pass 4 (max = 17858)
Updating inode references XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
The filesystem on /dev/loop0 is now 1701696 (4k) blocks long.
Error: Can't have overlapping partitions.
fdisk print of partition table of input image:
mauro@mauro-VirtualBox:~$ sudo fdisk /media/sf_condivisione/tjbot_2.img
Welcome to fdisk (util-linux 2.27.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 /media/sf_condivisione/tjbot_2.img: 14,9 GiB, 15931539456 bytes, 31116288 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: 0x00052c15
any idea on how to overcame this problem?
Thanks in advance,
Mauro
As mentioned in this forum I want to mention there is an alternative way to resize the partition on first boot, instead of touching the /etc/rc.local
file.
You can add init=/usr/lib/raspi-config/init_resize.sh
space separated to the end of the first line in the /boot/cmdline.txt
file. This file resides on the boot partition and as you surely know, the boot partition has FAT32 filesystem. That script also removes the init
statement after resizing.
I think this is the way it is meant to be.
Hello there
This pishrink script saved my day :-)
I use minibian and create my images with win32diskimager
I got this warning:
Warning: The resulting partition is not properly aligned for best performance
that was fixed by adding -a minimal on line 167, like this:
part2=parted -a minimal "$img" unit B mkpart primary $partstart $newpartend
it did not work with optimal
Thank you, this is amazing :-)
Hello,
sorry if this is a noob question but I can't get it working. I'm running raspbian jessie and trying to resize an image from a windows network share. I made the image using Win32DiskImager 1.0. This is all what i get
sudo ~/bin/pishrink.sh /mnt/network/1.img
Creating new /etc/rc.local
/dev/loop0: Inodes wurden gefunden, die Teil einer defekten verketteten Liste von
verwaisten Inodes waren.
/dev/loop0: UNERWARTETE INKONSISTENZ; fsck MANUELL AUSFÜHREN
(d.h. ohne die Optionen -a oder -p)
resize2fs 1.43.3 (04-Sep-2016)
Bitte lassen Sie zuerst „e2fsck -f /dev/loop0“ laufen.
/home/pi/bin/pishrink.sh: Zeile 148: 7659520 - : Syntax Fehler: Operator erwartet. (Fehlerverursachendes Zeichen ist \"- \").
resize2fs 1.43.3 (04-Sep-2016)
Bitte lassen Sie zuerst „e2fsck -f /dev/loop0“ laufen.
ERROR: resize2fs failed...
my command: pishrink 'imagefile' 'newimagefile'
in this case, pishrink copies the imagefile and than runs parted.
I have a corrupt imagefile. Linux command cp doesn't recognize a problem.
Copying takes 10 minutes on a RPi 3B. After copying, Linux parted runs in an error.
I would prefer running parted checking the imagefile before copying the image.
I'm running PiShrink from the current Raspian-stretch. I can never get PiShrink to run using the command in your example:
sudo ./pishrink.sh ...
I get an error: -bash: ./pishrink.sh: No such file or directory
If I omit the leading ./ and just enter sudo pishrink.sh...
everything runs fine.
I thought ./ just meant use a relative directory reference, so it was the same as leaving it out, but I guess that's not the case.
Did I make an error in my installation? (I followed the commands suggested by @framps in issue #36.)
Just wondering if I did something wrong, or if the ./ should be deleted from your example.
and BTW, it might be worth including the recommended commands to install this properly in the instructions/examples on your "code" page. Us newbs have a tough time figuring that stuff out.
Hello,
I am getting an error while launching the /etc/rc.local file and thus also my partition does not get resized. It seems my rc.local file has trash in it.
I am using your script within a VirtualBox instance with Debian OS targeting a vbox shared folder of my host OS (Windows) containing a modified .img file cloned from a RPi SD card.
Maybe you have some insight on what is happening. Below you can find the related messages.
Output from serial console:
Starting /etc/rc.local Compatibility...
Starting Permit User Sessions...
[FAILED] Failed to start /etc/rc.local Compatibility.
See 'systemctl status rc-local.service' for details.
Output from systemctl status rc-local.service
:
● rc-local.service - /etc/rc.local Compatibility
Loaded: loaded (/lib/systemd/system/rc-local.service; static; vendor preset: enabled)
Drop-In: /lib/systemd/system/rc-local.service.d
└─debian.conf
/etc/systemd/system/rc-local.service.d
└─ttyoutput.conf
Active: failed (Result: exit-code) since Sat 2017-09-30 10:36:21 UTC; 5min ago
Process: 312 ExecStart=/etc/rc.local start (code=exited, status=203/EXEC)
Sep 30 10:36:20 raspberrypi systemd[1]: Starting /etc/rc.local Compatibility...
Sep 30 10:36:21 raspberrypi systemd[1]: rc-local.service: Control process exited, code=exited status=203
Sep 30 10:36:21 raspberrypi systemd[1]: Failed to start /etc/rc.local Compatibility.
Sep 30 10:36:21 raspberrypi systemd[1]: rc-local.service: Unit entered failed state.
Sep 30 10:36:21 raspberrypi systemd[1]: rc-local.service: Failed with result 'exit-code'.
Warning: rc-local.service changed on disk. Run 'systemctl daemon-reload' to reload units.
Contents of the file /etc/rc.local
(seems to contain UTF-16 encoded stuff, so maybe filesystem related or vbox related!?):
PFKF@▒▒
▒ (F▒FF▒▒▒▒▒▒D▒F▒d▒▒▒0▒▒ %` q▒▒▒▒▒▒-▒▒▒-▒▒▒▒
▒▒P▒!PFnh@▒▒
Contents of the file /etc/rc.local.bak
:
#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.
# Print the IP address
_IP=$(hostname -I) || true
if [ "$_IP" ]; then
printf "My IP address is %s\n" "$_IP"
fi
exit 0
Thanks in advance,
Julian
Hi, I used your tool months ago and it worked like a charm. Today I shrinked a new image, everything looks ok to me (no errors on console while using your tool).
The problem is, when I write the 4.4Gb-shrinked-image to my 32Gb-microSD... the system boots ok, but the content of /etc/rc.local
is not executed, so the system is not auto-expanded, also the original rc.local is not replaced and there is no reboot at the end.
If I reboot again the system, rc.local still won't auto-execute, BUT if I run manually /etc/rc.local
then all the commands are executed, system is expanded, rc.local is restored and finally the system goes to reboot.
Can you help me with that? I can send you more information, also I can send you my *.img file so you can test that issue! Thanks :)
UPDATE: I think commit cad2e0e is causing the issue
Hi @Drewsif - thanks for creating this!
I'm hitting a is not an SD card...
issue with Raspbian 2017-07-05. Looking at the release notes for Raspbian I see that 2017-04-10 added:
Use PARTUUID to support USB boot
My /boot/cmdline.txt
looks like this:
dwc_otg.lpm_enable=0 console=serial0,115200 console=tty1 root=PARTUUID=52a14e38-02 rootfstype=ext4 elevator=deadline fsck.repair=yes rootwait
And I see there's already a pull request that reverts the ROOT_PART stuff anyways: #15
Command: ./pishrink.sh "corrupt_image" "newimagefile"
parted fails with "Error: Can't have a partition outside the disk!" but pishrink continues with additional errors.
Sorry for missing leading spaces in consol output. The code tag eats them.
root@raspberrypi5:~# ./pishrink.sh /mnt/ntfs/pishrink/2018-02-06-jessie-3.2-waveshare-peppy--2016-11-08-pitft-35r.img /mnt/ntfs/pishrink/2018-02-06-jessie-3.2-waveshare-peppy--2016-11-08-pitft-35r-pishrink.img
Copying /mnt/ntfs/pishrink/2018-02-06-jessie-3.2-waveshare-peppy--2016-11-08-pitft-35r.img to /mnt/ntfs/pishrink/2018-02-06-jessie-3.2-waveshare-peppy--2016-11-08-pitft-35r-pishrink.img...
Error: Can't have a partition outside the disk!
tune2fs: Invalid argument while trying to open /dev/loop2
Couldn't find valid filesystem superblock.
mount: wrong fs type, bad option, bad superblock on /dev/loop2,
missing codepage or helper program, or other error
In some cases useful info is found in syslog - try
dmesg | tail or so.
md5sum: /tmp/tmp.AysbwzUb5G/etc/rc.local: No such file or directory
./pishrink.sh: line 69: [: !=: unary operator expected
umount: /tmp/tmp.AysbwzUb5G: not mounted
e2fsck: Invalid argument while trying to open /dev/loop2
/dev/loop2:
The superblock could not be read or does not describe a valid ext2/ext3/ext4
filesystem. If the device is valid and it really contains an ext2/ext3/ext4
filesystem (and not swap or ufs or something else), then the superblock
is corrupt, and you might try running e2fsck with an alternate superblock:
e2fsck -b 8193 <device>
or
e2fsck -b 32768 <device>
resize2fs 1.42.12 (29-Aug-2014)
resize2fs: Invalid argument while trying to open /dev/loop2
./pishrink.sh: line 142: [[: Couldn'tfindvalidfilesystemsuperblock.: syntax error: invalid arithmetic operator (error token is "'tfindvalidfilesystemsuperblock.")
./pishrink.sh: line 148: - Couldn'tfindvalidfilesystemsuperblock.: syntax error: invalid arithmetic operator (error token is "'tfindvalidfilesystemsuperblock.")
resize2fs 1.42.12 (29-Aug-2014)
resize2fs: Invalid argument while trying to open /dev/loop2
Couldn't find valid filesystem superblock.
ERROR: resize2fs failed...
mount: wrong fs type, bad option, bad superblock on /dev/loop2,
missing codepage or helper program, or other error
In some cases useful info is found in syslog - try
dmesg | tail or so.
mv: cannot stat ‘/tmp/tmp.AysbwzUb5G/etc/rc.local.bak’: No such file or directory
umount: /tmp/tmp.AysbwzUb5G: not mounted
Add an output option to write the new smaller file to a different file
I run backups of my Raspberries onto a NAS.
These images should be shrinked nightly via cron, but
Copying /mnt/backup/pi/pis0-20171031.img to /mnt/backup/pi/shrinked/pis0-20171031.img...
Creating new /etc/rc.local
e2fsck 1.43.4 (31-Jan-2017)
e2fsck: A terminal is needed for interactive repairs
resize2fs 1.43.4 (31-Jan-2017)
resize2fs 1.43.4 (31-Jan-2017)
Please run first "e2fsck -f / dev / loop0".
ERROR: resize2fs failed...
OS
Linux nas 3.16.0-4-amd64 #1 SMP Debian 3.16.43-2 (2017-04-30) x86_64 GNU/Linux
PRETTY_NAME="Debian GNU/Linux 9 (stretch)"
NAME="Debian GNU/Linux"
VERSION_ID="9"
VERSION="9 (stretch)"
ID=debian
HOME_URL="https://www.debian.org/"
SUPPORT_URL="https://www.debian.org/support"
BUG_REPORT_URL="https://bugs.debian.org/"
Is there chance to run it in background?
https://github.com/Drewsif/PiShrink/blob/master/pishrink.sh#L158 uses a $rc
variable, but this is never defined anywhere.
If pishrink does not leave properly, the loop device remains open. This takes up memory space until the loop device is deleted.
Remedy could bring a trap handler
I add some comments in pishrink.sh, as follow:
#Shrink partition
losetup -d $loopback
echo disatch dev
part3=parted $img print
partnewsize=expr $minsize \* $blocksize | tr -d '\n'
newpartend=expr $partstart + $partnewsize | tr -d '\n'
echo cal partsize
part1=parted $img rm $partnum
part3=parted $img print
echo $part1 delete part
echo $img $partstart $newpartend
part2=parted $img unit B mkpart primary $partstart $newpartend
echo finish Shrink partition
my running result is :
yhzk@yhzk-B85M-D3H:~/PiShrink-master$ sudo ./pishrink.sh webmin-jessie.img
[sudo] password for yhzk:
Creating new /etc/rc.local
e2fsck 1.42.9 (4-Feb-2014)
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
/dev/loop0: 188860/300144 files (0.2% non-contiguous), 1167002/1187107 blocks
resize2fs 1.42.9 (4-Feb-2014)
resize2fs 1.42.9 (4-Feb-2014)
Resizing the filesystem on /dev/loop0 to 1181653 (4k) blocks.
Begin pass 2 (max = 4637)
Relocating blocks XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Begin pass 3 (max = 37)
Scanning inode table XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
The filesystem on /dev/loop0 is now 1181653 blocks long.
finish Shrink filesystem
disatch dev
cal partsize
delete part
webmin-jessie.img 48570880 4888621568
pishrink always waitting at "part2=parted $img unit B mkpart primary $partstart $newpartend
"
The command is correct,but it never exit.
It trouble me for a long time . I'm looking forward to your reply.
I'm not sure why I seem to have a lot of errors against my image when running PiShrink.
I'll get a lot of errors:
inode 139115 has compression flag set on filesystem without compression support. Clear<y>?
inode 139115 has INDEX FL flag set but its not a directory. Clear HTree index<y>?
The trouble is it feels like all the inodes are broken so I keep pressing Y
and it moves onto the next one.
So two questions really, is my image fubar'ed / have I done something so wrong, it's a doomed exercise? and 2) is there an option I can pass through to what ever is doing the check to default my action to Y
? (I've left a weight on the keyboard for now!)
Thanks
I appreciate all that you do. I just haven't been able to get PiShrink to work. This is a log of my latest attempt to run PiShrink. My computer is a Raspberry Pi 3 running the latest version of Raspbian. PiShrink.sh and the 16GB recalbox410.img are on my external hard drive. The resulting image is unusable. It seems a temp folder with rc.local missing. I can see in my File Manager that the folder is created. I lack permissions to look inside the folder. I also tried this from Ubuntu MATE and received the same error. My Recalbox image has about 3 gigabytes of free space.
Just FYI. Thanks in advance, if you do read this.
pi@raspberrypi:/media/pi/FreeAgentDrive/PiShrink-master $ sudo ./pishrink.sh recalbox410.img
md5sum: /tmp/tmp.kpSNXt6TfD/etc/rc.local: No such file or directory
./pishrink.sh: line 69: [: !=: unary operator expected
share0: 8149/794624 files (0.7% non-contiguous), 2407673/3169792 blocks
resize2fs 1.43.4 (31-Jan-2017)
resize2fs 1.43.4 (31-Jan-2017)
Resizing the filesystem on /dev/loop0 to 2883672 (4k) blocks.
Begin pass 2 (max = 216118)
Relocating blocks XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Begin pass 3 (max = 97)
Scanning inode table XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
The filesystem on /dev/loop0 is now 2883584 (4k) blocks long.
Error: You requested a partition from 2822766592B to 14634287104B (sectors 5513216..28582592).
The closest location we can manage is 15816720384B to 15816720384B (sectors 30892032..30892032).
Shrunk recalbox410.img from 15G to 15G
Suggestion: add zerofree as an additional step at the end of PiShrink.
I used a image file (3.27GB after PiShrink, 1.19GB zipped). After running sudo zerofree "$loopback"
the image become 0.99GB zipped. Running zerofree was very fast on a shrinked image. It produced a sizeable saving for how image files are typically stored and transferred.
I had no other way to contact you and congrats about the BEAUTIFUL work you have done here!
I had a 128GB image and was struggling to find a way to resize it without having to install or virtualize a linux environment (every other script that i hitted needs gparted or dcfldd)
I discovered that my "old" Synology DS214play was perfectly capable of running it.
I had to install parted using "opkg install parted" (bootstrap is required to use opkg/ipkg) and all the other dependences were already satisfied by the fact that the NAS deals with partitions by definition, i was even able to burn the img to an sd card by dd'ing it directly to the embedded sd card reader.
Feel free to add a note somewhere about it if you want.
I hope that i brighten your day and keep up with the cool work! Thanks again!
Marco
If path to image contains blank spaces the script won't find the image correctly.
Hi,
on first boot I got
[ 1.618965] mmcblk0: p1 p2
and then the Pi Zero froze.
I've solved the problem so I though I'd post here to share the knowledge.
After resizing an image the first boot fails due to (I think) the changed naming of the partitions in /etc/fstab and cmdline.txt.
My solution after much googling was to replace:
PARTUUID=0e75fcd5-01 with /dev/mmcblk0p1
and
PARTUUID=0e75fcd5-02 with /dev/mmcblk0p2
in these files.
I did it by mounting the partitions, but making the change before creating the full fat image would probably be easier. Unless it involves creating a time machine.
HTH
Bruce
Edit,
more info here
https://raspberrypi.stackexchange.com/questions/68082/why-wont-my-raspberrypi-boot-if-i-use-parted-to-adjust-the-partition
As far as I understand the script there is an option to create a copy of the original file and then pishrink works on this copy.
This, however, creates high simultaneous read/write operations on one HDD (slow, not good for the hardware).
What I would like to have is to specifiy an input file (that will not be changed) and a different output file (that will be created with shrinked size).
This way, I would have only read operations on HDD1, and write opeations on HDD2 (faster, and good for the hardware).
Is this possible?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.