archiso-zfs/init
2020-05-06 11:21:39 +02:00

182 lines
4.7 KiB
Bash
Executable file

#!/bin/bash
# This script load zfs kernel module for any archiso.
# github.com/eoli3n
# Thanks to CalimeroTeknik on #archlinux-fr, FFY00 on #archlinux-projects, JohnDoe2 on #regex
### Functions
print () {
echo -e "\n\033[1m> $1\033[0m\n"
}
get_running_kernel_version () {
# Returns running kernel version
# Get running kernel version
kernel_version=$(LANG=C pacman -Qi linux | sed -n 's/^Version\s*: //p')
print "Current kernel version is $kernel_version"
}
get_iso_build_date () {
# Returns running kernel build date formated as 'YYYY/MM/DD'
# Get running kernel build date
kernel_date=$(date +%Y/%m/%d -d "$(LANG=C pacman -Qi linux |sed -n 's/^Install Date\s*: //p')")
}
init_archzfs () {
print "Adding archzfs repo"
cat >> /etc/pacman.conf <<"EOF"
[archzfs]
Server = http://archzfs.com/archzfs/x86_64
Server = http://mirror.sum7.eu/archlinux/archzfs/archzfs/x86_64
Server = https://mirror.biocrafting.net/archlinux/archzfs/archzfs/x86_64
EOF
print "Fetching archzfs signing keys"
pacman-key --recv-keys F75D9D76 2>/dev/null
pacman-key --lsign-key F75D9D76 2>/dev/null
pacman -Sy
}
init_archlinux_archive () {
# $1 is date formated as 'YYYY/MM/DD'
# Returns False if repo does not exists
# Set repo
repo="Server=https://archive.archlinux.org/repos/$1/\$repo/os/\$arch"
pacman -Syy
# If repo exists, set it, if not, return False
curl -s "$repo" && echo "$repo" > /etc/pacman.d/mirrorlist || return 1
return 0
}
search_package () {
# $1 is package name to search
# $2 is version to match
# Set regex to match package
local regex='href="\K'"$1"'-[0-9].*?'"$2"'.*?(?!.+\.sig)x86_64[^\"]+'
# href=" # match href="
# \K # don't return anything matched prior to this point
# '"$1"'-[0-9] # find me package-# escaped by shell
# .*? # match anything but newlines 0 or more times, as few times as possible (non-greedy)
# '"$2"' # match version escaped by shell
# .*? # match anything but newlines 0 or more times, as few times as possible (non-greedy)
# (?![^"\n]+\.sig) # remove .sig matches
# x86_64 # now match architecture
# [^\n"]+ # continue matching anything but newlines and ", 1 or more times, until end
# Set archzfs URLs list
local urls="http://archzfs.com/archzfs/x86_64/ http://archzfs.com/archive_archzfs/"
# Loop search
for url in $urls
do
print "Searching on $url..."
# Query url and try to match package
local package=$(curl -s "$url" | grep -Po "$regex" | tail -n 1)
# If a package is found
if [[ -n $package ]]
then
print "$package found"
# Build package url
package_url="$url$package"
fi
done
}
download_package () {
# $1 is package url to download in tmp
# It returns downloaded file path
local filename="${url##*/}"
# Download package in tmp
wget "$1" -P /tmp
# Set out file
package_file="/tmp/$filename"
print "Downloading $output ..."
}
### Main
print "Testing if archiso is running"
grep archiso /proc/cmdline > /dev/null
print "Increasing cowspace to half of RAM"
mount -o remount,size=50% /run/archiso/cowspace
# Init archzfs repository
init_archzfs
# Search kernel package
# https://github.com/archzfs/archzfs/issues/337#issuecomment-624312576
get_running_kernel_version
kernel_version_fixed="${kernel_version//-/\.}"
# Search zfs-linux package matching running kernel version
search_package "zfs-linux" "$kernel_version_fixed"
# If a package is found
if [[ -n $package_url ]]
then
# Download package
download_package $package_url
print "Extracting $zfs_linux_package"
# Extract package
tar xvf "$package_file" -C /tmp/extract
print "Searching zfs-utils version required"
# Get zfs-utils dependency version
zfs_utils_version=$(grep 'depend = zfs-utils' /tmp/extract/.PKGINFO | grep -o '[[:digit:]].*')
# Clean extracted dir
rm -Rf /tmp/extract
# Search zfs-utils package matching zfs-linux package dependency
search_package "zfs-utils" "$zfs_utils_version"
if [[ -n $package_url ]]
then
print "Installing zfs-utils"
# Install packages
pacman -U "$zfs_utils_url" --noconfirm
pacman -U "$zfs_linux_package" --noconfirm
print "Loading zfs kernel module"
# Load kernel module
modprobe zfs && print "ZFS module is working"
else
print "No module found for current kernel version on Archzfs repos"
fi
else
print "No module found for current kernel version on Archzfs repos"
fi