Skip to content

Instantly share code, notes, and snippets.

@publicarray
Forked from thde/alpine-install.sh
Last active March 9, 2019 07:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save publicarray/adb1012953d3d8022bd92e66f750ce23 to your computer and use it in GitHub Desktop.
Save publicarray/adb1012953d3d8022bd92e66f750ce23 to your computer and use it in GitHub Desktop.
A script to install alpine linux on a dedicated server. Tested on Hetzner, Kimsufi / OVH
#!/bin/sh
set -ex
PATH=/bin:/sbin:/usr/bin:/usr/sbin
KEYMAP="us us"
HOST=alpine
USER=anon
ROOT_FS=ext4
BOOT_FS=ext4
FEATURES="ata base ide scsi usb virtio $ROOT_FS"
MODULES="sd-mod,usb-storage,$ROOT_FS"
REL=3.9
MIRROR=http://dl-cdn.alpinelinux.org/alpine
REPO=$MIRROR/v$REL/main
APKV=2.10.3-r1
DEV=/dev/sdb
ROOT_DEV=${DEV}2
BOOT_DEV=${DEV}1
ROOT=/mnt
BOOT=/mnt/boot
ARCH=$(uname -m)
DISK_SIZE=9G
#debian rescure disk (OVH)
#login: ssh debian@127.0.0.1 -o UserKnownHostsFile=/dev/null
#root: sudo su
apt update
apt install curl -y
sgdisk -Z $DEV
sgdisk -n 1:0:+512M $DEV
sgdisk -t 1:8300 $DEV
sgdisk -c 1:boot $DEV
sgdisk -n 2:0:+$DISK_SIZE $DEV
sgdisk -t 2:8300 $DEV
sgdisk -c 2:root $DEV
sgdisk -A 1:set:2 $DEV
mkfs.$BOOT_FS -m 0 -q -L boot $BOOT_DEV
mkfs.$ROOT_FS -q -L root $ROOT_DEV
mount $ROOT_DEV $ROOT
mkdir $BOOT
mount $BOOT_DEV $BOOT
curl -s $MIRROR/v$REL/main/$ARCH/apk-tools-static-${APKV}.apk | tar xz
./sbin/apk.static --repository $REPO --update-cache --allow-untrusted --root $ROOT --initdb add alpine-base syslinux dhcpcd
cat << EOF > $ROOT/etc/fstab
$ROOT_DEV / $ROOT_FS defaults,noatime 0 0
$BOOT_DEV /boot $BOOT_FS defaults 0 2
EOF
echo $REPO > $ROOT/etc/apk/repositories
cat /etc/resolv.conf > $ROOT/etc/resolv.conf
cat << EOF > $ROOT/etc/update-extlinux.conf
overwrite=1
vesa_menu=0
default_kernel_opts="quiet"
modules=$MODULES
root=$ROOT_DEV
verbose=0
hidden=1
timeout=1
default=vanilla
serial_port=
serial_baud=115200
xen_opts=dom0_mem=256M
password=''
EOF
cat << EOF > $ROOT/etc/network/interfaces
auto lo
iface lo inet loopback
auto eth0
iface eth0 inet dhcp
hostname $HOST
EOF
mount --bind /proc $ROOT/proc
mount --bind /dev $ROOT/dev
mount --bind /sys $ROOT/sys
chroot $ROOT /bin/sh -x << CHROOT
apk update
apk add openssh
setup-hostname -n $HOST
rc-update -q add devfs sysinit
rc-update -q add dmesg sysinit
rc-update -q add mdev sysinit
rc-update -q add hwdrivers sysinit
rc-update -q add hwclock boot
rc-update -q add modules boot
rc-update -q add sysctl boot
rc-update -q add hostname boot
rc-update -q add bootmisc boot
rc-update -q add syslog boot
rc-update -q add networking boot
rc-update -q add urandom boot
rc-update -q add dhcpcd boot
rc-update -q add mount-ro shutdown
rc-update -q add killprocs shutdown
rc-update -q add savecache shutdown
rc-update -q add acpid default
rc-update -q add crond default
rc-update -q add sshd default
echo features=\""$FEATURES"\" > /etc/mkinitfs/mkinitfs.conf
apk add linux-vanilla
extlinux -i /boot
dd bs=440 conv=notrunc count=1 if=/usr/share/syslinux/gptmbr.bin of=$DEV
update-extlinux
mkinitfs $(ls /lib/modules)
CHROOT
chroot $ROOT passwd
chroot $ROOT adduser -s /bin/ash -D $USER
chroot $ROOT passwd $USER
umount $ROOT/proc
umount $ROOT/dev
umount $ROOT/sys
umount $BOOT
umount $ROOT
@publicarray
Copy link
Author

publicarray commented Mar 9, 2019

Any additional setup: e.g. setup-ntp

mount /dev/sdb2 /mnt
mount /dev/sdb1 /mnt/boot
mount --bind /proc /mnt/proc
mount --bind /dev /mnt/dev
mount --bind /sys /mnt/sys
chroot /mnt /bin/sh

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment