【问题标题】:Swapfile is not created after reboot using cloud-init使用 cloud-init 重新启动后未创建交换文件
【发布时间】: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


    【解决方案1】:

    • 您的问题是由于配置错误导致 Azure Linux 代理和云初始化代理尝试配置交换文件。当 cloud-init 负责配置时,交换文件必须由 cloud-init 配置为仅启用一个代理(cloud-init 或 waagent)进行配置。由于 waagent 守护程序的启动时间,此问题可能是间歇性的。

    • 您可以通过禁用磁盘格式化然后交换 waagent 配置文件(即 /etc/waagent.conf)中的配置并确保 azure linux 代理没有像应该那样挂载临时磁盘来解决此问题由 cloud-init 代理处理。为此,请设置如下参数:-

    #vi /etc/waagent.conf

    #资源盘的挂载点

    ResourceDisk.MountPoint=/mnt
    

    #在资源盘上创建并使用交换文件

    ResourceDisk.EnableSwap=n
    

    #交换文件的大小

    ResourceDisk.SwapSizeMB=0
    

    • 重新启动 Azure Linux 代理并确保将 VM 配置为通过云初始化创建交换文件。此外,将以下脚本添加到“/var/lib/cloud/scripts/per-boot”并使用“# chmod +x create_swapfile.sh”命令使文件可执行:-

    #!/bin/sh
    
     if [ ! -f '/mnt/swapfile' ]; then
    
     fallocate --length 2GiB /mnt/swapfile
    
     chmod 600 /mnt/swapfile
    
     mkswap /mnt/swapfile
    
     swapon /mnt/swapfile
    
     swapon -a
    
     else
    
     swapon /mnt/swapfile; fi
    

    • 完成后,停止并启动 VM 并检查是否启用了交换。下面是它的例子。此外,比较 /var/log/waagent.log 和 /var/log/cloud-init.log 中的日志以了解重启时间范围。要完全避免这种情况,请在配置期间使用交换配置自定义数据部署 VM。

    有关更多信息,请查找以下文档:-

    https://docs.microsoft.com/en-us/troubleshoot/azure/virtual-machines/swap-file-not-recreated-linux-vm-restart

    https://docs.microsoft.com/en-us/azure/virtual-machines/extensions/update-linux-agent

    谢谢你,

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-05-05
      • 1970-01-01
      • 1970-01-01
      • 2018-10-09
      • 1970-01-01
      • 2018-06-20
      • 2021-07-09
      相关资源
      最近更新 更多