Computing Tips

I collect a few handy tips of improving computing experience. Hope you find it helpful.

Here is an index of the topics.

Secure Network-transparent Storage with autofs and sshfs

A basic way to understanding is abstraction. We see three abstractions the operating system makes for us on the computer right before you

So far, so good.

It would be nice if we extend the file-system abstractions to include storage beyond the disks attached to your personal computer. Here is how we can do it under Linux with autofs and sshfs.

Suppose we have three computers, office, home, and laptop. Our goal is to seamlessly access files on office and home through laptop. More specifically, on laptop, when we access /media/ssh/office, we are actually accessing files on office through the Internet. Besides, we want the access to be encrypted.

For the sake of illustration, I use the Arch Linux distribution to illustrate the procedure because that is what I have on my computers. Please refer to your package manager's documentation or you can always build the package yourself---that is one of the reasons I like Open Source/Free Software. To find your reason to like or hate it, go ahead trying it; see for yourself.

First, install the packages.

pacman -S autofs fuse sshfs openssh
Your package manager (e.g., aptitude, yum, emerge, zypper, or BSD's pkgadd) should take care of rest of the dependencies.

autofs and fuse need some initial setup; check this ArchWiki entry out! In summary, you need to

  1. modprobe autofs4 fuse; also add them to the MODULES line in /etc/rc.conf.
  2. Edit /etc/autofs/auto.master. Set proper --timeout, e.g., --timeout=60 sets auto-umount timeout to 60 seconds.
  3. A suggestion is to visudo your sudoers file with the line ALL ALL=(ALL) NOPASSWD: /usr/bin/killall -USR1 automount, which allows non-privileged user to umount unused mounts with killall -USR1 automount. See automount(8) for the explanation of -USR1 signal.
  4. Add a line automount: files to /etc/nsswitch.conf.
  5. Add automount's startup option to /etc/conf.d/autofs's daemonoptions option, e.g., daemonoptions='--verbose'. Start the service with /etc/rc.d/autofs start
Add the following line to your /etc/autofs/auto.master
/media/ssh /etc/autofs/auto.ssh --timeout=60
Suppose the SSH server ports for office and home are 22 and 20022, respectively. Edit /etc/autofs/auto.ssh as (office.my.net and home.my.net are the host names of office and home, respectively).
office  -fstype=fuse.sshfs,rw,allow_other,IdentityFile=/home/wei/.ssh/id_dsa,port=22 \
   :pw@office.my.net\:/home/pw/
home  -fstype=fuse,rw,allow_other,IdentityFile=/home/wei/.ssh/id_dsa,port=20022 \
   :pw@home.my.net\:/home/pw/

Now, we need to establish a password-less ssh connection to both office and home. Do not worry, the "password-less" does not mean you go without encryption. It only means that you do not need password (or "to unlock the key with the passphrase" in ssh jargon) to ssh to office and home. To do this, we first need to generate a key (if you have not done so yet; check you $HOME/.ssh/ for files like id_dsa or id_rsa)

ssh-keygen -t dsa
Then, we copy the key to the office and home's authorized_keys. I used to do this with some thing like
cat ~/.ssh/id_dsa.pub | ssh -p 22 pw@office.my.net 'echo - >> ~/.ssh/authorized_keys'
But we can do it more conveniently with
ssh-copy-id -i ~/.ssh/id_dsa.pub pw@office.my.net
Unfortunately, my current ssh-copy-id does not has an option for specifying port. So we need to resort to the first method for home which has a non-default SSH server port of 20022.
cat ~/.ssh/id_dsa.pub | ssh -p 20022 pw@home.my.net 'echo - >> ~/.ssh/authorized_keys'

Now, before starting the autofs daemon automount, we need to do this important but easily forgotten step. I learn its importance the hard way; do not repeat my mistake.

What we need to do is to establish an SSH connection to both office and home as root. The reason for doing this is that, in the OpenSSH implementation, ssh will warn you about "unrecognized host" if that host's signature is not in the $HOME/.ssh/known_hosts file; ssh needs your explicit permission (i.e., input "yes" after the prompt) to establish the connection. Since automount daemon runs as root, it will connect to the remote host as root with ssh. If the remote host is not in root's .ssh/known_hosts, the background ssh will waits futilely for your confirmation and the automount will hung there forever.

OK, we know what we need to do. The actual doing is very simple.

sudo ssh -p 22 -i /home/pw/.ssh/id_dsa pw@office.my.net
sudo ssh -p 20022 -i /home/pw/.ssh/id_dsa pw@home.my.net

Finally, we can start the service with

/etc/rc.d/autofs start
Now you can access office and home as /media/ssh/office and /media/ssh/home, respectively. Neat, huh? :-)>

We can restart the service with

/etc/rc.d/autofs restart
or simply ask automount to re-read its configuration with
killall -HUP automount
Read the Notes section in the manpage automount(8).

Preparing Cheatsheet with LaTeX

Cheatsheet represents a class of docuemnt which requires maximal usage of white spaces on the paper. You certainly want to cram a lot of equations on the 1-page double-sided letter-sized notes to your exam. Here is how I do it with LaTeX.

You can take a look at the cheatsheet I prepares for the mid-term exam of the pattern recognition class here (and the LaTeX source here).

The content does not concern us here (and it is actually incomplete and probably contains a bunch of errors due to limited time I have in preparing it). The point is that it contains mathematical equations (which is TeX/LaTeX's strength) and that it needs to be done compactly (which is not what we get from LaTeX by default).

Here is how I do it. Plainly, I use a few LaTeX packages (geometry, multicol, mdwlist, and a few other standard ones such as graphicx, if you want to include graphics) and tweak the TeX/LaTeX parameters a bit. Documentation to these packages can be found at your local site (e.g. /usr/local/share/texmf/doc or by a Google query <package-name> latex filetype:pdf. Read them; documentation is your friend.

Without further ado, here is the template.

\documentclass[landscape,letterpaper,final]{article}

\usepackage[scale={0.95,0.95},centering,includeall,noheadfoot,nomarginpar,dvips]{geometry}
\usepackage{palatino}
\usepackage{amsmath,amsthm,amssymb}
\usepackage[dvips]{hyperref}
\usepackage{url}
\usepackage{graphicx}
\DeclareGraphicsExtensions{.eps}
\usepackage{mdwlist} % use enumerate* and itemize* instead

\usepackage{multicol}
\setlength{\columnseprule}{1pt}
\setlength{\columnsep}{4ex}

\usepackage[compact]{titlesec}
\titlespacing{\section}{0pt}{*0}{*0}
\titlespacing{\subsection}{0pt}{*0}{*0}
\titlespacing{\subsubsection}{0pt}{*0}{*0}

\pagestyle{empty}
%\setlength{\parindent}{0pt}
\setlength{\parskip}{0pt}
\setlength{\parsep}{0pt}
\setlength{\partopsep}{0pt}
\linespread{0.5}

\newcommand{\mathd}{\,\mathrm{d}}
\newcommand{\erf}{\,\mathrm{erf}}
\newcommand{\oops}[1]{\textbf{#1}}

%% End of preamble

\begin{document}
\setlength{\abovedisplayshortskip}{0pt}
\setlength{\belowdisplayshortskip}{0pt}
\setlength{\abovedisplayskip}{0ex}
\setlength{\belowdisplayskip}{0ex}

\begin{multicols*}{3} % * version disables multicol's auto balancing
  \begin{center}
    {\LARGE \oops{Cheatsheet How-to}\vspace{.5ex}}
    {\large John Doe\vspace{.5ex}}
  \end{center}
  \section{What Cheatsheet}
  \section{When Cheatsheet}
  \section{Why Cheatsheet}
  \subsection{How Cheatsheet}
\end{multicols*}
\end{document}

Name it cheatsheet.tex and put the following Makefile along with it. Then, we can produce cheatsheet.pdf simply with make.

TARGET=cheatsheet
RM=rm -f

.PHONY: all clean dist-clean

all: $(TARGET).pdf 

%.pdf: %.ps
	ps2pdf -dPDFSETTINGS=/prepress -dMaxSubsetPct=100 -dCompatibilityLevel=1.4 \
		-dSubsetFonts=true -dEmbedAllFonts=true $<

%.ps: %.dvi
	dvips -Ppdf -G0 -t landscape $<

%.dvi: %.tex
	latex $*
	latex $*

dist-clean: clean
	$(RM) *.dvi *.pdf *.eps
clean:
	$(RM) *.log *.aux *.out *.bbl
Moreover, we can clean the intermediate by-product with make make clean and all but the source with make dist-clean.