ubuntu-24.04.2-live-server-arm64基于cloud-init实现分区自动扩容(LVM分区模式)

1. 环境

虚拟机镜像ISO:ubuntu-24.04.2-live-server-arm64.iso


2. 定制cloud-init镜像

2.1 安装OS

基于ubuntu-24.04.2-live-server-arm64.iso,通过virt-manager安装操作系统,语言建议选择英文,分区选择基于LVM的自动分区,具体的操作系统安装步骤可以查阅网上资料。

2.2 配置静态IP地址

vim /etc/netplan/01-netcfg.yaml

复制代码
network:
  version: 2
  renderer: networkd
  ethernets:
    enp1s0: 
      dhcp4: no
      addresses:
        - 172.19.0.200/24
      routes:
        - to: default
          via: 172.19.0.254
      nameservers:
        addresses:
          - 223.5.5.5

2.3 配置源

vim /etc/apt/sources.list.d/ubuntu.sources

复制代码
Types: deb deb-src
URIs: http://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/
Suites: noble noble-updates noble-security
Components: main restricted universe multiverse
Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg

#Types: deb
#URIs: http://ports.ubuntu.com/ubuntu-ports/
#Suites: noble noble-updates noble-backports
#Components: main restricted universe multiverse
#Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg
#
#Types: deb
#URIs: http://ports.ubuntu.com/ubuntu-ports/
#Suites: noble-security
#Components: main restricted universe multiverse
#Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg

2.4 安装开发包

复制代码
sudo apt update
sudo apt build-dep linux linux-image-unsigned-$(uname -r)
sudo apt install fakeroot llvm libncurses-dev dwarves gawk flex bison openssl libssl-dev dkms libelf-dev libudev-dev libpci-dev libiberty-dev autoconf git
apt source linux-image-unsigned-$(uname -r)
sudo apt install docker.io

2.5 安装cloud-init

复制代码
sudo apt install cloud-init cloud-guest-utils

2.6 配置启用密码登录

vim /etc/cloud/cloud.cfg

复制代码
disable_root: false

2.7 配置自动扩容

通过fdisk -l系统分区情况,如下所示:

复制代码
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: gpt
Disk identifier: C992E753-0498-4FFE-B859-9B5B8C0EF2A5

Device       Start      End  Sectors  Size Type
/dev/vda1     2048  1953791  1951744  953M EFI System
/dev/vda2  1953792  5623807  3670016  1.8G Linux filesystem
/dev/vda3  5623808 41940991 36317184 17.3G Linux filesystem


Disk /dev/mapper/ubuntu--vg-ubuntu--lv: 10 GiB, 10737418240 bytes, 20971520 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

通过blkid查看块设备详情,如下所示:

复制代码
/dev/sr0: BLOCK_SIZE="2048" UUID="2025-02-16-22-52-55-00" LABEL="Ubuntu-Server 24.04.2 LTS arm64" TYPE="iso9660" PTTYPE="dos"
/dev/mapper/ubuntu--vg-ubuntu--lv: UUID="99a93588-73ac-47d9-af78-5104f694157c" BLOCK_SIZE="4096" TYPE="ext4"
/dev/vda2: UUID="2a798038-e186-4211-a68e-40c5f0de176b" BLOCK_SIZE="4096" TYPE="ext4" PARTUUID="d87cecc0-0786-4b52-b2e8-a9e17c4deb75"
/dev/vda3: UUID="4BxQxy-zrfh-QVKZ-xqtA-WVbz-KGGt-2319JR" TYPE="LVM2_member" PARTUUID="e58c8695-ab27-4980-b52f-b7010f2fc1ce"
/dev/vda1: UUID="5D46-E9E3" BLOCK_SIZE="512" TYPE="vfat" PARTUUID="dfcdd546-be2e-44eb-a1a5-7071fb372135"

根据以上信息,得知我们需要自动扩容vda3分区(growpart)、扩容LVM和ext4文件系统(runcmd)。

创建文件/etc/cloud/cloud.cfg.d/06_growpart.cfg

vim /etc/cloud/cloud.cfg.d/06_growpart.cfg

复制代码
#cloud-config
growpart:
  mode: auto
  devices: [/dev/vda3]
  ignore_growroot_disabled: false
runcmd:
  - [pvresize,/dev/vda3]
  - [lvextend,-l,+100%FREE,/dev/mapper/ubuntu--vg-ubuntu--lv]
  - [resize2fs,/dev/mapper/ubuntu--vg-ubuntu--lv]

2.8 配置网络位置为DHCP

vim /etc/netplan/01-netcfg.yaml

复制代码
network:
  version: 2
  renderer: networkd
  ethernets:
    enp1s0: 
      dhcp4: yes

2.9 清理cloud-init信息

cat /etc/cloud/cloud-init.disabled

复制代码
Disabled by Ubuntu live installer after first boot.
To re-enable cloud-init on this image run:
  sudo cloud-init clean --machine-id

执行如下命令,关闭虚拟机,镜像制作完毕

复制代码
sudo cloud-init clean --machine-id

2.10 上传至云平台

复制代码
source /root/admin-openrc.sh
glance image-create --name ubuntu-24.04.2-live-server-arm64-20250418 --disk-format qcow2 --container-format bare --file ubuntu-24.04.2-live-server-arm64-20250418.qcow2 --property hw_scsi_model=virtio-scsi --property hw_disk_bus=virtio --property os_distro=kylin --visibility public --progress

注意:由于上述分区采用的是virtio模式,故这个属性也必须设置为--property hw_disk_bus=virtio。

启动虚拟机,指定相应的磁盘大小,如200GB,进入虚拟机查看根分区扩容情况,df -hl查看如下所示:

复制代码
Filesystem                         Size  Used Avail Use% Mounted on
tmpfs                              792M  1.2M  791M   1% /run
efivarfs                           256K   13K  244K   5% /sys/firmware/efi/efivars
/dev/mapper/ubuntu--vg-ubuntu--lv  195G  6.3G  180G   4% /
tmpfs                              3.9G     0  3.9G   0% /dev/shm
tmpfs                              5.0M     0  5.0M   0% /run/lock
/dev/vda2                          1.7G  100M  1.5G   7% /boot
/dev/vda1                          952M  6.4M  945M   1% /boot/efi
tmpfs                              792M   12K  792M   1% /run/user/0

虚拟机启动和调整参考各个openstack发行版本或商业版本,可能略有不同,这里不再赘述。


3. 参考文献

Kylin v10基于cloud-init实现分区自动扩容(LVM分区模式)
Kylin v4基于cloud-init实现分区自动扩容

相关推荐
梁萌33 分钟前
10-DevOps-Jenkins参数化构建实现多版本发布
运维·gitlab·jenkins·devops·tag
雨月琉琉39 分钟前
备份jenkins
运维·servlet·jenkins
新青年57942 分钟前
CentOS的安装以及网络配置
linux·网络·centos
一只帆記1 小时前
Jenkins 简易使用记录
运维·jenkins
Guheyunyi1 小时前
安全调度系统:安全管理的智能中枢
运维·安全·信息可视化·数据挖掘·数据分析
网络研究院1 小时前
安全文件共享实际上是什么样的呢?
运维·网络·安全
奈斯ing2 小时前
【prometheus+Grafana篇】从零开始:Linux 7.6 上二进制安装 Prometheus、Grafana 和 Node Exporter
运维·grafana·prometheus
level_xiwei2 小时前
Linux之信号
linux·运维·服务器
YuSun_WK2 小时前
Ubuntu与Linux的关系
linux·运维·ubuntu
茉莉玫瑰花茶2 小时前
网络基础概念(下)
运维·服务器·网络