Skip to content

Instantly share code, notes, and snippets.

@andras-tim
Forked from wpbrown/user-data.yaml
Last active August 28, 2020 03:35
Show Gist options
  • Save andras-tim/ea4ce2dc16e11c59f661a5f96f12fafa to your computer and use it in GitHub Desktop.
Save andras-tim/ea4ce2dc16e11c59f661a5f96f12fafa to your computer and use it in GitHub Desktop.
ubuntu autoinstall with btrfs subvolumes
#cloud-config
# based on
# * https://ubuntu.com/server/docs/install/autoinstall-quickstart
# * https://curtin.readthedocs.io/en/latest/topics/storage.html
# * https://gist.github.com/wpbrown/b688a934339cb4228c3faf5b527fbe5b
autoinstall:
version: 1
identity:
hostname: autoinstalled
password: "$6$exDY1mhS4KUYCE/2$zmn9ToZwTKLhCw.b4/b.ZRTIZM30JZ4QrOQ2aOXJ8yk96xpcCof0kxKwuX1kqLG/ygbJ1f8wxED22bTL4F46P0"
username: foo
locale: en_US.UTF-8
layout: us
ssh:
install-server: true
# early-commands:
# - "blkdiscard /dev/sda || true"
storage:
version: 1
swap:
size: 0
config:
# disks
- id: ssd0
type: disk
path: /dev/sda
match:
ssd: yes
ptable: gpt
grub_device: true
wipe: superblock
# partitions
- id: ssd0p1_bios_grub
type: partition
device: ssd0
number: 1
size: 1M
flag: bios_grub
- id: ssd0p2_swap
type: partition
device: ssd0
number: 2
size: 4G
flag: swap
- id: ssd0p3_root
type: partition
device: ssd0
number: 3
size: 90G
# formats
- id: fs_swap
type: format
volume: ssd0p2_swap
fstype: swap
- id: fs_root
type: format
volume: ssd0p3_root
fstype: btrfs
# mounts
- id: mnt_swap
type: mount
device: fs_swap
path: none
- id: mnt_root
type: mount
device: fs_root
path: /
late-commands:
- |
set -ex
# getting info
ROOT_DEV="$(awk '$2 ~ "^/target$" { print $1 }' /proc/mounts)"
ROOT_UUID="$(blkid -o value "${ROOT_DEV}" | head -n 1)"
# unmount current installed system
awk '$2 ~ "^/target/" { print $2 }' /proc/mounts | xargs umount
# migrate top-level subvolume to @
btrfs subvolume create /target/@
find /target -mindepth 1 -maxdepth 1 -not -name @ -exec mv '{}' /target/@/ \;
# prepare sub-volume @ as the new /
mkdir /mnt/@
mount -o subvol=@ "${ROOT_DEV}" /mnt/@
sed -i "/${ROOT_UUID}/s/defaults/defaults,noatime,subvol=@/" /mnt/@/etc/fstab
# prepare sub-volume @home as the new /home
btrfs subvolume create /target/@home
echo "/dev/disk/by-uuid/${ROOT_UUID} /home btrfs defaults,noatime,subvol=@home 0 0" >> /mnt/@/etc/fstab
# prepare new root for chroot
mount -o bind /dev /mnt/@/dev
mount -o bind /sys /mnt/@/sys
mount -o bind /proc /mnt/@/proc
# re-configure boot related stuff
chroot /mnt/@ update-grub
chroot /mnt/@ grub-install /dev/sda
#cloud-config
# based on
# * https://ubuntu.com/server/docs/install/autoinstall-quickstart
# * https://ubuntu.com/server/docs/install/autoinstall-reference
# * https://curtin.readthedocs.io/en/latest/topics/storage.html
# * https://gist.github.com/wpbrown/b688a934339cb4228c3faf5b527fbe5b
autoinstall:
version: 1
identity:
hostname: autoinstalled
password: "$6$exDY1mhS4KUYCE/2$zmn9ToZwTKLhCw.b4/b.ZRTIZM30JZ4QrOQ2aOXJ8yk96xpcCof0kxKwuX1kqLG/ygbJ1f8wxED22bTL4F46P0"
username: foo
locale: en_US.UTF-8
layout: us
ssh:
install-server: true
# early-commands:
# - "blkdiscard /dev/sda || true"
storage:
version: 1
swap:
size: 0
config:
# disks
- id: ssd0
type: disk
path: /dev/sda
match:
ssd: yes
ptable: gpt
grub_device: true
wipe: superblock-recursive
# preserve: true
# partitions
- id: ssd0p1_bios_grub
type: partition
device: ssd0
number: 1
size: 1M
flag: bios_grub
- id: ssd0p2_swap
type: partition
device: ssd0
number: 2
size: 4G
flag: swap
- id: ssd0p3_lvm
type: partition
device: ssd0
number: 3
size: 90G
#preserve: true
# lvm: volume groups
- id: vg_ssd
type: lvm_volgroup
name: tia_server_ssd
devices:
- ssd0p3_lvm
#preserve: true
# lvm: logical volumes
- id: lv_root
type: lvm_partition
volgroup: vg_ssd
name: root
size: 30G
- id: lv_datastore
type: lvm_partition
volgroup: vg_ssd
name: datastore
size: 55G
#preserve: true
# formats
- id: fs_swap
type: format
volume: ssd0p2_swap
fstype: swap
- id: fs_root
type: format
volume: lv_root
fstype: btrfs
- id: fs_datastore
type: format
volume: lv_datastore
fstype: btrfs
#preserve: true
# mounts
- id: mnt_swap
type: mount
device: fs_swap
path: none
- id: mnt_root
type: mount
device: fs_root
path: /
late-commands:
- | # lv_root
set -ex
DEV_PATH='/dev/mapper/tia_server_ssd-root'
# unmount current installed system
awk '$2 ~ "^/target/" { print $2 }' /proc/mounts | xargs umount
# migrate top-level sub-volume to @
btrfs subvolume create /target/@
find /target -mindepth 1 -maxdepth 1 -not -name @ -exec mv '{}' /target/@/ \;
sed "s|^.* / btrfs .*$|${DEV_PATH} / btrfs defaults,noatime,subvol=@ 0 0|" -i /target/@/etc/fstab
# create sub-volume @home for /home
btrfs subvolume create /target/@home
echo "${DEV_PATH} /home btrfs defaults,noatime,subvol=@home 0 0" >> /target/@/etc/fstab
# prepare new root for chroot
mkdir /mnt/lv_root
mount -o subvol=@ "${DEV_PATH}" /mnt/lv_root
mount -o bind /dev /mnt/lv_root/dev
mount -o bind /sys /mnt/lv_root/sys
mount -o bind /proc /mnt/lv_root/proc
# re-configure boot related stuff
chroot /mnt/lv_root update-grub
chroot /mnt/lv_root grub-install /dev/sda
# unmount chroot system
umount -R /mnt/lv_root
- | # lv_datastore
set -ex
DEV_PATH='/dev/mapper/tia_server_ssd-datastore'
# mount top-level sub-volume
mkdir /mnt/lv_datastore
mount "${DEV_PATH}" /mnt/lv_datastore
# enable quota
btrfs quota enable /mnt/lv_datastore
# initialize sub-volumes (if necessary)
if [ ! -e /mnt/lv_datastore/@system ]; then
btrfs subvolume create /mnt/lv_datastore/@system
btrfs qgroup limit 25G /mnt/lv_datastore/@system
fi
if [ ! -e /mnt/lv_datastore/@private ]; then
btrfs subvolume create /mnt/lv_datastore/@private
btrfs qgroup limit 20G /mnt/lv_datastore/@private
fi
# unmount datastore
umount /mnt/lv_datastore
#cloud-config
# based on
# * https://ubuntu.com/server/docs/install/autoinstall-quickstart
# * https://curtin.readthedocs.io/en/latest/topics/storage.html
autoinstall:
version: 1
identity:
hostname: autoinstalled
password: "$6$exDY1mhS4KUYCE/2$zmn9ToZwTKLhCw.b4/b.ZRTIZM30JZ4QrOQ2aOXJ8yk96xpcCof0kxKwuX1kqLG/ygbJ1f8wxED22bTL4F46P0"
username: foo
locale: en_US.UTF-8
layout: us
ssh:
install-server: true
# early-commands:
# - "blkdiscard /dev/sda || true"
swap:
size: 0
storage:
version: 1
config:
- id: ssd0
type: disk
match:
ssd: yes
ptable: gpt
grub_device: true
wipe: superblock
- id: ssd0p1
type: partition
device: ssd0
number: 1
size: 1M
flag: bios_grub
- id: ssd0p2
type: partition
device: ssd0
number: 2
size: 90G
- id: vg
type: lvm_volgroup
name: server_tia
devices:
- ssd0p2
- id: lv_root
type: lvm_partition
volgroup: vg
name: root
size: 30G
- id: lv_swap
type: lvm_partition
volgroup: vg
name: swap
size: 4G
- id: lv_system
type: lvm_partition
volgroup: vg
name: system
size: 25G
extra_options: ['-b', '4096']
- id: lv_private
type: lvm_partition
volgroup: vg
name: private
size: 20G
extra_options: ['-b', '4096', '-m', '0']
- id: fs_root
type: format
volume: lv_root
fstype: ext4
label: root
- id: fs_swap
type: format
volume: lv_swap
fstype: swap
- id: fs_system
type: format
volume: lv_system
fstype: ext4
label: system
- id: fs_private
type: format
volume: lv_private
fstype: ext4
label: private
- id: mnt_root
type: mount
device: fs_root
path: /
options: 'defaults,discard,noatime,errors=remount-ro'
- id: mnt_system
type: mount
device: fs_system
path: /var/datastore/system
options: 'defaults,discard,noatime,errors=remount-ro'
- id: mnt_private
type: mount
device: fs_private
path: /var/datastore/private
options: 'defaults,discard,noatime,errors=remount-ro'
#cloud-config
autoinstall:
version: 1
identity:
hostname: autoinstalled
password: "$6$exDY1mhS4KUYCE/2$zmn9ToZwTKLhCw.b4/b.ZRTIZM30JZ4QrOQ2aOXJ8yk96xpcCof0kxKwuX1kqLG/ygbJ1f8wxED22bTL4F46P0"
username: foo
storage:
config:
### drive
- id: disk0
type: disk
ptable: gpt
grub_device: true
preserve: false
wipe: superblock
### partitions
- id: efi_partition
type: partition
size: 512MB
device: disk0
flag: boot
grub_device: true
preserve: false
- id: swap_partition
type: partition
size: 8GB
device: disk0
flag: swap
preserve: false
- id: root_partition
type: partition
size: -1
device: disk0
preserve: false
### format and mount efi partition
- id: efi_format
type: format
fstype: fat32
volume: efi_partition
preserve: false
- id: efi_mount
type: mount
path: /boot/efi
device: efi_format
### format and mount swap partition
- id: swap_format
type: format
fstype: swap
volume: swap_partition
preserve: false
- id: swap_mount
path: none
type: mount
device: swap_format
### format and mount root partition
- id: root_format
type: format
fstype: btrfs
volume: root_partition
preserve: false
- id: root_mount
type: mount
path: /
device: root_format
late-commands:
- |
set -ex
EFI_DEV=$(awk '$2 == "/target/boot/efi" { print $1 }' /proc/mounts)
ROOT_DEV=$(awk '$2 ~ "^/target$" { print $1 }' /proc/mounts)
ROOT_UUID=$(blkid -o value $ROOT_DEV | head -n 1)
awk '$2 ~ "^/target/" { print $2 }' /proc/mounts | xargs umount
btrfs subvolume snapshot /target /target/rootfs
mkdir /mnt/rootfs
mount -o subvol=rootfs $ROOT_DEV /mnt/rootfs
mount $EFI_DEV /mnt/rootfs/boot/efi
mount -o bind /dev /mnt/rootfs/dev
mount -o bind /sys /mnt/rootfs/sys
mount -o bind /proc /mnt/rootfs/proc
sed -i "/$ROOT_UUID/s/defaults/defaults,noatime,subvol=rootfs/" /mnt/rootfs/etc/fstab
chroot /mnt/rootfs update-grub
chroot /mnt/rootfs grub-install --efi-directory=/boot/efi
find /target -mindepth 1 -maxdepth 1 -not -name rootfs -exec rm -rf '{}' \;
btrfs subvolume create /target/home
echo "/dev/disk/by-uuid/$ROOT_UUID /home btrfs defaults,noatime,subvol=home 0 0" >> /mnt/rootfs/etc/fstab
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment