Debian11基于zfs安装PVE7

发布于 2023-09-13  993 次阅读


内容纲要

第一步 :准备安装环境

  1. 启动 Debian GNU/Linux Live CD,使用用户名 user和密码live登录,连接网络。
  2. 设置和更新存储库
    sudo vi /etc/apt/sources.list
    # 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释
    deb http://mirrors.tuna.tsinghua.edu.cn/debian/ bullseye main contrib non-free
    # deb-src http://mirrors.tuna.tsinghua.edu.cn/debian/ bullseye main contrib non-free
    
    deb http://mirrors.tuna.tsinghua.edu.cn/debian/ bullseye-updates main contrib non-free
    # deb-src http://mirrors.tuna.tsinghua.edu.cn/debian/ bullseye-updates main contrib non-free
    
    deb http://mirrors.tuna.tsinghua.edu.cn/debian/ bullseye-backports main contrib non-free
    # deb-src http://mirrors.tuna.tsinghua.edu.cn/debian/ bullseye-backports main contrib non-free
    
    deb http://mirrors.tuna.tsinghua.edu.cn/debian-security bullseye-security main contrib non-free
    # deb-src http://mirrors.tuna.tsinghua.edu.cn/debian-security bullseye-security main contrib non-free
    
    # deb http://security.debian.org/debian-security bullseye-security main contrib non-free
    # # deb-src http://security.debian.org/debian-security bullseye-security main contrib non-free
    sudo apt update
  3. (可选) :在 Live CD 环境中安装并启动 OpenSSH 服务器

    这样进行后续配置会非常方便

    sudo apt install --yes openssh-server
    sudo sed -i '/^#PermitRootLogin/aPermitRootLogin yes' /etc/ssh/sshd_config
    sudo systemctl restart ssh

    修改root密码,直接使用root进行登录

    sudo passwd root

    在使用ssh登录之前,建议将debian的自动休眠关闭

  4. 禁用自动挂载

    如果磁盘之前已被使用过(分区具有相同的偏移量),则以前的文件系统(例如 ESP)将在未禁用的情况下自动挂载

    gsettings set org.gnome.desktop.media-handling automount false
  5. 切换为root用户
    su - root
  6. 在 Live CD 环境中安装 ZFS
    apt install --yes debootstrap gdisk zfsutils-linux

第二步 :磁盘格式化

  1. 使用磁盘名称设置变量
    DISK1=/dev/disk/by-id/nvme-Fanxiang_S790_2TB_FXS790232410610;
    DISK3=/dev/disk/by-id/nvme-Fanxiang_S790_2TB_FXS790232410612;
    DISK2=/dev/disk/by-id/nvme-Fanxiang_S790_2TB_FXS790232410652;
    DISK4=/dev/disk/by-id/nvme-Fanxiang_S790_2TB_FXS790232420604;
    DISK5=/dev/disk/by-id/nvme-Fanxiang_S790_2TB_FXS790232420607;
    DISK0=/dev/disk/by-id/nvme-Fanxiang_S790_2TB_FXS790232420611;

    始终对 ZFS 使用长/dev/disk/by-id/*别名。直接使用 /dev/sd*设备节点可能会导致偶发性导入失败,尤其是在具有多个存储池的系统上

    提示:

    • ls -la /dev/disk/by-id将列出别名
    • 选择启动池大小时,请考虑如何使用该空间。一个内核和initrd可能消耗100M左右。如果您有多个内核并拍摄快照,您可能会发现启动池空间不足,特别是如果您需要重新生成 initramfs 映像(每个映像可能约为 85M)。根据您的需要适当调整启动池的大小
    • 对于镜像或 raidz 拓扑,请使用DISK1DISK2等。
  2. 如果您要重复使用磁盘,请根据需要清除它

    确保交换分区未被使用

    swapoff --all

    如果该磁盘以前与 zfs 一起使用过

    wipefs -a $DISK0 
    wipefs -a $DISK1
    wipefs -a $DISK2
    wipefs -a $DISK3
    wipefs -a $DISK4
    wipefs -a $DISK5

    一旦擦除分区信息所有数据会丢失

    对于基于闪存的存储,如果磁盘以前被使用过,您可能希望进行全盘丢弃(TRIM/UNMAP),这可以提高性能

    blkdiscard -f  $DISK0 $DISK1 $DISK2 $DISK3 $DISK4 $DISK5

    清除分区表

    sgdisk --zap-all $DISK0 $DISK1 $DISK2 $DISK3 $DISK4 $DISK5

    如果您收到有关内核仍在使用旧分区表的消息,请重新启动并重新开始

  3. 对磁盘进行分区
    sgdisk -a1 -n1:24K:+1000K -t1:EF02 $DISK0;
    sgdisk -a1 -n1:24K:+1000K -t1:EF02 $DISK1;
    sgdisk -a1 -n1:24K:+1000K -t1:EF02 $DISK2;
    sgdisk -a1 -n1:24K:+1000K -t1:EF02 $DISK3;
    sgdisk -a1 -n1:24K:+1000K -t1:EF02 $DISK4;
    sgdisk -a1 -n1:24K:+1000K -t1:EF02 $DISK5;
    
    sgdisk     -n2:1M:+512M   -t2:EF00 $DISK0;
    sgdisk     -n2:1M:+512M   -t2:EF00 $DISK1;
    sgdisk     -n2:1M:+512M   -t2:EF00 $DISK2;
    sgdisk     -n2:1M:+512M   -t2:EF00 $DISK3;
    sgdisk     -n2:1M:+512M   -t2:EF00 $DISK4;
    sgdisk     -n2:1M:+512M   -t2:EF00 $DISK5;
    
    sgdisk     -n3:0:+1G      -t3:BF01  $DISK0;
    sgdisk     -n3:0:+1G      -t3:BF01  $DISK1;
    sgdisk     -n3:0:+1G      -t3:BF01  $DISK2;
    sgdisk     -n3:0:+1G      -t3:BF01  $DISK3;
    sgdisk     -n3:0:+1G      -t3:BF01  $DISK4;
    sgdisk     -n3:0:+1G      -t3:BF01  $DISK5;
    
    sgdisk     -n4:0:+40G        -t4:BF00 $DISK0;
    sgdisk     -n4:0:+40G        -t4:BF00 $DISK1;
    sgdisk     -n4:0:+40G        -t4:BF00 $DISK2;
    sgdisk     -n4:0:+40G        -t4:BF00 $DISK3;
    sgdisk     -n4:0:+40G        -t4:BF00 $DISK4;
    sgdisk     -n4:0:+40G        -t4:BF00 $DISK5;
    
    sgdisk     -n5:0:0        -t5:BF00 $DISK0;
    sgdisk     -n5:0:0        -t5:BF00 $DISK1;
    sgdisk     -n5:0:0        -t5:BF00 $DISK2;
    sgdisk     -n5:0:0        -t5:BF00 $DISK3;
    sgdisk     -n5:0:0        -t5:BF00 $DISK4;
    sgdisk     -n5:0:0        -t5:BF00 $DISK5;
  4. 创建启动池
    zpool create 
       -o ashift=12 
       -o autotrim=on -d 
       -o cachefile=/etc/zfs/zpool.cache 
       -o feature@async_destroy=enabled 
       -o feature@bookmarks=enabled 
       -o feature@embedded_data=enabled 
       -o feature@empty_bpobj=enabled 
       -o feature@enabled_txg=enabled 
       -o feature@extensible_dataset=enabled 
       -o feature@filesystem_limits=enabled 
       -o feature@hole_birth=enabled 
       -o feature@large_blocks=enabled 
       -o feature@livelist=enabled 
       -o feature@lz4_compress=enabled 
       -o feature@spacemap_histogram=enabled 
       -o feature@zpool_checkpoint=enabled 
       -O devices=off 
       -O acltype=posixacl -O xattr=sa 
       -O compression=lz4 
       -O normalization=formD 
       -O relatime=on 
       -O canmount=off -O mountpoint=/boot -R /mnt 
       bpool raidz ${DISK0}-part3 ${DISK1}-part3 ${DISK2}-part3 ${DISK3}-part3 ${DISK4}-part3 ${DISK5}-part3

    GRUB 不支持所有 zpool 功能。请参阅spa_feature_names grub -core/fs/zfs/zfs.c。此步骤创建一个单独的引导池,/boot其功能仅限于 GRUB 支持的功能,从而允许根池使用任何/所有功能。请注意,GRUB 以只读方式打开池,因此 GRUB “支持”所有只读兼容功能。

  5. 创建根池
    zpool create 
       -o ashift=12 
       -o autotrim=on 
       -O acltype=posixacl -O xattr=sa -O dnodesize=auto 
       -O compression=lz4 
       -O normalization=formD 
       -O relatime=on 
       -O canmount=off -O mountpoint=/ -R /mnt 
       rpool raidz ${DISK0}-part4 ${DISK1}-part4 ${DISK2}-part4 ${DISK3}-part4 ${DISK4}-part4 ${DISK5}-part4
  6. 创建数据池
    zpool create 
       -o ashift=12 
       -o autotrim=on 
       -O acltype=posixacl -O xattr=sa -O dnodesize=auto 
       -O compression=lz4 
       -O normalization=formD 
       -O relatime=on 
       -O canmount=off -O mountpoint=/data -R /mnt  
       data raidz ${DISK0}-part5 ${DISK1}-part5 ${DISK2}-part5 ${DISK3}-part5 ${DISK4}-part5 ${DISK5}-part5

第三步 :系统安装

  1. 创建文件系统数据集作为容器
    zfs create -o canmount=off -o mountpoint=none rpool/ROOT
    zfs create -o canmount=off -o mountpoint=none bpool/BOOT
  2. 为根文件系统和引导文件系统创建文件系统数据集
    zfs create -o canmount=noauto -o mountpoint=/      rpool/ROOT/debian
    zfs create -o canmount=noauto -o mountpoint=/data  data
    zfs mount rpool/ROOT/debian
  3. 创建数据集
    zfs create                     rpool/home
    zfs create -o mountpoint=/root rpool/home/root
    chmod 700 /mnt/root
    zfs create -o canmount=off     rpool/var
    zfs create -o canmount=off     rpool/var/lib
    zfs create                     rpool/var/log
    zfs create                     rpool/var/spool
    
    zfs create -o com.sun:auto-snapshot=false rpool/var/cache
    zfs create -o com.sun:auto-snapshot=false rpool/var/lib/nfs
    zfs create -o com.sun:auto-snapshot=false rpool/var/tmp
    chmod 1777 /mnt/var/tmp
    
    zfs create -o com.sun:auto-snapshot=false rpool/var/lib/docker
  4. 在 /run 挂载 tmpfs
    mkdir /mnt/run
    mount -t tmpfs tmpfs /mnt/run
    mkdir /mnt/run/lock
  5. 安装最小系统
    debootstrap bullseye /mnt
  6. 复制到 zpool.cache 中
    mkdir /mnt/etc/zfs
    cp /etc/zfs/zpool.cache /mnt/etc/zfs/

第四步 :系统配置

  1. 配置主机名
    hostname pve.theever.top
    hostname > /mnt/etc/hostname
    vi /mnt/etc/hosts

    添加下面这一行

    127.0.1.1      pve.theever.top 
  2. 配置网络接口

    找到接口名称

    ip addr show

    调整NAME以下内容以匹配您的接口名称,enp15s0为作者设备接口名称,请自行更换

    vi /mnt/etc/network/interfaces.d/enp15s0

    添加以下内容,enp15s0为作者设备接口名称,请自行更换

    auto enp15s0
    iface enp15s0 inet static
    address 192.168.31.10
    netmask 255.255.255.0
    gateway 192.168.31.1
    dns-nameservers 192.168.31.1
  3. 配置包源
    vi /mnt/etc/apt/sources.list
    # 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释
    deb http://mirrors.tuna.tsinghua.edu.cn/debian/ bullseye main contrib non-free
    # deb-src http://mirrors.tuna.tsinghua.edu.cn/debian/ bullseye main contrib non-free
    
    deb http://mirrors.tuna.tsinghua.edu.cn/debian/ bullseye-updates main contrib non-free
    # deb-src http://mirrors.tuna.tsinghua.edu.cn/debian/ bullseye-updates main contrib non-free
    
    deb http://mirrors.tuna.tsinghua.edu.cn/debian/ bullseye-backports main contrib non-free
    # deb-src http://mirrors.tuna.tsinghua.edu.cn/debian/ bullseye-backports main contrib non-free
    
    deb http://mirrors.tuna.tsinghua.edu.cn/debian-security bullseye-security main contrib non-free
    # deb-src http://mirrors.tuna.tsinghua.edu.cn/debian-security bullseye-security main contrib non-free
    
    # deb http://security.debian.org/debian-security bullseye-security main contrib non-free
    # # deb-src http://security.debian.org/debian-security bullseye-security main contrib non-free
  4. 将虚拟文件系统从 LiveCD 环境绑定到新系统并绑定chroot到其中
    mount --make-private --rbind /dev  /mnt/dev
    mount --make-private --rbind /proc /mnt/proc
    mount --make-private --rbind /sys  /mnt/sys

    这里使用DISK0

    chroot /mnt /usr/bin/env DISK=$DISK0 bash --login
  5. 配置基本系统环境
    ln -s /proc/self/mounts /etc/mtab
    apt update
    
    apt install --yes console-setup locales

    即使您更喜欢非英语系统语言,也请始终确保该语言 en_US.UTF-8可用

    dpkg-reconfigure locales tzdata keyboard-configuration console-setup
  6. 在新系统的 chroot 环境中安装 ZFS
    apt install --yes dpkg-dev linux-headers-generic linux-image-generic
    
    apt install --yes zfs-initramfs
    
    echo REMAKE_INITRD=yes > /etc/dkms/zfs.conf

    注意:忽略任何显示和的错误消息

  7. 安装GRUB

    安装 GRUB 以进行 UEFI 引导

    apt install dosfstools
    
    mkdosfs -F 32 -s 1 -n EFI ${DISK0}-part2
    mkdir /boot/efi
    echo /dev/disk/by-uuid/$(blkid -s UUID -o value ${DISK0}-part2) 
      /boot/efi vfat defaults 0 0 >> /etc/fstab
    mount /boot/efi
    apt install --yes grub-efi-amd64 shim-signed

    笔记:

    • 对于镜像或 raidz 拓扑,此步骤仅在第一个磁盘上安装 GRUB。其他磁盘将在稍后处理
    • 如果提示无法找到DISK0,请重新执行
      第二步 :使用磁盘名称设置变量
  8. 可选:删除 os-prober
    apt purge --yes os-prober

    这可以避免来自update-grub 的错误消息。 os-prober仅在双引导配置中才是必需的

  9. 设置root密码
    passwd
  10. 启用导入 bpool

    这确保了bpool始终被导入,无论是否 /etc/zfs/zpool.cache存在、是否在缓存文件中、或者是否zfs-import-scan.service启用

    vi /etc/systemd/system/zfs-import-bpool.service
    [Unit]
    DefaultDependencies=no
    Before=zfs-import-scan.service
    Before=zfs-import-cache.service
    
    [Service]
    Type=oneshot
    RemainAfterExit=yes
    ExecStart=/sbin/zpool import -N -o cachefile=none bpool
    # Work-around to preserve zpool cache:
    ExecStartPre=-/bin/mv /etc/zfs/zpool.cache /etc/zfs/preboot_zpool.cache
    ExecStartPost=-/bin/mv /etc/zfs/preboot_zpool.cache /etc/zfs/zpool.cache
    
    [Install]
    WantedBy=zfs-import.target
    systemctl enable zfs-import-bpool.service
  11. 可选(但推荐):挂载 tmpfs/tmp
    cp /usr/share/systemd/tmp.mount /etc/systemd/system/
    systemctl enable tmp.mount
  12. 可选:安装 SSH
    apt install --yes openssh-server
    
    vi /etc/ssh/sshd_config
    # Set: PermitRootLogin yes

第五步 :GRUB安装

  1. 验证 ZFS 引导文件系统是否被识别
    grub-probe /boot
  2. 刷新 initrd 文件
    update-initramfs -c -k all

    注意:忽略任何显示和的错误消息

  3. GRUB 缺少 zpool-features 支持的解决方法
    vi /etc/default/grub
    # Set: GRUB_CMDLINE_LINUX="root=ZFS=rpool/ROOT/debian"
  4. 可选(但强烈推荐):使调试 GRUB 更容易
    vi /etc/default/grub
    # Remove quiet from: GRUB_CMDLINE_LINUX_DEFAULT
    # Uncomment: GRUB_TERMINAL=console
    # Save and quit.

    稍后,一旦系统重新启动两次并且您确定一切正常,您可以根据需要撤消这些更改

  5. 更新启动配置
    update-grub

    注意:忽略来自 的错误osprober(如果存在)

  6. 安装引导加载程序

    对于 UEFI 引导,将 GRUB 安装到 ESP

    grub-install --target=x86_64-efi --efi-directory=/boot/efi 
       --bootloader-id=debian --recheck --no-floppy

    此处无需指定磁盘。如果您要创建镜像或 raidz 拓扑,则将稍后处理其他磁盘

  7. 修复文件系统挂载顺序

    我们需要激活zfs-mount-generator. 这使得 systemd 知道单独的挂载点,这对于/var/log 和之类的东西很重要/var/tmp。反过来,rsyslog.service依赖于使用systemd 自动使用功能var-log.mount 的方式local-fs.target和服务。PrivateTmp``After=var-tmp.mount

    mkdir /etc/zfs/zfs-list.cache
    touch /etc/zfs/zfs-list.cache/bpool
    touch /etc/zfs/zfs-list.cache/rpool
    zed -F &

    通过确保这些不为空来验证是否zed更新了缓存

    cat /etc/zfs/zfs-list.cache/bpool
    cat /etc/zfs/zfs-list.cache/rpool

    如果其中一个为空,则强制更新缓存并再次检查

    zfs set canmount=on     bpool/BOOT/debian
    zfs set canmount=noauto rpool/ROOT/debian

    如果它们仍然为空,请停止 zed(如下),启动 zed(如上所述),然后重试

    一旦文件有数据,停止zed

    fg
    Press Ctrl-C

    修复要消除的路径/mnt

    sed -Ei "s|/mnt/?|/|" /etc/zfs/zfs-list.cache/*

第六步 :首次启动

  1. 可选:初始安装快照
    zfs snapshot bpool/BOOT/debian@install
    zfs snapshot rpool/ROOT/debian@install

    将来,您可能希望在每次升级之前拍摄快照,并在某个时候删除旧快照(包括此快照)以节省空间

  2. chroot环境退出回到LiveCD环境
    exit
  3. 在 LiveCD 环境中运行以下命令以卸载所有文件系统
    mount | grep -v zfs | tac | awk '//mnt/ {print $3}' | 
       xargs -i{} umount -lf {}
    zpool export -a
  4. 如果 rpool 失败,则在启动时安装它将会失败,您需要在 initamfs 提示符中执行zpool import -f rpool , 然后exit
  5. 重启
    reboot

    等待新安装的系统正常启动。以 root 身份登录

  6. 配置zfs内存使用

    默认情况下, ZFS 将50%的主机内存用于自适应替换缓存(ARC)。为 ARC 分配足够的内存对于 IO 性能至关重要,因此请谨慎减少内存。根据一般经验,至少分配2 GiB Base + 1 GiB/TiB-Storage。例如,如果您有一个具有 8 TiB可用存储空间的池,那么您应该为 ARC使用10 GiB内存

    请将12修改为自己主机的参数

    echo "$[12 * 1024*1024*1024]" >/sys/module/zfs/parameters/zfs_arc_max
  7. 可选:删除初始安装的快照
    sudo zfs destroy bpool/BOOT/debian@install
    sudo zfs destroy rpool/ROOT/debian@install

第七步 :安装PVE

  1. 安装pve仓库源
    echo "deb http://mirrors.tuna.tsinghua.edu.cn/proxmox/debian/pve bullseye pve-no-subscription" > /etc/apt/sources.list.d/pve-no-subscription.list
  2. 添加Proxmox VE 存储库密钥
    wget https://enterprise.proxmox.com/debian/proxmox-release-bullseye.gpg -O /etc/apt/trusted.gpg.d/proxmox-release-bullseye.gpg
  3. 更新您的存储库和系统
    apt update && apt full-upgrade
  4. 安装Proxmox VE 内核
    apt install pve-kernel-5.15
    
    systemctl reboot
  5. 安装Proxmox VE 包
    apt install proxmox-ve postfix open-iscsi
  6. 移除debian内核
    apt remove linux-image-amd64 'linux-image-5.10*'
  7. 更新并检查 grub2 配置
    update-grub
  8. 镜像GRUB

    如果安装到多个磁盘,请在其他磁盘上安装 GRUB

    对于 UEFI 启动

    umount /boot/efi

    对于第二个及后续磁盘(将 debian-2 增加到 -3 等)

    dd if=${DISK0}-part2 
      of=${DISK1}-part2
    dd if=${DISK0}-part2 
      of=${DISK2}-part2
    dd if=${DISK0}-part2 
      of=${DISK3}-part2
    dd if=${DISK0}-part2 
      of=${DISK4}-part2
    dd if=${DISK0}-part2 
      of=${DISK5}-part2
    efibootmgr -c -g -d ${DISK1}-part2 
       -p 2 -L "debian-2" -l 'EFIdebiangrubx64.efi'
    efibootmgr -c -g -d ${DISK2}-part2 
       -p 2 -L "debian-3" -l 'EFIdebiangrubx64.efi'
    efibootmgr -c -g -d ${DISK3}-part2 
       -p 2 -L "debian-4" -l 'EFIdebiangrubx64.efi'
    efibootmgr -c -g -d ${DISK4}-part2 
       -p 2 -L "debian-5" -l 'EFIdebiangrubx64.efi'
    efibootmgr -c -g -d ${DISK5}-part2 
       -p 2 -L "debian-6" -l 'EFIdebiangrubx64.efi'
    
    mount /boot/efi
  9. 可选(但建议):删除 os-prober 软件包
    apt remove os-prober

第八步 :配置PVE

  1. 连接到 Proxmox VE 网络界面

    连接到管理 Web 界面 ( https://your-ip-address:8006)。如果您是全新安装且尚未添加任何用户,则应选择 PAM 身份验证领域并使用root用户帐户登录

  2. 创建Linux网桥

    登录后,创建一个名为vmbr0Linux 网桥,并向其中添加您的第一个网络接口,网络接口名称同上

  3. 重启该服务器
最后更新于 2023-09-13