Ubuntu官網會給各種公有雲平臺提供cloud鏡像, 例如AWS, MS Azure, Google Cloud, 以及本地虛機環境例如 QEMU, VMware, Vagrant等, 這些鏡像在 https://cloud-images.ubuntu.com 上可以下載. 這裡說明如何在 KVM... ...
Ubuntu Cloud Images
Ubuntu官網會給各種公有雲平臺提供cloud鏡像, 例如AWS, MS Azure, Google Cloud, 以及本地虛機環境例如 QEMU, VMware, Vagrant等, 這些鏡像在 https://cloud-images.ubuntu.com 上可以下載.
Ubuntu Cloud 鏡像類型
cloud-images.ubuntu.com 上面的鏡像主要可以分成兩大類, daily 和 release, 每個大類里又區分為 minimal(最小安裝)和普通版本.
- 如果對這些沒概念, 下載 release 版本, 在 releases 目錄下有從 8.04 到 22.04 的各個發行版鏡像
- 從上面進入的發行版目錄下麵, 是按日期排列的目錄, 用最新日期的那個目錄
- 目錄下麵, 都是形如 ubuntu-22.04-server-cloudimg-[宿主架構]-xxx.[img/vmdk/ova] 這種文件名的文件, 對應KVM需要下載
- 如果只是需要能運行的最小系統, 在 minimal 目錄下 releases 目錄找對應發行版的迷你安裝. 標準版的大小是迷你版的兩倍左右
- 迷你版只有給x86-64架構宿主的鏡像
KVM 中安裝Cloud Image 虛機
以迷你版為例
前提
機器上要安裝好 KVM 環境,
sudo apt install -y qemu-kvm cloud-image-utils
創建好橋接網路, 假設橋接網口為 br0
準備 seed.img
當cloud image啟動時, 會搜索包含實例信息的數據源, 用於初始化當前的實例, 包括 hostname, 網路設置等, 通常包含兩類內容:
- metadata: 這一般是由雲平臺提供的唯一編號, 包含 hostname, 網路信息, SSH keys等.
- user data: 用於指定登錄虛機的用戶的相關設置.
創建文件 metadata.yaml
instance-id: iid-local01
local-hostname: cloudimg
- instance-id:
- local-hostname: 虛機啟動後看到的 hostname
創建文件 user-data.yaml
用id_rsa密鑰登錄的例子
#cloud-config
ssh_authorized_keys:
- ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDSc4mEaOsvQusPZRUIV6PUz2yM2D/Td3FKkdumu... .............. ... ......hQI6ofjLCxBKRqBbTl milton@somewherec
ssh_import_id:
- gh:
- lp:
- ssh_authorized_keys 用於免密登錄的公鑰. 需要在本地預先創建好密鑰對, 並配置到 .ssh/config 里. 用戶是預設的ubuntu, 不需要指定
- ssh_import_id 用於第三方平臺的密鑰, 這裡都留空
用密碼登錄的例子
#cloud-config
password: ubuntu
chpasswd: { expire: False }
ssh_pwauth: True
ssh_import_id: ${USER}
- password: ubuntu 設置的密碼為 ubuntu
- chpasswd: { expire: False } 不過期
- ssh_pwauth: True 設置為使用密碼登錄
- ssh_import_id: ${USER} 這裡設置的是創建的用戶名
創建 seed.img
用 cloud-localds 命令創建 seed.img
cloud-localds seed.img user-data.yaml metadata.yaml
用 Cloud Image 啟動
命令例子1
virt-install --name ubuntu2004_1 \
--vcpus 2 --memory 4096 \
--graphics none \
--import \
--os-type linux --os-variant ubuntu20.04 \
--network bridge=br0,model=virtio \
--disk /data/vms/ubuntu20.04_1.img \
--disk /data/vms/seed.img
其中 ubuntu20.04_1.img 是重命名後的迷你版鏡像.
命令例子2
kvm-spice -m 2048 \
-drive file=ubuntu-18.04-server-cloudimg-amd64.img,if=virtio,cache=writeback \
-cdrom seed.img \
-net nic,model=virtio \
-net user,hostfwd=tcp::222-:22
這個會將虛機的22埠映射到宿主的222埠
執行命令後會啟動到登錄提示符. 過程中會穿插著執行初始化, 顯示機器的信息, 包括IP地址, 網路環境, 導入的用戶key等
Starting Initial cloud-ini… (metadata service crawler)...
cloud-init[271]: Cloud-init v. 22.2-0ubuntu1~20.04.3 running 'init' at Thu, 13 Oct 2022 07:39:43 +0000. Up 10.34 seconds.
cloud-init[271]: ci-info: +++++++++++++++++++++++++++++++++++++++++++Net deviceinfo++++++++++++++++++++++++++++++++++++++++++++
cloud-init[271]: ci-info: +--------+------+---------------------------------------+---------------+--------+-------------------+
cloud-init[271]: ci-info: | Device | Up | Address | Mask | Scope | Hw-Address |
cloud-init[271]: ci-info: +--------+------+---------------------------------------+---------------+--------+-------------------+
cloud-init[271]: ci-info: | enp1s0 | True | 192.168.9.106 | 255.255.255.0 | global | 13:54:00:01:e0:29 |
...
cloud-init[271]: ci-info: ++++++++++++++++++++++++++++++Route IPv4 info++++++++++++++++++++++++++++++
cloud-init[271]: ci-info: +-------+-------------+-------------+-----------------+-----------+-------+
cloud-init[271]: ci-info: | Route | Destination | Gateway | Genmask | Interface | Flags |
cloud-init[271]: ci-info: +-------+-------------+-------------+-----------------+-----------+-------+
cloud-init[271]: ci-info: | 0 | 0.0.0.0 | 192.168.9.1 | 0.0.0.0 | enp1s0 | UG |
...
cloud-init[271]: ci-info: ++++++++++++++++++++++++++++++++Route IPv6 info++++++++++++++++++++++++++++++++
...
cloudimg login: cloud-init[481]: 2022-10-13 07:39:58,045 ERROR ssh-import-id protocol handler {'gh': not found or cannot execute
cloud-init[481]: Cloud-init v. 22.2-0ubuntu1~20.04.3 running 'modules:config' at Thu, 13 Oct 2022 07:39:57 +0000. Up 24.65 seconds.
cloud-init[481]: 2022-10-13 07:39:58,089 - util.py[WARNING]: Failed to run command to import ubuntu SSH ids
cloud-init[481]: 2022-10-13 07:39:58,100 - util.py[WARNING]: ssh-import-id failed for: ubuntu ["{'gh': None}", "{'lp': None}"]
cloud-init[481]: 2022-10-13 07:39:58,101 - util.py[WARNING]: Running module ssh-import-id (<module 'cloudinit.config.cc_ssh_import_id' from '/usr/lib/python3/dist-packages/cloudinit/config/cc_ssh_import_id.py'>) failed
ci-info: +++++++++++++++++++++++++++++++++Authorized keys from /home/ubuntu/.ssh/authorized_keys for user ubuntu+++++++++++++++++++++++++++++++++
ci-info: +---------+-------------------------------------------------------------------------------------------------+---------+----------------+
ci-info: | Keytype | Fingerprint (sha256) | Options | Comment |
ci-info: +---------+-------------------------------------------------------------------------------------------------+---------+----------------+
ci-info: | ssh-rsa | 33:11:22:d7:b1:f9:83:3b:b8:94:9f:f3:33:33:33:33:12:12:8f:d4:34:2e:60:12:12:12:ae:12:12:12:d4:60 | - | milton@somewhe |
ci-info: +---------+-------------------------------------------------------------------------------------------------+---------+----------------+
使用顯示的IP, 以及之前配置的私鑰就可以直接登錄
修改虛機磁碟大小
預設的鏡像只有2GB的磁碟大小, 如果要修改大小, 需要先關閉虛機
查看鏡像的磁碟情況
$ qemu-img info ubuntu20.04_1.img
image: ubuntu20.04_1.img
file format: qcow2
virtual size: 2.2 GiB (2361393152 bytes)
disk size: 273 MiB
cluster_size: 65536
Format specific information:
compat: 0.10
refcount bits: 16
修改磁碟到20G
$ qemu-img resize ubuntu20.04_1.img 20G
qemu-img: Could not open 'ubuntu20.04_1.img': Could not open 'ubuntu20.04_1.img': Permission denied
milton@miltmac:/data/vms$ sudo qemu-img resize ubuntu20.04_1.img 20G
[sudo] password for milton:
Image resized.
檢查設置
$ qemu-img info ubuntu20.04_1.img
image: ubuntu20.04_1.img
file format: qcow2
virtual size: 20 GiB (21474836480 bytes)
disk size: 273 MiB
cluster_size: 65536
Format specific information:
compat: 0.10
refcount bits: 16
重啟虛機, 應用設置
啟動後df -h
就能看到磁碟大小的變化
$ virsh start ubuntu2004_1
Domain ubuntu2004_1 started
參考
- Launching Ubuntu Cloud Images with QEMU https://powersj.io/posts/ubuntu-qemu-cli/