Skip to content

Instantly share code, notes, and snippets.

@tsangwpx
tsangwpx / apt-srv-debian-docker.md
Last active February 26, 2024 06:20
apt SRV records and debian-based docker image

apt/apt-get commands may use SRV record to find nearby APT service. [1]

However, offical Debian/Ubuntu images cannot utilize such functonality unless /etc/services file is present.

This means APT will download packages from deb.debian.org / archive.ubuntu.com as usual. 🤣

This also means local APT (cache/proxy) servers is bypassed and the build process may be slowed down if the bandwidth is limited.

Workaround

@tsangwpx
tsangwpx / hyperv-guest-bitlocker.md
Created May 21, 2022 11:12
HyperV guest resume into broken state

Description

After Hyper-V host shutdown / reboot, the guest machine had been saved and was resumed into broken state. Block drives were inaccessible and the guest kernel issued I/O errors. Those virtual disk are vhdx files located in BitLocker-enabled hypervisor drives.

The problem is likely that the BitLocker drives is still locked during guest resume, even though autounlock is enabled. This is because SATA drives are removable by default and auto-unlocking removable drives require user login? Since Fast Startup is enabled, windows save its hypervisor state (including guest?) after user logout. When host boot, the hypervisor state is directly resumed from disk. However, those drives containing required vhdx files are not unlocked (no credential available?) so guests run into broken state.

@tsangwpx
tsangwpx / hv-and-bitlocker.md
Created October 20, 2021 05:28
Hyper-V save & resume, broken state, IO error, and BitLocker

If the disk image is stored in encrypted volume,

  • the image may not be accessible before user login.
  • the virtual machine may not start at host boot.
  • the virtual machine may not resume successfully.

The fast boot option in power settings enables saving kernel to disk when host shutdown (hiberation?). It seems that the VM memory is also saved and the START and SHUTDOWN actions in Hyper-V configuration is ignored.

@tsangwpx
tsangwpx / argmin.py
Created July 29, 2021 19:50
argmin in pure Python
def argmin(seq: Sequence[int]):
indices = range(len(seq))
return min(indices, key=seq.__getitem__)
@tsangwpx
tsangwpx / vbox-cpuid-flags.md
Last active May 17, 2021 08:20
VirtualBox CPUID flags
~# cat /proc/cpuinfo

processor       : 0
vendor_id       : AuthenticAMD
cpu family      : 23
model           : 113
model name      : AMD Ryzen 9 3900X 12-Core Processor
stepping        : 0
microcode       : 0xffffffff
@tsangwpx
tsangwpx / click-shared-option-boilerplate.py
Last active April 26, 2021 07:35
click shared option boilerplate
"""
A shared option boilerplate with click.
The top-level do NOT collect enough information about shared options because of how click work.
To achieve the goal, the logic handling shared options must be postponed until shared options in interest are collected.
=====================
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
def alphaonly_ordinal(n):
"""
Use alphabets as ordinal symbols
a = 1, b = 2, ..., z = 26,
aa = 27, ab = 28, etc
"""
assert n > 0
s = ''

A volume group is unable to activate with the following syslog messages.

Feb  6 15:57:24 hostname kernel: [31991.352746] device-mapper: cache metadata: blocks_are_clean_separate_dirty: cache block 0 is dirty
Feb  6 15:57:24 hostname kernel: [31991.354279] device-mapper: table: 253:47: cache: Cannot enter passthrough mode unless all blocks are clean
Feb  6 15:57:24 hostname kernel: [31991.354291] device-mapper: ioctl: error adding target to table
Feb  6 15:57:24 hostname lvm[318]:   device-mapper: reload ioctl on  (253:47) failed: Invalid argument

There is something wrong with the dirty bit of the cache.

from __future__ import annotations
"""
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
@tsangwpx
tsangwpx / code.py
Created November 17, 2020 20:26
Performance comparsion between long if-else and mapped function
def fn_ifelse(i):
if i == 0:
return i
elif i == 1:
return i
elif i == 2:
return i
elif i == 3:
return i
elif i == 4: