xref: /freebsd/libexec/rc/rc.d/nuageinit (revision 7fdf597e96a02165cfe22ff357b857d5fa15ed8a)
1#!/bin/sh
2#
3
4# PROVIDE: nuageinit
5# REQUIRE: mountcritlocal zfs devmatch
6# BEFORE: NETWORKING
7# KEYWORD: firstboot
8
9. /etc/rc.subr
10
11name="nuageinit"
12desc="Limited Cloud Init configuration"
13start_cmd="nuageinit_start"
14stop_cmd=":"
15rcvar="nuageinit_enable"
16
17fetch_openstack()
18{
19	cd /media/nuageinit/openstack/latest
20	for file in meta_data.json network_data.json user_data; do
21		fetch http://169.254.169.254/openstack/latest/$file || :
22	done
23	if [ -f user_data ]; then
24		chmod 755 user_data
25	fi
26	cd -
27}
28
29nuageinit_start()
30{
31	local citype
32	# detect cloud init provider
33	# according to the specification, the config drive
34	# is either formatted in vfat or iso9660 and labeled
35	# config-2
36	for f in iso9660 msdosfs; do
37		drive="/dev/$f/[cC][oO][nN][fF][iI][gG]-2"
38		if [ -e $drive ]; then
39			citype=config-2
40			break
41		fi
42		drive="/dev/$f/[cC][iI][dD][aA][tT][aA]"
43		if [ -e $drive ]; then
44			citype=nocloud
45			break
46		fi
47		unset drive
48	done
49	if [ -n "$drive" ]; then
50		mkdir -p /media/nuageinit
51		fs=$(fstyp $drive 2> /dev/null)
52		mount -t $fs $drive /media/nuageinit
53	else
54		product=$(kenv smbios.system.product)
55		case "$product" in
56		OpenStack*)
57			mkdir -p /media/nuageinit/openstack/latest
58			ifaces=$(ifconfig -l ether)
59			set -- $ifaces
60			dhclient -p /tmp/ephemeraldhcp.pid $1
61			fetch_openstack
62			pkill -F /tmp/ephemeraldhcp.pid
63			citype=config-2
64			;;
65		*)
66			# try to detect networked based instance
67			err 1 "Impossible to find a cloud init provider"
68			;;
69		esac
70	fi
71	# according to the specification, the content is either
72	# in the openstack or ec2 directory
73	case "$citype" in
74	config-2)
75		for d in openstack ec2; do
76			dir=/media/nuageinit/$d/latest
77			if [ -d $dir ]; then
78				/usr/libexec/nuageinit $dir $citype
79				break
80			fi
81		done
82		;;
83	nocloud)
84		/usr/libexec/nuageinit /media/nuageinit $citype
85		;;
86	esac
87	if [ -n "$drive" ]; then
88		umount /media/nuageinit
89		rmdir /media/nuageinit
90	else
91		rm -rf /media/nuageinit
92	fi
93}
94
95load_rc_config $name
96run_rc_command "$1"
97