Welcome to saghul’s notes

This is a collection of personal notes, tips and tricks or something like that so I don’t forget things.

Contents:

Debian

Some Debian tips and tricks.

Check for obsolete packages

Obsolete packages are those which cannot be installed from any of the configured repositories.

aptitude search ?obsolete

I have no idea if this can be accomplished with plain apt-get.

Override DNS resolvers received over DHCP

If you want to override the DNS resolvers received over DHCP, add the following to /etc/dhcp/dhclient.conf

interface "eth0" {
    supersede domain-name-servers 8.8.8.8, 8.8.4.4;
}

Adjust accordingly.

Install all build dependencies for a source package

Sometimes you can’t use apt-get build-dep (the first time you are building the package for example). We are going to use mk-build-deps from the devscripts package (equivs also needs to be installed.

mk-build-deps -ir -t "apt-get -qq -y --no-install-recommends"

Put a package on hold

Putting a package on hold:

sudo apt-mark hold package

Putting a package out of hold:

sudo apt-mark unhold package

Docker

The container technology everyone talks about. Website.

Using official Docker on Debian Sid

In order to use the latest (and greatest?) version it’s best to use the repositories provided by Docker. As root:

apt-key adv --keyserver hkp://p80.pool.sks-keyservers.net:80 --recv-keys 58118E89F3A912897C070ADBF76221572C52609D
echo "deb https://apt.dockerproject.org/repo debian-stretch main" > /etc/apt/sources.list.d/docker.list
apt-get update
apt-get install docker-engine

Using OverlayFS as the storage backend

The default storage backend on Debian seems to be devicemapper using a loop file. If you want to use OverlayFS, a systemd drop-in file is needed. Create a file in /etc/systemd/system/docker.service with the following content:

[Service]
ExecStart=
ExecStart=/usr/bin/docker daemon -H fd:// --graph="/storage/docker" --storage-driver=overlay

Remove all containers at once

Sometimes you gotta start clean.

docker rm -f $(docker ps -a -q)

Remove all images at once

Sometimes you gotta start clean.

docker rmi -f $(docker images -q)

Git

Using patches in mbox format

Git allows the creation of patch files in mbox format, which can be then sent over email or used in any other way.

First, we will use git format-patch to create the mbox file:

git format-patch --stdout HEAD~3 > tmp.patch

In the above example we created a tmp.patch file which contains the last 3 commits in the current branch. We can now use git am to apply them:

git am tmp.patch

And the 3 commits will be applied in the git tree,.

Aborting an incorrect amend

Isn’t it annoying when you do a git commit –amend when you didn’t intent to amend? If you are using wim to edit the commit message, you can abort the commit by exiting vim like this:

:cq

Caching HTTP(S) credentials

When cloning over HTTP(S) the following can be done so the credentials acre cached (by default for 15 minutes) so one doesn’t need to enter them all the time:

git config --global credential.helper cache

Reference.

Hardware

Things!

Arduino

Official Website

Signed kext for CH34x chipsets in clones

Some Arduino clones use a CH34X USB-serial bridge, which is not immediately recognized by OSX. A signed kext is available here.

BeagleBone Black

Official Wiki

Fix second boot on FreedomBox

As part of its first boot, FreedomBox uses the flash-kernel utility to update the kernel on the BBB. Unfortunately this doesn’t work. To prevent this from happening create the follopwing empty file before the first boot: /var/freedombox/dont-tweak-kernel

Raspberry Pi

The tiny computer everyone loves.

Extra current for the USB ports (B+/2 only)

Raspberry Pi models B+ and 2 are able to supply up to 1.2A over USB using a little trick. This allows us to connect portable hard drives without using powered USB hubs.

Edit /boot/config.txt and add the following:

# extra power for USB ports
max_usb_current=1

Warning

Do this at your own risk. A good power supply giving a stable current of 2A is required.

Linux

Or GNU/Linux.

Map Caps Lock to Control

But not the other way around. Put this in .profile or .bashrc:

setxkbmap -layout us -option ctrl:nocaps

LUKS

Simple use, for external devices, using a passphrase. For more options check this out.

Creating a LUKS volume

cryptsetup -v luksFormat /dev/sdaX
cryptsetup open /dev/sdaX LUKS001
mkfs -t ext4 /dev/mapper/LUKS001
cryptsetup close LUKS001

Mounting a LUKS volume

cryptsetup open /dev/sdaX LUKS001
mount -t ext4 /dev/mapper/LUKS001 /mnt/usb

Unmounting a LUKS volume

umount /mnt/usb
cryptsetup close LUKS001

Creating a ‘fake’ webcam device

For testing, one might need some ‘fake’ webcam, here is how to do it.

  • Install v4l2loopback - this is typically available as a package in your distro
  • Load the kernel module with the amount of virtual video devices you want:
sudo modprobe v4l2loopback devices=X
  • Get GStreamer 0.10 - looks like 1.0 doesn’t work well with the videotestsrc plugin see here
  • Launch GStreamer as follows:
gst-launch-0.10 -v videotestsrc pattern=snow ! "video/x-raw-yuv,width=640,height=480,framerate=15/1,format=(fourcc)YUY2" ! v4l2sink device=/dev/videoX

The pattern can be a number between 0 and 16 or the name of a pattern. More examples here.

SMART

Monitor the hard drives health with SMART. Full guide here.

Configuration

The following configuration will:

  • Monitor all drives
  • Except the ones in standby
  • Run a short self-test every day between 2-3am, and an extended self test weekly on Saturdays between 3-4am
  • Log temperature changes of 4 degrees or more, log when temp reaches 35 degrees, and log/email a warning when temp reaches 40
  • Alert using Pushover

/etc/smartd.conf

DEVICESCAN -a -n standby,q -s (S/../.././02|L/../../6/03) -W 4,35,40 -m root -M exec /usr/share/smartmontools/smartd-runner

/etc/smartmontools/run.d/10pushover

#!/bin/bash

# Send notification
echo "$SMARTD_MESSAGE" | pushover-notify -t "SMART failure: $SMARTD_FAILTYPE"

/usr/local/bin/pushover-notify

#!/usr/bin/env python

import argparse
import requests
import sys

APP_TOKEN = 'TODO'
USER_KEY  = 'TODO'
PUSHOVER_URL = 'https://api.pushover.net/1/messages.json'


parser = argparse.ArgumentParser()
parser.add_argument('-t', '--title', help='Title for the notification')
args = parser.parse_args()

title = args.title or ''
data = sys.stdin.read()

payload = {'token': APP_TOKEN, 'user': USER_KEY, 'title': title, 'message': data}
requests.post(PUSHOVER_URL, data=payload)

CUPS

Sharing a printer without the drivers (raw)

The following method facilitates sharing a printer in a network, without having the drivers for it. This means the computers wantint to print on it will have to have the drivers, but that’s ok.

Install CUPS and configure it for remote access (run all these commands as root):

apt install cups
usermod -a -G lpadmin pi
cupsctl --remote-admin --remote-any
systemctl restart cups

Now open http://IP:631 in a browser and chick “add printer”. Select the “Raw” make and “Raw Queue” as the model.

Install the drivers on the client computer and add the remote printer. Done!

chroot

chroot is a great way for repairing a system that won’t boot. In order to do so, run the following commands from a rescue shell (a Debian installer shell, for example):

# Mount the target in /mnt
cd /mnt
mount -t proc proc proc/
mount --rbind /sys sys/
mount --rbind /dev dev/
cd ..
chroot /mnt /bin/bash

OpenBSD

Some OpenBSD tips and tricks.

Start a network interface

sh /etc/netstart re0

Replace re0 with the interface name.

Use a serial console for installation

At the boot prompt:

> stty com0 115200
> set tty com0

Adjust the baudrate accordingly.

Reload pf rules

pfctl -f /etc/pf.conf -F all

The -F all part can be omitted if you don’t want to flush the current rules.

Programming

C

Compilation with musl

First install the musl-gcc wrapper (on Debian):

apt-get install musl-tools

Now set some environment variables:

export CC=musl-gcc
export LINK=musl-gcc
export CFLAGS=-static

That’s it, you can now compile your library and it will be linked with musl instead of glibc.

Feature test macros

Interesting article here.

CSS

Vertically center a div within another div

.parent-element {
    -webkit-transform-style: preserve-3d;
    -moz-transform-style: preserve-3d;
    transform-style: preserve-3d;
}

.element {
    position: relative;
    top: 50%;
    transform: translateY(-50%);
}

Source here.

SSH

Using a jump host

$ ssh -o ProxyCommand="ssh -W %h:%p firewall.example.com" server2.example.org

or

$ ssh -tt firewall.example.com ssh -tt server2.example.org

Source here.

Xcode

Useful shortcuts

Clean build directory

Shift + Command + Option + k

Clean project

Shift + Command + k