Skip to content

Instantly share code, notes, and snippets.

@Gutem
Last active May 21, 2020 05:31
Show Gist options
  • Save Gutem/45e091e472c4f712b1f09df1e6680814 to your computer and use it in GitHub Desktop.
Save Gutem/45e091e472c4f712b1f09df1e6680814 to your computer and use it in GitHub Desktop.
#!/bin/bash
################################################################################
### Variables ###
################################################################################
TARGET_DRIVER="/dev/sda"
SWAP="FALSE"
################################################################################
### Functions ###
################################################################################
helpFunction() {
echo ""
echo "Usage: $0 [options...]"
echo -e " --mbr \tSet the boot configuration to use MBR/Legacy"
echo -e " --uefi \tSet the boot configuration to use UEFI"
echo -e " --swap \tSet swap as a partition instead of file(default)"
echo -e " --about \tAbout this script"
echo -e " -v, --version \tShows the Script version"
echo -e " -h, --help \tThis help"
exit 1 # Exit script after printing help
}
################################################################################
### Arguments parser ###
################################################################################
if [[ $# == 0 ]]; then
echo -e "You should pass the required arguments"
helpFunction
exit 1
fi
while [[ $# -gt 0 ]]; do
case "$1" in
--uefi*)
UEFI="TRUE"
if [[ $MBR == "TRUE" ]]; then
echo -e "You should choosse only one boot configuration: MBR or UEFI"
exit 1
fi
# echo "UEFI: $UEFI"
;;
--mbr*)
MBR="TRUE"
if [[ $UEFI == "TRUE" ]]; then
echo -e "You should choosse only one boot configuration: MBR or UEFI"
exit 1
fi
# echo "MBR: $MBR"
;;
--swap*)
SWAP="TRUE"
;;
--help|-h*)
helpFunction
exit 0
;;
*)
>&2 printf "Error: Invalid argument\n"
exit 1
;;
esac
shift
done
################################################################################
### Partitioning ###
################################################################################
# to create the partitions programatically (rather than manually)
# we're going to simulate the manual input to fdisk
# The sed script strips off all the comments so that we can
# document what we're doing in-line with the actual commands
# Note that a blank line (commented as "defualt" will send a empty
# line terminated with a newline to take the fdisk default.
(
if [[ $MBR == "TRUE" ]]; then
echo o # Create a new empty DOS partition table
echo n # Add a new partition
echo p # Primary partition
echo # Partition number (Accept default: 1)
echo # default, start immediately after preceding partition
echo # default, extend partition to end of disk
echo a # make a partition bootable
elif [[ $UEFI == "TRUE" ]]; then
echo g #Create a new empty GPT partition table
echo n # Add a new partition
echo p # Primary partition
echo 1 # Partition number 1
echo # First sector (Accept default: 1) - start at beginning of disk
echo +512M # 512 MB boot parttion
echo n # Add a new partition
echo p # Primary partition
echo 2 # Partition number 2
echo # default, start immediately after preceding partition
echo # default, extend partition to end of disk
echo a # make a partition bootable
echo 1 # bootable partition is partition 1 -- /dev/sda1
fi
echo p # print the in-memory partition table
echo w # Write changes
# echo q # We're done
) | sudo fdisk ${TARGET_DRIVER}
sleep 5
################################################################################
### Partition formatting ###
################################################################################
if [[ $MBR == "TRUE" ]]; then
# Make EXT4 Filesystem on /dev/sda1
mkfs.ext4 /dev/sda1
elif [[ $UEFI == "TRUE" ]]; then
# Make FAT32 Filesystem on /dev/sda1 (bootable)
mkfs.fat -F32 /dev/sda1
# Make EXT4 Filesystem on /dev/sda2
mkfs.ext4 /dev/sda2
fi
if [[ $SWAP == "TRUE" ]]; then
# Make Swap on /dev/sda4
mkswap /dev/sda4
fi
sleep 5
################################################################################
### Partition mounting ###
################################################################################
if [[ $MBR == "TRUE" ]]; then
mount /dev/sda1 /mnt
elif [[ $UEFI == "TRUE" ]]; then
mkdir /mnt/boot
mkdir /mnt/boot/efi
mount /dev/sda1 /mnt/boot/efi
mount /dev/sda2 /mnt
fi
if [[ $HOME == "TRUE" ]]; then
mkdir /mnt/home
mount /dev/sda3 /mnt/home
fi
if [[ $SWAP == "TRUE" ]]; then
swapon /dev/sda4
fi
sleep 5
################################################################################
### System installing ###
################################################################################
pacstrap /mnt base linux linux-firmware
sleep 5
genfstab -U -p /mnt >> /mnt/etc/fstab
sleep 5
arch-chroot /mnt
sleep 5
ln -sf /usr/share/zoneinfo/America/Sao_Paulo /etc/localtime
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment