KVM导入Ubuntu/Centos Cloud Image创建虚机及调整磁盘大小
Cloud Image
Ubuntu Cloud Images
Ubuntu官网会给各种公有云平台提供cloud镜像, 例如AWS, MS Azure, Google Cloud, 以及本地虚机环境例如 QEMU, VMware, Vagrant等, 这些镜像的下载地址:
- 官网 https://cloud-images.ubuntu.com
- USTC镜像 http://mirrors.ustc.edu.cn/ubuntu-cloud-images
- 清华镜像 https://mirrors.tuna.tsinghua.edu.cn/ubuntu-cloud-images/
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架构宿主的镜像
Centos Cloud Images
- 下载页面 https://cloud.centos.org/centos/
- Centos 7: https://cloud.centos.org/centos/7/images/
- Centos 8: https://cloud.centos.org/centos/8/
- 国内镜像
如果运行KVM的是普通x86_64机器, 下载其中的 x86_64 版本, 建议使用 qcow2 或 qcow2c 后缀. 后者是压缩格式, 下载能省点时间, 使用区别不大.
安装 KVM, 配置桥接网络
机器上要安装好 KVM 环境,
sudo apt install -y qemu-kvm cloud-image-utils
创建好桥接网络, 假设桥接网口为 br0, 具体步骤可以参考之前的文章, 这里给一个例子
$ more /etc/netplan/00-installer-config.yaml
# This is the network config written by 'subiquity'
network:
ethernets:
enp2s0f0:
dhcp4: false
dhcp6: false
version: 2
bridges:
br0:
interfaces: [enp2s0f0]
addresses:
- 192.168.1.152/24
routes:
- to: default
via: 192.168.1.1
nameservers:
addresses:
- 192.168.1.1
- 8.8.8.8
search: []
修改虚机磁盘大小
默认的镜像只有2GB的磁盘大小, 可以在导入前通过 qemu-img resize 修改大小, 如果安装后需要修改大小, 要先关闭虚机再修改
查看镜像的磁盘情况
$ 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
准备 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
手册: https://cloudinit.readthedocs.io/en/latest/reference/modules.html#users-and-groups
用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 用于第三方平台的密钥, 这里都留空
用密码登录的例子, 会创建默认用户 ubuntu
#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} 这里设置的是创建的用户名
除了默认的ubuntu用户, 新增自定义用户的例子
#cloud-config
users:
- default
- name: milton
groups: [sudo]
sudo: ALL=(ALL) NOPASSWD:ALL
shell: /bin/bash
lock_passwd: false
plain_text_passwd: ubuntu
chpasswd:
expire: False
ssh_pwauth: True
创建 seed.img
用 cloud-localds 命令创建 seed.img
cloud-localds seed.img user-data.yaml metadata.yaml
导入 Cloud Image
以迷你版为例. 这个过程对于 Ubuntu 和 Centos 的 Cloud Image 都是通用的
例子1
virt-install --name ubuntu2004_1 \
--vcpus 2 --memory 4096 \
--graphics none \
--import \
--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 是重命名后的cloud镜像, seed.img 可以直接挂载
例子2
virt-install --name vm_centos7a \
--vcpus 2 --memory 4096 \
--graphics none --import \
--os-variant=rhel7.9 \
--network bridge=br0,model=virtio \
--disk /vms/vm_centos7a.qcow2 \
--disk /vms/seed.img,device=cdrom
centos可以加device=cdrom
挂载, ubuntu不能用这种方式, import后不识别, 只能用--disk /vms/seed.img
例子3
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, 以及之前配置的私钥就可以直接登录
一个完整的新虚机安装流程
Copy and resize image to 50GB
sudo cp /data/backup/CentOS-7-x86_64-GenericCloud.qcow2c vm_centos7a.qcow2
qemu-img info vm_centos7a.qcow2
sudo qemu-img resize vm_centos7a.qcow2 50G
# double check
qemu-img info vm_centos7a.qcow2
Prepare 2 files
metadata.yaml
instance-id: iid-local02
local-hostname: vm_u2204b
- change
instance-id
to a different id - change
local-hostname
to the virtual machine hostname
user-data.yaml
The following config will
- create default users and
- create a additional sudo user milton
- enable password login
#cloud-config
users:
- default
- name: milton
groups: [sudo]
sudo: ALL=(ALL) NOPASSWD:ALL
shell: /bin/bash
lock_passwd: false
plain_text_passwd: ubuntu
chpasswd:
expire: False
ssh_pwauth: True
-default
is the default cloud-init users (by default it is user namedubuntu
)users - name
your user nameusers - shell
must specified, or no shell presentsusers - lock_passwd
set it to false to allow loginusers - plain_text_passwd
use this to set the user passwordchpasswd
andssh_pwauth
seed.img
cloud-localds seed.img user-data.yaml metadata.yaml
copy the ubuntu cloud image to vms folder, and resize it to 50GB
cd /vms/
sudo cp /data/backup/ubuntu-22.04-minimal-cloudimg-amd64.img vm_u2204b.img
qemu-img info vm_u2204b.img
sudo qemu-img resize vm_u2204b.img 50g
# recheck
qemu-img info vm_u2204b.img
Then start to install
virt-install --name vm_u2204b --vcpus 2 --memory 4096 --graphics none --import --os-variant ubuntu22.04 --network bridge=br0,model=virtio --disk /vms/vm_u2204b.img --disk /vms/seed.img
参考
- Launching Ubuntu Cloud Images with QEMU https://powersj.io/posts/ubuntu-qemu-cli/
- Cloud-config manual https://cloudinit.readthedocs.io/en/latest/reference/modules.html
- Bash script to import a cloud image https://gist.github.com/giovtorres/0049cec554179d96e0a8329930a6d724