Archlinux automated installation script

meant for VMs

Disclaimer: use this at your own risk. No warranties are given.

I got bored typing stuff in arch for basically every time same installation. Everything after pacstrap can also be used for live system. Just be sure to not run partitioning by default on someone’s desktop.

Here’s the script:

#!/bin/bash

#
# prereqs: vm with working network
# limitations: 1 drive, 1 network card
#
# instructions:
# when booted archiso, 
# 1. systemctl start sshd
# 2. passwd root
# 3. remote login
# 4. start bash by typing bash (arch by default uses ksh)
# 5. then run this script by c/p it into the open terminal window
#    (it has to be done like this because of arch-chroot)
#

BLOCK_DEVICE_SHORT=$(lsblk -dn -o NAME,TYPE | awk '{ if ($2 == "disk") { print $1 } }' | head -n 1)
BLOCK_DEVICE="/dev/$BLOCK_DEVICE_SHORT"

if [ ! -f /etc/pacman.d/mirrorlist.orig ]; then 
mv /etc/pacman.d/mirrorlist /etc/pacman.d/mirrorlist.orig
fi

grep ".de/" /etc/pacman.d/mirrorlist.orig > /etc/pacman.d/mirrorlist

parted --script /dev/vda -s \
mklabel msdos \
mkpart primary 1MiB 1GiB \
mkpart primary 1GiB 100% \
set 1 boot on \
p

mkfs.xfs -f "${BLOCK_DEVICE}1"
mkfs.xfs -f "${BLOCK_DEVICE}2"

mount /dev/vda2 /mnt
mkdir -p /mnt/boot
mount /dev/vda1 /mnt/boot

pacstrap /mnt base lz4 grub mc vim openssh

genfstab -U /mnt >> /mnt/etc/fstab

arch-chroot /mnt bash

ln -sf /usr/share/zoneinfo/Europe/Vienna /etc/localtime

sed -i "/^#en_US\.UTF-8 UTF-8/s/^#//" /etc/locale.gen

locale-gen

HOSTNAME="vm-arch"

echo "LANG=en_US.UTF-8" > /etc/locale.conf
echo "KEYMAP=us" > /etc/vconsole.conf
echo "$HOSTNAME" > /etc/hostname

cat > /etc/hosts <<EOL
127.0.0.1 localhost
::1 localhost
127.0.1.1 $HOSTNAME.localdomain $HOSTNAME
EOL

NETWORK_INTERFACE=$(ls /sys/class/net | grep -v "lo" | head -n 1)

cat > /etc/systemd/network/10-wired.network <<EOL
[Match]
Name=$NETWORK_INTERFACE

[Network]
DHCP=ipv4
EOL

sed -i "/^#.*lz4/s/^#//" /etc/mkinitcpio.conf

mkinitcpio -p linux

BLOCK_DEVICE_SHORT=$(lsblk -dn -o NAME,TYPE | awk '{ if ($2 == "disk") { print $1 } }' | head -n 1)
BLOCK_DEVICE="/dev/$BLOCK_DEVICE_SHORT"

grub-install --target=i386-pc $BLOCK_DEVICE
grub-mkconfig -o /boot/grub/grub.cfg

systemctl enable systemd-networkd
systemctl enable systemd-resolved
systemctl enable sshd
useradd -m -g wheel -G users -s /bin/bash <me-as-a-user>
echo "root:<password-to-be-set>" | chpasswd

# pacman -S sddm plasma-desktop plasma-wayland-session konsole kate dolphin papirus-icon-theme && systemctl enable sddm

If you want additionally to try something, then

pacman -S spice-vdagent qemu-guest-agent virglrenderer xf86-video-qxl

systemctl enable spice-vdagentd