Skip to content

Instantly share code, notes, and snippets.

@macserv
Last active July 21, 2021 06:16
Show Gist options
  • Save macserv/a8ba84a26ad6373f5a492a8ac66f7e2a to your computer and use it in GitHub Desktop.
Save macserv/a8ba84a26ad6373f5a492a8ac66f7e2a to your computer and use it in GitHub Desktop.
Script to format a USB drive and make it a bootable ThinStation disk.
#!/usr/bin/zsh
#
# mktsusbstick
# Configure a specific disk as a ThinStation USB Stick
#
###
# CONFIGURATION
#
# The path to the drive device, as shown in /dev/disk/by-id/.
# It must link to the device for the disk, not a partition
# within the drive.
DISK_ID="path_to_your_usb_drive_id"
# The name to be given to the FAT32 filesystem which is
# created on your drive during this process.
PARTITION_NAME="TS-TEST"
# The path to your ThinStation repository clone, which was
# used to build your SYSLINUX image. This is the repository
# root, not any directory within.
TS_REPO="/home/_REPLACE_WITH_YOUR_USER_/Projects/thinstation"
function mktsusbstick
{
local DEV_DISK_PATH="/dev/disk/by-id/${DISK_ID}"
[[ -e ${DEV_DISK_PATH} ]] || { echo ; echo '[ERROR] Specified disk not found. Check that your USB stick is connected.' ; return 10 }
local DEV_SD_PATH=$(readlink -f "/dev/disk/by-id/${DISK_ID}")
[[ -e ${DEV_SD_PATH} ]] || { echo ; echo '[ERROR] Disk device not found.' ; return 20 }
local TS_MBR_PATH="${TS_REPO}/ts/build/packages/installer/install/mbr.bin"
[[ -e "${TS_MBR_PATH}" ]] || { echo ; echo '[ERROR] ThinStation mbr.bin not found at expected path in repository.' ; return 30 }
local DEV_PARTITION_PATH="${DEV_SD_PATH}1"
local TEMP_MOUNT_POINT="/mnt/${PARTITION_NAME}"
umount -l ${DEV_SD_PATH}?* # (?* glob for all partitions)
dd if=/dev/zero of=${DEV_SD_PATH} bs=1M count=1
parted --script "${DEV_SD_PATH}" mklabel msdos && sync
parted --script --align optimal "${DEV_SD_PATH}" mkpart primary '0%' '100%' && sync
mkfs.vfat -n "${PARTITION_NAME}" "${DEV_PARTITION_PATH}" && sync
syslinux --install ${DEV_PARTITION_PATH} && sync
parted ${DEV_SD_PATH} set 1 boot on && sync
dd if=${TS_MBR_PATH} of=${DEV_SD_PATH} conv=notrunc bs=440 count=1 && sync
mkdir "${TEMP_MOUNT_POINT}"
mount ${DEV_PARTITION_PATH} ${TEMP_MOUNT_POINT}
cp -v -R ${TS_REPO}/build/boot-images/syslinux/boot ${TEMP_MOUNT_POINT}/.
umount -l ${DEV_SD_PATH}?* && sync
eject ${DEV_SD_PATH}
rm -rf ${TEMP_MOUNT_POINT}
echo ; echo "[INFO] ThinStation USB Stick preparation complete. Please remove the USB drive."
}
###
# EXECUTE
#
mktsusbstick
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment