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

参考

KVM导入Ubuntu Cloud 镜像创建虚机及调整磁盘大小的更多相关文章

  1. OpenStack 创建虚机过程简要汇总

    1. 总体流程 翻译自原文(英文):https://ilearnstack.com/2013/04/26/request-flow-for-provisioning-instance-in-opens ...

  2. Openstack之Nova创建虚机流程分析

    前言        Openstack作为一个虚拟机管理平台,核心功能自然是虚拟机的生命周期的管理,而负责虚机管理的模块就是Nova. 本文就是openstack中Nova模块的分析,所以本文重点是以 ...

  3. Docker创建虚机和swarm

    创建虚机: First, quickly create a virtual switch for your virtual machines (VMs) to share, so they will ...

  4. virtualbox创建虚机后配置网络上网

    一般来说常用的会配置两个网卡:(两个网卡应该在安装虚拟机之前就设置好) 1.NAT网络: 用于上外网: 2.host-only: 用于ssh连接,可以被其他人远程访问. 前提: 如图:在virtual ...

  5. 吃透KVM创建虚机和KVM命令

    1.创建虚拟机 1.1创建虚拟机磁盘 #使用qemu命令来创建磁盘 qemu-img create -f qcow2 /var/lib/libvirt/images/centos7.2.qcow2 2 ...

  6. nova创建虚拟机源码分析系列之八 compute创建虚机

    /conductor/api.py _build_instance()  /conductor/rpcapi.py  _build_instance() 1 构造一些数据类型2 修改一些api版本信息 ...

  7. openstack nova 创建虚机流程

    1文件 nova.api.openstack.coumpute.servers1函数 def create(self, req, body):1调用 (instances, resv_id) = se ...

  8. KVM 虚机镜像操作, 扩容和压缩

    KVM镜像操作 qemu-img命令 创建镜像 qemu-img create # 创建一个设备空间大小为10G的镜像 qemu-img create -f qcow2 centos7-guest.q ...

  9. 启动第一个 KVM 虚机 - 每天5分钟玩转 OpenStack(4)

    本节演示如何使用 virt-manager 启动 KVM 虚机. 首先通过命令 virt-manager 启动图形界面 # virt-manager 点上面的图标创建虚机 给虚机命名为 kvm1,这里 ...

随机推荐

  1. Azure Devops(十五) 使用Azure的私有Maven仓库

    上一篇文章中,我们介绍了如何使用Azure的nuget仓库,今天我们来研究一下如何使用azure给我们提供的maven仓库. 首先,我们打开azureDevops,点击到制品界面,然后选择maven. ...

  2. mysql 经典案例

    MySQL多表联合查询是MySQL数据库的一种查询方式,下面就为您介绍MySQL多表联合查询的语法,供您参考学习之用. MySQL多表联合查询语法: SELECT * FROM 插入表 LEFT JO ...

  3. Windows Embedded CE 6.0开发环境的搭建(2)

    最近开始在学习嵌入式,在这里首先得安装Windows Embedded CE 6.0,其中遇到了很多问题,电脑的系统以及相关配置都会在安装过程中受到影响,因此笔者就安装中的问题以及环境搭建来介绍一下. ...

  4. Luogu2543[AHOI2004]奇怪的字符串 (动态规划 LCS)

    04年的省选这么water吗,开个滚动数组算了 #include <iostream> #include <cstdio> #include <cstring> # ...

  5. 清晰梳理最全日志框架关系与日志配置-SpringBoot 2.7.2 实战基础

    优雅哥 SpringBoot 2.7.2 实战基础 - 07 - 日志配置 Java 中日志相关的 jar 包非常多,log4j.log4j2.commons-logging.logback.slf4 ...

  6. java-运算符以及简单运用

    运算符: 1)赋值运算符:= 2)算术运算符:+-*/%,++,-- 3)关系运算符:>,<,>=,<=,==,!= boolean 4)逻辑运算符:&&,|| ...

  7. 从C过渡到C++(1)——GNU/Linux

    从C过渡到C++(1)--GNU/Linux 目录 从C过渡到C++(1)--GNU/Linux 大名鼎鼎的GNU/Linux GNU GNU的组成 一点补充 MinGW 运行时库 额外的内容 Min ...

  8. kubernetes之镜像拉取策略ImagePullSecrets;

    1.容器镜像是什么? 1.容器镜像(Container Image)是最终运行的软件: 2.容器镜像(最初为Docker镜像,现在叫OCI镜像更合适)是将软件打包的形式.但是容器镜像还可以携带额外的设 ...

  9. 第三十九篇:Vue3 watch(ref和reactive的监视)

    好家伙, 1.vue2中的watch是调用配置项,(只能写一个) vue3中的watch是一个函数(可以写很多个) 2.watch一些用法: 这里是定义的数据 set up(){ let sum =r ...

  10. 总在用户态调试 C# 程序,终还是搭了一个内核态环境

    一:背景 一直在用 WinDbg 调试用户态程序,并没有用它调试过 内核态,毕竟不是做驱动开发,也没有在分析 dump 中需要接触用内核态的需求,但未知的事情总觉得很酷,加上最近在看 <深入解析 ...