Skip to content

Instantly share code, notes, and snippets.

View apsun's full-sized avatar

Andrew Sun apsun

View GitHub Profile
@apsun
apsun / repl.py
Last active December 22, 2023 06:12
One-liner to drop into a Python REPL
__import__("code").InteractiveConsole({**globals(), **locals()}).interact()
@apsun
apsun / ios-exif-rename.sh
Last active November 21, 2023 20:18
Rename iOS photos/videos using file metadata
#!/bin/bash
#
# A note on timezone handling: this script uses the local timezone in which
# the photo/video was originally taken.
#
# "CreateDate" stores the local time on images, but UTC on videos.
# "CreationDate" stores the full timestamp with timezone, but is only
# available on videos.
shopt -s nullglob

Windows 10 on an existing UEFI Linux setup

I will assume that we are starting from a Linux-only setup with free space available on the disk, e.g. by following everything at https://gist.github.com/apsun/1f7b1da40b028a9ed1e0409ca8c3b3cc.

Starting out, the disk should look like:

/dev/sda
  sda1 <-- ESP
  sda2 <-- Linux
@apsun
apsun / cpp_exceptions_tldr.md
Last active April 26, 2022 04:09
C++ Itanium ABI for exception handling

This aims to be a brief introduction to how C++ exceptions are implemented on Linux. It's based on an afternoon of reading and may contain serious inaccuracies, so read at your own risk.

We start from the point where an exception is thrown. First, we allocate an exception object on the heap using __cxa_allocate_exception. This exception object contains:

  • the actual object you're throwing (hereafter referred to as the "user exception")
  • a C++ exception library header (__cxa_exception) which contains:
    • the std::type_info and destructor of the user exception
    • a reference count for the exception
    • some other stuff which I'll gloss over for the sake of brevity
  • a language-agnostic unwind library header (_Unwind_Exception) which contains:
  • a tag specifying whether this is a C++ exception (it is!)
@apsun
apsun / lrucache.go
Last active May 15, 2022 13:10
LRU cache using Go 1.18 generics
package main
type lruNode[K comparable, V any] struct {
prev *lruNode[K, V]
next *lruNode[K, V]
key K
value V
}
type LRUCache[K comparable, V any] struct {

Encrypted Arch Linux on UEFI

This documents how to install Arch Linux with full disk encryption under UEFI (not using secure boot, although it will be easy to enable later on if you want it). It assumes you're familiar with installing Arch Linux, but new to encryption/UEFI.

Partition the disk

Boot into the Arch Linux live USB and enter the installation environment.

Hereafter, /dev/nvme0n1 refers to the disk you are installing Arch on. /dev/nvme0n1p1 refers to the EFI system partition, and /dev/nvme0n1p2 refers to the encrypted Linux partition.

@apsun
apsun / server.conf
Last active March 2, 2022 16:17
Unbound config for a forwarding DNS server for use with WireGuard
server:
verbosity: 1
interface: 0.0.0.0
interface: ::0
port: 53
tls-cert-bundle: /etc/ssl/certs/ca-certificates.crt
# allow access from anywhere
# access-control: 0.0.0.0/0 allow
# access-control: ::0/0 allow
@apsun
apsun / extdrv-backup.sh
Last active March 4, 2023 18:13
Backup files using rsync to an external disk encrypted with LUKS
#!/bin/bash
set -euo pipefail
EXTDRV_FILE='/dev/disk/by-uuid/408acd50-8b18-470c-b4f3-39d8e6351107'
EXTDRV_LABEL='extdrv'
EXTDRV_MOUNT='/mnt/extdrv'
SRC_ROOT='/home/andrew'
DEST_ROOT='/mnt/extdrv/andrew'
@apsun
apsun / win7iso.md
Last active December 16, 2023 06:19

Windows 7 install ISO with slipstreamed updates

Preparation

Create three directories. For the sake of this tutorial, they will be:

  • c:\image - holds the contents of the ISO file
  • c:\mount - mount point for the install.wim file
  • c:\updates - holds the update files
@apsun
apsun / wg0-client.conf
Last active February 21, 2022 06:22
Example WireGuard configuration
[Interface]
PrivateKey = <REDACTED>
Address = 10.19.84.2/32,fd07:d3fc:8a02::2/128
DNS = 10.19.84.1,fd07:d3fc:8a02::1
MTU = 1280
[Peer]
PublicKey = <REDACTED>
PresharedKey = <REDACTED>
AllowedIPs = 0.0.0.0/0,::/0