Skip to content

Instantly share code, notes, and snippets.

@vspiewak
Created July 16, 2020 00:22
Show Gist options
  • Save vspiewak/3ea2ce0c7c247f42fe9a01bd0641d608 to your computer and use it in GitHub Desktop.
Save vspiewak/3ea2ce0c7c247f42fe9a01bd0641d608 to your computer and use it in GitHub Desktop.
arch-install-tmp
#!/usr/bin/env bash
#
# curl -L bit.ly/2yy0s3O | tr -d '\r' | sh
check_arch_iso() {
command -v pacstrap
if [ "$?" -ne "0" ]
then
whiptail --msgbox "Arch Linux ISO not mounted, can't install" --title "Arch ISO error" 7 45 3>&1 1>&2 2>&3
echo "Can't install. exit."
exit 1
fi
}
install_ok() {
if ! (whiptail --clear --defaultno --yesno "This will install Arch. Are you sure ?" --title "Arch installation" 7 42 3>&1 1>&2 2>&3)
then
echo "Cancel install, exit."
exit 1
fi
}
set_keyboard() {
loadkeys us
key_maps=$(find /usr/share/kbd/keymaps -type f | sed -n -e 's!^.*/!!p' | grep ".map.gz" | sed 's/.map.gz//g' | sed 's/$/ -/g' | sort)
while (true)
do
KEYBOARD=$(whiptail --nocancel --menu "Choose a layout" --title "Keyboard layout" 18 60 10 \
"us" "United States" \
"de" "German" \
"el" "Greek" \
"hu" "Hungarian" \
"es" "Spanish" \
"fr" "French" \
"it" "Italian" \
"pt-latin9" "Portugal" \
"ro" "Romanian" \
"ru" "Russian" \
"sv" "Swedish" \
"uk" "United Kingdom" \
"other" "Other" 3>&1 1>&2 2>&3)
if [ "${KEYBOARD}" = "other" ]; then
KEYBOARD=$(whiptail --menu "Choose a layout" --title "Keyboard layout" 19 60 10 $key_maps 3>&1 1>&2 2>&3)
if [ "$?" -eq "0" ]; then
break
fi
else
break
fi
done
loadkeys ${KEYBOARD}
TEST_KEYBOARD=$(whiptail --cancel-button "Choose another layout" --inputbox "Is the new layout ok ?" 8 78 --title "Keyboard layout" 3>&1 1>&2 2>&3)
if [ "$?" -eq "1" ]
then
set_keyboard
fi
}
set_timezone() {
zonelist=$(find /usr/share/zoneinfo -maxdepth 1 | sed -n -e 's!^.*/!!p' | grep -v "posix\|right\|zoneinfo\|zone.tab\|zone1970.tab\|W-SU\|WET\|posixrules\|MST7MDT\|iso3166.tab\|CST6CDT" | sort | sed 's/$/ -/g')
while (true)
do
ZONE=$(whiptail --nocancel --menu "Choose your timezone" 19 60 11 $zonelist 3>&1 1>&2 2>&3)
if (find /usr/share/zoneinfo -maxdepth 1 -type d | sed -n -e 's!^.*/!!p' | grep "$ZONE" &> /dev/null); then
sublist=$(find /usr/share/zoneinfo/"$ZONE" -maxdepth 1 | sed -n -e 's!^.*/!!p' | sort | sed 's/$/ -/g' | grep -v "$ZONE")
SUBZONE=$(dialog --cancel-button "Back" --menu "Choose your zone" 18 60 11 $sublist 3>&1 1>&2 2>&3)
if [ "$?" -eq "0" ]; then
if (find /usr/share/zoneinfo/"$ZONE" -maxdepth 1 -type d | sed -n -e 's!^.*/!!p' | grep "$SUBZONE" &> /dev/null); then
sublist=$(find /usr/share/zoneinfo/"$ZONE"/"$SUBZONE" -maxdepth 1 | sed -n -e 's!^.*/!!p' | sort | sed 's/$/ -/g' | grep -v "$SUBZONE")
SUB_SUBZONE=$(dialog --cancel-button "Back" --menu "Choose your sub zone" 15 60 7 $sublist 3>&1 1>&2 2>&3)
if [ "$?" -eq "0" ]; then
ZONE="${ZONE}/${SUBZONE}/${SUB_SUBZONE}"
break
fi
else
ZONE="${ZONE}/${SUBZONE}"
break
fi
fi
else
break
fi
done
}
set_hostname() {
while (true)
do
HOSTNAME=$(whiptail --nocancel --inputbox "Enter the hostname ?" 8 78 --title "Hostname" 3>&1 1>&2 2>&3)
if [ "$?" -eq "0" ] && [ "${HOSTNAME}" != "" ]
then
break
fi
done
}
set_username() {
while (true)
do
USERNAME=$(whiptail --nocancel --inputbox "Choose a username" 8 78 ${DEFAULT_SWAP_SIZE} --title "User name" 3>&1 1>&2 2>&3)
if [ "$?" -eq "0" ] && [ ! -z "${USERNAME##*[!0-9a-zA-Z]*}" ]
then
break
fi
done
}
set_password() {
while (true)
do
PASSWORD=$(whiptail --nocancel --passwordbox "Enter your password" 8 78 --title "Password" 3>&1 1>&2 2>&3)
if [ "$?" -eq "0" ] && [ "${PASSWORD}" != "" ]
then
REPASSWORD=$(whiptail --passwordbox "Re-type your password" 8 78 --title "Confirm password" 3>&1 1>&2 2>&3)
if [ "$?" == "0" ]
then
if [ "${PASSWORD}" == "${REPASSWORD}" ]
then
break
fi
fi
fi
done
}
set_swap() {
while (true)
do
RAM_SIZE=$(free --giga | head -n 2 | tail -n 1 | awk '{ print $2 }')
DEFAULT_SWAP_SIZE=$((RAM_SIZE + 1))
INPUT_SWAP_SIZE=${SWAP_SIZE:=$DEFAULT_SWAP_SIZE}
SWAP_SIZE=$(whiptail --nocancel --inputbox "Enter the swap size in Go" 8 78 ${INPUT_SWAP_SIZE} --title "Swap partition" 3>&1 1>&2 2>&3)
if [ "$?" -eq "0" ] && [ ! -z "${SWAP_SIZE##*[!0-9]*}" ] && [ ${SWAP_SIZE} -gt 0 ]
then
break
fi
done
}
change_menu() {
while (true)
do
MENU_OPTION=$(whiptail --cancel-button "Abort Installation" --title "Installation" --menu "Choose an option" 25 75 16 \
"Change Keyboard" " ${KEYBOARD}" \
"Change Timezone" " ${ZONE}" \
"Change Hostname" " ${HOSTNAME}" \
"Change Username" " ${USERNAME}" \
"Change Password" " $(echo $PASSWORD | sed "s/./\*/g")" \
"Change Swap" " ${SWAP_SIZE} Go" \
"" "" \
"Install Arch Linux" "" \
3>&1 1>&2 2>&3)
if [ "$?" -eq "1" ]
then
exit 1
fi
case "${MENU_OPTION}" in
"Change Keyboard")
set_keyboard
;;
"Change Timezone")
set_timezone
;;
"Change Hostname")
set_hostname
;;
"Change Username")
set_username
;;
"Change Password")
set_password
;;
"Change Swap")
set_swap
;;
"Install Arch Linux")
break
;;
esac
done
}
#check_arch_iso
#install_ok
#set_keyboard
#set_timezone
#set_hostname
#set_username
#set_password
#set_swap
#change_menu
clear
file_progress=$(mktemp /tmp/progress.XXXXXX)
file_title=$(mktemp /tmp/title.XXXXXX)
progress() {
{
for ((i = 0 ; i <= 100 ; )); do
sleep 0.1
i=$(cat ${file_progress})
echo XXX
echo ${i}
echo $(cat ${file_title})
echo XXX
done
} | whiptail --gauge "Installing..." 6 50 0
}
progress_to() {
echo "$1" > $file_progress
echo "$2" > $file_title
}
progress &
PID_PROGRESS=$!
sleep 2
progress_to "30" "set disks"
sleep 2
progress_to "100" "finished"
sleep 2
progress_to "101" "set disks"
# wait whiptail gauge end
wait $PID_PROGRESS 2>/dev/null
exit 0
# update system clock
timedatectl set-ntp true
# wipe disk
sgdisk --zap-all /dev/nvme0n1 &> /dev/null
wipefs -a /dev/nvme0n1 &> /dev/null
# format disk
parted /dev/nvme0n1 mklabel gpt &> /dev/null
parted /dev/nvme0n1 mkpart primary fat32 1MiB 513MiB &> /dev/null
parted /dev/nvme0n1 set 1 esp on &> /dev/null
parted /dev/nvme0n1 mkpart primary ext4 513MiB 100% &> /dev/null
# make luks
echo -n "$PASSWORD" | cryptsetup -q luksFormat /dev/nvme0n1p2 -d -
echo -n "$PASSWORD" | cryptsetup -q open /dev/nvme0n1p2 cryptlvm -d -
# create lvm
pvcreate /dev/mapper/cryptlvm
vgcreate vg0 /dev/mapper/cryptlvm
# create lvm partitions
lvcreate -L ${SWAP_SIZE}G vg0 -n swap
lvcreate -l 100%FREE vg0 -n root
# format lvm partitions
mkfs.fat -F32 /dev/nvme0n1p1
mkfs.ext4 /dev/vg0/root
mkswap /dev/vg0/swap
# mount /root
mount /dev/vg0/root /mnt
# mount /boot
mkdir /mnt/boot
mount /dev/nvme0n1p1 /mnt/boot
# mount swap
swapon /dev/vg0/swap
# install essential packages
pacstrap /mnt base linux linux-firmware lvm2
# generate /etc/fstab
genfstab -U /mnt >> /mnt/etc/fstab
#
# need to chroot at this point
#
# add keyboard keymap encrypt lvm2 in HOOKS
cp /mnt/etc/mkinitcpio.conf /mnt/etc/mkinitcpio.conf.bkp
sed -i '/^HOOKS=.*/s/\<keyboard\> *//' /mnt/etc/mkinitcpio.conf
sed -i '/^HOOKS=.*/s/\<keymap\> *//' /mnt/etc/mkinitcpio.conf
sed -i '/^HOOKS=.*/s/\<encrypt\> *//' /mnt/etc/mkinitcpio.conf
sed -i '/^HOOKS=.*/s/\<lvm2\> *//' /mnt/etc/mkinitcpio.conf
sed -i 's/^HOOKS=.*\<block\>/& keyboard keymap encrypt lvm2/' /mnt/etc/mkinitcpio.conf
# recreate initramfs
arch-chroot /mnt mkinitcpio -p linux
# install micro code
arch-chroot /mnt pacman -Syu --noconfirm intel-ucode
# install grub
arch-chroot /mnt pacman -Syu --noconfirm grub efibootmgr
# configure grub
cp /mnt/etc/default/grub /mnt/etc/default/grub.bkp
export CRYPT_GRUB="cryptdevice=UUID=$(blkid /dev/nvme0n1p2 -s UUID -o value):cryptlvm root=/dev/vg0/root"
sed -i 's@^GRUB_CMDLINE_LINUX="[^"]*@& '"${CRYPT_GRUB}"'@' /mnt/etc/default/grub
# install grub to /boot
arch-chroot /mnt grub-install --target=x86_64-efi --efi-directory=/boot --bootloader-id="Arch Linux"
# generate grub config
arch-chroot /mnt grub-mkconfig -o /boot/grub/grub.cfg
# set timezone
arch-chroot /mnt ln -sf /usr/share/zoneinfo/${ZONE} /etc/localtime
# generate /etc/adjtime
arch-chroot /mnt hwclock --systohc
# localization
sed -i 's/^#en_US.UTF-8 UTF-8$/en_US.UTF-8 UTF-8/' /mnt/etc/locale.gen
touch /mnt/etc/locale.conf
sed -i 's/^LANG=.*/LANG=en_US.UTF-8/' /mnt/etc/locale.conf
echo "KEYMAP=${KEYBOARD}" > /mnt/etc/vconsole.conf
arch-chroot /mnt locale-gen
# hostname
echo "${HOSTNAME}" > /mnt/etc/hostname
# /etc/hosts
LINE="127.0.0.1\tlocalhost"; grep -Pq "${LINE}" /mnt/etc/hosts || echo -e "${LINE}" >> /mnt/etc/hosts
LINE="::1\t\tlocalhost"; grep -Pq "${LINE}" /mnt/etc/hosts || echo -e "${LINE}" >> /mnt/etc/hosts
LINE="127.0.1.1\t${HOSTNAME}.localdomain\t${HOSTNAME}"; grep -Pq "${LINE}" /mnt/etc/hosts || echo -e "${LINE}" >> /mnt/etc/hosts
# install dhcpcd
arch-chroot /mnt pacman -Syu --noconfirm dhcpcd
arch-chroot /mnt systemctl enable dhcpcd
# set root password
echo -e "${PASSWORD}\n${PASSWORD}" | passwd --root /mnt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment