Skip to content

Instantly share code, notes, and snippets.

@signed-log
Last active December 22, 2021 18:29
Show Gist options
  • Save signed-log/6f82935e214e025a4356082638ce5613 to your computer and use it in GitHub Desktop.
Save signed-log/6f82935e214e025a4356082638ce5613 to your computer and use it in GitHub Desktop.
Formatter script
#!/bin/bash
# THIS SCRIPT IS LICENSED UNDER CC0 v.1 AVAILABLE AT https://creativecommons.org/publicdomain/zero/1.0/legalcode
# THIS SCRIPT IS THEREFORE AVAILABLE IN THE PUBLIC DOMAIN
set -ex
FILE=img.img
SIZE=32000
cd ~
dd if=/dev/urandom of="$FILE" bs=1M count="$SIZE" # Create an image file of $SIZE * 1MB
sgdisk --clear --mbrtogpt "$FILE" # Clear any glitched partition table and convert to GPT
sgdisk --print "$FILE" # Make sgdisk aware of the new part table
sgdisk -n 0:0:+2GiB -t 0:0700 "$FILE" # Create a partition of 2GiB from the first available sector and assign it the "MS Basic Data type"
sgdisk -n 0:0:0 -t 0:8300 "$FILE" # Create a partition that uses the rest of the drive under the "Linux Filesystem" type
sudo kpartx -ag "$FILE" # Mount the image file as a block device
NAME=$(lsblk -P --nodeps | grep loop | grep \"\" | awk -F= '{print $2}'| awk '{print $1}' | tr -d '"')
LOOP_P1="/dev/mapper/${NAME}p1"
LOOP_P2="/dev/mapper/${NAME}p2"
[[ -f $LOOP_P1 ]] || exit # Test if the loop exists
#Format both partitions
mkfs.vfat -F32 -s2 "$LOOP_P1"
mkfs.ext4 "$LOOP_P2"
# Unmount the loop device
sudo dmsetup remove_all && sudo losetup -d "/dev/${NAME}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment