[ Content | Sidebar ]

Android Connectivity Check and NTP in Mainland China

October 13th, 2019

If you’re using an Android phone with LineageOS or similar close-to-stock ROM in mainland China you may have problems with NTP not syncing and every WiFi network being marked as “no internet access”.

By default Android tries to access http://connectivitycheck.android.com/generate_204 to determine if a network has internet access. This is also used for captive portal detection. The android.com domain is completely blocked by the Great Firewall.

The default NTP server is 2.android.pool.ntp.org. At lease some of the servers in this pool are either blocked or have poor connectivity from China.

You could work around this by using a VPN all the time. But it’s actually easy to change these settings, and you only need ADB access, not root.

First change the connectivity check URL to Google’s China holding page.

adb shell settings put global captive_portal_http_url 'https://www.google.cn/generate_204'
adb shell settings put global captive_portal_https_url 'https://www.google.cn/generate_204'

You can use another URL that generates a HTTP 204 response if you don’t want to rely on anything Google.

Next change the NTP server:

adb shell settings put global ntp_server pool.ntp.org

pool.ntp.org should give servers geographically close to you. Alternatively you might like to use cn.pool.ntp.org.

Lego Submarine

October 11th, 2019

When I was child I had a bit of a thing about building Lego submarines. I think it started after we went on a day out to the soviet submarine in Folkstone, sadly no longer open to the public. Anyway it seems I’ve still got the skillz.

At the front we have the sleeping area for the crew and some missiles. The next room is the on-board dentist because… reasons. Followed by the crew relaxation area and the nuclear reactor / engine room at the rear. Upstairs is the control room.

Around Willingdon

October 6th, 2019

I went for a short walk on the South Downs near Willingdon with my parents last week.

Going up the steep hill from the village

This isn’t far from where I grew up but I only got into walking long after I moved away from Hastings so I haven’t explored here at all. Hope I can come back one day and do a longer linear walk, to Brighton, say.

Hastings Beach

October 4th, 2019

Back in Hastings at the moment. The weather this week has been rather uninspiring but it did clear up a bit yesterday afternoon and I took some photos on the beach.

Fishing boats

This ferris wheel is relatively new. Never seen it going…

The harbour arm

Lunar Lander 0.7

October 1st, 2019

I spent a little time recently updating my lunar lander game. Nothing much changed gameplay-wise but I cleaned up the code and build system, and rewrote all the graphics code away from the “legacy” OpenGL fixed-function pipeline. I also fixed a number of small issues and removed the artificial fixed 30fps frame rate.

I’ve made an experimental foray into Flatpak packaging so the game is also available on Flathub! This the most convenient way to install. Either find it in Gnome Software or use the command line:

flatpak install flathub uk.me.doof.Lander

If you want you can also get the source code on GitHub or directly.

Unlocking Encrypted Home Partition on Login

September 22nd, 2019

I recently did a new Debian install on my laptop after upgrading the NVMe and this time round I set up LUKS disk encryption for my /home partition. I want this to be as hassle-free as possible, which means having the partition automatically unlocked and mounted when I log in, rather than having to type a separate password on boot.

It’s not as straightforward as you might think, I guess because everyone’s setup and requirements are a little different. So I’ll write my notes here in case it’s useful to someone else. I’m doing this on Debian, but I cribbed a lot of it from the excellent Arch wiki.

When first setting up the encrypted partition make sure that the disk password is the same as your login password. This will be important later.

The file /etc/crypttab is read early in boot by systemd (see its crypttab map page). Systemd then calls cryptsetup on each entry in this file to unlock the partition. This is where the boot time password prompt that we want to get rid of comes from. Simply add noauto to options list at the end and systemd will skip it:

nvme0n1p3_crypt UUID=XXXX-XXXX none luks,discard,noauto

Also edit /etc/fstab to comment out or remove the entry for /home: we’ll be using pam_mount to do this directly.

sudo apt install libpam-mount

This is a PAM plugin that can mount arbitrary filesystems whenever a user logs in and unmount them when they log out. We can also use it to unlock and encrypted partition using the user’s password before mounting. This is why the login password and the disk password must be the same. Open /etc/security/pam_mount.conf.xml and add these lines to it:

<volume user="nick" fstype="crypt" path="/dev/nvme0n1p3"                        
        mountpoint="nvme0n1p3_crypt" />                                         
 
<volume user="nick" fstype="auto" path="/dev/mapper/nvme0n1p3_crypt"            
        mountpoint="/home" options="defaults,relatime,discard" />                       
 
<cryptmount>cryptsetup open --allow-discards %(VOLUME) %(MNTPT)</cryptmount>    
<cryptumount>cryptsetup close %(MNTPT)</cryptumount>

We need to add two <volume> entries. The first with fstype="crypt" unlocks the physical LUKS partition (/dev/nvme0n1p3) and creates a new volume that we can mount as a normal filesystem (/dev/mapper/nvme0n1p3_crypt). Obviously change the user name and physical device path to match your system.

The <cryptmount> and <cryptumount> entries tell pam_mount how to open and close the encrypted partition when fstype="crypt". Note that I’ve added the --allow-discard option here which enables the SSD TRIM command to reduce wear on the disk, but has some security implications which you might want to read up on.

Reboot and check everything works. If you have problems try adding:

<debug enable="1" />

to pam_mount.conf.xml and log in on a text console. This will print some diagnostic messages.

Nanpu Bridge

August 14th, 2019

Time was this blog had a bit of an obsession with bridges. I think it might have started with Naburn railway bridge. Anyway that source of content seems to have dried up of late so here’s a very large bridge a short subway trip from home.

This is the Nanpu bridge in Shanghai. The 57th longest cable-stayed bridge in the world. Hull’s got a longer one 😏.

You can take a ferry across the river and underneath the bridge which is quite fun, and there’s a walking path along both banks.

Long Time No Suzhou

August 11th, 2019

I went back to Suzhou for a weekend recently and visited a few new places, mostly around Mudu (木渎) to the west of the city.

Taiping mountain scenic area

Net master garden

I’ve been to Suzhou so many times but this was the first time I made it to Taihu, one of China’s largest inland lakes.

Taihu lake

And finally I visited Mudu old town. But sadly I got there too late to go in any of the attractions.

Mudu old street

Linus Torvalds

July 13th, 2019

I finally saw Linus Torvalds live! I think, however, reading his online rants is considerably more interesting than watching a staged conversation. 🙃

Making Emacs GUD Usable

June 9th, 2019

Emacs provides a wrapper for various debuggers including GDB called the Grand Unified Debugger (GUD). I’ve tried it in the past but always run into lots of minor annoyances with the UI so I just use command line GDB instead. But recently I’ve being trying to adopt a more “Emacs native” workflow, including using EShell instead of a separate terminal window for Bash, Magit instead of command line git, ERC for IRC, etc. So let’s see if we can fix these GUD problems…

Basic configuration

(setq gdb-many-windows t
      gdb-use-separate-io-buffer t)

The default mode of GUD just creates a single window with the the normal GDB terminal. This doesn’t seem to offer much over running GDB directly. The “many windows” mode splits the screen into six separate windows showing the current source file, locals/registers, output, etc.

Source file opens in the wrong window

By default if you jump to a source file from e.g. the stack trace window it will open on top of the command input window (labeled “2” below) rather than the source file window “1”.

This seems to be “normal” behaviour, and there are loads of threads on Stack Overflow complaining about it but with no conclusive solution. E.g. see here or here.

The problem here is that GUD makes all the popup windows “dedicated” except for the command window. When you jump to a file it opens in the first non-dedicated window, which sort-of makes sense. The function that sets up the windows is called gdb-setup-windows so we can use Emacs’ “advice” system to hook this function and run some extra code afterwards to make the command window dedicated:

(advice-add 'gdb-setup-windows :after
            (lambda () (set-window-dedicated-p (selected-window) t)))

This works because gdb-setup-windows always leaves the command window selected when it finishes.

Quitting messes up the window configuration

How do you quit anyway? I think the correct way is just to run quit in the command window. But no matter how you quit GUD always messes up whatever window configuration you had before you opened it.

We can fix that by saving the window layout when we run M-x gdb by storing the layout into a register in gud-mode-hook. The gud-sentinal function runs when some event occurs on the inferior gdb process. We can hook that to restore the window state when the process exits.

(defconst gud-window-register 123456)
 
(defun gud-quit ()
  (interactive)
  (gud-basic-call "quit"))
 
(add-hook 'gud-mode-hook
          (lambda ()
            (gud-tooltip-mode)
            (window-configuration-to-register gud-window-register)
            (local-set-key (kbd "C-q") 'gud-quit)))
 
(advice-add 'gud-sentinel :after
            (lambda (proc msg)
              (when (memq (process-status proc) '(signal exit))
                (jump-to-register gud-window-register)
                (bury-buffer))))

I’ve bound C-q to gud-quit which send the quit command to GDB to save typing.