Skip to content

Commit

Permalink
PUBAPI-1420 Add ability to mount NFS volumes with CreateMachine endpoint
Browse files Browse the repository at this point in the history
Reviewed by: Jerry Jelinek <jerry.jelinek@joyent.com>
Approved by: Julien Gilli <julien.gilli@joyent.com>
  • Loading branch information
joshwilsdon committed Sep 25, 2017
1 parent aa87923 commit f881d61
Show file tree
Hide file tree
Showing 8 changed files with 467 additions and 6 deletions.
71 changes: 68 additions & 3 deletions overlay/generic/lib/svc/method/mdata-fetch
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
#
# CDDL HEADER END
#
# Copyright (c) 2015, Joyent, Inc. All rights reserved.
# Copyright (c) 2017, Joyent, Inc. All rights reserved.
#

export PS4='[\D{%FT%TZ}] ${BASH_SOURCE}:${LINENO}: ${FUNCNAME[0]:+${FUNCNAME[0]}(): }'
Expand All @@ -37,6 +37,13 @@ if [ ! -x /usr/sbin/mdata-get ]; then
exit ${SMF_EXIT_ERR_FATAL}
fi

function fatal() {
if [[ -n $1 ]]; then
echo "FATAL: $*" >&2
fi
exit ${SMF_EXIT_ERR_FATAL}
}

# isAddr will test whether or not an IP address is valid. The first argument
# is the method to call on the Node "net" module, and the second one is the
# actual address to test.
Expand Down Expand Up @@ -96,8 +103,7 @@ fi

if [[ ! -e /.zonecontrol/metadata.sock ]]; then
# this is a bug since filewait should not have returned until file existed.
echo "missing /.zonecontrol/metadata.sock, Unable to start mdata:fetch"
exit ${SMF_EXIT_ERR_FATAL}
fatal "missing /.zonecontrol/metadata.sock, Unable to start mdata:fetch"
fi

# Update sysinfo to ensure values that come from metadata are populated.
Expand Down Expand Up @@ -191,6 +197,65 @@ if [[ $? == 0 && -n ${tmpfs} && -f /etc/vfstab ]]; then
fi
fi

#
# If we have NFS volumes, we'll add them to vfstab, and enable the nfs/client
# service so that will mount the volumes for us.
#
echo "Retrieving volume metadata..."

volumes_added=0
while IFS="|" read -r nfsvolume mountpoint name mode type; do

cat >&2 <<EOF
*** VOLUME ***
NFSVOLUME: ${nfsvolume}
MOUNTPOINT: ${mountpoint}
MODE: ${mode}
NAME: ${name}
TYPE: ${type}
EOF

if [[ -n ${type} && ${type} != "tritonnfs" ]]; then
fatal "unsupported volume type: ${type}"
fi

# if we don't have volume and mountpoint, we can't add vfstab lines
if [[ -z ${nfsvolume} || -z ${mountpoint} ]]; then
fatal "invalid volume specification (need both volume & mountpoint)"
fi

if ! grep "^${nfsvolume}[ ]" /etc/vfstab; then
if [[ -z ${mode} ]]; then
mode="rw"
fi

if [[ ${mode} != "rw" && ${mode} != "ro" ]]; then
fatal "invalid volume mode: '${mode}'"
fi

line=$(printf "%s - %s nfs - yes %s\n" "${nfsvolume}" "${mountpoint}" "${mode}")
volumes_added=$((${volumes_added}+1))

mkdir -p ${mountpoint}
echo "${line}" >> /etc/vfstab
fi
done < <(/usr/sbin/mdata-get sdc:volumes \
| /usr/bin/json \
-d '|' \
-a nfsvolume mountpoint name mode type)

if [[ ${volumes_added} -gt 0 ]]; then
for svc in \
svc:/network/nfs/nlockmgr:default \
svc:/network/nfs/status:default \
svc:/network/rpc/bind:default \
svc:/network/nfs/client:default \
; do

svcadm enable ${svc} || fatal "Unable to enable ${svc}"
done
fi

# We use the special sdc:nics value here though this is not an interface for
# use elsewhere. If this is changed please also update agent.js in the metadata
# agent.
Expand Down
6 changes: 4 additions & 2 deletions src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,8 @@ C_CHECK_TARGETS=\
sysevent.c \
dockerinit/src/dockerexec.c \
dockerinit/src/dockerinit.c \
dockerinit/src/docker-common.c
dockerinit/src/docker-common.c \
lx_hook_postnet/main.c

#
# Generic flags we apply by default to everything.
Expand Down Expand Up @@ -304,7 +305,7 @@ SMARTDC_LIB_TARGETS = \
#
# Subdirectory management
#
SUBDIRS = dockerinit routeinfo
SUBDIRS = dockerinit routeinfo lx_hook_postnet

SUBDIRS_ENV = \
CTFCONVERT=$(CTFCONVERT) \
Expand Down Expand Up @@ -413,6 +414,7 @@ install: all $(SUBDIRS)
mkdir -m 0755 -p $(DESTDIR)/usr/lib/sdc
cp -p net-boot-config $(DESTDIR)/usr/lib/sdc
cd routeinfo && $(MAKE) install $(SUBDIRS_ENV)
cd lx_hook_postnet && $(MAKE) install $(SUBDIRS_ENV)
mkdir -p $(SYSINFO_MOD_DIR)
rm -f $(SYSINFO_MOD_DIR)/sysinfo_mod.so
/usr/sbin/install -f $(SYSINFO_MOD_DIR) -m 0555 sysinfo_mod.so
Expand Down
3 changes: 2 additions & 1 deletion src/dockerinit/src/docker-common.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@ typedef enum {
ERR_CONTRACT,
ERR_MOUNT_DEVSHM,
ERR_INVALID_NFS_VOLUMES,
ERR_MOUNT_NFS_VOLUME
ERR_MOUNT_NFS_VOLUME,
ERR_UNKNOWN_VOLUME_TYPE
} dockerinit_err_t;

typedef enum {
Expand Down
2 changes: 2 additions & 0 deletions src/lx_hook_postnet/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.o
lx_hook_postnet
135 changes: 135 additions & 0 deletions src/lx_hook_postnet/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
#
# This file and its contents are supplied under the terms of the
# Common Development and Distribution License ("CDDL"), version 1.0.
# You may only use this file in accordance with the terms of version
# 1.0 of the CDDL.
#
# A full copy of the text of the CDDL should have accompanied this
# source. A copy of the CDDL is also available via the Internet at
# http://www.illumos.org/license/CDDL.
#

#
# Copyright (c) 2017, Joyent, Inc.
#

BASE := $(PWD)
DESTDIR = $(BASE)/../../proto
NATIVEDIR = $(BASE)/../../proto.strap

PROGRAM = lx_hook_postnet

GCC = $(NATIVEDIR)/usr/bin/gcc
GXX = $(NATIVEDIR)/usr/bin/g++

SYSINCDIRS = /usr/include
SYSLIBDIRS = /usr/lib /lib

CFLAGS = -std=gnu99 -Wall -Wextra -Werror -m32 -g \
-I$(DOCKER_DIR) \
-Wno-unused-parameter
CPPFLAGS = $(SYSINCDIRS:%=-isystem $(DESTDIR)/%)
LDFLAGS = $(SYSLIBDIRS:%=-L$(DESTDIR)/%) \
-Wl,-zassert-deflib -Wl,-zfatal-warnings

STRINGS_DIR = $(BASE)/../common/strings
DOCKER_DIR = $(BASE)/../dockerinit

LXINIT_HOOK_OBJECTS = \
main.o

MDATA_OBJECTS = \
dynstr.o \
proto.o \
common.o \
base64.o \
crc32.o \
reqid.o \
sunos.o \
unix_common.o

custr.o : CFLAGS += -I$(STRINGS_DIR)
mdata_%.o : CFLAGS += -I$(DOCKER_DIR)/mdata-client -D__HAVE_BOOLEAN_T
main.o: CFLAGS += -I$(STRINGS_DIR)
docker_common.o: CFLAGS += -I$(DOCKER_DIR)/src
strlist.o: CFLAGS += -I$(STRINGS_DIR)
strpath.o: CFLAGS += -I$(STRINGS_DIR)

OBJECTS = \
$(MDATA_OBJECTS:%=mdata_%) \
custr.o \
docker_common.o \
json-nvlist.o \
main.o \
strlist.o \
strpath.o

json-nvlist.o : CFLAGS += -I$(DOCKER_DIR)/json-nvlist -I$(STRINGS_DIR)

docker_common.o : CFLAGS += -I$(DOCKER_DIR)/json-nvlist -I$(STRINGS_DIR)

LIBS = -lnvpair -lnsl -lsocket -lsmbios -lgen

CTFCONVERT = /usr/bin/true
CTFMERGE = /usr/bin/true

CC=$(GCC)
CFLAGS += -Wall -Wextra -Werror -D__HAVE_BOOLEAN_T -m32
CSTYLE = ../../tools/cstyle

COMPILE_C = $(CC) $(CPPFLAGS) $(LDFLAGS) $(CFLAGS) -c -o $@ $<

INSTALL_PROG = mkdir -p $(@D) && rm -f $@ && cp $^ $@ && \
chmod 755 $@ && touch $@

.PHONY: all
all: $(PROGRAM)

.PHONY: check
check: $(LXINIT_HOOK_OBJECTS:%.o=%.check)

%.check: %.c
$(CSTYLE) -cPp $<

lint:
/usr/bin/true

clean:
rm -f $(OBJECTS)
rm -f $(PROGRAM)

#
# Compilation and linking targets
#
$(PROGRAM): $(OBJECTS)
$(CC) $(CPPFLAGS) $(LDFLAGS) $(CFLAGS) -o $@ $(OBJECTS) $(LIBS)
$(CTFMERGE) -L VERSION -o $@ $(OBJECTS)

%.o: $(STRINGS_DIR)/%.c
$(COMPILE_C)
$(CTFCONVERT) -L VERSION $@

mdata_%.o: $(DOCKER_DIR)/mdata-client/%.c
$(COMPILE_C)
$(CTFCONVERT) -L VERSION $@

mdata_%.o: $(DOCKER_DIR)/mdata-client/plat/%.c
$(COMPILE_C)
$(CTFCONVERT) -L VERSION $@

docker_common.o: $(DOCKER_DIR)/src/docker-common.c
$(COMPILE_C)
$(CTFCONVERT) -L VERSION $@

%.o: $(DOCKER_DIR)/json-nvlist/%.c
$(COMPILE_C)
$(CTFCONVERT) -L VERSION $@

#
# Installation
#
.PHONY: install
install: $(DESTDIR)/usr/lib/brand/lx/$(PROGRAM)

$(DESTDIR)/usr/lib/brand/lx/$(PROGRAM): $(PROGRAM)
$(INSTALL_PROG)

0 comments on commit f881d61

Please sign in to comment.