Page 1 of 1

Raspberry Pi: Installing images on Linux

Posted: Wed Oct 26, 2016 5:04 pm
by ^rooker
Most of the documentation found for installing some distributions on SD card for Raspberry Pi are tailored for Windows or Mac users.
This is pretty useless for someone using GNU/Linux, so I'm collecting some information here for how to write these image files (.img.gz) to an SD-card using non-fancy, plain-vanilla tools available under most distros.

Links: Works-on-my-machine BASH script, so I don't need to remember how to dd the images on the SD cards:

Code: Select all

#!/bin/bash

# Installation HowTo:
#   http://wiki.openelec.tv/index.php/HOW-TO:Installing_OpenELEC/Writing_The_Disk_Image


function install_image
{
    local IMAGE_MASK="$1"
    local DEVICE="$2"

    if [ -z "$DEVICE" ]; then
        echo "ERROR: No device name given."
        exit 1
    else
        DEVICE="/dev/$DEVICE"
        if [ -b "$DEVICE" ]; then
            echo "Device '$DEVICE' is a block device. Good."
        else
            echo "ERROR: Device '$DEVICE' is NOT a block device."
            exit 2
        fi
    fi

    for IMAGE_GZ in `ls $IMAGE_MASK`; do
        echo "Image file: '$IMAGE_GZ'"

        IMAGE_FILE="$(basename $IMAGE_GZ '.gz')"
        IMAGE_DIR="$(dirname $IMAGE_GZ)"
        IMAGE="$IMAGE_DIR/$IMAGE_FILE"

        if [ ! -s "$IMAGE" ]; then
            echo "Need to unpack '$IMAGE_GZ'..."
            CMD="gunzip -c -d $IMAGE_GZ > $IMAGE"
            echo "$CMD"
            eval "$CMD"
        else
            echo "Unpacked file '$IMAGE' already exists."
        fi

        echo ""
        read -p "Press any key to start writing on '$DEVICE'..."
        echo ""

        echo "Copying image to device '$DEVICE'..."
        CMD="sudo dd if=$IMAGE of=$DEVICE bs=4M"
        echo "$CMD"
        eval "$CMD"

        # We only do the most recent file. Therefore exit for-loop now:
        break;
    done
}


function write_header
{
    DISTRO="$1"
    DEVICE="$2"
    VERSION="$3"
    IMAGE_MASK="$4"

    echo "============================="
    echo "$DISTRO (v$VERSION)"
    echo ""
    echo "Writing on device '$DEVICE'."
    echo "Using filemask '$IMAGE_MASK'."
    echo "============================="
    echo ""
    read -p "Press any key to continue..."
}


ACTION="$1"
DEVICE="$2"
VERSION="$3"

case "$ACTION" in
    retropie)
        IMAGE_MASK="RetroPie/retropie-$VERSION-*.img.gz"
        write_header "RetroPie" "$DEVICE" "$VERSION" "$IMAGE_MASK"
        install_image "$IMAGE_MASK" "$DEVICE"
    ;;

    libreelec)
        # Default SSH login:
        #   root / libreelec

        IMAGE_MASK="LibreELEC/LibreELEC-RPi.arm-$VERSION.img.gz"
        write_header "RetroPie" "$DEVICE" "$VERSION" "$IMAGE_MASK"
        install_image "$IMAGE_MASK" "$DEVICE"
    ;;

    openelec)
        # Default SSH login:
        #   root / openelec

        IMAGE_MASK="OpenELEC/OpenELEC-RPi.arm-$VERSION.img.gz"
        write_header "RetroPie" "$DEVICE" "$VERSION" "$IMAGE_MASK"
        install_image "$IMAGE_MASK" "$DEVICE"
    ;;

    raspbian)
        # Default SSH login:
        #   pi / raspberry (watchout! US keyboard)

        IMAGE_MASK="Raspbian/*-raspbian-$VERSION-lite.img.gz"
        write_header "Raspbian" "$DEVICE" "$VERSION" "$IMAGE_MASK"
        install_image "$IMAGE_MASK" "$DEVICE"
    ;;


    *)
        echo ""
        echo "SYNTAX: $0 (retro|openelec|libreelec|raspbian) devicename version"
        echo ""
        echo "The 'devicename' must be without '/dev/'."
        echo ""
        echo ""
    ;;
esac