How to install Alpine Linux on OVH

2020-06-01

Alpine linux is my favorite Linux distribution. It is — as its slogan says — “Small. Simple. Secure.” which is exactly the aesthetic I want. However many hosting providers, including by favorite OVH doesn’t support it out of the box. We can work around this by net booting another Linux distro, and then installing Alpine from the “inside”. Once completed, and rebooted, you have a clean Alpine install with no traces of the net booted distro that is used in the install.

OVH has three “brands” — with price points targeting entry level, mid level, and pro users.

The screenshots and step by step instructions below are using So You Start but the general technique is applicable to any of the brands, and their control panels are similar.

Lets get started

  1. Login, the first view you see will be the Dashboard, click on Netboot → Rescue → rescue64-pro

  2. Click Next → Confirm

  3. Restart → Confirm

  4. Check your email, they will send you a message with the username and password. It may take several minutes, but is shouldn’t be more than 5 min.

  5. Install Alpine

    Login to the net booted rescue system with the username and password from your email. Then copy and paste each code block, along with your own modifications, into the shell.

    First we will setup some options for later use:

    We need to decide on the host name. I like to name my hosts after tree species, here I am using larch. You will also want to create a non-root user, here I am using my name:

    HOST=larch
    USER=jordan

    You can also select the root and boot file system type, ext4 is a good default.

    ROOT_FS=ext4
    BOOT_FS=ext4

    We need to grab the version of Alpine that we are installing, you can always get the latest from Alpine Linux.org we just want the major and minor version numbers (i.e. 3.12 not 3.12.0)

    REL=3.12

    We also need to get the version of apk the Alpine Package Manager that matches the version of Alpine we are using. You can look that up in the Alpine package server use the drop downs to select your Alpine version and architecture. For v3.12 it is 2.10.5-r1.

    APKV=2.10.5-r1

    Alright, we are almost done with the decisions. The last important step is to choose your boot device. You can explore the disks connected to your system with lsblk or lshw -class disk. We are going to keep things simple and just use the first hard drive.

    DEV=/dev/sda
    BOOT_DEV=${DEV}1
    ROOT_DEV=${DEV}2
    ROOT=/mnt
    BOOT=/mnt/boot

    We’ll also need these, but you generally shouldn’t need to edit them.

    PATH=/bin:/sbin:/usr/bin:/usr/sbin
    FEATURES="ata base ide scsi usb virtio $ROOT_FS"
    MODULES="sd-mod,usb-storage,$ROOT_FS"
    MIRROR=http://dl-cdn.alpinelinux.org/alpine
    REPO=$MIRROR/v$REL/main
    ARCH=$(uname -m)

    Alright, we are ready to roll!

    Setup partitions:

    sgdisk --zap-all $DEV
    sgdisk --new 1:0:+512M $DEV
    # the type is from sgdisk --list-type 8300 is Linux filesystem
    sgdisk --typecode 1:8300 $DEV
    sgdisk --change-name 1:boot $DEV
    sgdisk --new 2:0:0 $DEV # zero means max here
    sgdisk --typecode 2:8300 $DEV
    sgdisk --change-name 2:root $DEV
    sgdisk --attributes 1:set:2 $DEV

    Format the disk

    # -m reserved-blocks-percentage
    # -q Quiet execution.
    # -L new-volume-label
    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

    Grab apk

    curl -s $MIRROR/v$REL/main/$ARCH/apk-tools-static-${APKV}.apk | tar xz

    You may get some errors like:

    # tar: Ignoring unknown extended header keyword 'APK-TOOLS.checksum.SHA1'
    # tar: Ignoring unknown extended header keyword 'APK-TOOLS.checksum.SHA1'

    what is fine, just proceed.

    Install the base system

    ./sbin/apk.static \
    --repository $REPO \
    --update-cache \
    --allow-untrusted \
    --root $ROOT \
    --initdb add alpine-base syslinux dhcpcd linux-lts

    In the next sections it is important to copy and paste the entire section as one piece.

    Setup fstab

    cat << EOF > $ROOT/etc/fstab
    $ROOT_DEV / $ROOT_FS defaults,noatime 0 0
    $BOOT_DEV /boot $BOOT_FS defaults 0 2
    EOF

    Package repository and networking

    echo $REPO > $ROOT/etc/apk/repositories
    cat /etc/resolv.conf > $ROOT/etc/resolv.conf

    Booting

    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
    serial_port=
    serial_baud=115200
    password=''
    EOF

    More networking…

    cat << EOF > $ROOT/etc/network/interfaces
    auto lo
    iface lo inet loopback

    auto eth0
    iface eth0 inet dhcp
    hostname $HOST
    EOF

    Now we will mount the newly created system and finalize setting it up.

    Mount

    mount --bind /proc $ROOT/proc
    mount --bind /dev $ROOT/dev
    mount --bind /sys $ROOT/sys

    Setup

    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

    extlinux -i /boot
    dd bs=440 conv=notrunc count=1 if=/usr/share/syslinux/gptmbr.bin of=$DEV
    update-extlinux
    CHROOT

    Lets set the root password and our user’s password:

    chroot $ROOT passwd
    chroot $ROOT adduser -s /bin/ash -D $USER
    chroot $ROOT passwd $USER

    And finally we unmount

    umount $ROOT/proc
    umount $ROOT/dev
    umount $ROOT/sys
    umount $BOOT
    umount $ROOT

    Then logout of your SSH session.

  6. Return to the Dashboard

  7. Click Netboot → Hard disk → Next → Confirm

  8. Restart

  9. Wait for an email letting you know it has restarted.

  10. You can now login with the username and password that you set earlier — but not the root user as remote login as root is disabled.

Good luck!