├── PiShrink-macOS.xcodeproj
├── project.xcworkspace
│ ├── contents.xcworkspacedata
│ └── xcshareddata
│ │ └── WorkspaceSettings.xcsettings
└── project.pbxproj
├── Makefile
├── LICENSE
├── make_e2fstools
├── pishrink
└── README.md
/PiShrink-macOS.xcodeproj/project.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/PiShrink-macOS.xcodeproj/project.xcworkspace/xcshareddata/WorkspaceSettings.xcsettings:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/Makefile:
--------------------------------------------------------------------------------
1 | # Makefile to easily build all needed tools
2 |
3 | BINS = e2fsck resize2fs tune2fs truncate
4 |
5 | $(BINS):
6 | ./make_e2fstools
7 |
8 | clean:
9 | rm -f $(BINS)
10 |
11 | install:
12 | install -d /usr/local/bin
13 | install -m 755 e2fsck /usr/local/bin
14 | install -m 755 resize2fs /usr/local/bin
15 | install -m 755 tune2fs /usr/local/bin
16 | install -m 755 pishrink /usr/local/bin
17 | install -m 755 truncate /usr/local/bin
18 |
19 | uninstall:
20 | rm -f /usr/local/bin/e2fsck /usr/local/bin/resize2fs /usr/local/bin/tune2fs /usr/local/bin/pishrink /usr/local/bin/truncate
21 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | The MIT License (MIT)
2 |
3 | Copyright (c) 2016 Drew Bonasera
4 | (c) 2018 Simone Karin Lehmann
5 |
6 | Permission is hereby granted, free of charge, to any person obtaining a copy
7 | of this software and associated documentation files (the "Software"), to deal
8 | in the Software without restriction, including without limitation the rights
9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | copies of the Software, and to permit persons to whom the Software is
11 | furnished to do so, subject to the following conditions:
12 |
13 | The above copyright notice and this permission notice shall be included in all
14 | copies or substantial portions of the Software.
15 |
16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | SOFTWARE.
23 |
24 |
25 |
--------------------------------------------------------------------------------
/make_e2fstools:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 |
3 | # make_e2fstools
4 | # PiShrink-macOS
5 | #
6 | # Created by Simone Karin Lehmann on 13.03.18.
7 | # Copyright © 2018 LisaNet, BSD 2 clause license
8 | #
9 |
10 | ### functions
11 | download ()
12 | {
13 | cd "$PACKAGES"
14 | down_FILE=$(basename "$1")
15 | test -e "$down_FILE" || curl -LO "$1"
16 | cd "$CMPL"
17 | tar -xzf "$PACKAGES/$down_FILE"
18 | }
19 |
20 | BASE=$(pwd)
21 | PACKAGES="/Volumes/Ramdisk/packages"
22 | TARGET="/Volumes/Ramdisk/sw"
23 | CMPL="/Volumes/Ramdisk/compile"
24 | export CC=clang
25 |
26 | ### create RAM disk
27 | if [ ! -e "/Volumes/Ramdisk" ]; then
28 | DEVICE=$(hdiutil attach -nomount ram://2048000)
29 | newfs_hfs -v Ramdisk $DEVICE
30 | diskutil mount $DEVICE
31 | fi
32 |
33 | ### set up dirs
34 | mkdir "$PACKAGES"
35 | mkdir "$TARGET"
36 | mkdir "$CMPL"
37 |
38 | download https://netix.dl.sourceforge.net/project/e2fsprogs/e2fsprogs/v1.46.5/e2fsprogs-1.46.5.tar.gz
39 | cd e2fs*
40 | ./configure --prefix "$TARGET" --disable-nls
41 | make
42 | cp -a e2fsck/e2fsck "$BASE"
43 | cp -a resize/resize2fs "$BASE"
44 | cp -a misc/tune2fs "$BASE"
45 |
46 | download https://ftp.gnu.org/gnu/coreutils/coreutils-9.1.tar.xz
47 | cd core*
48 | ./configure --prefix "$TARGET" --disable-nls
49 | make
50 | cp -a src/truncate "$BASE"
51 |
52 | cd "$BASE"
53 | diskutil eject "/Volumes/Ramdisk"
54 |
--------------------------------------------------------------------------------
/pishrink:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 |
3 | usage() { echo "Usage: $0 imagefile.img [newimagefile.img]"; exit -1; }
4 |
5 | #Args
6 | img="$1"
7 |
8 | #Usage checks
9 | if [[ -z "$img" ]]; then
10 | usage
11 | fi
12 | if [[ ! -f "$img" ]]; then
13 | echo "ERROR: $img is not a file..."
14 | exit -2
15 | fi
16 |
17 | #Check that what we need is installed
18 | for command in tune2fs e2fsck resize2fs; do
19 | which $command 2>&1 >/dev/null
20 | if (( $? != 0 )); then
21 | echo "ERROR: $command is not installed."
22 | exit -4
23 | fi
24 | done
25 |
26 | #Copy to new file if requested
27 | if [ -n "$2" ]; then
28 | echo "Copying $1 to $2..."
29 | cp "$1" "$2"
30 | if (( $? != 0 )); then
31 | echo "ERROR: Could not copy file..."
32 | exit -5
33 | fi
34 | img="$2"
35 | fi
36 |
37 | #Gather info
38 | beforesize=$(ls -lh "$img" | tr -s " " | cut -d " " -f 5)
39 | partnum=$(fdisk "$img" | grep Linux | cut -d ":" -f1 | tr -d " ")
40 | partstart=$(fdisk -d "$img" | grep 0x83 | cut -d "," -f1)
41 | loopback=$(hdiutil attach -nomount "$img" | grep Linux | cut -d " " -f1)
42 | tune2fs_output=$(tune2fs -l "$loopback")
43 | currentsize=$(echo "$tune2fs_output" | grep '^Block count:' | tr -d ' ' | cut -d ':' -f 2)
44 | blocksize=$(echo "$tune2fs_output" | grep '^Block size:' | tr -d ' ' | cut -d ':' -f 2)
45 |
46 | #Make sure filesystem is ok, force yes on all questions
47 | e2fsck -y -f "$loopback"
48 | minsize=$(resize2fs -P "$loopback" | cut -d ':' -f 2 | tr -d ' ')
49 |
50 | # add 200 MB of extra space, the system may need it to run, minsize is in blocks unit
51 | minsize=$(($minsize + 200 * 1048576 / $blocksize))
52 |
53 | if [[ $minsize -gt $currentsize ]]; then
54 | echo "ERROR: Image already shrunk to smallest size"
55 | exit -6
56 | fi
57 |
58 | #Shrink filesystem
59 | resize2fs -p "$loopback" $minsize
60 | if [[ $? != 0 ]]; then
61 | echo "ERROR: resize2fs failed..."
62 | hdiutil detach $loopback
63 | exit -7
64 | fi
65 | sleep 1
66 |
67 | #Shrink partition
68 | hdiutil detach $loopback
69 | # fdisk uses a block size of 512 Bytes!
70 | partnewsize=$(($minsize * $blocksize / 512))
71 | newpartend=$(($partstart + $partnewsize))
72 |
73 | # now use fdisk to change the partition
74 | fdisk -e "$img" <