logo
tt's Note
  • 运维
    • 数据库
    • linux
    • vpn
    • 日志
    • 中间件
    • 服务
    • 监控
    • shell
    • windows
    • 语言
    • 云服务
    • 其他
  • 开发
    • 工具
  • 软件
    • 浏览器
    • 多端
    • win
    • mac
    • 网站
  • 项目
    • 效率工具
    • 兴趣
  • 脚本
    • jenkins
    其他概览
    pve
    服务器购买
    上一页其他概览下一页服务器购买

    #pve

    #初始化

    # 修改 pve 源
    
    mv /etc/apt/sources.list /etc/apt/sources.list.bak
    cat << EOF > /etc/apt/sources.list
    # 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释
    deb https://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm main contrib non-free non-free-firmware
    # deb-src https://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm main contrib non-free non-free-firmware
    
    deb https://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm-updates main contrib non-free non-free-firmware
    # deb-src https://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm-updates main contrib non-free non-free-firmware
    
    deb https://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm-backports main contrib non-free non-free-firmware
    # deb-src https://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm-backports main contrib non-free non-free-firmware
    
    # 以下安全更新软件源包含了官方源与镜像站配置,如有需要可自行修改注释切换
    # deb https://security.debian.org/debian-security bookworm-security main contrib non-free non-free-firmware
    # deb-src https://security.debian.org/debian-security bookworm-security main contrib non-free non-free-firmware
    EOF
    
    sed -i 's|http://download.proxmox.com|https://mirrors.ustc.edu.cn/proxmox|g' /usr/share/perl5/PVE/APLInfo.pm
    
    mv /etc/apt/sources.list.d/pve-enterprise.list /etc/apt/sources.list.d/pve-enterprise.list.bak
    
    cat << EOF > /etc/apt/sources.list.d/pve-no-subscription.list
    deb http://mirrors.ustc.edu.cn/proxmox/debian bookworm pve-no-subscription
    EOF
    
    apt update
    # apt upgrade -y
    
    systemctl restart pvedaemon.service
    
    # 时间同步
    # 查看可用时区
    timedatectl set-timezone "Asia/Shanghai"
    
    apt install -y systemd-timesyncd
    
    cat << EOF > /etc/systemd/timesyncd.conf
    #  This file is part of systemd.
    #
    #  systemd is free software; you can redistribute it and/or modify it under the
    #  terms of the GNU Lesser General Public License as published by the Free
    #  Software Foundation; either version 2.1 of the License, or (at your option)
    #  any later version.
    #
    # Entries in this file show the compile time defaults. Local configuration
    # should be created by either modifying this file, or by creating "drop-ins" in
    # the timesyncd.conf.d/ subdirectory. The latter is generally recommended.
    # Defaults can be restored by simply deleting this file and all drop-ins.
    #
    # See timesyncd.conf(5) for details.
    
    [Time]
    #NTP=
    #FallbackNTP=0.debian.pool.ntp.org 1.debian.pool.ntp.org 2.debian.pool.ntp.org 3.debian.pool.ntp.org
    #RootDistanceMaxSec=5
    #PollIntervalMinSec=32
    #PollIntervalMaxSec=2048
    #ConnectionRetrySec=30
    #SaveIntervalSec=60
    NTP=ntp.ntsc.ac.cn cn.ntp.org.cn
    EOF
    
    timedatectl set-ntp true
    systemctl restart systemd-timesyncd
    systemctl enable systemd-timesyncd

    #删除 local-lvm合并至 local分区

    lvremove pve/data
    lvextend -rl +100%FREE pve/root
    resize2fs /dev/mapper/pve-root
    
    # 然后网页上删除local-lvm

    #硬盘直通

    ls -i /dev/disk/by-id/
    
    # 找到想要的id
    qm set 100 --ide1 /dev/disk/by-id/ata-ST2000LM015-2E8174_ZDZ1KP7W

    #复制虚拟机

    #!/bin/bash
    
    # 模板 VM ID
    TEMPLATE_ID=101
    # 需要克隆的pve虚拟机个数
    CLONE_COUNT=$1
    # 虚拟机当前已经存在的最大编号,分配的ip与该字段有关
    PVE_ID=$2
    
    if [ ! -n "$CLONE_COUNT" ] || [ ! -n "$PVE_ID" ]; then
      echo "CLONE_COUNT 或 PVE_ID 为空"
      exit 1
    fi
    
    # 获取当前最大 VM ID
    current_max_id=$(pvesh get /cluster/resources --type vm |awk -F'qemu' '{print $2}'|awk '{print $1}'|awk -F'/' '{print $2}'|awk 'NF'|tail -n 1)
    
    # 开始批量克隆
    for i in $(seq 1 "$CLONE_COUNT"); do
    # 自增获取新的 VM ID
    new_id=$((current_max_id + i))
    new_pve_id=$((PVE_ID + i))
    
        # 克隆虚拟机
        qm clone "$TEMPLATE_ID" "$new_id" --name "vm-${new_pve_id}" --full
        echo "克隆完成:新虚拟机 ID 为 $new_id"
    
        # 为虚拟机新增一个 20GB 的硬盘设备
        #qm set "$new_id" --scsi1 local-lvm:20G
        #echo "已为虚拟机 $new_id 添加新的 20GB 硬盘设备"
    
        # 获取 MAC 地址
        mac_address=$(qm config "$new_id" | grep -i "net0" | grep -oE "([[:xdigit:]]{2}:){5}[[:xdigit:]]{2}")
        if [ -z "$mac_address" ];then
          echo "请检查虚拟机${new_id}是否创建"
          exit 0
        fi
    
    done
    

    #不使用swap

    echo "vm.swappiness = 0" >> /etc/sysctl.conf
    sysctl -w vm.swappiness=0