Sunday, April 24, 2016

An Arch VM

While I learned linux on Ubuntu and I'm still fond of its balance of usability and stability, I find that whenever I'm doing anything halfway interesting I need the very latest version of my software. This generally consists of finding PPA's which are of varying quality. Of late, I've instead been using linuxbrew, which gives me the latest version of nearly everything built from source. This is great, but with a very small user base, it's quite common for me to run up against bugs that nobody has found. The homebrew/linuxbrew folk have been quite friendly about accepting my patches, but I'd rather not.

Enter Arch Linux. The one killer feature of Arch is its "rolling release". The only supported version of archlinux contains the latest version of all software, always. Arch doesn't even support partial upgrades, for this reason. While I can't imagine how you could run a business on top of this model (not to say someone smarter than me couldn't), it does nicely solve my above problem. Arch's second killer feature is its wiki. It's a really exhaustive and valuable resource on how to configure everything Linux.

The reason Arch's wiki is so great is that the wiki is essentially the installer. Arch's installation process drops you into a shell and tells you to read the wiki. This isn't quite as bad as it seems since the wiki is truly great. The mistake I made the first time trying arch was to start with the "Installation Guide" which walks through everything with almost no detail. The "Beginners' Guide" got me through the process successfully.

For my own reference, I'll walk through exactly what I did to get my Arch VM were I wanted it, below.


Pre-install

I'll be using virtualbox. Create a new VM. The current virtualbox actually has an "Arch Linux" setting. Weird. I'll give it 1.5G memory, 16G disk and access to all my CPUs. I've also enabled 3D acceleration and allowed 128MB video memory under the "Display" tab. Download the installation media (torrent is the fastest way) and mount it to the VM's CD drive.

Disk Partitioning and Formatting

On boot, you'll be greeted by a cheery "root@archiso ~ #" prompt. Arch's installation process begins here. It's entirely manual, but quite well documented on the wiki. You can follow along under  "Beginners' Guide". What appears below is a summary of the steps I actually took.
parted /dev/sda
  mklabel msdos
  mkpart primary linux-swap 1MiB 1GiB
  mkpart primary ext4 1GiB 100%
  set 2 boot on
  quit
mkswap /dev/sda1
mkfs.ext4 /dev/sda2
Now we have a valid partition table and two valid file systems, starting with 1GB swap followed by a large '/' partition. I've put the swap partition first just to make resizing the main partition easier if it comes to that. (I've since discovered cfdisk. Let's use that next time!) Let's mount them up:
swapon /dev/sda1
mount /dev/sda2 /mnt

Installing the Basic System

Now let's put our mirror list in order. This gives me the top six mirrors in the United States:
cd /etc/pacman.d
cp mirrorlist mirrorlist.orig
grep 'United States' -A1 ./mirrorlist.orig | grep -v -- -- > mirrorlist.us
rankmirrors -n 6 ./mirrorlist.us > mirrorlist
Now the big download:
pacstrap -i /mnt base base-devel

Pre-Boot Configuration 

Things are basically installed now, but we still need to configure things before the system can reboot by itself. First, let's put the fstab in order:
genfstab -U /mnt >> /mnt/etc/fstab
Chroot lets us pretend we've already booted to the new disk:
arch-chroot /mnt /bin/bash
Locale stuff:
sed -i '/^#en_US.UTF-8 / s/#//' /etc/local.gen
locale-gen
echo LANG=en_US.UTF-8 > /etc/locale.conf
ln -sf /usr/share/zoneinfo/America/Los_Angeles /etc/localtime
I've picked  "arch-vbox" as my hostname:
echo arch-vbox > /etc/hostname
echo 127.0.1.1 arch-vbox.localdomain arch-vbox >> /etc/hosts
Get our bootloader in order (see GRUB: install to disk)
grub-install --target=i386-pc /dev/sda
grub-mkconfig -o /boot/grub/grub.cfg
Now we can eject the (virtual) installation CD and reboot to complete the installation.

Network Setup

By default, arch gives some unique names to your network devices, but we can find it in the /sys filesystem. Below we enable DHCP for that interface at boot.
IFACE=$(ls /sys/class/net | grep -vw lo)
sudo systemctl enable --now dhcpcd@$IFACE.service

Setting up Users

I'll set up my user (named "buck") with full admin privileges via sudo:
groupadd user
groupadd sudo
useradd -m -g user -G sudo buck
passwd buck
vi /etc/sudoers
(uncomment the line below "allow members of group sudo")
sudo -iu buck
  sudo -l
  (it should say "(ALL) ALL")
  exit
After making double-sure that sudo works well, let's remove root's password; it's one less security hole that way. That's how Ubuntu works, and I've gotten used to it.
passwd -l root

A (very) minimal desktop environment

I'd like to be able to use some graphical tools even while working in this VM, but I don't need all the bells and whistles of a "full-featured" desktop system. As a bare minimum, I've settled on OpenBox with lxpanel and lxterminal.
sudo pacman -S xorg-server xorg-xinit xf86-video-fbdev
sudo pacman -S virtualbox-guest-utils virtualbox-guest-modules-arch
sudo pacman -S lxde-common lxsession openbox gnome-themes-standard
sudo pacman -S lxpanel lxterminal obconf tmux

sudo systemctl enable --now vboxservice.service
sudo systemctl restart systemd-modules-load.service

cp /etc/X11/xinit/xinitrc ~/.xinitrc
vim ~/.xinitrc

# added by me
VBoxClient-all &
lxpanel &
lxterminal -e 'tmux new' &
exec openbox-session

startx

And that's it! This is the bare-bones environment I've been using for a few weeks now. On login, I run startx and I'm presented with a desktop running a terminal with copy-and-paste working.

Addendum A: Shared Folders

Configuring shared folders in virtualbox is straightforward, but getting them mounted correctly is less so. My solution was to add this line to /etc/fstab. The uid/gid bit means I don't have to worry about funky permissions on files in the host or guest.

buck    /mnt/vbox_buck  vboxsf  rw,nodev,uid=buck,gid=user      0 0