Skip to content

Commit

Permalink
refactor(bootstrap): modularize bootstrap script for improved readabi…
Browse files Browse the repository at this point in the history
…lity and maintenance

Refactors `make-bootstrap.sh` by modularizing key functions (e.g., `check_dependencies`, `clean_pacman_cache`, `setup_locale`, `setup_network`, `update_system`). Adds configuration variables for URLs, file names, and color codes for improved readability. Simplifies maintenance and expands functionality to include system cleanup, dependency checking, and structured configuration of the Arch Linux rootfs for WSL.
  • Loading branch information
ruizlenato committed Nov 3, 2024
1 parent 7b5e764 commit 2dafbd3
Showing 1 changed file with 100 additions and 41 deletions.
141 changes: 100 additions & 41 deletions make-bootstrap.sh
Original file line number Diff line number Diff line change
@@ -1,67 +1,126 @@
#!/usr/bin/env bash

# Copyright (C) 2024 Luiz Renato
# SPDX-License-Identifier: GPL-3.0-only

# Clear shell
# Script configuration
readonly ROOTFS_URL="https://mirror.rackspace.com/archlinux/iso/latest/archlinux-bootstrap-x86_64.tar.zst"
readonly ROOTFS_FILE="archlinux-bootstrap-x86_64.tar.zst"
readonly ROOTFS_DIR="root.x86_64"
readonly PKGLIST_FILE="pkglist.x86_64.txt"
readonly OUTPUT_FILE="ArchWSLBootstrap-$(date -u +%Y-%m-%d).tar.gz"

# Color codes
readonly RED='\e[1;31m'
readonly GREEN='\e[1;32m'
readonly YELLOW='\e[1;33m'
readonly NC='\e[m' # No Color

clear

# Dependency check
if ! dpkg -s wget >/dev/null 2>&1; then
sudo apt-get install get
fi
check_dependencies() {
local deps=("wget" "zstd")
for dep in "${deps[@]}"; do
if ! dpkg -s "$dep" >/dev/null 2>&1; then
sudo apt-get install -y "$dep"
fi
done
}

start() {
echo -e '\e[1;31mBuilding bootstrap\e[m'
download
check_dependencies
echo -e "${RED}Building bootstrap${NC}"
download
}

download() {
echo -e '\e[1;33mDownloading Arch Linux rootfs\e[m'
wget https://mirror.rackspace.com/archlinux/iso/latest/archlinux-bootstrap-x86_64.tar.zst -q
printf "\033[A\033[K"
echo -e '\e[1;32m✓\e[m Arch Linux rootfs downloaded!'
extract
echo -e "${YELLOW}Downloading Arch Linux rootfs${NC}"
wget "$ROOTFS_URL" -q
printf "\033[A\033[K"
echo -e "${GREEN}${NC} Arch Linux rootfs downloaded!"
extract
}

extract() {
echo -e '\e[1;33mExtracting Arch Linux rootfs\e[m'
sudo tar --xattrs --xattrs-include="security.capability" -I zstd -xpf archlinux-bootstrap-x86_64.tar.zst &>/dev/null
printf "\033[A\033[K"
echo -e '\e[1;32m✓\e[m Extract Arch Linux rootfs!'
configure
echo -e "${YELLOW}Extracting Arch Linux rootfs${NC}"
sudo tar --xattrs --xattrs-include="security.capability" -I zstd -xpf "$ROOTFS_FILE" &>/dev/null
printf "\033[A\033[K"
echo -e "${GREEN}${NC} Extract Arch Linux rootfs!"
configure
}

clean_pacman_cache() {
yes | sudo chroot "$ROOTFS_DIR" /usr/bin/pacman -Scc &>/dev/null
}

update_mirrorlist() {
sudo curl "https://archlinux.org/mirrorlist/?country=US&protocol=https&ip_version=4" \
-o "$ROOTFS_DIR/etc/pacman.d/mirrorlist" &>/dev/null
sudo sed -i -e "s/#Server/Server/g" "$ROOTFS_DIR/etc/pacman.d/mirrorlist"
}

update_system() {
sudo cp -f /etc/resolv.conf "$ROOTFS_DIR/etc/resolv.conf"
sudo chroot "$ROOTFS_DIR" /usr/bin/pacman -Syu --noconfirm &>/dev/null
}

setup_locale() {
sudo sed -i -e "s/#en_US.UTF-8/en_US.UTF-8/" "$ROOTFS_DIR/etc/locale.gen"
sudo sh -c 'echo "LANG=en_US.UTF-8" > "$ROOTFS_DIR/etc/locale.conf"'
sudo ln -sf /etc/locale.conf "$ROOTFS_DIR/etc/default/locale"
}

setup_network() {
sudo rm -rf "$ROOTFS_DIR/etc/resolv.conf"
sudo sh -c 'echo "LANG=en_US.UTF-8" > "$ROOTFS_DIR/etc/locale.conf"'
sudo cp -f /etc/resolv.conf "$ROOTFS_DIR/etc/resolv.conf"
}

copy_config_files() {
sudo cp wsl.conf "$ROOTFS_DIR/etc/wsl.conf"
sudo cp bash_profile "$ROOTFS_DIR/root/.bash_profile"
}

cleanup_system_files() {
local files=(
"/etc/machine-id"
"/usr/lib/systemd/system/sysinit.target.wants/systemd-firstboot.service"
)

for file in "${files[@]}"; do
sudo rm -rf "${ROOTFS_DIR}${file}"
done
}

configure() {
echo -e '\e[1;33mSetting up a few basic things in bootstrap\e[m'
sudo curl "https://archlinux.org/mirrorlist/?country=US&protocol=https&ip_version=4" -o root.x86_64/etc/pacman.d/mirrorlist &>/dev/null
sudo sed -i -e "s/#Server/Server/g" root.x86_64/etc/pacman.d/mirrorlist
sudo cp wsl.conf root.x86_64/etc/wsl.conf
sudo cp bash_profile root.x86_64/root/.bash_profile
sudo sed -i -e "s/#en_US.UTF-8/en_US.UTF-8/" root.x86_64/etc/locale.gen
echo "LANG=en_US.UTF-8" | sudo tee root.x86_64/etc/locale.conf
sudo ln -sf /etc/locale.conf root.x86_64/etc/default/locale
sudo rm -rf root.x86_64/etc/resolv.conf
sudo rm -rf root.x86_64/etc/machine-id
sudo rm -rf root.x86_64/usr/lib/systemd/system/sysinit.target.wants/systemd-firstboot.service
echo "# This file was automatically generated by WSL." | sudo tee root.x86_64/etc/resolv.conf
printf "\033[A\033[K"
echo -e '\e[1;32m✓\e[m Complete setup!'
compress
echo -e "${YELLOW}Setting up a few basic things in bootstrap${NC}"

clean_pacman_cache
update_mirrorlist
copy_config_files
setup_locale
setup_network
cleanup_system_files
update_system

printf "\033[A\033[K"
echo -e "${GREEN}${NC} Complete setup!"
compress
cleanup
}

compress() {
echo -e '\e[1;33mBuilding bootstrap\e[m'
sudo tar --xattrs --xattrs-include="security.capability" -zcpf ArchWSLBootstrap-$(date -u +%Y-%m-%d).tar.gz -C root.x86_64 .
sudo chown root:root ArchWSLBootstrap-$(date -u +%Y-%m-%d).tar.gz
printf "\033[A\033[K"
echo -e '\e[1;32m✓\e[m Building bootstrap!'
echo -e "${YELLOW}Building bootstrap${NC}"
sudo tar --xattrs --xattrs-include="security.capability" \
-zcpf "$OUTPUT_FILE" -C "$ROOTFS_DIR" .
sudo chown root:root "$OUTPUT_FILE"
printf "\033[A\033[K"
echo -e "${GREEN}${NC} Building bootstrap!"
}

cleanup() {
sudo rm -rf archlinux-bootstrap-x86_64.tar.zst root.x86_64/
clear
echo -e '\e[1;32mAll done.\e[m'
sudo rm -rf "$ROOTFS_FILE" "$ROOTFS_DIR" "$PKGLIST_FILE"
echo -e "${GREEN}All done.${NC}"
}

start
start

0 comments on commit 2dafbd3

Please sign in to comment.