#!/bin/bash if [ "$(whoami)" != "root" ]; then echo "this script needs to be run as root in order to modify partitions etc. please provide your root password for sudo to execute this script as root" sudo -s /bin/bash "$0" "$@" exit $? fi image=$1 card=$2 if [ -z $1 ]; then echo "usage: raspberry_f2fs.sh " echo "example: raspberry_f2fs.sh Downloads/raspberryos.img /dev/sdb" exit 100 fi if [ ! -f $image ]; then echo "ERROR" echo "file $image not found," echo "first parameter should be the raspberry os image" exit 1 fi if ! kpartx -l $image | grep -q "loop.\+p1 : 0"; then echo "ERROR" echo "image $image not readable by kpartx or kpartx not found" echo "first parameter should be the raspberry os image, and make sure kpartx is installed" exit 2 fi if [ ! -b $card ]; then echo "ERROR" echo "$card is not a block device" echo "second argument should be a block device" exit 3 fi echo read -p "I will now ERASE EVERYTHING on $card, do you want to continue! (yN)" -n 1 -r echo if [[ ! $REPLY =~ ^[Yy]$ ]]; then echo "aborted" exit 101 fi # ok we are good to go :) set -e function clean_up(){ umount /tmp/sd umount /tmp/img kpartx -d $image rmdir /tmp/{sd,img} echo echo "aborted" } trap clean_up EXIT SIGINT echo "make sure the card is unmounted" set +e umount $card* 2>/dev/null set -e echo "erase old partitions" wipefs -af $card echo "create new partitions" parted -s $card mklabel msdos parted -s $card mkpart primary fat32 0% 256MB parted -s $card mkpart primary 256MB 100% partprobe $card sleep 2 if echo "$card" | grep -q "mmcblk"; then partbase="${card}p" else partbase=$card fi echo "format boot partition" mkfs.vfat ${partbase}1 echo "format os partition with f2fs" mkfs.f2fs -f ${partbase}2 echo "create mountpoints and mount boot partition" mkdir -p /tmp/{sd,img} mount ${partbase}1 /tmp/sd echo "load image as loopback device and mount boot partition" out=$(kpartx -av $image) echo "$out" loopdev=$(echo "$out" | sed 's/^add map \(loop[^p]\+\)p. .*$/\1/' | head -1) mount /dev/mapper/${loopdev}p1 /tmp/img echo "copy the contents of the boot partition" rsync -av /tmp/img/ /tmp/sd/ echo "adjust cmdline.txt" partuuidbase=$(blkid $card | sed -e 's/^.*PTUUID="\([^"]*\)".*$/\1/') sed -i "s/\(PARTUUID=\)[^ ]*\(-02 \)/\1$partuuidbase\2/" /tmp/sd/cmdline.txt sed -i 's/init=[^ ]*//' /tmp/sd/cmdline.txt sed -i 's/ext4/f2fs/' /tmp/sd/cmdline.txt echo echo echo "I am done with the boot partition, now is the time to configure your network for headless operation if you want to.." echo read -p "do you want to configure a wireless lan? if so, type \"y\" and i will open a configuration template in nano for you. simply adjust, quit and save and you are all set, otherwise press any key to continue without configuring a wlan" -n 1 -r echo if [[ $REPLY =~ ^[Yy]$ ]]; then cat > /tmp/sd/wpa_supplicant.conf <