Proxmox cloud-init で Ubuntu 20.04 を起動する
Proxmox cloud-init で Ubuntu 20.04 を起動する
- Ref
準備
イメージの修正の所で使うのでインストールしておく
apt-get install libguestfs-tools
次にエディターを nano に設定する
printenv | grep EDITOR
export EDITOR=nano
イメージの修正
cloud-init を利用してパッケージなどがすでに入った状態のイメージを作成したいため、ダウンロードしたイメージを編集する
今回は Ubuntu 20.04 LTS を使うためダウンロードする
wget https://cloud-images.ubuntu.com/focal/current/focal-server-cloudimg-amd64.img
cp -av focal-server-cloudimg-amd64.img ubuntu-20.04.img
以下のファイルを作成した
やってること
- history の設定を変更
- apt/source.list を
jp.archive.ubuntu.com
に向ける - パッケージのインストール
- ntp を chrony でマルチフィードと NICT をサーバーとして登録
- TimeZone を Asia/Tokyo に
- 全部流せてたら 1分後に再起動
11_template.cfg
#cloud-config
# This will cause the set+update hostname module to not operate (if true)
preserve_hostname: false
write_files:
- content: |
# history initialization
HISTTIMEFORMAT='%Y-%m-%dT%H:%M:%S.%s%z '
HISTSIZE=10000
HISTFILESIZE=10000
HISTCONTROL=ignoreboth
PROMPT_COMMAND='history -a; history -c; history -r'
path: /etc/profile.d/servers.sh
append: false
apt:
preserve_sources_list: false
primary:
- arches: [amd64, i386]
uri: http://jp.archive.ubuntu.com/ubuntu
package_upgrade: true
packages:
- qemu-guest-agent
- nano
- wget
- curl
- mtr
- tcpdump
- traceroute
- htop
- net-tools
ntp:
enabled: true
ntp_client: chrony
servers:
- ntp1.jst.mfeed.ad.jp
- ntp2.jst.mfeed.ad.jp
- ntp3.jst.mfeed.ad.jp
- ntp.nict.jp
timezone: "Asia/Tokyo"
power_state:
delay: "+1"
mode: reboot
message: Bye Bye
timeout: 30
- 作成したファイルをイメージにコピーする
virt-copy-in -a ubuntu-20.04.img 11_template.cfg /etc/cloud/cloud.cfg.d
念の為確認しとく
# virt-ls -a ubuntu-20.04.img 11_template.cfg /etc/cloud/cloud.cfg.d/
05_logging.cfg
11_template.cfg
90_dpkg.cfg
README
テンプレート用VM作成
ちょくちょく出てくる物を変数として定義しとく 変数の中身は各自変更する事
export PVE_TMP_VMID=9000
export PVE_VOLUME=cephrbd01
export PVE_NET0_VLAN=221
export PVE_USERNAME=naa0yama
続いてテンプレートにするためのVMを作る
弊宅ではVLAN が引かれてるので tag=${PVE_NET0_VLAN}
を付けているが必要ない場合は後で抜くかコマンドから抜く
qm create ${PVE_TMP_VMID} --name "Ubuntu-20.04" --cores 2 --memory 2048 --net0 virtio,bridge=vmbr0,tag=${PVE_NET0_VLAN}
qm importdisk ${PVE_TMP_VMID} ubuntu-20.04.img ${PVE_VOLUME}
qm set ${PVE_TMP_VMID} --scsihw virtio-scsi-pci --scsi0 ${PVE_VOLUME}:vm-${PVE_TMP_VMID}-disk-0
qm set ${PVE_TMP_VMID} --ide2 ${PVE_VOLUME}:cloudinit
qm set ${PVE_TMP_VMID} --boot c --bootdisk scsi0
qm set ${PVE_TMP_VMID} --serial0 socket --vga serial0
qm set ${PVE_TMP_VMID} --ostype l26
qm set ${PVE_TMP_VMID} --agent enabled=1,fstrim_cloned_disks=1
qm set ${PVE_TMP_VMID} --ciuser ${PVE_USERNAME}
curl -sfSL https://github.com/${PVE_USERNAME}.keys -o ${PVE_USERNAME}.keys
qm set ${PVE_TMP_VMID} --sshkeys ${PVE_USERNAME}.keys
qm template ${PVE_TMP_VMID}
qm create ${PVE_TMP_VMID} --name "Ubuntu-20.04" --cores 2 --memory 2048 --net0 virtio,bridge=vmbr0,tag=${PVE_NET0_VLAN}
ダウンロードしてきたイメージをインポート
qm importdisk ${PVE_TMP_VMID} ubuntu-20.04.img ${PVE_VOLUME}
アタッチ
qm set ${PVE_TMP_VMID} --scsihw virtio-scsi-pci --scsi0 ${PVE_VOLUME}:vm-${PVE_TMP_VMID}-disk-0
続いて、Cloud-initが利用するCDROMドライブを設定します。
qm set ${PVE_TMP_VMID} --ide2 ${PVE_VOLUME}:cloudinit
先ほどアタッチしたディスクをブートディスクとして設定します。
qm set ${PVE_TMP_VMID} --boot c --bootdisk scsi0
Cloud-initはシリアルコンソールを使用するため、その設定をします。
qm set ${PVE_TMP_VMID} --serial0 socket --vga serial0
- OS Type の設定 (l26 はGUIの
Linux 2.6 - 5.X Kernel
と同じ意味) - QEMU Guset Agent をオンにする
qm set ${PVE_TMP_VMID} --ostype l26
qm set ${PVE_TMP_VMID} --agent enabled=1,fstrim_cloned_disks=1
- cloud-init の User
qm set ${PVE_TMP_VMID} --ciuser ${PVE_USERNAME}
- cloud-init の SSH public Key を設定
- github から取ってきてファイルを指定する
curl -sfSL https://github.com/${PVE_USERNAME}.keys -o ${PVE_USERNAME}.keys
qm set ${PVE_TMP_VMID} --sshkeys ${PVE_USERNAME}.keys
- cloud-init の IP Config (net0) を開いて DHCP にしておくと少し楽になるかも
カスタムが完了したのでテンプレートにする
qm template ${PVE_TMP_VMID}
使い方
- テンプレートを選択して
Full Clone
する Cloud-init
タブでIP Config(net0)
をダブルクリック IP の静的設定するOptions
タブでName
を修正する- 起動する
CentOS の場合
qcow2c
をダウンロードして raw イメージにしておく
wget https://cloud.centos.org/centos/7/images/CentOS-7-x86_64-GenericCloud-2009.qcow2c
- qcow2c イメージを qcow2 イメージに変更
qemu-img convert -f qcow2 -O qcow2 CentOS-7-x86_64-GenericCloud-2009.qcow2c CentOS-7-x86_64-GenericCloud-2009.qcow2
rm CentOS-7-x86_64-GenericCloud-2009.qcow2c
mv CentOS-7-x86_64-GenericCloud-2009.qcow2 CentOS-7-2009.qcow2
- Ubuntuの時と同じ方法で cloud-init の設定を送り込む
virt-copy-in -a CentOS-7-2009.qcow2 11_template.cfg /etc/cloud/cloud.cfg.d
- 仮想マシンを作る
qm create ${PVE_TMP_VMID} --name "CnetOS-7-2009" --cores 2 --memory 2048 --net0 virtio,bridge=vmbr0,tag=${PVE_NET0_VLAN}
qm importdisk ${PVE_TMP_VMID} CentOS-7-2009.qcow2 ${PVE_VOLUME}
qm set ${PVE_TMP_VMID} --scsihw virtio-scsi-pci --scsi0 ${PVE_VOLUME}:vm-${PVE_TMP_VMID}-disk-0
qm set ${PVE_TMP_VMID} --ide2 ${PVE_VOLUME}:cloudinit
qm set ${PVE_TMP_VMID} --boot c --bootdisk scsi0
qm set ${PVE_TMP_VMID} --serial0 socket --vga serial0
qm set ${PVE_TMP_VMID} --ostype l26
qm set ${PVE_TMP_VMID} --agent enabled=1,fstrim_cloned_disks=1
qm set ${PVE_TMP_VMID} --ciuser ${PVE_USERNAME}
curl -sfSL https://github.com/${PVE_USERNAME}.keys -o ${PVE_USERNAME}.keys
qm set ${PVE_TMP_VMID} --sshkeys ${PVE_USERNAME}.keys
qm template ${PVE_TMP_VMID}
※ ホスト名は設定が書き換えできないようなので下記のコマンドで書き換えておく
sudo hostnamectl set-hostname persistent-hostname
GitHub にレポジトリを作った
- Cumulus Linux の場合
- Ubuntu 20.04 用ダウンロードして template 登録まで