【发布时间】:2021-10-18 07:46:39
【问题描述】:
我的配置
我正在尝试使用 Oracle Linux 映像(版本 8.4,第 2 代)在 Azure 中部署虚拟机。我们将 Azure 临时磁盘 (ephemeral0) 的挂载点更改为 /mnt/resource。此外,我在临时磁盘上创建了一个交换文件。我在部署期间使用了以下自定义 cloud-init 脚本:
#cloud-config
datasource_list: [ Azure ]
mounts:
- [ ephemeral0, /mnt/resource, auto, "defaults,nofail,x-systemd.requires=cloud-init.service" ]
mount_default_fields: [ None, None, "auto", "defaults,nofail", "0", "2" ]
swap:
filename: /mnt/resource/swap.img
size: "auto" # or size in bytes
max_size: 17179869184 # 16GB
在虚拟机创建后第一次启动时,一切(ephemeral0 和交换)都按预期工作。
如果我查看/var/log/cloud-init.log,您可以看到以下条目:
[root@test01 ~]# cat /var/log/cloud-init.log | grep swap
2021-08-16 08:19:29,814 - cc_mounts.py[DEBUG]: Attempting to determine the real name of swap
2021-08-16 08:19:29,814 - cc_mounts.py[DEBUG]: changed default device swap => None
2021-08-16 08:19:29,814 - cc_mounts.py[DEBUG]: Ignoring nonexistent default named mount swap
2021-08-16 08:19:29,815 - cc_mounts.py[DEBUG]: suggest 4096.0 MB swap for 7672.03125 MB memory with '17018.25390625 MB' disk given max=None [max=4254.5634765625 MB]'
2021-08-16 08:19:29,815 - cc_mounts.py[DEBUG]: Creating swapfile in '/mnt/resource/swap.img' on fstype 'xfs' using 'fallocate'
2021-08-16 08:19:29,815 - subp.py[DEBUG]: Running command ['fallocate', '-l', '4096M', '/mnt/resource/swap.img'] with allowed return codes [0] (shell=False, capture=True)
2021-08-16 08:19:29,849 - subp.py[DEBUG]: Running command ['mkswap', '/mnt/resource/swap.img'] with allowed return codes [0] (shell=False, capture=True)
2021-08-16 08:19:29,887 - util.py[DEBUG]: Setting up swap file took 0.072 seconds
2021-08-16 08:19:29,893 - cc_mounts.py[DEBUG]: Changes to fstab: ['+ /dev/disk/cloud/azure_resource-part1 /mnt/resource auto defaults,nofail,x-systemd.requires=cloud-init.service,comment=cloudconfig 0 2', '+ /mnt/resource/swap.img none swap sw,comment=cloudconfig 0 0']
2021-08-16 08:19:29,893 - subp.py[DEBUG]: Running command ['swapon', '-a'] with allowed return codes [0] (shell=False, capture=True)
2021-08-16 08:19:29,929 - cc_mounts.py[DEBUG]: Activate mounts: PASS:swapon -a
如您所见,cloud-init 建议将 4096 MB 作为交换大小。
在/etc/fstab 中添加了以下条目:
/dev/disk/cloud/azure_resource-part1 /mnt/resource auto defaults,nofail,x-systemd.requires=cloud-init.service,comment=cloudconfig 0 2
/mnt/resource/swap.img none swap sw,comment=cloudconfig 0 0
另外swapon -s 声明交换配置正确:
Filename Type Size Used Priority
/mnt/resource/swap.img file 4194300 0 -2
问题
现在,如果我取消分配虚拟机并重新启动它,临时磁盘将被删除并按预期重新创建。它在/mnt/resource 下再次挂载,但不再创建交换。 /var/log/cloud-init.log 状态:
2021-08-16 08:29:33,331 - cc_mounts.py[DEBUG]: Attempting to determine the real name of swap
2021-08-16 08:29:33,331 - cc_mounts.py[DEBUG]: changed default device swap => None
2021-08-16 08:29:33,331 - cc_mounts.py[DEBUG]: Ignoring nonexistent default named mount swap
2021-08-16 08:29:33,331 - util.py[DEBUG]: Reading from /proc/swaps (quiet=False)
2021-08-16 08:29:33,331 - util.py[DEBUG]: Read 37 bytes from /proc/swaps
2021-08-16 08:29:33,331 - cc_mounts.py[DEBUG]: swap file /mnt/resource/swap.img exists, but not in /proc/swaps
2021-08-16 08:29:33,332 - cc_mounts.py[DEBUG]: suggest 0.0 MB swap for 7672.03125 MB memory with '12889.515625 MB' disk given max=None [max=3222.37890625 MB]'
2021-08-16 08:29:33,332 - cc_mounts.py[DEBUG]: Not creating swap: suggested size was 0
2021-08-16 08:29:33,337 - cc_mounts.py[DEBUG]: Changes to fstab: ['- /mnt/resource/swap.img none swap sw,comment=cloudconfig 0 0']
据我了解,cloud-init 的 cc_mounts 模块建议交换大小为 0 MB,因为它确定临时磁盘仅剩下大约 12 GB 空间。这似乎是错误的,因为 (a) 磁盘由于解除分配而为空,并且 (b) df -h 声明它有大约 15 GB 可用:
[root@test01 ~]# df -h /mnt/resource/
Filesystem Size Used Avail Use% Mounted on
/dev/sdb1 16G 45M 15G 1% /mnt/resource
我在这里遗漏了什么吗?谁能解释一下为什么 cloud-init 会这样,以及如何为每次重启正确创建交换文件?
【问题讨论】:
标签: azure azure-virtual-machine cloud-init