Skip to content

Instantly share code, notes, and snippets.

@darkn3rd
Last active July 10, 2023 14:32
Show Gist options
  • Save darkn3rd/65257c45e6a38cd45c53eab1f776ef76 to your computer and use it in GitHub Desktop.
Save darkn3rd/65257c45e6a38cd45c53eab1f776ef76 to your computer and use it in GitHub Desktop.
Arch Linux as VirtualBox Guest

ArchLinux Virtualbox STEP-by-STEP

These are my ArchLinux on Virtualbox notes with some added explanations.

  • Joaquin Menchaca (Nov 6, 2017)

Create Virtualbox System

This is handcrafted creation of using Virtualbox using defaults with these adjustments:

  • Virtualbox Settings (ver. 5.2.0r118431)
    • System
      • Motherboard: Base Meory = 512 MB
      • Process: PAE/NX enabled
    • Audio: disabled
    • Network
      • Adapter 1:
        • Attached to: NAT
        • Port Forwarding:
          • name: SSH
          • protocol: tcp
          • host port: 2222
          • guest port: 22

Getting Info about Virtualbox

$ vboxmanage list vms
"ArchLinux-20171101-x86_64" {6a5a537b-5272-44e7-bd5b-b2cf9c114f35}
$ vboxmanage showvminfo ArchLinux-20171101-x86_64 > info.out

Enable SSH on Arch Boot System

Optionally, you can enable SSH on the Arch boot system, so you can you the host's terminal to copy/paste commands:

PASSWORD=$(openssl passwd -crypt 'vagrant')
echo "==> Enabling SSH"
# Vagrant-specific configuration
useradd --password ${PASSWORD} --comment 'Vagrant User' --create-home --user-group vagrant
echo 'Defaults env_keep += "SSH_AUTH_SOCK"' > /etc/sudoers.d/10_vagrant
echo 'vagrant ALL=(ALL) NOPASSWD: ALL' >> /etc/sudoers.d/10_vagrant
chmod 0440 /etc/sudoers.d/10_vagrant
systemctl start sshd.service

After this on the host, you should be able to log in using something like this:

$ ssh vagrant@localhost -p 2222
$ sudo su -
#

Prepare Hard Drive

From the Arch boot system, create partitions on the virtual guest:

Partitioning Disks

List the current disks attached to this system:

# List the Disks
fdisk -l | \grep '/dev'
Disk /dev/sda: 40 GiB, 42949672960 bytes, 83886080 sectors
Disk /dev/loop0: 408.5 MiB, 428281856 bytes, 836488 sectors

The partitioning scheme will be more complex for bare-metal (or virtual) workstation or server. For a small test environment, we can just have a single large partition:

fdisk /dev/sda << EOF
n
p
1


w
y
EOF

Format Partition

mkfs.ext4 /dev/sda1

Installing Arch Linux base system

Using the Arch install scripts that come with the Arch boot CD, we can do the following:

# mount newly created/formated disk
mount /dev/sda1 /mnt

# Use the pacstrap script to install the base package group
#  and other package groups
pacstrap /mnt base base-devel linux

# generate file system table
genfstab /mnt >> /mnt/etc/fstab

# check results
\grep /sda1 /mnt/etc/fstab
/dev/sda1               /               ext4            rw,relatime,data=ordered        0 1

Configure Arch Linux system

We want to chroot into the newly created drive and install some packages and configure the system.

# Variables used in this section
LANGUAGE='en_US.UTF-8'
ZONE='America/Los_Angeles'
FQDN='vagrant-arch64.dev'
PASSWORD=$(openssl passwd -crypt 'vagrant')

# Chroot: Switch to Arch Linux Base System
arch-chroot /mnt /bin/bash

# Locale
sed -i "s/#${LANGUAGE}/${LANGUAGE}/" /etc/locale.gen
echo "LANG=${LANGUAGE}" > /etc/locale.conf
locale-gen

# Time Zone: Set time zone and generate /etc/adjtime:
ln -sf /usr/share/zoneinfo/${ZONE} /etc/localtime
hwclock --systohc --utc

# Hostname: Create the hostname file
echo "${FQDN}" > /etc/hostname

# Root Password
usermod --password ${PASSWORD} root

# enable DHCPD
systemctl enable dhcpcd

# SSH: Install/Enable SSH
pacman -S --noconfirm openssh
systemctl enable sshd.service

# BootLoader
pacman -S --noconfirm grub os-prober
grub-install /dev/sda
grub-mkconfig -o /boot/grub/grub.cfg

Vagrant specific configuration

While still in the chrooted system, run these commands:

# Variables used in this section
PASSWORD=$(openssl passwd -crypt 'vagrant')
VAGRANT_KEY="https://raw.github.com/mitchellh/vagrant/master/keys/vagrant.pub"

# Vagrant User Configuration
useradd --password ${PASSWORD} --comment 'Vagrant User' --create-home --user-group vagrant

# Sudoers Vagrant Configuration
echo 'Defaults env_keep += "SSH_AUTH_SOCK"' > /etc/sudoers.d/10_vagrant
echo 'vagrant ALL=(ALL) NOPASSWD: ALL' >> /etc/sudoers.d/10_vagrant
chmod 0440 /etc/sudoers.d/10_vagrant

# SSH Authorization Configuration
install --directory --owner=vagrant --group=vagrant --mode=0700 /home/vagrant/.ssh
curl --output /home/vagrant/.ssh/authorized_keys --location ${VAGRANT_KEY}
chown vagrant:vagrant /home/vagrant/.ssh/authorized_keys
chmod 0600 /home/vagrant/.ssh/authorized_keys

Virtualbox Guest Editions

While still in the chrooted system run these:

# Guest Editions and allow video resizing
# Info: https://wiki.archlinux.org/index.php/VirtualBox
pacman -S --noconfirm linux-headers virtualbox-guest-utils virtualbox-guest-modules-arch nfs-utils
echo -e 'vboxguest\nvboxsf\nvboxvideo' > /etc/modules-load.d/virtualbox.conf

systemctl enable vboxservice.service
systemctl enable rpcbind.service

# Add groups for VirtualBox folder sharing
usermod --append --groups vagrant,vboxsf vagrant

Exit and Reboot

Now we exit the chrooted system, unmount the partition(s), and reboot.

exit
umount /mnt
reboot
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment