Skip to content

Instantly share code, notes, and snippets.

@niflostancu
Created January 4, 2017 19:55
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 10 You must be signed in to fork a gist
  • Save niflostancu/03810a8167edc533b1712551d4f90a14 to your computer and use it in GitHub Desktop.
Save niflostancu/03810a8167edc533b1712551d4f90a14 to your computer and use it in GitHub Desktop.
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.
UBUNTU_VERSION=xenial
MNT=/mnt
mkfs.btrfs -m raid1 -d raid1 $DEV1 $DEV2
# alt: mkfs.ext4 /dev/sdX2
mount $DEV1 $MNT
debootstrap --arch=amd64 --variant=buildd $UBUNTU_VERSION "$MNT" http://archive.ubuntu.com/ubuntu/
sudo cp /etc/resolv.conf "$MNT/etc/resolv.conf"
cat <<REPO_LIST | sudo tee "$MNT/etc/apt/sources.list"
# Repositories
deb http://archive.ubuntu.com/ubuntu $UBUNTU_VERSION main restricted universe multiverse
deb http://archive.ubuntu.com/ubuntu $UBUNTU_VERSION-security main restricted universe multiverse
deb http://archive.ubuntu.com/ubuntu $UBUNTU_VERSION-updates main restricted universe multiverse
deb http://archive.ubuntu.com/ubuntu $UBUNTU_VERSION-backports main restricted universe multiverse
REPO_LIST

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
BOOT_DEV=/dev/sdc1
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:

passwd
# 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