Sunday, 21 May 2023

Enabling TRIM with USB SSD

From time to time I had hiccups with my USB 3 SSD when run Raspberry Pi diagnostics (agnostics). Occasionally it did not reach the 160MB/s advertised write speed. They were a bargain on amazon so I thought I have to live with this. 64GB SSD drive was 10 GBP while the USB 3 enclosure was 20 GBP.

64 GB SSD drive

USB 3 enclosure

This article from Jeff Geerling explains how to configure TRIM on SSD:

Enabling TRIM on an external SSD on a Raspberry Pi

Initially I observed that TRIM was not configured on my boot drive:

jordana@pi4g:~ $ sudo fstrim -v /
fstrim: /: the discard operation is not supported
jordana@pi4g:~ $ lsblk -D
NAME   DISC-ALN DISC-GRAN DISC-MAX DISC-ZERO
sda           0      0B       0B         0
├─sda1        0      0B       0B         0
└─sda2        0      0B       0B         0

I installed the SCSI tools based on the article and confirmed that my drive supports TRIM:

jordana@pi4g:~ $ sudo apt-get install -y sg3-utils lsscsi
jordana@pi4g:~ $ sudo sg_vpd -p bl /dev/sda
Block limits VPD page (SBC):
  Maximum unmap LBA count: 4194240
  Maximum unmap block descriptor count: 8
jordana@pi4g:~ $ sudo sg_vpd -p lbpv /dev/sda
Logical block provisioning VPD page (SBC):
  Unmap command supported (LBPU): 1

I checked that the provisioning mode is full and changed it to unmap:

jordana@pi4g:~ $ sudo find /sys/ -name provisioning_mode -exec grep -H . {} + | sort
/sys/devices/platform/scb/fd500000.pcie/pci0000:00/0000:00:00.0/0000:01:00.0/usb2/2-1/2-1:1.0/host0/target0:0:0/0:0:0:0/scsi_disk/0:0:0:0/provisioning_mode:full
jordana@pi4g:~ $ sudo su
root@pi4g:/home/jordana# echo unmap > /sys/devices/platform/scb/fd500000.pcie/pci0000:00/0000:00:00.0/0000:01:00.0/usb2/2-1/2-1:1.0/host0/target0:0:0/0:0:0:0/scsi_disk/0:0:0:0/provisioning_mode

This enabled TRIM and made my write speed consistent:

jordana@pi4g:~ $ sudo find /sys/ -name provisioning_mode -exec grep -H . {} + | sort
/sys/devices/platform/scb/fd500000.pcie/pci0000:00/0000:00:00.0/0000:01:00.0/usb2/2-1/2-1:1.0/host0/target0:0:0/0:0:0:0/scsi_disk/0:0:0:0/provisioning_mode:unmap
jordana@pi4g:~ $ lsblk -D
NAME   DISC-ALN DISC-GRAN DISC-MAX DISC-ZERO
sda           0      512B       4G         0
├─sda1        0      512B       4G         0
└─sda2        0      512B       4G         0
jordana@pi4g:~ $ sudo fstrim -v /
/: 42.3 GiB (45465530368 bytes) trimmed
jordana@pi4g:~ $ sh /usr/share/agnostics/sdtest.sh
Run 1
prepare-file;0;0;164663;321
seq-write;0;0;163840;320
rand-4k-write;0;0;60179;15044
rand-4k-read;53108;13277;0;0
Sequential write speed 163840 KB/sec (target 10000) - PASS
Random write speed 15044 IOPS (target 500) - PASS
Random read speed 13277 IOPS (target 1500) - PASS
jordana@pi4g:~ $ systemd-analyze
Startup finished in 2.275s (kernel) + 14.610s (userspace) = 16.885s 
graphical.target reached after 10.126s in userspace

To make this setting persistent I had to find out my idVendor:idProduct with lsusb and configure udev rule as follows:

jordana@pi4g:~ $ lsusb -tv
/:  Bus 02.Port 1: Dev 1, Class=root_hub, Driver=xhci_hcd/4p, 5000M
    ID 1d6b:0003 Linux Foundation 3.0 root hub
    |__ Port 1: Dev 2, If 0, Class=Mass Storage, Driver=uas, 5000M
        ID 2109:0715 VIA Labs, Inc. VL817 SATA Adaptor
jordana@pi4g:~ $ sudo nano /etc/udev/rules.d/10-trim.rules
ACTION=="add|change", ATTRS{idVendor}=="2109", ATTRS{idProduct}=="0715", SUBSYSTEM=="scsi_disk", ATTR{provisioning_mode}="unmap"

Saturday, 13 May 2023

Install zerotier and use it with zeronsd on WSL

This is a guide how to install zerotier and use it with zeronsd on Windows Subsystem for Linux (WSL).

To install WSL run Windows Powershell as Administrator. As zerotier requires systemd I installed Debian:

PS C:\Windows\system32> wsl --install -d Debian
PS C:\Windows\system32> wsl --set-version Debian 2
PS C:\Windows\system32> wsl -l -v
  NAME      STATE           VERSION
* Debian    Stopped         2

Once installed I started Debian, updated its packages and added configuration to enable systemd:

jordana@dad-pc:~$ sudo apt update
jordana@dad-pc:~$ sudo apt upgrade

jordana@dad-pc:~$ sudo nano /etc/wsl.conf
[boot]
systemd=true

After reboot of Debian I had to install openssh server and reboot again to see systemd starting properly. 

jordana@dad-pc:~$ sudo apt install openssh-server

After this I have seen systemd starting properly so I could install zerotier with curl:

jordana@dad-pc:~$ systemctl list-unit-files --type=service
jordana@dad-pc:~$ sudo apt install curl

jordana@dad-pc:~$ curl -s https://install.zerotier.com | sudo bash
jordana@dad-pc:~$ sudo zerotier-cli join xxxxxxxxxxxxxxx
200 join OK

In order to configure local domain name resolution is routed to zeronsd, I had to disable that WSL generates /etc/resolv.conf in /etc/wsl.conf and add localhost to /etc/resolv.conf and reboot again.

jordana@dad-pc:~$ sudo nano /etc/wsl.conf
[network]
generateResolvConf = false
jordana@dad-pc:~$ sudo nano /etc/resolv.conf
nameserver localhost

Finally I could install dnsmasq and configure zeronsd local domain routed towards zeronsd server:

jordana@dad-pc:~$ sudo apt install dnsmasq
jordana@dad-pc:~$ sudo nano /etc/dnsmasq.conf
server=/zeronsd/192.168.193.1
server=8.8.8.8
jordana@dad-pc:~$ sudo systemctl restart dnsmasq

In order to let ansible to keep all apt packages latest I had to install python3 and configure sudo without password:

jordana@dad-pc:~$ sudo apt install python3
jordana@dad-pc:~$ sudo nano /etc/sudoers.d/nopasswd
jordana ALL=(ALL) NOPASSWD: ALL