Previously I wrote about cloud-init as part of something a cloud service provider might offer. (Or perhaps I simply called it user-data; I don't accurately recall) Recently I have learned that it is something provisioned by the operating system itself. (Especially linux based servers). I do not know the history of the os feature as such, but if you are someone who plays around with Oracle Virtualbox for provisioning VMs then you need to know how to benefit from cloud-init.
I had to go through several webpages and perform several experiments to get it right. So in this post I will collate the steps for you to get easily started.
- (Step 1) TLDR: Grab the vmdk file. Create a blank VM (no disk) and attach the disk you have downloaded.
You need to grab a cloud image. Every distro has it. Its a vmdk file, that is it is a virtual box disk file. (Not the traditional iso). If you created a blank VM and attached this disk, and, powered up the VM, the os will boot and display the login prompt. But since you don't know the credentials, you cannot proceed to work with the vm instance any further. - Compose the cloud-init disk. Multiple steps required here. Hence find the dedicated section below
- Attach the cloud-init disk to the above, and, boot.
Goodbye secure practices👋🫡
Configuring the cloud-init disk
I will reproduce the commands in a slightly different way below. Now is probably a good time to check out some cloud-init docs and tutorial videos. It should give you a precursor to the stuff that I write, for e.g. in the user-data or meta-data file below. The tutorials you find online, are vastly different from what you are going to go through out here.
0. What am I doing here?
1. create a user-data file:
#cloud-config
users:
- default
ssh_pwauth: true
chpasswd: { expire: false }
preserve_hostname: False
hostname: osbox03
runcmd:
- [ ls, -l, / ]
- [ sh, -xc, "echo $(date) ': hello world!'" ]
- [ sh, -c, echo "=========hello world=========" ]
- [ mkdir, "/home/ubuntu/nodejs" ]
- [ wget, https://nodejs.org/dist/v20.11.1/node-v20.11.1-linux-x64.tar.xz, -O, /home/ubuntu/nodejs/node-v20.11.1-linux-x64.tar.xz ]
- [ tar, xvf, /home/ubuntu/nodejs/node-v20.11.1-linux-x64.tar.xz, -C, /home/ubuntu/nodejs/ ]
- [ ln, -s, /home/ubuntu/nodejs/node-v20.11.1-linux-x64/bin/node, /bin/node ]
- [ ln, -s, /home/ubuntu/nodejs/node-v20.11.1-linux-x64/bin/npx, /bin/npx ]
- [ ln, -s, /home/ubuntu/nodejs/node-v20.11.1-linux-x64/bin/npm, /bin/npm ]
- [ rm, /home/ubuntu/nodejs/node-v20.11.1-linux-x64.tar.xz ]
system_info:
default_user:
name: ubuntu
plain_text_passwd: 'ubuntu'
shell: /bin/bash
lock_passwd: false
gecos: ubuntu user
2. Create meta-data file:
instance-id: my-instance-1
3. Create the cloud-init disk:
# Create empty virtual hard drive file
dd if=/dev/zero of=config.img bs=1 count=0 seek=2M
# put correct filesystem and disk label on
mkfs.vfat -n cidata config.img
# mount it somewhere so you can put the config data on
sudo mount config.img /mnt
Copy the user-data and meta-data files to /mnt, and, then unmount:
sudo cp user-data meta-data /mnt
sudo umount /mnt
config.img is hydrated with the cloud-init setup. We need to convert the file with img extension to vmdk extension.
sudo apt-get install qemu-kvm
qemu-img convert -O vmdk config.img config.vmdk
Now attach config.vmdk to the VM created in step #1, and power it up.
Now after you have powered up your VM, you can physically log-in to the terminal. You can quickly inspect the /home/ubuntu/nodejs folder. If contents don't exist, you may have to wait a while for cloud-init to conclude its work. You can run the following commands to inspect the cloud-init output: