how to manually install alpine linux on any linux distribution

# Assuming

You can easily change these factors when you notice them, for example in the alpine rootfs you can always change the version, or in timezone, well time timezone, the drive whenever it comes up, though networking is needed here

# Installation (pt. 1)

# Installation (pt. 2)

# Mount root

1
2
mkdir -p /mnt/alpine
mount /dev/sda3 /mnt/alpine

# Mount boot

1
2
mkdir -p /mnt/alpine/boot
mount /dev/sda1 /mnt/alpine/boot

# Download an extract the RootFS

1
2
3
cd /mnt/alpine
wget https://dl-cdn.alpinelinux.org/alpine/v3.16/releases/x86_64/alpine-minirootfs-3.16.2-x86_64.tar.gz
tar xpvf alpine-minirootfs-3.16.2-x86_64.tar.gz --xattrs-include='*.*' --numeric-owner

# FSTAB generation

1
2
3
wget https://raw.githubusercontent.com/cemkeylan/genfstab/master/genfstab
sh genfstab -U /mnt/alpine >>/mnt/alpine/etc/fstab
cat /mnt/alpine/etc/fstab
1
echo "$(blkid /dev/sda2 | awk '{print $2}' | sed 's/"//g') none swap sw 0 0" >>/mnt/alpine/etc/fstab

# Mount the needed fake filesystems

1
2
3
4
5
6
7
mount --types proc /proc /mnt/alpine/proc
mount --rbind /sys /mnt/alpine/sys
mount --make-rslave /mnt/alpine/sys
mount --rbind /dev /mnt/alpine/dev
mount --make-rslave /mnt/alpine/dev
mount --bind /run /mnt/alpine/run
mount --make-slave /mnt/alpine/run

# Copy host's resolv.conf to the chroot environment

1
cp /etc/resolv.conf /mnt/alpine/etc/resolv.conf

# Chroot

1
chroot /mnt/alpine /bin/ash

# Setup PATH

1
export PATH='/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin'

# Update and install setup scripts

1
2
apk update
apk add alpine-conf openrc --no-cache

# Installation (pt. 3)

If you see any errors regarding rc-service, rc-update, etc. ignore them

This part is based off https://docs.alpinelinux.org/user-handbook/0.1a/Installing/manual.html

# Setup keymap

1
setup-keymap us us  # This will use the US keyboard

# Setup hostname

1
2
export HOSTNAME='alpine'
setup-hostname "$HOSTNAME"  # Hostname will be alpine

# Setup hosts file

1
2
3
4
tee /etc/hosts <<EOF
127.0.0.1 localhost.localdomain localhost $HOSTNAME.localdomain $HOSTNAME
::1       localhost.localdomain localhost $HOSTNAME.localdomain $HOSTNAME
EOF

# Setup networking

https://docs.alpinelinux.org/user-handbook/0.1a/Installing/manual.html#_networking

# Timezone

1
2
3
4
5
apk add tzdata
export _TZ='Europe/Vilnius'
install -Dm 0644 "/usr/share/zoneinfo/$_TZ" "/etc/zoneinfo/$_TZ"
export TZ="$_TZ"
echo "export TZ='$TZ'" >> /etc/profile.d/timezone.sh

# Root password

1
passwd

# Networking tools

1
apk add dhcp wpa_supplicant

# Bootloader (GRUB) and kernel

1
apk add grub grub-efi efibootmgr linux-lts
1
apk add linux-firmware
1
2
grub-install --target=x86_64-efi --efi-directory=/boot --bootloader-id=ALPINE
grub-mkconfig -o /boot/grub/grub.cfg

# Enable vital services

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
rc-update add hostname boot
rc-update add devfs sysinit
rc-update add cgroups sysinit
rc-update add bootmisc boot
rc-update add binfmt boot
rc-update add fsck boot
rc-update add urandom boot
rc-update add root boot
rc-update add procfs boot
rc-update add swap boot
rc-update add sysfs sysinit
rc-update add localmount boot
rc-update add sysctl boot

# Make grub use a menu

1
2
3
4
5
6
7
tee -a /etc/default/grub <<EOF
GRUB_TIMEOUT_STYLE=menu
GRUB_GFXMODE=1920x1080
GRUB_GFXPAYLOAD_LINUX=keep
GRUB_CMDLINE_LINUX="rootfstype=ext4 loglevel=3"
EOF
grub-mkconfig -o /boot/grub/grub.cfg

# Exiting the installation

1
2
3
exit
umount -a
poweroff