Home / Articles

Alpine Linux with custom partitions

2020-09-21T03:28:59.000Z.

Changelog:

This article describes how to install Alpine Linux with custom partiions.

Partition planning

Plan the disk partition. Assume we want custom disk partiions for a small disk (20GB):

Install tools

Install tools that are required to setup the disk partition:


# You may need to setup the repositories first.
setup-apkrepos
apk update

apk add e2fsprogs parted

Disk partitioning

We can now create partitions. Assume the disk we will work on is /dev/vda:


parted -a optimal /dev/vda
mklabel msdos

# The /boot partition.
mkpart primary ext4 0% 100M
# The / partition.
mkpart primary ext4 100M 9G
# The swap partition.
mkpart primary linux-swap 9G 10G
# The /home partition.
mkpart primary ext4 10G 100%

# Set the bootable flag on the /boot partition.
set 1 boot on

We can confirm the disk paritions by the print command. The output looks like the following:


Model: Virtio Block Device (virtblk)
Disk /dev/vda: 21.5GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:

Number  Start   End     Size    Type     File system     Flags
  1      1049kB  99.6MB  98.6MB  primary  ext4            boot, lba
  2      99.6MB  9000MB  8900MB  primary  ext4            lba
  3      9000MB  10.0GB  1000MB  primary  linux-swap(v1)  lba
  4      10.0GB  21.5GB  11.5GB  primary  ext4            lba

Create and mount filesystems

Create filesystems on the partitions:


mkfs.ext4 /dev/vda1
mkfs.ext4 /dev/vda2
mkswap /dev/vda3
mkfs.ext4 /dev/vda4

Now we can mount the partitions for installing Alpine Linux:


# Mount the / partition first.
mount -t ext4 /dev/vda2 /mnt/
# Create the /boot directory and mount the partition for /boot.
mkdir /mnt/boot
mount -t ext4 /dev/vda1 /mnt/boot
# Create the /home directory and mount the partition for /home.
mkdir /mnt/home
mount -t ext4 /dev/vda4 /mnt/home
# Enable swap on the swap partition.
swapon /dev/vda3

Install Alpine Linux

Alpine Linux can be installed on the customized disk partitions as simple as:


setup-disk -m sys /mnt/

The installer does not create mount points for the swap partition and we have to add an entry in /mnt/etc/fstab (which will become /etc/fstab when we boot the new system). First find out the UUID of the swap partition:


blkid -s UUID -o value /dev/vda3

The output should be similar to the following:


2f1c2ec9-c396-4162-bd4b-b5e3ad9772b0

Now add an entry in /mnt/etc/fstab using a text editor:


UUID=2f1c2ec9-c396-4162-bd4b-b5e3ad9772b0 swap swap defaults 0 0

Install bootloader

Install bootloader and write the MBR:


apk add syslinux
chroot /mnt/
update-extlinux
exit
# /dev/vda is the /boot partition.
dd bs=440 count=1 conv=notrunc if=/mnt/usr/share/syslinux/mbr.bin of=/dev/vda

Now the installation is complete. Reboot to login the new system.

References