Category Archives: Linux

PinkyCtrls

A couple months ago, I made the switch from Vim to Emacs. However, I’m using Evil mode, so I still have all the nice Vim keybindings.

One of my biggest complaints about Emacs is ergonomics – “Emacs Pinky” is a legitimate concern:

One solution is getting foot pedals or a fancy ergonomic keyboard like the Ergodox or Keyboardio, but I didn’t want to commit to the investment just yet. Thus, PinkyCtrls was born! I forked an existing project that turned the space key into a control key if you held it, but worked as space if you tapped it (Space2Ctrl). However, with Space2Ctrl I found that I generally mistyped spaces a lot, since I tend to mash the space key soon after typing words. So I decided to shove the functionality onto the Caps Lock & Minus key (where the quote key is on QWERTY layouts).

After about 2 weeks of using this mod, I find it uncomfortable to use the normal control keys, and I have no problems with all the finger stress when using Emacs.

Note: The code in my PinkyCtrls repo is currently pretty ugly… I may clean it up eventually, but for now it does the job. If you want to fork PinkyCtrls to work on another layout or with other keys, let me know and I’ll show you what you need to change!

Hello World, using only C syscalls

I’m currently working on implementing system calls in BlarbVM (see my previous post on it), so I am trying to get comfortable using only syscalls.

In Linux, you can change the heap size through brk (syscall #12). To write a string to standard output, you must provide a pointer to a location on the heap.

The following code:

  1. Get’s the end of the heap address
  2. Increments the end of the heap address by 6 bytes
  3. Set’s the value of those 6 bytes to a null terminated “hello\n” string
  4. Runs a syscall on the desired heap position to print “hello\n”

GDB Basics: Stack Traces

C beginners are often very lost when their program crashes from a segmentation fault or similar issues. Most of them know about GDB but are pretty intimidated by it.

Here is a super simple example about how to find a stack trace from a program crash.

Of course, this will result in the console simply printing “Segmentation Fault” which doesn’t help very much, especially in more complex programs.

Using GDB, you just have to remember 2 commands to get a stack trace: “run” and “backtrace”.

To compile, use the “-g” flag with gcc to include debugging symbols for gdb:

To run this in gdb, run:

Now all you have to do is type “run” into the gdb console. It will show you the line where it crashed, but to show the stack trace you must run the “backtrace” command:

GDB has a lot of other great features like breakpoints and memory analysis. If you are comfortable with assembly, you can even run commands while your program is running!

How to change login shell without chsh permissions

On the University of Toronto servers and Linux lab computers, the default shell is tcsh, and we don’t have chsh access *shakes fist*.

If you are in a similar circumstance, plop this in ~/.cshrc:

If you just run exec /bin/bash (without checking for the interactive shell), it will break non-interactive shells like scp and the GNOME startup scripts. If you just use bash you can ignore the first conditional. The reason why I have bash as a backup is because the lab computers have zsh installed, but the servers only have bash.

Bai 🙂

P.S. A little known fact about the UofT computing services is that you can SSH into lab computers directly. For instance, dh2026pc02.utm.utoronto.ca connects to “PC #02” in DH2026.

Switching from Arch to Debian

Last night was a sad night. After 5 years of using Arch Linux, I decided to leave it for Debian. While I learnt a lot while using Arch, it was often disruptive to my work when it required maintenance. I woke up last night to work on a Physics assignment, and sure enough, my kernel panicked. I don’t blame Arch per se. If I retained a legacy kernel, it would have been fine. But that is the reason why I left – it would be nice if my computer worked out of the box with minimal maintenance.

One thing I will really miss about Arch is yaourt – a package manager which connects with the Arch User Repository. Basically any package you could think of would be contained in there. As an alternative, I installed Linuxbrew. It doesn’t seem to have as many packages, but I am really digging how the formulae are easily modifiable in ruby! (expect to see a linuxbrew-formulae repository in my Github soon…).

rm -rf roulette

So at work, I recently migrated over from my Linux box to an OS X box, and the old box will soon be formatted… so I decided to have a bit of fun with it!

I present you with rm_rf_roulette.sh!!

 

 

I plan to run this in a loop until it no longer has the capability of running 🙂

 

UPDATE:

Before:

Screen Shot 2015-06-25 at 2.39.31 PM

After:

Screen Shot 2015-06-25 at 3.15.31 PM

 

I would say this is a success.

Setting up a BTRFS and rsync backup server

Who is this for?

This is for people who came across the following issues with current backup solutions:

  • You have multiple computers, but you want to sync all backups onto one of them.
  • You want the benefits of rsnapshot – daily, weekly, and monthly snapshots.
  • You want the efficient COW (copy-on-write) benefits of BTRFS.
  • You want to back all of this up onto a single, designated backup disk.

Syncing from remote nodes to the server.

On the client, nothing complicated is really happening. Just a simple cron job is syncing down to the server using rsync.

For this to work, you will need to add your client public key to the authorized_keys on the server, and install rsync on both the client and server.

Here is a little script I wrote to handle things nicely:

I recommend placing this in “/usr/local/bin/” and giving it the permissions “700” for root only.

In my root crontab, I have this script set to run every hour at ??:30. On the server, we will be snapshotting everything at ??:00, so this prevents any possible conflicts.

Setting up the server.

If you haven’t already guessed, you should have a BTRFS formatted drive connected to your backup server and mounted (possibly at /mnt/backup).

Normally with rsnapshot, everything is synced into a .sync directory. BUT. With this solution, we create .sync as a BTRFS subvolume:

You will need a few hacky scripts to trick rsnapshot into creating new subvolumes instead of directories as well. Place these in /usr/local/bin:

rsnapshot_cp_btrfs:

rsnapshot_rm_btrfs:

Now in /etc/rsnapshot.conf, you will want to set the following params:

As well of this, you will of course want to configure the interval options.

Here are mine:

Now you just configure your rsnapshot backup directories. Here is my config:

Last but not least, we add some crontab entries to snapshot ALL THE THINGS:

Now you have a nice backup system, and don’t have to worry about losing files whenever you throw your laptop out the window!

Acknowledgements.

I would like to thank Walter Werther who wrote an article that I ripped some techniques out of.