Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Bash script to automate an arch install for virtual machines
#!/bin/bash
DISK=""
USERNAME="user"
PASSWORD="pass"
btrfs_setup()
{
subvols=(
""
"home"
)
paths=(
"/"
"/home"
)
# Mount the Root Partition
mount "${DISK}3" /mnt
# Create Ubuntu-style Btrfs Subvolumes
pushd /mnt
for index in ${!subvols[*]}; do
btrfs subvolume create @"${subvols[$index]}"
btrfs quota enable @"${subvols[$index]}"
mkdir -p @"${paths[$index]}"
done
mkdir -p @/boot
popd
# Unmount the Root and Boot Partitions
umount /mnt
# Remount the Root Partition with Subvolumes
for index in ${!subvols[*]}; do
mount -o compress=lzo,ssd,noatime,nodiratime,space_cache,discard,subvol=@"${subvols[$index]}" \
"${DISK}3" "/mnt${paths[$index]}"
done
mount "${DISK}1" /mnt/boot
}
partition_disk()
{
(
printf "o\n" # Set DOS Type
printf "n\n" # New Partition
printf "p\n" # Primary Partition
printf "1\n" # Partition 1 - Boot
printf " \n" # From First Sector
printf "+512M\n" # 512MB Boot Partition
printf "n\n" # New Partition
printf "p\n" # Primary Partition
printf "2\n" # Partition 2 - Swap
printf " \n" # From Next Sector
printf "+2G\n" # 2GB Swap Partition
printf "n\n" # New Partition
printf "p\n" # Primary Partition
printf "3\n" # Partition 3 - Root
printf " \n" # From Next Sector
printf " \n" # NGB Root Partition
printf "a\n" # Set Bootable Flag
printf "1\n" # Partition 1 - Bootable
printf "w\n" # Write Changes to Disk
) | sudo fdisk "${DISK}"
# Format the Boot Partition
mkfs.ext4 "${DISK}1"
# Format the Swap Partition
mkswap "${DISK}2"
# Enable the Swap Partition
swapon "${DISK}2"
# Format the Root Partition
mkfs.btrfs -L root "${DISK}3"
}
prompt_disk()
{
fdisk -l
echo -e "\n\nEnter your disk (/dev/sda): "
while read DISK; do
if [ ! -b "${DISK}" ]; then
echo "Invalid disk: ${DISK}."
continue
else
break
fi
done
}
timedatectl set-ntp true
prompt_disk
partition_disk
btrfs_setup
pacstrap /mnt base base-devel linux linux-firmware
genfstab -U /mnt >> /mnt/etc/fstab
cat << EOF > /mnt/root/stage2.sh
DISK="${DISK}"
localization()
{
# Set Time Zone
ln -sf "/usr/share/zoneinfo/\${1}/\${2}" /etc/localtime
hwclock --systohc
# Set Locale
echo "LANG=en_US.UTF-8" >> /etc/locale.conf
sed -i '/^#en_US.UTF-8/s/^#//' /etc/locale.gen
locale-gen
}
setup_bootloader()
{
pacman -S grub os-prober --noconfirm
grub-install --target=i386-pc "${DISK}"
grub-mkconfig -o /boot/grub/grub.cfg
}
intel_microcode()
{
if grep "Intel" /proc/cpuinfo; then
pacman -S intel-ucode --noconfirm
fi
}
# Arg1 is the username for the new user
# Arg2 is the password for the new user
add_user()
{
useradd -m -G wheel -s /bin/bash "\${1}"
echo "\${1}:\${2}" | chpasswd
}
enable_wheel()
{
sed -i '0,/^# %wheel/ s/^# //' /etc/sudoers
}
setup_de()
{
# Install Gnome
pacman -S gnome gnome-tweaks --noconfirm
# Enable GDM
systemctl enable gdm
# Enable NetworkManager
systemctl enable NetworkManager
# Set Graphical Option
systemctl set-default graphical.target
}
setup_snapshotting()
{
pacman -S snap-pac --noconfirm
# AFAIK, this has to be run after reboot to avoid "Failure (org.freedesktop.DBus.Error.ServiceUnknown)"
# snapper -c root create-config /
}
sed -i 's/fsck/btrfs/g' /etc/mkinitcpio.conf
localization "America" "Chicago"
echo "dev" > /etc/hostname
intel_microcode
setup_bootloader
enable_wheel
add_user "${USERNAME}" "${PASSWORD}"
setup_de
setup_snapshotting
rm /root/stage2.sh
EOF
chmod +x /mnt/root/stage2.sh
arch-chroot /mnt /root/stage2.sh
umount -R /mnt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment