在 Hetzner 专用服务器上的 ZFS 根目录上获取 Ubuntu 16.04

在 Hetzner 专用服务器上的 ZFS 根目录上获取 Ubuntu 16.04

现在,Ubuntu 16.04 完全可以在 ZFS 根文件系统上运行。Ubuntu 16.04 默认的软件包管理器中有 ZFS,并且有如下指南,入门并不难。

但是,我见过的所有指南都要求能够从 Ubuntu 安装映像启动。对于 Hetzner 专用服务器来说,这是一个不常见的安装程序,因为它需要工程师访问服务器并插入远程 KVM。

默认情况下,专用服务器启动时会进入救援系统,该系统允许通过其“installiamge”脚本安装各种Linux发行版。但是,此脚本尚不支持ZFS。

如何让 Hetzner 专用服务器在 ZFS 根上运行?

答案1

基本思路是将 Ubuntu 安装在硬盘上的一个小分区上,对硬盘进行分区以使用剩余空间用于 ZFS,然后将安装复制过去。我主要使用本指南关于如何做到这一点的说明。

懒惰,并且有使用 Ansible 的经验?我编写了一些脚本来自动执行这些步骤。它们可以在以下位置找到:https://github.com/tijszwinkels/hetzner-ubuntu-16.04-zfs-root-ansible/blob/master/hetzner-ubuntu-16.04.yml 请注意,这些脚本假定主机已启动到 Hetzner 救援系统,并且它们将首先擦除您的驱动器。使用时风险自负!

# SSH into the host.

# Wipe the drives. Assuming SSDs on 'sda' and 'sdb'.
/sbin/blkdiscard /dev/sda
/sbin/blkdiscard /dev/sdb

# Install Ubuntu 16.04 on a 4G partition using the Hetzner 'installimage' script
/root/.oldroot/nfs/install/installimage -a -n my-hostname -r yes -l 1 -p /:ext4:4G -K /root/.ssh/robot_user_keys -i /root/.oldroot/nfs/install/../images/Ubuntu-1604-xenial-64-minimal.tar.gz

# Reboot the system.
/sbin/shutdown -r now

# Wait for the host to come back up, and SSH in.

# Install the 'parted' parition editor
apt-get update && apt-get install -y parted

# Run parted on the first drive, create a partition in all remaining space. (UNTESTED!)
sudo parted /dev/sda
(parted) mkpart primary 4097MB -1s
(parted) quit

# Run parted on the second drive, create a partition in all remaining space. (UNTESTED!)
sudo parted /dev/sdb
(parted) mkpart primary 4097MB -1s
(parted) quit

# Install required ZFS packages
apt-get install -y zfs-dkms zfs-initramfs

# Create a ZFS pool named 'tank'
# Please note that I'm using the /dev/disk/by-id interface. This is more resilient than /dev/sda and /dev/sdb
zpool create -f -o ashift=13 -O atime=off -O dedup=off -O compression=lz4  tank mirror `ls /dev/disk/by-id/ata-*-part2` 

# Create OS partiton
zfs create tank/os

# Rsync the current system to the new partition.
rsync -a --one-file-system / /tank/os/

# Chroot into the system
cd /tank/os
mount --bind /dev dev
mount --bind /proc proc
mount --bind /sys sys
mount --bind /run run
chroot .

# Install GRUB into the drives
export ZPOOL_VDEV_NAME_PATH=YES
update-grub
grub-install /dev/sda
grub-install /dev/sdb

现在,您应该拥有一台 Hetzner 专用服务器,它可以使用 ZFS 根文件系统顺利启动到 Ubuntu 16.04。祝您好运!

答案2

非常棒的指南,谢谢@TinkerTank。

如果您使用较新的 NVMe 服务器按照本指南,您可能希望使用以下步骤和设备名称:


# Boot into rescue from Robot
# SSH into the host.

# Wipe the drives. Assuming SSDs on 'sda' and 'sdb'.
# For SSD Servers
#/sbin/blkdiscard /dev/sda
#/sbin/blkdiscard /dev/sdb
# For NVMe Servers
/sbin/blkdiscard /dev/nvme0n1
/sbin/blkdiscard /dev/nvme1n1

# Install Ubuntu 16.04 on a 16G partition using the Hetzner 'installimage' script
# For Ubuntu 18.04 it should be this, but FAILS TO BOOT
#/root/.oldroot/nfs/install/installimage -a -n my-hostname -r yes -l 1 -p /:ext4:16G -K /root/.ssh/robot_user_keys -i /root/.oldroot/nfs/install/../images/Ubuntu-1804-bionic-64-minimal.tar.gz
# For Ubuntu 16.04:
/root/.oldroot/nfs/install/installimage -a -n my-hostname -r yes -l 1 -p /:ext4:16G -K /root/.ssh/robot_user_keys -i /root/.oldroot/nfs/install/../images/Ubuntu-1604-xenial-64-minimal.tar.gz
# Press x to continue immediately or wait a few seconds...

# Reboot the system.
#/sbin/shutdown -r now

reboot

# Wait for the host to come back up, and SSH in.
# Update the server
apt update
apt upgrade

# Create a partition on first disk with all the remaining space.
fdisk /dev/nvme0n1
# Press n then accept all defaults and save with w 

# Create a partition on the second disk with all remaining space.
fdisk /dev/nvme1n1
Press n then accept all defaults and save with w 

reboot

# Install required ZFS packages
apt install zfsutils-linux

# Create a ZFS pool named 'tank'
# Please note that I'm using the /dev/disk/by-id interface. This is more resilient than /dev/sda and /dev/sdb
# For SSD servers @hetznet: zpool create -f -o ashift=13 -O atime=off -O dedup=off -O compression=lz4  tank mirror `ls /dev/disk/by-id/ata-*-part2` 
# For NVMe servers:
zpool create -f -o ashift=13 -O atime=off -O dedup=off -O compression=lz4  tank mirror nvme0n1p2 nvme1n1p2

# Create OS partition
zfs create tank/os

# Rsync the current system to the new partition.
rsync -a --one-file-system / /tank/os/

# Chroot into the system
cd /tank/os
mount --bind /dev dev
mount --bind /proc proc
mount --bind /sys sys
mount --bind /run run
chroot .

# Install GRUB into the drives
export ZPOOL_VDEV_NAME_PATH=YES
update-grub
# For SSD Servers:
#grub-install /dev/sda
#grub-install /dev/sdb
# For NVMe servers:
grub-install /dev/nvme0n1p2
grub-install /dev/nvme1n1p2

exit
reboot


现在,您应该拥有一台 Hetzner 专用服务器,它可以使用 ZFS 根文件系统顺利启动到 Ubuntu 16.04。祝您好运!

在 Ubuntu 16.04 上无缝运行,在 Ubuntu 18.04 上无法启动。有人知道原因以及如何解决吗?

注意:我启动初始操作系统时使用的是 16GB 分区,而不是原始教程中指定的 4GB 硬盘空间。较新的服务器至少配备 2 个 512GB NVMe,因此您可以将这 2 个 16 分区转换为 2 个 16GB SWAP 分区,Linux 内核将自行进行条带化(比使用单个 Raid 分区更快)。

相关内容