Skip to content

Instantly share code, notes, and snippets.

@wopfel
Last active September 3, 2017 09:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wopfel/fa237925c5495b4b7376e8d8a8b9c94e to your computer and use it in GitHub Desktop.
Save wopfel/fa237925c5495b4b7376e8d8a8b9c94e to your computer and use it in GitHub Desktop.
Basic setup for an Arch Linux VM
#!/bin/bash
# Basic setup for an Arch Linux VM
# Usage in a fresh VM (example):
# # script -c 'bash basic-setup-archlinux-vm' --timing=/tmp/capture-timing /tmp/capture
# # cp /tmp/capture /mnt/root/script-installation-recording
# # cp /tmp/capture-timing /mnt/root/script-installation-recording-timing
# Exit script on any error
set -e
# Configure VM here...
VMNAME=system01
PRIMARY_NETWORK_DEVICE=$( ip link | grep -E '[[:digit:]]: ens[[:digit:]]+: ' | head -n 1 | sed -e 's/^[[:digit:]]: //' | sed -e 's/:.*$//' )
IP_ADDRESS=192.168.2.250/24
ROUTER=192.168.2.1
DNS_SERVER=192.168.2.1
DOMAIN=local
DEFAULT_PASSWORD=Pass$( date +%m%d )
SALT_SERVER=
# Exit immediately if there are some partitions (prevents damage to an existing system)
if [[ `grep -c 'sda[0-9]' /proc/partitions` -gt 0 ]] ; then
>&2 echo "Error: Partitions on sda disk found."
exit 1
fi
# Exit immediately if there is a partition layout (prevents damage to an existing system)
if [[ `fdisk -l /dev/sda | grep -ci 'disklabel'` -gt 0 ]] ; then
>&2 echo "Error: No blank sda disk found."
exit 1
fi
# Create partitions on /dev/sda (credits: https://superuser.com/a/984637)
sed -e 's/\s*\([\+0-9a-zA-Z]*\).*/\1/' << EOF | fdisk /dev/sda
o # clear the in memory partition table
n # new partition
p # primary partition
1 # partition number 1
# default - start at beginning of disk
+1G # 1 GB boot parttion
n # new partition
p # primary partition
2 # partion number 2
# default, start immediately after preceding partition
# default, extend partition to end of disk
t # change partition type
# default, 2nd partition (the last one)
8e # Linux VM
a # make a partition bootable
1 # bootable partition is partition 1 -- /dev/sda1
p # print the in-memory partition table
w # write the partition table
q # and we're done
EOF
# Format partitions, create LVM layout
mkfs.ext4 -L boot -O '^64bit' /dev/sda1
pvcreate /dev/sda2
vgcreate main /dev/sda2
lvcreate -L 1GB -n swap main
lvcreate -L 8GB -n root main
mkswap -L swap /dev/mapper/main-swap
mkfs.ext4 -L root /dev/mapper/main-root
lsblk
# Mount partitions
mount /dev/mapper/main-root /mnt
mkdir /mnt/boot
mount /dev/sda1 /mnt/boot
# Install base system
pacstrap /mnt base base-devel syslinux zsh vim rsync grml-zsh-config openssh cronie tmux
syslinux-install_update -i -a -m -c /mnt
sed -i 's,APPEND root.*,APPEND root=/dev/mapper/main-root rw lang=de locale=de_DE.UTF-8,' /mnt/boot/syslinux/syslinux.cfg
swapon -L swap
genfstab -U -p /mnt >> /mnt/etc/fstab
cat /mnt/etc/fstab
# Configure system
echo LANG=de_DE.UTF-8 >> /mnt/etc/locale.conf
echo LC_COLLATE=C >> /mnt/etc/locale.conf
echo LC_TIME=de_DE.UTF-8 >> /mnt/etc/locale.conf
sed -i 's,^#de_DE,de_DE,' /mnt/etc/locale.gen
echo $VMNAME >> /mnt/etc/hostname
echo KEYMAP=de-latin1 >> /mnt/etc/vconsole.conf
echo FONT=lat9w-16 >> /mnt/etc/vconsole.conf
echo FONT_MAP=8859-1_to_uni >> /mnt/etc/vconsole.conf
sed -i 's,^HOOKS=.*,HOOKS="base udev autodetect modconf block lvm2 filesystems keyboard fsck shutdown",' /mnt/etc/mkinitcpio.conf
cat << EOF > /mnt/etc/systemd/network/25-wired.network
[Match]
Name=$PRIMARY_NETWORK_DEVICE
[Network]
Address=$IP_ADDRESS
Gateway=$ROUTER
EOF
cat << EOF > /mnt/etc/resolv.conf
domain $DOMAIN
nameserver $DNS_SERVER
EOF
arch-chroot /mnt locale-gen
arch-chroot /mnt ln -sf /usr/share/zoneinfo/Europe/Berlin /etc/localtime
arch-chroot /mnt mkinitcpio -p linux
arch-chroot /mnt systemctl enable sshd systemd-networkd
echo "root:$DEFAULT_PASSWORD" | arch-chroot /mnt chpasswd
arch-chroot /mnt chsh -s /bin/zsh root
# Install and configure salt?
if [[ ! -z "$SALT_SERVER" ]] ; then
echo Installing salt...
arch-chroot /mnt pacman -S salt --noconfirm
sed -i "s,^#master: salt,master: $SALT_SERVER," /mnt/etc/salt/minion
arch-chroot /mnt systemctl enable salt-minion.service
fi
# Additional steps...
# useradd -m -G wheel -s /bin/zsh USERNAME
# passwd USERNAME
# sed -i 's,^# %wheel ALL=(ALL) ALL$,%wheel ALL=(ALL) ALL,' /etc/sudoers
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment