Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Installing Ubuntu 16.04 on btrfs using debootstrap

Installing Ubuntu 16.04 with btrfs using debootstrap

Partition layout

First, create a GPT partition table.

  1. boot partition, label: EFI, flags: boot + ESP; size: 1GB, format to FAT32;
  2. root partition (label: root), must same size on all devices!
  3. swap partition.

Installing Ubuntu Server

First, install the debootstrap and btrfs-tools package.

Then, format the partition as btrfs (raid1) on two devices and use debootstrap to install the OS:

DEV1=/dev/sdc2  # modify to installation root partition.
DEV2=/dev/sdd2  # modify to mirror root partition.
mkfs.btrfs -m raid1 -d raid1 $DEV1 $DEV2
# alt: mkfs.ext4 /dev/sdX2
mount $DEV1 $MNT
debootstrap --arch=amd64 --variant=buildd $UBUNTU_VERSION "$MNT"
sudo cp /etc/resolv.conf "$MNT/etc/resolv.conf"
cat <<REPO_LIST | sudo tee "$MNT/etc/apt/sources.list"
# Repositories
deb $UBUNTU_VERSION main restricted universe multiverse
deb $UBUNTU_VERSION-security main restricted universe multiverse
deb $UBUNTU_VERSION-updates main restricted universe multiverse
deb $UBUNTU_VERSION-backports main restricted universe multiverse

Chroot to the newly installed root filesystem:

mount $DEV1 $MNT
mount --bind /dev $MNT/dev
mount --bind /proc $MNT/proc
mount --bind /sys $MNT/sys
chroot "$MNT" /usr/bin/env PATH=/usr/sbin:/usr/bin/:/bin:/sbin bash -i

In the chroot, configure the system:

apt-get update
apt-get install -y btrfs-tools ubuntu-minimal language-pack-en-base vim ssh iputils-arping \
    grub-efi-amd64 bash-completion parted
# Install Grub
mount "$BOOT_DEV" /boot
grub-install --target=x86_64-efi --efi-directory=/boot --bootloader-id=grub
mkdir /boot/EFI/boot
cp /boot/EFI/grub/grubx64.efi /boot/EFI/boot/bootx64.efi
# also install a kernel
apt-get install linux-generic

Then fill out /etc/fstab:

echo "" > /etc/fstab
echo "PARTLABEL=root / btrfs defaults 0 1" >> /etc/fstab
echo "PARTLABEL=EFI /boot vfat defaults 0 2" >> /etc/fstab

Initramfs configuration for btrfs:

cp /usr/share/initramfs-tools/scripts/local-premount/btrfs /etc/initramfs-tools/scripts/local-premount/btrfs
# edit it and enter a "sleep 5" before the line with "btrfs device scan"
vim /etc/initramfs-tools/scripts/local-premount/btrfs
# build the new initramfs:
update-initramfs -u
# backup the boot partition
mkdir /boot_bkp
cp -rf /boot/* /boot_bkp

Change root password, optionally create your account:

# insert your new root password

At the end, unmount the partition:

exit  # from the chroot
umount "$MNT/boot"
umount "$MNT/dev"
umount "$MNT/proc"
umount "$MNT/sys"
umount "$MNT"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.