Ubuntu官网会给各种公有云平台提供cloud镜像, 例如AWS, MS Azure, Google Cloud, 以及本地虚机环境例如 QEMU, VMware, Vagrant等, 这些镜像在 https://cloud-images.ubuntu.com 上可以下载.
cloud-images.ubuntu.com 上面的镜像主要可以分成两大类, daily 和 release, 每个大类里又区分为 minimal(最小安装)和普通版本.
如果运行KVM的是普通x86_64机器, 下载其中的 x86_64 版本, 建议使用 qcow2 或 qcow2c 后缀. 后者是压缩格式, 下载能省点时间, 使用区别不大.
机器上要安装好 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
$ 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
当cloud image启动时, 会搜索包含实例信息的数据源, 用于初始化当前的实例, 包括 hostname, 网络设置等, 通常包含两类内容:
instance-id: iid-local01
local-hostname: cloudimg
手册: 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:
用密码登录的例子, 会创建默认用户 ubuntu
#cloud-config
password: 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
用 cloud-localds 命令创建 seed.img
cloud-localds seed.img user-data.yaml metadata.yaml
以迷你版为例. 这个过程对于 Ubuntu 和 Centos 的 Cloud Image 都是通用的
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挂载
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
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
instance-id to a different idlocal-hostname to the virtual machine hostnameuser-data.yaml
The following config will
#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 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 and ssh_pwauthseed.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
出于纯粹的兴趣,我很好奇如何按顺序创建PI,而不是在过程结果之后生成数字,而是让数字在过程本身生成时显示。如果是这种情况,那么数字可以自行产生,我可以对以前看到的数字实现垃圾收集,从而创建一个无限系列。结果只是在Pi系列之后每秒生成一个数字。这是我通过互联网筛选的结果:这是流行的计算机友好算法,类机器算法:defarccot(x,unity)xpow=unity/xn=1sign=1sum=0loopdoterm=xpow/nbreakifterm==0sum+=sign*(xpow/n)xpow/=x*xn+=2sign=-signendsumenddefcalc_pi(digits
我的目标是转换表单输入,例如“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看起来疯狂不安全。所以,功能正常,
关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭4年前。Improvethisquestion我想在固定时间创建一系列低音和高音调的哔哔声。例如:在150毫秒时发出高音调的蜂鸣声在151毫秒时发出低音调的蜂鸣声200毫秒时发出低音调的蜂鸣声250毫秒的高音调蜂鸣声有没有办法在Ruby或Python中做到这一点?我真的不在乎输出编码是什么(.wav、.mp3、.ogg等等),但我确实想创建一个输出文件。
使用带有Rails插件的vim,您可以创建一个迁移文件,然后一次性打开该文件吗?textmate也可以这样吗? 最佳答案 你可以使用rails.vim然后做类似的事情::Rgeneratemigratonadd_foo_to_bar插件将打开迁移生成的文件,这正是您想要的。我不能代表textmate。 关于ruby-使用VimRails,您可以创建一个新的迁移文件并一次性打开它吗?,我们在StackOverflow上找到一个类似的问题: https://sta
我对最新版本的Rails有疑问。我创建了一个新应用程序(railsnewMyProject),但我没有脚本/生成,只有脚本/rails,当我输入ruby./script/railsgeneratepluginmy_plugin"Couldnotfindgeneratorplugin.".你知道如何生成插件模板吗?没有这个命令可以创建插件吗?PS:我正在使用Rails3.2.1和ruby1.8.7[universal-darwin11.0] 最佳答案 随着Rails3.2.0的发布,插件生成器已经被移除。查看变更日志here.现在
如何使用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
关闭。这个问题需要detailsorclarity.它目前不接受答案。想改进这个问题吗?通过editingthispost添加细节并澄清问题.关闭8年前。Improvethisquestion为什么SecureRandom.uuid创建一个唯一的字符串?SecureRandom.uuid#=>"35cb4e30-54e1-49f9-b5ce-4134799eb2c0"SecureRandom.uuid方法创建的字符串从不重复?
我正在阅读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方法
我试过重新启动apache,缓存的页面仍然出现,所以一定有一个文件夹在某个地方。我没有“公共(public)/缓存”,那么我还应该查看哪些其他地方?是否有一个URL标志也可以触发此效果? 最佳答案 您需要触摸一个文件才能清除phusion,例如:touch/webapps/mycook/tmp/restart.txt参见docs 关于ruby-如何在Ubuntu中清除RubyPhusionPassenger的缓存?,我们在StackOverflow上找到一个类似的问题:
我正在尝试按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']