cloud-init中NoCloud配置

本文章主要记录cloud-init工具中NoCloud数据源的使用方法,可以搭配KVM镜像制作系列文章,为用户定制操作系统。


NoCloud

数据源NoCloud允许用户在不运行网络服务的情况下向虚拟机实例提供用户数据user-data和元数据meta-data:

  • 可以通过vfat或者iso9660文件系统上的文件为虚拟机的引导提供数据,文件系统卷标签必须是cidata或者CIDATA,主要介绍。
  • 另外可以通过内核命令或者SMBIOS序列号选项来提供数据,在这里不多介绍。

使用方法

1. 安装并初始化文件

参考KVM镜像制作系列文章中对应操作系统的第三步进行静态IP配置中的第1、2点进行cloud-init的安装和重命名文件。

2. 修改cloud-init配置文件

进入/etc/cloud/,备份并修改cloud.cfg文件

sudo cp cloud.cfg cloud.cfg.bak
sudo vim cloud.cfg

将cloud.cfg中的所有内容修改为以下内容,该文件以yaml格式为准,需要注意格式

datasource_list: [ 'NoCloud' ]

# The modules that run in the 'init' stage
cloud_init_modules:
 - migrator
 - seed_random
 - bootcmd
 - write-files
 - growpart
 - resizefs
 - disk_setup
 - mounts
 - set_hostname
 - update_hostname
 - update_etc_hosts
 - ca-certs
 - rsyslog
 - users-groups
 - ssh

# The modules that run in the 'config' stage
cloud_config_modules:
# Emit the cloud config ready event
# this can be used by upstart jobs for 'start on cloud-config'.
 - emit_upstart
 - snap
 - ssh-import-id
 - locale
 - set-passwords
 - grub-dpkg
 - apt-pipelining
 - apt-configure
 - ubuntu-advantage
 - ntp
 - timezone
 - disable-ec2-metadata
 - runcmd
 - byobu

# The modules that run in the 'final' stage
cloud_final_modules:
 - package-update-upgrade-install
 - fan
 - landscape
 - lxd
 - ubuntu-drivers
 - puppet
 - chef
 - mcollective
 - salt-minion
 - reset_rmc
 - refresh_rmc_and_interface
 - rightscale_userdata
 - scripts-vendor
 - scripts-per-once
 - scripts-per-boot
 - scripts-per-instance
 - scripts-user
 - ssh-authkey-fingerprints
 - keys-to-console
 - phone-home
 - final-message
 - power-state-change

主要是**datasource_list: [ ‘NoCloud’ ]**这一句配置,将数据源指定为NoCloud,之后的所有命令都不需要在该文件中编写,而是在虚拟机外通过磁盘挂载的方式将用户数据传入到虚拟机中并执行。

3. 编写用户数据

在服务器上创建文件夹用于存储user-data并编写用户数据文件

mkdir cloud-init-file
cd cloud-init-file
vim my-user-data

my-user-data内容如下:

#cloud-config

chpasswd: 
    list: 
        - ubuntu:root
        - root:root
    expire: false

hostname: ubuntu1604

以下为内容详解

1. 文件必须以#cloud-config开头,指明该文件为cloud-init配置文件
2. chpasswd命令用于修改用户的密码,包括一个list和expire子项,其中list下需要按照“用户名:密码”的格式来指定用户的新密码,用户和密码之间不能存在空格,否则无法实现该功能,expire子项用于指定该密码是否有过期时间,一般设置为false即可。
3. hostname命令用于修改虚拟机主机名
4. 其他命令可以参考cloud-init官方文档进行编写

4. 制作配置镜像

安装镜像制作工具

sudo apt-get install cloud-image-utils

制作镜像

sudo cloud-localds -m local my-seed.img my-user-data

-m指定cloud-init的工作模式,local的意思是不需要依赖网络,完成上述命令后将生成my-seed.img

5. 挂载镜像

编辑虚拟机的xml配置文件

sudo virsh edit ubuntu16.04(虚拟机名称)

添加以下内容

<disk type='file' device='disk'>
   <driver name='qemu' type='raw' cache='none' io='native'/>
   <source file='xxx/cloud-init-file/my-seed.img'/> # my-seed.img文件路径
   <target dev='vdb' bus='virtio'/>
   <readonly/>
</disk>

6. 重启虚拟机

重新启动虚拟机后会自动按照该镜像中user-data文件的内容执行。

参考链接

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值