3

To reduce the pain of installing Arch, I just put all usual commands with slight modification in a file and put #!/bin/bash on top. Here's how it looks like:

#!/bin/bash
pacman -Sy reflector --noconfirm
timedatectl set-ntp true

parted --script /dev/sda \
    mklabel msdos \
    mkpart primary ext4 1MiB 10GiB \
    set 1 boot on \
    mkpart primary ext4 10GiB 100%

mkfs.ext4 -F /dev/sda1
mkfs.ext4 -F /dev/sda2
mount /dev/sda1 /mnt

reflector --country Bangladesh --country 'United States' --protocol http --protocol https --sort rate --save /etc/pacman.d/mirrorlist

pacstrap /mnt base
genfstab -U /mnt >> /mnt/etc/fstab
arch-chroot /mnt

ln -sf /usr/share/zoneinfo/Asia/Dhaka /etc/localtime
hwclock --systohc
sed -i -e 's/#en_US.UTF-8 UTF-8/en_US.UTF-8 UTF-8/g' /etc/locale.gen
locale-gen
echo 'LANG=en_US.UTF-8' > /etc/locale.conf
echo 'haque' > /etc/hostname

echo '127.0.0.1 localhost
::1 localhost
127.0.1.1   haque.localdomain   haque' >> /etc/hosts

pacman -S base-devel grub bash-completion sddm plasma-desktop plasma-nm plasma-pa konsole kwrite dolphin breeze-gtk kde-gtk-config falkon sudo --noconfirm

grub-install --target=i386-pc /dev/sda
grub-mkconfig -o /boot/grub/grub.cfg

echo '[Theme]
Current=breeze' >> /etc/sddm.conf

sed -i -e 's/# %wheel ALL=(ALL) ALL/%wheel ALL=(ALL) ALL/g' /etc/sudoers

echo 'Set root password'
passwd

echo 'Set Username'
read name
useradd -m $name
echo "Set password for $name"
passwd $name
usermod -aG wheel,audio,video,optical,storage $name

systemctl enable sddm NetworkManager

echo 'Setup Complete!'
exit

as soon as it hits

arch-chroot /mnt 

execution stops! Can I do it with a single script? or I've to split the script into two and execute second script with rest of the commands in chroot?

  • 2
    I don't know if this will work. The usage says arch-chroot chroot-dir [command]. So how about arch-chroot /mnt another_script.sh. another_script.sh contains the rest of the command in chroot environment. – Biswapriyo Sep 15 '19 at 07:32
  • @Biswapriyo, great! It works. You could post it as an answer. –  Sep 15 '19 at 09:05

2 Answers2

3

As suggested by Biswapriyo, I've splitted the script into two: script1 and script2. Contents of script1 is:

#!/bin/bash
pacman -Sy reflector --noconfirm
timedatectl set-ntp true

parted --script /dev/sda \
    mklabel msdos \
    mkpart primary ext4 1MiB 10GiB \
    set 1 boot on \
    mkpart primary ext4 10GiB 100%

mkfs.ext4 -F /dev/sda1
mkfs.ext4 -F /dev/sda2
mount /dev/sda1 /mnt

reflector --country Bangladesh --country 'United States' --protocol http --protocol https --sort rate --save /etc/pacman.d/mirrorlist

pacstrap /mnt base
genfstab -U /mnt >> /mnt/etc/fstab
cp script2 /mnt/home/
arch-chroot /mnt sh /home/script2

rm /mnt/home/script2
echo 'Setup Complete!'
echo 'type "reboot" and remove installation media.'

and the same of script2 is:

#!/bin/bash
ln -sf /usr/share/zoneinfo/Asia/Dhaka /etc/localtime
hwclock --systohc
sed -i -e 's/#en_US.UTF-8 UTF-8/en_US.UTF-8 UTF-8/g' /etc/locale.gen
locale-gen
echo 'LANG=en_US.UTF-8' > /etc/locale.conf
echo 'haque' > /etc/hostname
echo '127.0.0.1 localhost
::1 localhost
127.0.1.1   haque.localdomain   haque' >> /etc/hosts

pacman -S base-devel grub bash-completion sddm plasma-desktop plasma-nm plasma-pa konsole kwrite dolphin breeze-gtk kde-gtk-config falkon --noconfirm

grub-install --target=i386-pc /dev/sda
grub-mkconfig -o /boot/grub/grub.cfg

echo '[Theme]
Current=breeze' >> /etc/sddm.conf

sed -i -e 's/# %wheel ALL=(ALL) ALL/%wheel ALL=(ALL) ALL/g' /etc/sudoers

echo 'Set root password'
passwd

read -p "Enter username: " name
useradd -m $name
echo "Set password for $name"
passwd $name

usermod -aG wheel,audio,video,optical,storage $name
systemctl enable sddm NetworkManager

exit 

With these two in live usb, I just have to type sh script1 at first, set root password, username and user password when prompted and at the end type reboot.

Now I can install Arch in less than 5 mins!

0

This is the script I use for setting Archlinux for VMs

I have run this script from archiso and works in both qemu-kvm & virtualbox installations by changing the primary name of the disk (sda Vs vda).

Let me work you through it:

  • As failsafe, tell bash to exit the script when an error occurs (set -e)
  • Create the partition table with sfdisk, one partition under IBM-DOS partition table
  • Format partition and mounted under /mnt/ directory
  • update pacman's repo
  • install archlinux base with pacstrap under /mnt/
  • Generate fstab
  • Prepare grub.sh. This file will install & setup grub (legacy) within archroot. As addition, I create a swapfile to use as swap.
  • Change Root into /mnt/ and run the grub.sh file.
  • Reboot

Pros:

  • Super easy and fast
  • Use of sfdisk (oneline) to prepare the partition table.
  • Use of grub.sh to run every command we need inside arch-root
  • Automation !
  • Easy to adapt and change.

Cons

  • Nothing fancy
  • No language setup (default)
  • Network should already exist
  • No passwords (root/user)

this script has automate my builds. Then I usually run ansible (configuration management) to setup more complex setups. But what I need, is a super fast way to build archlinux without anything extra. Reboot -> Login and fix everything else AFTER , not before.

#!/usr/bin/bash
set -e

# Archlinux Installation Script for VMs
# ebal, Wed 21 Aug 2019

#Disk=sda # VirtualBox
Disk=vda # Qemu/KVM

echo ',,,*;' | sfdisk /dev/$Disk

mkdir -pv /mnt/$Disk
mkfs.ext4 -v -L rootfs /dev/${Disk}1
mount /dev/${Disk}1 /mnt/$Disk

pacman -Syy

pacstrap /mnt/$Disk base
genfstab -U /mnt/$Disk >> /mnt/$Disk/etc/fstab

# Install Grub
cat > /mnt/$Disk/root/grub.sh <<EOF
pacman -S --noconfirm grub vim
grub-install /dev/$Disk
grub-mkconfig -o /boot/grub/grub.cfg
# Add Swap Partition
dd if=/dev/zero of=/swapfile bs=2048 count=1048576
mkswap /swapfile -L swapfs
chmod 0600 /swapfile
echo "/swapfile swap swap defaults 0 0" >> /etc/fstab
EOF

chmod +x /mnt/$Disk/root/grub.sh

arch-chroot /mnt/$Disk/ /root/grub.sh

reboot

hope you will find it useful with yours.

ebal
  • 465
  • 2
  • 6
  • you can run those without any modification on VM, it sets en_US.UTF-8 as language and you'll get prompts to set root password, username and user password at the end of installation. In fact, for the first time I used those scripts on a VM, you can see here. With slight modification I used it on an actual UEFI system. Thanks anyway for your additions and doing it with a single script. –  Sep 22 '19 at 05:36