PVE 制作ubuntu 1804 模板

Posted by Ksd on February 22, 2021

前言

初学pve,搭建方式非常简单,但创建vm时默认使用iso,这样使用起来比较麻烦,还是喜欢使用OpenStack那种直接使用镜像去创建vm的感觉。 研究了下,在pve中可以通过模板去创建vm,个人理解和vmware是类似,所以总结了创建ubuntu 1804 模板的过程

制作模板

下载 ubuntu 模板到pve主机上

下载地址:https://releases.ubuntu.com/18.04/

创建vm,导入镜像

#!/bin/bash

VM_NAME=ubuntu-bionic-server-template
VM_ID=9006
VM_CORES=2
VM_MEM=4096
VM_IMAGE=bionic-server-cloudimg-amd64.img
VM_DISK=40G

qm create $VM_ID --cores $VM_CORES --memory $VM_MEM --name $VM_NAME --net0 virtio,bridge=vmbr0

# 导入下载的镜像到local-lvm 存储空间
qm importdisk $VM_ID $VM_IMAGE local-lvm

# 将导入的磁盘以 scsi 方式挂载到虚拟机上面
qm set $VM_ID --scsihw virtio-scsi-pci --scsi0 local-lvm:vm-$VM_ID-disk-0

# 添加 Cloud-Init CDROM 驱动(必须添加这个vm才能启动cloud-init)
qm set $VM_ID --ide2 local-lvm:cloudinit

# resize 磁盘
qm resize $VM_ID scsi0 $VM_DISK

# 设置启动
qm set $VM_ID --boot c --bootdisk scsi0

设置 cloud-init

开机,修改虚拟机相关配置

  1. 修改时区:
    timedatectl set-timezone Asia/Shanghai
    
  2. 修改apt 源为阿里源
    cat /etc/apt/sources.list
    deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
    deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
    deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
    deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
    deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
    deb-src http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
    deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
    deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
    deb-src http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
    deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
    
  3. 注释掉apt 的配置,否则会继续沿用默认的apt配置
    vi /etc/cloud/cloud.cfg
    # - apt-configure
    
  4. 清除hostname
    truncate -s0 /etc/hostname
    
  5. 修改/etc/issue,将IP显示在console ``` vi /etc/issue IP: \4{eth0} # or IP: \4

5. 停用apt自动更新

systemctl mask apt-daily.service apt-daily-upgrade.service


6. 清理cloud-init

cloud-init clean && rm -rf /var/lib/cloud/*


7. 清空`/etc/machine-id`
  18.04默认使用/etc/machine-id作为 dhcp identifier,如果不清空,使用相同模板创建的vm,对应的IP是相同的。

echo -n > /etc/machine-id

  > 参考:https://superuser.com/questions/1338510/wrong-ip-address-from-dhcp-client-on-ubuntu-18-04

8. 清理残留log,清理bash history

apt clean cat /dev/null > ~/.bash_history && history -c history -w


9. 关机

shutdown -h now


#### 生成模板

qm template 9000 ```

接下来就可以利用模板 通过克隆出vm了