GithubHelp home page GithubHelp logo

shrink's Introduction

shrink

Linux bash script to resize Raspberry SD card images
consider further remarks

new version

- dependency tests
- low memory warning
- argument handling and help screen
- automated size retrieving from GParted
- progress bars with ETA for all time consuming actions
- environment variable support


download

download repository from GitHub,
unzip and copy for example to: ~/shrink

or

copy script to current directory
wget https://raw.github.com/qrti/shrink/master/script/shrink.sh

or

check if git is installed
$ git --version

if git is not installed
$ sudo apt-get install git-all

clone shrink repository to current directory
$ git clone https://github.com/qrti/shrink.git


necessary installs

$ sudo apt-get install gparted
$ sudo apt-get install pv


prepare

before executing the script the first time, insert your SD card

enter the following at the command line and find the name of your SD device and partitions
$ df -h

now extract the device name

example 1 for list entries like:
/dev/sdb1 + /dev/sdb2

omit digit at the end

result:
/dev/sdb

example 2 for list entries like:
/dev/mmcblk1p1 + /dev/mmcblk1p2

omit p and digit at the end

result:
/dev/mmcblk1

make script executable once

change directory
$ cd ~/shrink/script

$ chmod 755 shrink.sh
or
$ chmod a+x shrink.sh


execute

usage and help

Usage:
  (sudo) shrink.sh [<arguments>]
  (sudo) shrink.sh -h | --help
  (sudo) shrink.sh

examples:
$ sudo ./shrink.sh
read, resize and zero fill image

$ sudo ./shrink.sh --compress --skip-fill
read, resize and compress image without zero fill

$ sudo ./shrink.sh --device /dev/mmcblk1
read, resize and zero fill image from device /dev/mmcblk1

the help screen shows options and their current default values

Options:
  -h --help             show this screen
  --user                specify user who should own output files (default: root)
  --device              source and target SD card device (default: /dev/sdb)
  --date_name           image name, alternative with date and time: "image_191003113547 (default: image)"
  --image               image name with extension (default: image.img)
  --details             gparted details file path and name (default: ~/gparted_details.htm)
  --compress            compress new image (an extra file is generated) (default: false)
  --write               write new image to SD card (default: false)
  --skip-read           read image from SD card (false for an already existing image) (default: true)
  --skip-resize         resize image with GParted (default: true)
  --skip-fill           fill empty space of new image with zeroes, only possible if RESIZE=true (default: true)

changing default values by editing the script
$ nano shrink.sh

example for:
/dev/sdb1 + /dev/sdb2
omit digit

DEVICE=${DEVICE:-/dev/sdb}

example for:
/dev/mmcblk1p1 + /dev/mmcblk1p2
omit p and digit at the end

DEVICE=${DEVICE:-/dev/mmcblk1}

your username is filled in automatically, to override edit USER

USER=${USER:-`whoami`}

changing default values by using environment variables
default values can be overridden by passing them as env vars

for example to set DEVICE and READ enter at the command line
$ sudo DEVICE=/dev/sdb READ=false ./shrink.sh

explore the top of the script to configure some more things


remarks

- use this script completely at your own risk
be sure to have backups of your data

- new versions not widely tested yet
look for previous versions in directory 'deprecated'

- runs on physical or virtual Linux desktop systems

- cannot handle Raspberry NOOBS images

- do not shrink images to minimum
otherwise they won't start on your Raspberry
especially Raspbian Full Desktop images need some extra space, about >= 250 MB are advised
Raspbian Lite images might be more moderate

- when starting from a shrinked SD card for the first time
expand the filesystem to fill its space

by raspi-config
sudo raspi-config -> Expand Filesystem
sudo reboot
or
from command line
sudo raspi-config --expand-rootfs
sudo reboot

- the script is 'half automatic'
meaning at one point it will start GParted on desktop and guide you what to do

- progress display of 'fill empty space' may not end at 100 % exactly
because of difficult file system overhead calculation
nevertheless space will be filled correctly

- the script was developed and tested on a VirtualBox Windows host with Linux Mint guest

- inspired by
http://www.aoakley.com/articles/2015-10-09-resizing-sd-images.php

Donations are welcome!

https://www.paypal.com


history

V0.5
initial version, script keel fix by Barleyman

V0.6
mmcblk naming support

V0.7
simplifications

V0.71
adaption for parted 3.2, p -> print

V0.8
automated size retrieving from GParted
progress bars with ETA for all time consuming actions

V0.81
default value override by environment variables
thanks to Leon Miller-Out

V0.9
argument handling and help screen
thanks to Pedro Figueiredo e Silva

V0.91
low memory warning
gparted details path hint

V0.92
dependency tests
thanks to Tom Troyer


copyright

shrink is published under the terms of ISC license

Copyright (c) 2019 [email protected]

Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED 'AS IS' AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

shrink's People

Contributors

barleyman avatar qrti avatar sbleon avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

shrink's Issues

SD Card should be mounted at start of shrink.sh

I noticed that my card was available with fdisk -L as mmcblk0. It was not available with df -h.
The last is caused by not mounting the partitions (automounter off).
So to have the script proper working, mount the partitions first (maybe check really on /dev/mmcblk0p1/2 in the script).

Problem when using SD Card adaptors

First of all thank you for this handy script!
I encountered a problem as the script only handles adaptors whose devices are named /dev/sdx but it fails right at the start for common adaptors named /dev/mmcblk0p

Problem when target sd card has more or less partitions

First of all thank you for this great script!
My run ended abnormally because my target sd had only one partition so it failed to unmount the second. I think you cannot assume that any sd card to overwrite had two partitions as well.

cd Directory in ReadMe not correct.

--- a/README.md
+++ b/README.md
@@ -68,7 +68,7 @@ result:
make script executable once

change directory
-$ cd ~/shrink
+$ cd ~/shrink/script

$ chmod 755 shrink.sh
or

Add a hint on where to save the Gparted details

press button 'Save Details' and 'Save' in file requester doesn't really tell me where to save it. After reading through the code the default search path is ~/gparted_details.htm.

Maybe this path should be added to the instruction printed on the command line.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.