Skip to content

Instantly share code, notes, and snippets.

@thefekete
Last active January 13, 2018 21:14
Show Gist options
  • Save thefekete/197b10be4018bc1b5af29de3b907198c to your computer and use it in GitHub Desktop.
Save thefekete/197b10be4018bc1b5af29de3b907198c to your computer and use it in GitHub Desktop.
testing file for automated arch install
#!/bin/bash
# vim: fdm=marker
# the most unsecure way to use this:
# curl -sL -H 'Cache-Control: no-cache' https://git.io/vbh2O
set -e
if [ ! $(hostname) == "archiso" ]; then
>&2 echo "Not in the Arch install environment, exiting for safety"
exit 1
fi
# make sure device is not swapped or mounted
cat /proc/swaps \
| awk "/${1//\//\\\/}/ { print \$1 }" \
| xargs -r swapoff -v
cat /proc/mounts \
| awk "/${1//\//\\\/}/ { print \$2 }" \
| sort -r \
| xargs -r umount -v
# --- Partitioning --- {{{
# Create the following partition layout:
# /dev/sdX1 - ESP partition (512mb)
# /dev/sdX2 - Swap partition (same as RAM)
# /dev/sdX3 - btrfs root partition (the rest)
device=/dev/sda
ram=$(awk '/MemTotal/ { print int($2/1024)}' /proc/meminfo)
esp=( 1 1024 )
swap=( ${esp[1]} $(( ${esp[1]} + $ram )) )
root=( ${swap[1]} 100% )
mnt_opts="autodefrag,compress=lzo,discard,noatime,ssd"
subvols=( etc home opt root srv usr var )
parted --script $device \
mklabel gpt \
mkpart ESP fat32 ${esp[0]}MiB ${esp[1]}MiB \
set 1 boot on \
mkpart primary linux-swap ${swap[0]}MiB ${swap[1]}MiB \
mkpart primary btrfs ${root[0]}MiB ${root[1]} \
print
mkfs.fat -F32 ${device}1 # format boot partition
mkswap ${device}2 # format swap partition
swapon ${device}2 # enable swap
mkfs.btrfs -f ${device}3 # format btrfs partition
mount -v -o $mnt_opts ${device}3 /mnt
btrfs subvolume create /mnt/master
umount -v /mnt
mount -v -o $mnt_opts,subvol=master ${device}3 /mnt
btrfs subvolume create /mnt/slash_root # create the slash_root subvol
for v in ${subvols[*]}; do
btrfs subvolume create /mnt/$v
done
umount -v /mnt
# mount slash_root subvolume
mount -v -o $mnt_opts,subvol=master/slash_root ${device}3 /mnt
# mount boot subvolume
mkdir -vp /mnt/boot
mount -v ${device}1 /mnt/boot
# mount other subvolumes
for v in ${subvols[*]}; do
mkdir -vp /mnt/$v
mount -v -o $mnt_opts,subvol=master/$v ${device}3 /mnt/$v
done
#}}}
: <<SKIP_TO_HERE
# --- Target Linux Base Install --- {{{
pacman --noconfirm -Sy
pacman --noconfirm -S reflector
reflector --verbose -l 5 --sort rate --save /etc/pacman.d/mirrorlist
pacstrap /mnt base base-devel btrfs-progs
#}}}
# --- Target Configuration --- {{{
# generate fstab (and remove duplicate subvol opts)
genfstab -U -p /mnt \
| sed 's/\(,subvol=[^,]*\)*/\1/g' \
| tee -a /mnt/etc/fstab
# chroot into install and finish configuration
arch-chroot /mnt bash <<-EOF
set -e
# set locale
sed -i 's/#\(en_US\.UTF-8 UTF-8\)/\1/' /etc/locale.gen
locale-gen
echo LANG=en_US.UTF-8 > /etc/locale.conf
export LANG=en_US.UTF-8
# timezone
ln -sf /usr/share/zoneinfo/Europe/Berlin /etc/localtime
hwclock --systohc --utc
# repositories
# TODO finish up from here
# XXX install everything here (xorg, i3, pulse, apps, etc)?
EOF
#}}}
SKIP_TO_HERE
# TODO make sure VirtualBox directory is nocow!
#!/bin/bash
# vim: fdm=marker tw=72
set -e
# This should ONLY be run from the archiso environment {{{
if [ ! $(hostname) == "archiso" ]; then
>&2 echo "Not in the Arch install environment, exiting for safety"
exit 1
fi
#}}}
# Usage {{{
VERSION=0.0
SCRIPT="$(basename $0)"
USAGE="${SCRIPT} - BTRFS Arch Installer Script v${VERSION}
Usage:
${SCRIPT} [options] <device>
Options:
-h, --help Show this message and exit
-V, --version Show version info and exit
-v, --verbose Show more output from commands
Installs Vanille Arch on a btrfs filesystem to <device> (eg /dev/sda).
By default the btrfs pool will contain a subvolume for the filesystem
root named 'slash_root' and a subvolume for the home directories
named 'home'.
WARNING: This script will DESTROY ALL DATA ON THE DEVICE!!!
"
#}}}
# Argument/Option Parsing {{{
# defaults
verbose=""
subvols=( slash_root:/ home:/home )
while [[ $1 == -* ]]; do
case $1 in
#-f|--flag)
# flag=true
# ;;
#-o|--opt)
# opt="$2"
# shift # past value
# ;;
-s|--subvol)
subvols+=( "$2" )
shift # past value
;;
-h|--help)
echo "$USAGE"
exit
;;
-V|--version)
echo "${SCRIPT} v${VERSION}"
exit
;;
-v|--verbose)
verbose='--verbose'
;;
--)
shift
break
;;
*)
>&2 echo "${SCRIPT}: unknown option $1" \
"(run '${SCRIPT} --help' for available options)"
exit 1
esac
shift
done
if [ $# -ne 1 ]; then
>&2 echo "$SCRIPT: Please specify a drive to install to" \
" (run with -h for help)"
exit 1
fi
device=$1
if [ "$verbose" ]; then
echo "device: $device"
echo "verbose: $verbose"
echo "subvols:"
for sv in ${subvols[*]}; do
v="${sv%:*}"
m="${sv#*:}"
echo -e "\t$v @ $m"
done
fi
#}}}
# --- Partitioning and mounting {{{
# first, make sure device is not swapped or mounted
cat /proc/swaps \
| awk "/${device//\//\\\/}/ { print \$1 }" \
| xargs -r swapoff $verbose
cat /proc/mounts \
| awk "/${device//\//\\\/}/ { print \$2 }" \
| sort -r \
| xargs -r umount $verbose
# Create the following partition layout:
# /dev/sdX1 - ESP partition (512mb)
# /dev/sdX2 - Swap partition (same as RAM)
# /dev/sdX3 - btrfs root partition (the rest)
ram=$(awk '/MemTotal/ { print int($2/1024)}' /proc/meminfo)
esp=( 1 1024 )
swap=( ${esp[1]} $(( ${esp[1]} + $ram )) )
root=( ${swap[1]} 100% )
mnt_opts="defaults,autodefrag,compress=lzo,discard,noatime,ssd"
parted --script $device \
mklabel gpt \
mkpart ESP fat32 ${esp[0]}MiB ${esp[1]}MiB \
set 1 boot on \
mkpart primary linux-swap ${swap[0]}MiB ${swap[1]}MiB \
mkpart primary btrfs ${root[0]}MiB ${root[1]} \
$(test "$verbose" && print)
mkfs.fat -F32 ${device}1
mkswap ${device}2
swapon ${device}2
mkfs.btrfs $(test ! "$verbose" && echo -q) -f -L btrfs_pool ${device}3
# Create subvolumes
# <FS_TREE>
# └── root
# ├── .snapshots
# │ ├── yyyy-mm-ddThh:mm:ss+tz:tz
# │ ├── yyyy-mm-ddThh:mm:ss+tz:tz
# │ └── ...
# └── home
# └── .snapshots
# ├── yyyy-mm-ddThh:mm:ss+tz:tz
# ├── yyyy-mm-ddThh:mm:ss+tz:tz
# └── ...
mount $verbose -o $mnt_opts ${device}3 /mnt
btrfs subvolume create /mnt/root
umount $verbose /mnt
mount $verbose -o $mnt_opts,subvol=root ${device}3 /mnt/
btrfs subvolume create /mnt/.snapshots
btrfs subvolume create /mnt/home
btrfs subvolume create /mnt/home/.snapshots
# don't forget about the boot partition!
mkdir -p $verbose /mnt/boot
mount $verbose ${device}1 /mnt/boot
# create first snapshots
btrfs subvolume snapshot -r /mnt/ /mnt/.snapshots/$(date -Is)
btrfs subvolume snapshot -r /mnt/home /mnt/home/.snapshots/$(date -Is)
#}}}
: <<'SKIP_TO_HERE'
# --- Target Linux Base Install {{{
pacman --noconfirm -Sy
pacman --noconfirm -S reflector
reflector $verbose -l 5 --sort rate --save /etc/pacman.d/mirrorlist
pacstrap /mnt base base-devel btrfs-progs snapper
#}}}
# --- Target Configuration {{{
# generate fstab (and remove duplicate subvol opts)
genfstab -U -p /mnt \
| sed 's/\(,subvol=[^,]*\)*/\1/g' \
| tee -a /mnt/etc/fstab
# chroot into install and finish configuration
arch-chroot /mnt bash <<EOF
set -e
# set locale
sed -i 's/#\(en_US\.UTF-8 UTF-8\)/\1/' /etc/locale.gen
locale-gen
echo LANG=en_US.UTF-8 > /etc/locale.conf
export LANG=en_US.UTF-8
# timezone
ln $verbose -sf /usr/share/zoneinfo/Europe/Berlin /etc/localtime
hwclock --systohc --utc
# repositories
# TODO finish up from here
# XXX install everything here (xorg, i3, pulse, apps, etc)?
EOF
#}}}
SKIP_TO_HERE
# XXX install and boot like the following link:
# https://medium.com/hello-im-alan/installing-linux-under-uefi-on-a-thinkpad-successfully-518136d175fe
# TODO in second phase, disable compression for Music, pics etc..
# TODO disable CoW for virtual machine dir!!! with chattr +C ~/VirtualMachines
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment