Azure VM cloud-init 覆盖 /etc/fstab

Azure VM cloud-init 覆盖 /etc/fstab

我添加了一个额外的数据磁盘,如中所述将数据盘附加到Linux VM文档

有问题的分区是/dev/sdc1.我将这一行添加到了最底部。不幸的是,cloud-init 的魔力以一种糟糕的方式发挥了它的奇迹,并在 /etc/fstab 中的条目周围移动,最终我得到了一个“隐藏”的挂载点。

root@qwerz:/mnt/builds/docker# cat /etc/fstab
# CLOUD_IMG: This file was created/modified by the Cloud Image build process
UUID=cfadafca-7199-49b1-a353-072629b7fcdf       /        ext4   defaults,discard        0 0
LABEL=UEFI      /boot/efi       vfat    defaults,discard        0 0
UUID=0195f789-b5fa-4bea-a91d-dc120bafb23a       /mnt/builds             ext4    defaults,nofail   1   2
#/dev/sdc1      /mnt/builds     ext4    default,nofail  0       0
/dev/disk/cloud/azure_resource-part1    /mnt    auto    defaults,nofail,x-systemd.requires=cloud-init.service,comment=cloudconfig       0       2
root@qwerz:/mnt/builds/docker#

问题 1:我应该将自定义点移到 /mnt 之外吗?
问题 2:cloud-init 的东西到底在做什么,我可以摆脱它吗?

root@qwerz:/mnt/builds/docker# dpkg -l |grep cloud-init
ii  cloud-init                          19.1-1-gbaa47854-0ubuntu1~18.04.1           all          Init scripts for cloud instances
ii  cloud-initramfs-copymods            0.40ubuntu1.1                               all          copy initramfs modules into root filesystem for later use
ii  cloud-initramfs-dyn-netconf         0.40ubuntu1.1                               all          write a network interface file in /run for BOOTIF

据我了解,cloud-init 应该只在虚拟机第一次启动时运行。然而我有一种感觉,它也会在每次之后运行解除分配任何给定虚拟机的(以节省一些积分)。

cloud-init.log 有趣的部分看起来像这样

2019-06-26 06:01:49,392 - util.py[DEBUG]: Reading from /etc/fstab (quiet=False)
2019-06-26 06:01:49,392 - util.py[DEBUG]: Read 451 bytes from /etc/fstab
2019-06-26 06:01:49,392 - cc_mounts.py[DEBUG]: Attempting to determine the real name of ephemeral0
2019-06-26 06:01:49,392 - cc_mounts.py[DEBUG]: Mapped metadata name ephemeral0 to /dev/disk/cloud/azure_resource
2019-06-26 06:01:49,392 - cc_mounts.py[DEBUG]: changed default device ephemeral0 => /dev/disk/cloud/azure_resource-part1
2019-06-26 06:01:49,392 - cc_mounts.py[DEBUG]: Attempting to determine the real name of swap
2019-06-26 06:01:49,392 - cc_mounts.py[DEBUG]: changed default device swap => None
2019-06-26 06:01:49,392 - cc_mounts.py[DEBUG]: Ignoring nonexistent default named mount swap
2019-06-26 06:01:49,392 - cc_mounts.py[DEBUG]: no need to setup swap
2019-06-26 06:01:49,392 - util.py[DEBUG]: Reading from /proc/mounts (quiet=False)
2019-06-26 06:01:49,393 - util.py[DEBUG]: Read 2608 bytes from /proc/mounts
2019-06-26 06:01:49,393 - util.py[DEBUG]: Fetched {'sysfs': {'fstype': 'sysfs', 'mountpoint': '/sys', 'opts': 'rw,nosuid,nodev,noexec,relatime'}, 'proc': {'fstype': 'proc', 'mountpoint': '/proc', 'opts': 'rw,nosuid,nodev,noexec,relatime'}, 'udev': {'fstype': 'devtmpfs', 'mountpoint': '/dev', 'opts': 'rw,nosuid,relatime,size=8187860k,nr_inodes=2046965,mode=755'}, 'devpts': {'fstype': 'devpts', 'mountpoint': '/dev/pts', 'opts': 'rw,nosuid,noexec,relatime,gid=5,mode=620,ptmxmode=000'}, 'tmpfs': {'fstype': 'tmpfs', 'mountpoint': '/sys/fs/cgroup', 'opts': 'ro,nosuid,nodev,noexec,mode=755'}, '/dev/sda1': {'fstype': 'ext4', 'mountpoint': '/', 'opts': 'rw,relatime,discard'}, 'securityfs': {'fstype': 'securityfs', 'mountpoint': '/sys/kernel/security', 'opts': 'rw,nosuid,nodev,noexec,relatime'}, 'cgroup': {'fstype': 'cgroup', 'mountpoint': '/sys/fs/cgroup/memory', 'opts': 'rw,nosuid,nodev,noexec,relatime,memory'}, 'pstore': {'fstype': 'pstore', 'mountpoint': '/sys/fs/pstore', 'opts': 'rw,nosuid,nodev,noexec,relatime'}, 'systemd-1': {'fstype': 'autofs', 'mountpoint': '/proc/sys/fs/binfmt_misc', 'opts': 'rw,relatime,fd=32,pgrp=1,timeout=0,minproto=5,maxproto=5,direct,pipe_ino=18816'}, 'mqueue': {'fstype': 'mqueue', 'mountpoint': '/dev/mqueue', 'opts': 'rw,relatime'}, 'debugfs': {'fstype': 'debugfs', 'mountpoint': '/sys/kernel/debug', 'opts': 'rw,relatime'}, 'hugetlbfs': {'fstype': 'hugetlbfs', 'mountpoint': '/dev/hugepages', 'opts': 'rw,relatime,pagesize=2M'}, 'fusectl': {'fstype': 'fusectl', 'mountpoint': '/sys/fs/fuse/connections', 'opts': 'rw,relatime'}, 'configfs': {'fstype': 'configfs', 'mountpoint': '/sys/kernel/config', 'opts': 'rw,relatime'}, '/dev/loop0': {'fstype': 'squashfs', 'mountpoint': '/snap/dotnet-sdk/39', 'opts': 'ro,nodev,relatime'}, '/dev/loop1': {'fstype': 'squashfs', 'mountpoint': '/snap/core/7169', 'opts': 'ro,nodev,relatime'}, '/dev/loop2': {'fstype': 'squashfs', 'mountpoint': '/snap/dotnet-sdk/38', 'opts': 'ro,nodev,relatime'}, '/dev/loop3': {'fstype': 'squashfs', 'mountpoint': '/snap/core/6964', 'opts': 'ro,nodev,relatime'}, '/dev/loop4': {'fstype': 'squashfs', 'mountpoint': '/snap/dotnet-sdk/35', 'opts': 'ro,nodev,relatime'}, '/dev/sda15': {'fstype': 'vfat', 'mountpoint': '/boot/efi', 'opts': 'rw,relatime,fmask=0022,dmask=0022,codepage=437,iocharset=iso8859-1,shortname=mixed,errors=remount-ro,discard'}} mounts from proc
2019-06-26 06:01:49,393 - util.py[DEBUG]: Writing to /etc/fstab - wb: [644] 451 bytes
2019-06-26 06:01:49,393 - cc_mounts.py[DEBUG]: No changes to /etc/fstab made.
2019-06-26 06:01:49,393 - util.py[DEBUG]: Running command ['mount', '-a'] with allowed return codes [0] (shell=False, capture=True)
2019-06-26 06:01:49,427 - cc_mounts.py[WARNING]: Activate mounts: FAIL:mount -a
2019-06-26 06:01:49,427 - util.py[WARNING]: Activate mounts: FAIL:mount -a
2019-06-26 06:01:49,427 - util.py[DEBUG]: Activate mounts: FAIL:mount -a
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/cloudinit/config/cc_mounts.py", line 495, in handle
    util.subp(cmd)
  File "/usr/lib/python3/dist-packages/cloudinit/util.py", line 2069, in subp
    cmd=args)
cloudinit.util.ProcessExecutionError: Unexpected error while running command.
Command: ['mount', '-a']
Exit code: 64
Reason: -
Stdout: 
Stderr: mount: /mnt/builds: mount point does not exist.

挂载点当然存在,但不在之前挂载的 ephemeral0 临时存储磁盘中,挂载在 /mnt :(

/mnt无论如何,为什么要在下面安装临时磁盘!?

答案1

我意识到这已经晚了,但我希望它有所帮助。

在 Azure 中,Linux 代理配置文件 (waagent.conf) 通常会设置挂载点,您可以从那里为大多数受支持的 Linux 发行版执行操作。您通常会发现默认安装点为“/mnt/resource”

然而,在 Ubuntu 16.04+ 中,cloud-init 会覆盖 waagent.conf ephemeral0 挂载点配置以及每次释放/启动时的 fstab。尽管如此,您仍然可以通过多种方式指定挂载点,以便在重新启动或取消分配/启动后继续存在。我将在下面讨论它们。

现在回答您的问题:

问题 1:我应该将自定义点移到 /mnt 之外吗? 我认为如果您要在 /mnt 上安装东西,您可能应该这样做,因为您可能不想在临时设备上安装东西。磁盘,除非您打算这样做。

问题 2:cloud-init 的东西到底在做什么,我可以摆脱它吗? 我认为你不应该摆脱它。相反,您可以尝试以正确的方式重新配置安装点。

如何让它坚持下去? 假设您想要安装 Azure 温度。 /mnt/resource 上的磁盘。

方法 1(干净):创建一个新的 cloud-init conf。 /etc/cloud/cloud.cfg.d 中的文件定义挂载点:

    cat >> /etc/cloud/cloud.cfg.d/91-azure_datasource.cfg <<EOF
mounts:
- [ ephemeral0, /mnt/resource ]
EOF

方法 2(脏):从 temp 中删除 cloud-init 挂载选项。磁盘 fstab 条目使其看起来像这样:

/dev/disk/cloud/azure_resource-part1    /mnt    auto    defaults,nofail       0       2

方法3(讨厌):在cc_mounts.py中查找挂载点行,路径:/usr/lib/python3/dist-packages/cloudinit/config/cc_mounts.py并更改代码中的挂载点。该行应如下所示:

defmnts = [["ephemeral0", "/mnt", "auto", defvals[3], "0", "2"],
           ["swap", "none", "swap", "sw", "0", "0"]]

试一试,看看效果如何。

答案2

我发现的简单解决方法是在编辑文件后/etc/fstab设置chattr +i /etc/fstab,这样 cloud-init 就无法覆盖它

相关内容