[stgt] [PATCH] Make backends loadable modules

Andy Grover agrover at redhat.com
Wed Aug 28 00:56:08 CEST 2013

On 08/20/2013 07:46 PM, Ronnie Sahlberg wrote:
> Tomo,
> Please find a second version of the patch to make the backing stores
> into loadable modules so that distributors can ship core tgtd as one
> package and individual backends as separate packages. This prevents
> having just a single module for everyone which depends on and
> requires a huge set of dependent packets.
> This is version 2 This version only changes bs.c into a shared
> library that the backend modules can link with at runtimie instead of
> as in the first patch changing all of tgtd into a shared library.
> regards ronnie sahlberg

Hi Ronnie,

I think you may be able to avoid making bs.c into a shared library if 
you link tgtd with the -E linker option.

I've attached a patch that Fedora tgt carried for a little while that 
modularized iser. I think my approach to iser modularization was bad and 
it got dropped, but you could loot it for some Makefile magic :)

Regards -- Andy
-------------- next part --------------
commit 1aa20610d4a3afc22e9a0b24efa4bbb7b02dfab2
Author: Andy Grover <agrover at redhat.com>
Date:   Wed Sep 28 11:21:38 2011 -0700

    Compile iser driver as a loadable module
    Drivers are already modularized in the code, so it is straightforward to
    change the build to target a separate loadable module. Also, modify
    get_driver_index to try to load a module when lld is unknown.
    This will allow packaging of base tgtd and iser module separately, so
    the majority of users will not have to install dependencies they don't
    Signed-off-by: Andy Grover <agrover at redhat.com>

diff --git a/usr/Makefile b/usr/Makefile
index e15ff6a..b8ab805 100644
--- a/usr/Makefile
+++ b/usr/Makefile
@@ -1,4 +1,5 @@
 sbindir ?= $(PREFIX)/sbin
+libdir ?= $(PREFIX)/lib/tgt
 ifneq ($(shell test -e /usr/include/linux/signalfd.h && echo 1),)
@@ -13,11 +14,6 @@ TGTD_OBJS += $(addprefix iscsi/, conn.o param.o session.o \
 TGTD_OBJS += bs_rdwr.o bs_aio.o
-ifneq ($(ISCSI_RDMA),)
-TGTD_OBJS += iscsi/iser.o iscsi/iser_text.o
-LIBS += -libverbs -lrdmacm
@@ -30,9 +26,13 @@ endif
 CFLAGS += -Wall -Wstrict-prototypes -fPIC
-LIBS += -lpthread
+LIBS += -lpthread -ldl
 PROGRAMS += tgtd tgtadm tgtimg
+ifneq ($(ISCSI_RDMA),)
+DRIVERS += tgt-iser.so
 TGTD_OBJS += tgtd.o mgmt.o target.o scsi.o log.o driver.o util.o work.o \
 		parser.o spc.o sbc.o mmc.o osd.o scc.o smc.o \
 		ssc.o bs_ssc.o libssc.o \
@@ -41,10 +41,12 @@ TGTD_OBJS += tgtd.o mgmt.o target.o scsi.o log.o driver.o util.o work.o \
 TGTD_DEP = $(TGTD_OBJS:.o=.d)
-all: $(PROGRAMS)
+LDFLAGS = -Wl,-E,-rpath=$(libdir)
 tgtd: $(TGTD_OBJS)
-	$(CC) $^ -o $@ $(LIBS)
+	$(CC) $^ -o $@ $(LDFLAGS) $(LIBS)
 -include $(TGTD_DEP)
@@ -58,15 +60,23 @@ tgtimg: tgtimg.o libssc.o libcrc32c.o
 -include tgtimg.d libssc.d
+tgt-iser.so: CFLAGS += -shared -libverbs -lrdmacm
+tgt-iser.so: iscsi/iser.o iscsi/iser_text.o
+	$(CC) $(CFLAGS) -o $@ $^
 %.o: %.c
 	$(CC) -c $(CFLAGS) $*.c -o $*.o
 	@$(CC) -MM $(CFLAGS) -MF $*.d -MT $*.o $*.c
 .PHONY: install
-install: $(PROGRAMS)
+install: $(PROGRAMS) $(DRIVERS)
 	install -d -m 755 $(DESTDIR)$(sbindir)
 	install -m 755 $(PROGRAMS) $(DESTDIR)$(sbindir)
+ifneq ($(DRIVERS),)
+	install -d -m 755 $(DESTDIR)$(libdir)
+	install -m 755 $(DRIVERS) $(DESTDIR)$(libdir)
 .PHONY: clean
-	rm -f *.[od] $(PROGRAMS) iscsi/*.[od] ibmvio/*.[od] fc/*.[od]
+	rm -f *.[od] $(PROGRAMS) *.so iscsi/*.[od] ibmvio/*.[od] fc/*.[od]
diff --git a/usr/tgtd.c b/usr/tgtd.c
index 30d5e9d..a9b2027 100644
--- a/usr/tgtd.c
+++ b/usr/tgtd.c
@@ -35,6 +35,7 @@
 #include <sys/epoll.h>
 #include <sys/types.h>
 #include <sys/wait.h>
+#include <dlfcn.h>
 #include "list.h"
 #include "tgtd.h"
@@ -411,6 +412,9 @@ static int lld_init(char *args)
 	int i, err, nr;
+	/* iser is modular, try to load it */
+	dlopen("tgt-iser.so", RTLD_LAZY);
 	for (i = nr = 0; tgt_drivers[i]; i++) {
 		if (tgt_drivers[i]->init) {
 			err = tgt_drivers[i]->init(i, args);

More information about the stgt mailing list