Cloud-Init 初始化自己的云镜像

在制作Cloud Init云镜像之前,我们需要下载一份打包好的空的云镜像,镜像一般选用bios+cloudinit+qcow2标签的

Ubuntu:http://cloud-images.ubuntu.com/releases
Debian:https://cloud.debian.org/images/cloud/
CentOS:https://cloud.centos.org/centos/
Alpine:https://dl-cdn.alpinelinux.org/alpine/v<版本号>/releases/<架构>/

一、导入云镜像

首先我们需要导入我们的云镜像,以PVE为例

$ qm importdisk 101 ubuntu24.04.qcow2 local --format=qcow2

二、进入系统/挂载系统盘

方法一:在大多数情况cloud init均为disable_root状态,但是为了避免新增用户影响,我们可以先用root登陆VNC再手动允许root登陆

$ vi /etc/ssh/sshd_config.d/60-cloudimg-settings.conf
PasswordAuthentication yes
PermitRootLogin yes

方法二:直接挂载qcow2到目录,直接修改相关配置

$ qemu-nbd --connect=/dev/nbd0 /var/lib/vz/images/401/vm-401-disk-0.qcow2
# 查看虚拟磁盘分区
$ fdisk -l /dev/nbd0
# 挂载对应盘符
$ mount /dev/nbd0p1 /qemu
$ df -h /qemu

三、修改Cloud Init配置

修改主要配置,允许Root,允许密码登陆,修改软件源

$ vi /etc/cloud/cloud.cfg
# 允许Root
disable_root: false
# 允许密码登陆
ssh_pwauth: true
# 修改Ubuntu/Debian/CentOS源
  package_mirrors:
    - arches: [i386, amd64]
      failsafe:
        primary: http://mirrors.0e7.cc/ubuntu
        security: http://mirrors.0e7.cc/ubuntu
    - arches: [arm64, armel, armhf]
      failsafe:
        primary: http://mirrors.0e7.cc/ubuntu-ports
        security: http://mirrors.0e7.cc/ubuntu-ports
    - arches: [default]
      failsafe:
        primary: http://mirrors.0e7.cc/ubuntu-ports
        security: http://mirrors.0e7.cc/ubuntu-ports
# 修改Alpine源
  apk_repos:
    alpine_repo:
      base_url: http://mirrors.0e7.cc/alpine
      community_enabled: true
      version: v3.20

添加自定义配置,如在sshd_config中允许密码登陆(有些云镜像会默认关闭,即使在上一步打开了也灭用),运行自定义程序等(我这里默认给pip/conda换源,安装qemu-guest-agent)

在cfg文件开头的#cloud-config时扫描配置文件需要的,不可以缺少

$ vi /etc/cloud/cloud.cfg.d/0e7.cfg
#cloud-config
write_files:
  - path: /etc/ssh/sshd_config
  append: true
  content: |
  PermitRootLogin yes
packages:
  - qemu-guest-agent
runcmd:
  - mkdir -p /root/.pip
  - curl -o /root/.condarc http://mirrors.0e7.cc/script/dot_condarc
  - curl -o /root/.pip/pip.conf http://mirrors.0e7.cc/script/dot_pip 
  - curl -o /bin/simple_relayer_cli http://mirrors.0e7.cc/script/simple_relayer_cli 
  - chmod +x /bin/simple_relayer_cli 
  - systemctl enable qemu-guest-agent.service --now
# - rc-update add qemu-guest-agent default

四、清除Cloud Init缓存

仅方法一需要清空缓存

$ cloud-init clean
$ rm -rf /var/lib/cloud/instances/
$ rm -rf /var/lib/cloud/instance
$ rm -rf /var/lib/cloud/data/
$ rm -rf /var/log/lastlog
# 这里还可以清理再细致点,我就不喜欢清理所以用了方法二
$ history -c && history -w && init 0

方法二仅需取消挂载即可

$ umount /qemu
$ qemu-nbd --disconnect /dev/nbd0

五、挂载Cloud Init盘符

修改相关用户密码配置,开机即可

暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇