xref: /freebsd/libexec/rc/rc.d/nuageinit (revision 90a7728cd8905cd26b90d06f7873df8bad43ae9a)
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			for iface in $ifaces; do
60				dhclient -p /tmp/ephemeraldhcp.$iface.pid $iface
61			done
62			pids=$(cat /tmp/ephemeraldhcp.*.pid)
63			left=$(pwait -op $pids 2>/dev/null)
64			for iface in $left; do
65				kill -15 $left
66			done
67			fetch_openstack
68			citype=config-2
69			;;
70		*)
71			# try to detect networked based instance
72			err 1 "Impossible to find a cloud init provider"
73			;;
74		esac
75	fi
76	# according to the specification, the content is either
77	# in the openstack or ec2 directory
78	case "$citype" in
79	config-2)
80		for d in openstack ec2; do
81			dir=/media/nuageinit/$d/latest
82			if [ -d $dir ]; then
83				/usr/libexec/nuageinit $dir $citype 2>&1 | tee -a /var/log/nuageinit.log
84				break
85			fi
86		done
87		;;
88	nocloud)
89		/usr/libexec/nuageinit /media/nuageinit $citype 2>&1 | tee -a /var/log/nuageinit.log
90		;;
91	esac
92	if [ -n "$drive" ]; then
93		umount /media/nuageinit
94		rmdir /media/nuageinit
95	else
96		rm -rf /media/nuageinit
97	fi
98}
99
100load_rc_config $name
101run_rc_command "$1"
102