草庐IT

KVM导入Ubuntu/Centos Cloud Image创建虚机及调整磁盘大小

Milton 2023-03-28 原文

Cloud Image

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架构宿主的镜像

Centos Cloud Images

如果运行KVM的是普通x86_64机器, 下载其中的 x86_64 版本, 建议使用 qcow2 或 qcow2c 后缀. 后者是压缩格式, 下载能省点时间, 使用区别不大.

安装 KVM, 配置桥接网络

机器上要安装好 KVM 环境,

sudo apt install -y qemu-kvm cloud-image-utils

创建好桥接网络, 假设桥接网口为 br0, 具体步骤可以参考之前的文章

修改虚机磁盘大小

默认的镜像只有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 可以直接挂载, 也可以加device=cdrom挂载

例子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

例子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 named ubuntu)
  • users - name your user name
  • users - shell must specified, or no shell presents
  • users - lock_passwd set it to false to allow login
  • users - plain_text_passwd use this to set the user password
  • chpasswd and ssh_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

参考

有关KVM导入Ubuntu/Centos Cloud Image创建虚机及调整磁盘大小的更多相关文章

  1. ruby - 如何在 Ruby 中顺序创建 PI - 2

    出于纯粹的兴趣,我很好奇如何按顺序创建PI,而不是在过程结果之后生成数字,而是让数字在过程本身生成时显示。如果是这种情况,那么数字可以自行产生,我可以对以前看到的数字实现垃圾收集,从而创建一个无限系列。结果只是在Pi系列之后每秒生成一个数字。这是我通过互联网筛选的结果:这是流行的计算机友好算法,类机器算法:defarccot(x,unity)xpow=unity/xn=1sign=1sum=0loopdoterm=xpow/nbreakifterm==0sum+=sign*(xpow/n)xpow/=x*xn+=2sign=-signendsumenddefcalc_pi(digits

  2. ruby-on-rails - 在 Rails 中将文件大小字符串转换为等效千字节 - 2

    我的目标是转换表单输入,例如“100兆字节”或“1GB”,并将其转换为我可以存储在数据库中的文件大小(以千字节为单位)。目前,我有这个:defquota_convert@regex=/([0-9]+)(.*)s/@sizes=%w{kilobytemegabytegigabyte}m=self.quota.match(@regex)if@sizes.include?m[2]eval("self.quota=#{m[1]}.#{m[2]}")endend这有效,但前提是输入是倍数(“gigabytes”,而不是“gigabyte”)并且由于使用了eval看起来疯狂不安全。所以,功能正常,

  3. python - 如何使用 Ruby 或 Python 创建一系列高音调和低音调的蜂鸣声? - 2

    关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭4年前。Improvethisquestion我想在固定时间创建一系列低音和高音调的哔哔声。例如:在150毫秒时发出高音调的蜂鸣声在151毫秒时发出低音调的蜂鸣声200毫秒时发出低音调的蜂鸣声250毫秒的高音调蜂鸣声有没有办法在Ruby或Python中做到这一点?我真的不在乎输出编码是什么(.wav、.mp3、.ogg等等),但我确实想创建一个输出文件。

  4. ruby - 使用 Vim Rails,您可以创建一个新的迁移文件并一次性打开它吗? - 2

    使用带有Rails插件的vim,您可以创建一个迁移文件,然后一次性打开该文件吗?textmate也可以这样吗? 最佳答案 你可以使用rails.vim然后做类似的事情::Rgeneratemigratonadd_foo_to_bar插件将打开迁移生成的文件,这正是您想要的。我不能代表textmate。 关于ruby-使用VimRails,您可以创建一个新的迁移文件并一次性打开它吗?,我们在StackOverflow上找到一个类似的问题: https://sta

  5. ruby-on-rails - 无法使用 Rails 3.2 创建插件? - 2

    我对最新版本的Rails有疑问。我创建了一个新应用程序(railsnewMyProject),但我没有脚本/生成,只有脚本/rails,当我输入ruby./script/railsgeneratepluginmy_plugin"Couldnotfindgeneratorplugin.".你知道如何生成插件模板吗?没有这个命令可以创建插件吗?PS:我正在使用Rails3.2.1和ruby​​1.8.7[universal-darwin11.0] 最佳答案 随着Rails3.2.0的发布,插件生成器已经被移除。查看变更日志here.现在

  6. ruby - 如何使用 RSpec::Core::RakeTask 创建 RSpec Rake 任务? - 2

    如何使用RSpec::Core::RakeTask初始化RSpecRake任务?require'rspec/core/rake_task'RSpec::Core::RakeTask.newdo|t|#whatdoIputinhere?endInitialize函数记录在http://rubydoc.info/github/rspec/rspec-core/RSpec/Core/RakeTask#initialize-instance_method没有很好的记录;它只是说:-(RakeTask)initialize(*args,&task_block)AnewinstanceofRake

  7. ruby - 为什么 SecureRandom.uuid 创建一个唯一的字符串? - 2

    关闭。这个问题需要detailsorclarity.它目前不接受答案。想改进这个问题吗?通过editingthispost添加细节并澄清问题.关闭8年前。Improvethisquestion为什么SecureRandom.uuid创建一个唯一的字符串?SecureRandom.uuid#=>"35cb4e30-54e1-49f9-b5ce-4134799eb2c0"SecureRandom.uuid方法创建的字符串从不重复?

  8. ruby - 有人可以帮助解释类创建的 post_initialize 回调吗 (Sandi Metz) - 2

    我正在阅读SandiMetz的POODR,并且遇到了一个我不太了解的编码原则。这是代码:classBicycleattr_reader:size,:chain,:tire_sizedefinitialize(args={})@size=args[:size]||1@chain=args[:chain]||2@tire_size=args[:tire_size]||3post_initialize(args)endendclassMountainBike此代码将为其各自的属性输出1,2,3,4,5。我不明白的是查找方法。当一辆山地自行车被实例化时,因为它没有自己的initialize方法

  9. ruby - 如何在 Ubuntu 中清除 Ruby Phusion Passenger 的缓存? - 2

    我试过重新启动apache,缓存的页面仍然出现,所以一定有一个文件夹在某个地方。我没有“公共(public)/缓存”,那么我还应该查看哪些其他地方?是否有一个URL标志也可以触发此效果? 最佳答案 您需要触摸一个文件才能清除phusion,例如:touch/webapps/mycook/tmp/restart.txt参见docs 关于ruby-如何在Ubuntu中清除RubyPhusionPassenger的缓存?,我们在StackOverflow上找到一个类似的问题:

  10. ruby - 使用多个数组创建计数 - 2

    我正在尝试按0-9和a-z的顺序创建数字和字母列表。我有一组值value_array=['0','1','2','3','4','5','6','7','8','9','a','b','光盘','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','','u','v','w','x','y','z']和一个组合列表的数组,按顺序,这些数字可以产生x个字符,比方说三个list_array=[]和一个当前字母和数字组合的数组(在将它插入列表数组之前我会把它变成一个字符串,]current_combo['0','0','0']

随机推荐