使用cloud-init在裸机环境中

huangapple go评论54阅读模式
英文:

Use cloud-init in a bare metal environment

问题

I'm quite new to cloud init. I started documenting few days ago, but all what I found is related to KVM or cloud enviroments, and they does not rapresent my need.

我对cloud init相当新。我几天前开始记录,但我找到的所有内容都与KVM或云环境有关,它们不符合我的需求。

I want to use cloud init in an bare metal environment (I'm using centOS).

我想在裸机环境中使用cloud init(我正在使用CentOS)。

Let's say that I do not want to use cloud init for the network configuration and all the other modules that are provided.

假设我不想使用cloud init进行网络配置和提供的所有其他模块。

My need is to use cloud-init to launch a command when the server reboots...
Suppose that I want to launch a touch commad:

  • touch /root/hello.txt

我的需求是在服务器重新启动时使用cloud-init来启动命令...
假设我想启动一个touch命令:

  • touch /root/hello.txt

How do I have to configure cloud-init in order to achieve this?

为了实现这一点,我如何配置cloud-init?

I see that there are some files and folders inside /etc/cloud/:

我看到在/etc/cloud/下有一些文件和文件夹:

  • /etc/cloud/cloud.cfg
  • /etc/cloud/cloud.cfg.d/

I see that inside the files /etc/cloud/cloud.cfg there are all the boot stages...

我看到在文件/etc/cloud/cloud.cfg内有所有的引导阶段...

Can you explain me step-by-step what I have to do in order to have my cloud-init that execute the command touch /root/hello.txt at the next reboot (and get rid of all the other operations that are done).

您能否逐步解释我需要做什么,以便在下一次重新启动时执行命令touch /root/hello.txt(并摆脱所有其他操作)?

I suppose that I have to set Datasource: NoCloud, and crate meta-data and user-data, but I cannot figure out how to do that.

我想我必须设置Datasource: NoCloud,并创建meta-datauser-data,但我无法弄清楚如何做到这一点。

Thanks in advance!

提前感谢您!

英文:

I'm quite new to cloud init. I started documenting few days ago, but all what I found is related to KVM or cloud enviroments, and they does not rapresent my need.

I want to use cloud init in an bare metal environment (I'm using centOS).
Let's say that I do not want to use cloud init for the the network configuration and all the other modules that are provided.

My need is to use cloud-init to launch a command when the server reboots...
Suppose that I want to launch a touch commad:

  • touch /root/hello.txt

How do I have to configure cloud-init in order to achieve this?

I see that there are some files and folders inside /etc/cloud/:

  • /etc/cloud/cloud.cfg
  • /etc/cloud/cloud.cfg.d/

I see that inside the files /etc/cloud/cloud.cfg there are all the boot stages...

Can you explain me step-by-step what I have to do in order to have my cloud-init that execute the command touch /root/hello.txt at the next reboot (and get rid of all the other operations that are done).

I suppose that I have to set Datasource: NoCloud, and crate meta-data and user-data, but I cannot figure out how to do that.

Thanks in advance!

答案1

得分: 1

让我从解释如何在裸机上使用 cloud-init 开始。

您不必显式配置 cloud-init 来使用 NoCloud 数据源。只要您提供一个带有标签为 cidata 的 vfat 或 iso9660 文件系统,其中包含名为 meta-datauser-data 的文件,cloud-init 就会自行检测到它。对于裸机,您可以将文件系统放在一个 USB 驱动器上。例如:

cat > user-data <<EOF
#cloud-config
runcmd:
  - touch /root/hello.txt
EOF
touch meta-data
genisoimage -output seed.iso -volid cidata -joliet -rock user-data meta-data

然后将 seed.iso 直接写入 USB 驱动器。例如,如果您的 USB 驱动器是 /dev/sdc,运行:

dd if=seed.iso of=/dev/sdc

将 USB 驱动器插入新的机器并引导 cloud-init 映像。它应该会找到并使用您放入 user-data 的配置,并在第一次引导时创建一个空的 /root/hello.txt。请注意,runcmd 模块只会在每个实例的首次引导时运行这些命令。

听起来您的问题实际上是关于在每次引导时运行脚本的问题。如果您想使用 cloud-init 完成这个任务,请参阅如何使 cloud-init 启动脚本在每次我的 EC2 实例引导时运行以获取详细信息。

您提出问题的方式也让我怀疑您可能没有在初始设置中使用 cloud-init,而您所寻找的只是在现有安装的每次重新启动时运行命令的方法。如果是这种情况,那么 cloud-init 可能不是适合您的正确解决方案。您应该设置一个类型为 oneshot 的 systemd 服务。

英文:

Let me start by explaining how to use cloud-init on bare metal.

You do not have to explicitly configure cloud-init to use the NoCloud datasource. It will detect this on its own as long as you provide a vfat or iso9660 filesystem labeled cidata that contains files named meta-data and user-data. For bare metal, you could put the filesystem on a USB drive. For example:

cat > user-data <<EOF
#cloud-config
runcmd:
  - touch /root/hello.txt
EOF
touch meta-data
genisoimage -output seed.iso -volid cidata -joliet -rock user-data meta-data

Then write seed.iso directly to a USB drive. For example, if your USB drive is /dev/sdc, run:

dd if=seed.iso of=/dev/sdc

Insert the USB drive into your new machine and boot the cloud-init image. It should find and use the configuration you put into user-data, and create an empty /root/hello.txt on the first boot. Note that the runcmd module only runs these commands once per instance.

It sounds like your question is actually about running a script on every boot. If you want to do this with cloud-init, see How do I make cloud-init startup scripts run every time my EC2 instance boots for details.

The way you've written your question also makes me suspect that you're not using cloud-init for initial setup, and all you're looking for is a way to run a command on every reboot for an existing install. If that's the case, then cloud-init probably isn't the right solution for you. You should just set up a systemd service of type oneshot.

huangapple
  • 本文由 发表于 2023年5月7日 00:50:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76190073.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定