Skip to content

Instantly share code, notes, and snippets.

View klutchell's full-sized avatar
🏠
Working from home

Kyle Harding klutchell

🏠
Working from home
View GitHub Profile
#!/usr/bin/env bash
set -eux
cleanup() {
sudo umount /mnt/flasher
sudo losetup -d "${loopback_device:-}"
}
trap cleanup EXIT
# Pull alpine image for the correct platform (avoid pulling the wrong platform on Pi Zero)
balena pull alpine --platform linux/arm/v6
# Run inotifywatch to gather filesystem access statistics for 5m
balena run --rm -it -v /mnt/data:/mnt/data:ro alpine sh -c \
'apk add --no-cache inotify-tools && /usr/bin/inotifywatch -v -t 300 -r /mnt/data'
# Run iostat with 5 min intervals
balena run --rm --privileged alpine iostat -z -k -t -d 300
@klutchell
klutchell / run.sh
Last active September 19, 2023 18:18
balena-os bulk edit gitmodules
#!/bin/bash
set -euo pipefail
GH_TOKEN="$(<"${HOME}/.github_pat")"
github_org="balena-os"
repo_yaml_match="yocto-based OS image"
clone_root_path="src" # Set the root repository clone path here
# known_branches=("pyro" "rocko" "sumo" "thud" "warrior" "zeus" "dunfell" "honister" "kirkstone" "master" "main") # Add your list of known branches here
#!/bin/bash
for file in .github/repos/*.yml; do
# delete custom merge settings
yq e 'del(.repository)' -i "$file"
# delete required approving review count as we use policy-bot instead
yq e 'del(.branches[].protection.required_pull_request_reviews.required_approving_review_count)' -i "$file"
if [ "$(yq e '.branches[].protection.required_pull_request_reviews' "$file")" = "{}" ]; then
@klutchell
klutchell / update_gitmodules.mjs
Created July 19, 2024 23:28
pin balenaOS submodules
// update_gitmodules.mjs
import fs from 'fs/promises';
import path from 'path';
import { fileURLToPath } from 'url';
import readline from 'readline';
import { parse } from 'ini';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
@klutchell
klutchell / update_gitmodules.mjs
Last active November 13, 2024 14:53
Update .gitmodules files in bulk
// update_gitmodules.mjs
import fs from 'fs/promises';
import path from 'path';
import { fileURLToPath } from 'url';
import readline from 'readline';
import { parse, stringify } from 'ini';
import simpleGit from 'simple-git';
import { createPatch } from 'diff';
const __filename = fileURLToPath(import.meta.url);
@klutchell
klutchell / dhcp-to-static.sh
Last active February 3, 2026 21:51
Safely migrate balenaOS from DHCP to static IP with automatic rollback
#!/bin/bash
#
# dhcp-to-static.sh - Safely migrate balenaOS between DHCP and static IP
#
# Uses a "dead man's switch" rollback pattern: a background timer will
# automatically revert to the previous configuration unless explicitly
# cancelled after successful connectivity verification.
#
# Usage:
# ./dhcp-to-static.sh [--dry-run] [--force] # DHCP -> static
@klutchell
klutchell / hetzner-nmconnection.sh
Last active February 10, 2026 17:23
Generate NetworkManager .nmconnection keyfile for Hetzner dedicated servers (queries Robot API, outputs to stdout)
#!/usr/bin/env bash
# Generate a NetworkManager .nmconnection keyfile for a Hetzner dedicated server.
# Queries the Hetzner Robot API for network details and writes keyfile to stdout.
#
# Usage: ROBOT_USER=xxx ROBOT_PASS=xxx ./hetzner-nmconnection.sh <server-number>
#
# Dependencies: curl, jq
# Output: .nmconnection keyfile on stdout; progress/errors on stderr
set -euo pipefail
@klutchell
klutchell / test-nmconnection.sh
Created February 10, 2026 17:31
Test a .nmconnection keyfile with automatic rollback (companion to hetzner-nmconnection.sh)
#!/usr/bin/env bash
# Test an .nmconnection keyfile and always revert. Writes result to /tmp/nm-test-result.
#
# Usage: ./test-nmconnection.sh <path-to.nmconnection>
#
# Designed to work with hetzner-nmconnection.sh:
# ./hetzner-nmconnection.sh 12345 > static.nmconnection
# ./test-nmconnection.sh static.nmconnection
set -euo pipefail
@klutchell
klutchell / leaseweb-nmconnection.sh
Created February 10, 2026 18:06
Generate NetworkManager .nmconnection keyfile for Leaseweb dedicated servers (queries Leaseweb API, outputs to stdout)
#!/usr/bin/env bash
# Generate a NetworkManager .nmconnection keyfile for a Leaseweb dedicated server.
# Queries the Leaseweb API for network details and writes keyfile to stdout.
#
# Usage: LSW_API_KEY=xxx ./leaseweb-nmconnection.sh <server-id>
#
# Dependencies: curl, jq
# Output: .nmconnection keyfile on stdout; progress/errors on stderr
set -euo pipefail