Stable trading relationships with nearby countries. Basic human rights. A planet capable of sustaining life. What do these three things have in common?
The answer is that they are all impermanent. One moment we have them, the next moment – whoosh! – they’re gone.
Today I decided I would embrace our new age of impermanence insofar as it pertains to my home directory. Specifically, I wondered whether I could configure a Linux installation so that my home directory was mounted in a ramdisk, created afresh each time I rebooted the server.
Why on earth would I want to do something like that?
The answer is that I have a Scala project, built using sbt (the Scala Build Tool), and I thought I’d clear some of the accumulated cruft out of the
build.sbt file, starting with the configured resolvers. These are basically the repositories which will be searched for the project’s dependencies – there were a few special case ones (e.g. one for JGit, another for MarkLogic) and I strongly suspected that the dependencies in question would now be found in the standard Maven repository. So they could probably be removed, but how to check, since all of the dependencies would now exist in caches on my local machine?
A simple solution would have been to delete the caches, but that involves putting some effort into finding them, plus I have developed a paranoid streak about triggering unnecessary file writes on my SSD. So I had a cunning plan – build a VirtualBox VM and arrange for the home directory on it to be a ramdisk, thus I could check the code out to it and verify that the code will build from such a checkout, and this would then be a useful resource for conducting similar experiments in the future.
Obviously this is not quite a trivial undertaking, because I need some bits of the home directory (specifically the
.ssh directory) to persist so I can create the SSH keys needed to authenticate with GitHub (and our internal GitLab). Recreating those each time the machine booted would be a pain.
After a bit of fiddling, my home-grown solution went something like this:
- Create a Virtualbox VM, give it 8G memory and a 4G disk (maybe a bit low if you think you’ll want docker images on it; I subsequently ended up creating a bigger disk and mounting it on
- Log into VM (my user is
daniel), install useful things like Git, curl, zip, unzip etc.
- Create SSH keys, upload to GitHub / GitLab / wherever
- Install SDKMAN! to manage Java versions
/var/danieland copy into it all of the directories and files in my home directory which I wanted to be persisted; these were basically
.sshfor SSH keys,
.sdkmanfor java installations,
.bashrcwhich now contains the SDKMAN! init code, and
- Save the following script as
/usr/local/bin/create_home_dir.sh– this wipes out
/home/daniel, recreates it and mounts it as tmpfs (i.e. a ramdisk) and then symlinks into it the stuff I want to persist (everything in
#!/bin/bash DIR=/home/daniel mount | grep $DIR && umount $DIR [ -d $DIR ] && rm -rf $DIR mkdir $DIR mount -t tmpfs tmpfs $DIR chown -R daniel:daniel $DIR ls -A /var/daniel | while read FILE do sudo -u daniel ln -s /var/daniel/$FILE $DIR/ done
- Save the following as
[Unit] description=Create home directory [Service] ExecStart=/usr/local/bin/create_home_dir.sh [Install] WantedBy=multi-user.target
- Enable the service with
systemctl enable create-home-dir
- Reboot and hope
And it turns out that this worked; when the server came back I could
ssh into it (i.e. the
authorized_keys file was recognised in the symlinked
.ssh directory) and I had a nice empty workspace; I could
git clone the repo I wanted, then build it and watch all of the dependencies get downloaded successfully. I note with interest that having done this,
.cache is 272M and
.sbt is 142M in size. That seems to be quite a lot of downloading! But at least it’s all in memory, and will vanish when the VM is switched off…