Skip to contentSkip to conversation

libqblock qemu block layer library

PATCH Superseded Wenchao Xia 2 participants 15 messages Nov 28, 2012 – Nov 29, 2012 Kbuild

v12 of 13 Superseded Latest is v13 →

lore
Share & tools
lore
#0 lore raw

v12 cover libqblock qemu block layer library

These patches introduce libqblock API, make subdir-libqblock and make
check-libqblock could build this library.
Functionalities:
1 create a new image.
2 sync access of an image.
3 basic image information retrieving such as backing file. 4 detect if a sector is allocated in an image.
Supported Formats:
ALL using file protocols.

Patch 1 to 3 prepares qemu to accept libqblock.

v2:
Insert reserved bytes into union.
Use uint64_t instead of size_t, offset.
Use const char * in filename pointer.
Initialization function removed and it was automatically executed when
library is loaded.
Added compile flag visibility=hidden, to avoid name space pollution. Structure naming style changed.
Using byte unit instead of sector for every API.
Added a member in image static information structure, to report logical
sector size, which is always 512 now.
Read and write API can take request not aligned to 512 now. It returns the
byte number that have succeed in operation, but now either negative value or the number requested would be returned, because qemu block sync I/O API would not return such number.
Typo fix due to comments and improved documents.

v3:
Removed the code about OOM error, introduced GError.
Used a table to map from string to enum types about format.
Use typedef for every structure.
Improved the gcc compiler macro to warn if gcc was not used. Global variable name changed with prefix libqb_.
The struct QBlockStaticInfo was changed to folder full format related
information inside, and a new member with pointers pointing to the mostly used members, such as backing file, virt size, was added. This would allow the user to get full information about how it is created in the future.
Each patch in the serial can work with qemu now.
Typo fixes.

v4:
Renamed QBroker to QBlockContext.
Removed tool objs out of libqblock.
Added a check in initialization about structure size for ABI. Added a new helper API to duplicate protocol information, helps to open files
in a backing file chain.
Check-libqblock will not rebuild libqblock every time now.
Test case file renamed to "libqblock-[FMT].c".
Test use gtest framework now.
Test do random creation of test file now, added check for information API in
it.
Test do random sync io instead of fixed offset io now.
Test accept one parameter about where to place the test image, now it is
./tests/libqblock/test_images.

v5:
Makefile of libqblock was adjusted to be similar as libcacard, added spec
file and install section.
Removed warning when GCC was not found.
Structure names were changed to better ones.
Removed the union typedef that contain reserved bytes to reduce the folder
depth.
Some format related enum options was changed to better name. Added accessors about image static information, hide indirect accessing
member detail in the structure.
Test Makefile do not create diretory now, test case create it themself. Test build system do not use libtool now, and removed qtest-obj-y in its
dependency, make check will automatically execute test anyway now.
Removed "ifeq ($(LIBTOOL),)" in Makefile.

v6:
Remove address pointer member in image static info structure.

v7:
Support out of tree building.

v8:
Fix a bug in out of tree building.

v9:
Rebase and splitted out small fix patch for qemu.

v10:
Rebased to upstream, adjusted libqblock build system according to Paolo's
comments.

v11:
Adjusting code in patch 4 to 7, details are in the child patch's commit
message.

v12:
Split a patch to add a function in stubs, other change are in patch 4 to 7
commit messages.

Wenchao Xia (7):
Build system clean tests directory clearly
block export function path_has_protocol
stubs add function qemu_set_fd_handler
libqblock build system
libqblock API design and type defines
libqblock API implement
libqblock test example

 .gitignore                     |    2 +
 Makefile                       |   31 +-
 block.c                        |    2 +-
 block.h                        |    2 +
 configure                      |   41 ++-
 libqblock/Makefile             |   69 +++
 libqblock/libqblock-aio.c      |   91 +++
 libqblock/libqblock-aio.h      |    8 +
 libqblock/libqblock-error.c    |   57 ++
 libqblock/libqblock-error.h    |   49 ++
 libqblock/libqblock-internal.h |   68 +++
 libqblock/libqblock-types.h    |  245 +++++++++
 libqblock/libqblock.c          | 1188 ++++++++++++++++++++++++++++++++++++++++
 libqblock/libqblock.h          |  356 ++++++++++++
 libqblock/libqblock.pc.in      |   13 +
 stubs/set-fd-handler.c         |    8 +
 tests/Makefile                 |   38 ++-
 tests/check-libqblock-qcow2.c  |  395 +++++++++++++
 18 files changed, 2653 insertions(+), 10 deletions(-)
 create mode 100644 libqblock/Makefile
 create mode 100644 libqblock/libqblock-aio.c
 create mode 100644 libqblock/libqblock-aio.h
 create mode 100644 libqblock/libqblock-error.c
 create mode 100644 libqblock/libqblock-error.h
 create mode 100644 libqblock/libqblock-internal.h
 create mode 100644 libqblock/libqblock-types.h
 create mode 100644 libqblock/libqblock.c
 create mode 100644 libqblock/libqblock.h
 create mode 100644 libqblock/libqblock.pc.in
 create mode 100644 tests/check-libqblock-qcow2.c
#1 lore raw

PATCH v12 1/7 Build system clean tests directory clearly

Currently root Makefile try clean tests/tcg, hard to extend.
This patch added command make check-clean, which clean all generated files used in tests. With this command root Makefile do not care tests clean method any more, it simply calls the command to do it, so any more clean script could be added in tests/Makefile, make it easier to extend.

v2:
Discard env MAKEFILES, always include tests/Makefile.

---
 Makefile       |    6 ++----
 configure      |    2 +-
 tests/Makefile |    7 +++++++
 3 files changed, 10 insertions(+), 5 deletions(-)
Makefile +2 -4
index 9ecbcbb..b04d862 100644
@@ -211,9 +211,7 @@ qemu-ga$(EXESUF): QEMU_CFLAGS += -I qga/qapi-generated
 
 gen-out-type = $(subst .,-,$(suffix $@))
 
-ifneq ($(wildcard config-host.mak),)
-include $(SRC_PATH)/tests/Makefile
-endif
+include tests/Makefile
 
 qapi-py = $(SRC_PATH)/scripts/qapi.py $(SRC_PATH)/scripts/ordereddict.py
 
@@ -259,7 +257,7 @@ clean:
 	rm -f $(foreach f,$(GENERATED_SOURCES),$(f) $(f)-timestamp)
 	rm -rf qapi-generated
 	rm -rf qga/qapi-generated
-	$(MAKE) -C tests/tcg clean
+	$(MAKE) check-clean
 	for d in $(ALL_SUBDIRS) $(QEMULIBS) libcacard; do \
 	if test -d $$d; then $(MAKE) -C $$d $@ || exit 1; fi; \
 	rm -f $$d/qemu-options.def; \
configure +1 -1
index 780b19a..801585c 100755
@@ -4166,7 +4166,7 @@ DIRS="$DIRS pc-bios/optionrom pc-bios/spapr-rtas"
 DIRS="$DIRS roms/seabios roms/vgabios"
 DIRS="$DIRS qapi-generated"
 DIRS="$DIRS libcacard libcacard/libcacard libcacard/trace"
-FILES="Makefile tests/tcg/Makefile qdict-test-data.txt"
+FILES="Makefile tests/Makefile tests/tcg/Makefile qdict-test-data.txt"
 FILES="$FILES tests/tcg/cris/Makefile tests/tcg/cris/.gdbinit"
 FILES="$FILES tests/tcg/lm32/Makefile libcacard/Makefile"
 FILES="$FILES pc-bios/optionrom/Makefile pc-bios/keymaps"
tests/Makefile +7 -0
index b60f0fb..fc816d0 100644
@@ -98,6 +98,7 @@ check-help:
 	@echo " make check-unit           Run qobject tests"
 	@echo " make check-block          Run block tests"
 	@echo " make check-report.html    Generates an HTML test report"
+	@echo " make check-clean          Clean the tests"
 	@echo
 	@echo "Please note that HTML reports do not regenerate if the unit tests"
 	@echo "has not changed."
@@ -152,4 +153,10 @@ check-unit: $(patsubst %,check-%, $(check-unit-y))
 check-block: $(patsubst %,check-%, $(check-block-y))
 check: check-unit check-qtest
 
+check-clean:
+	$(MAKE) -C tests/tcg clean
+	rm -f $(check-unit-y)
+	rm -f $(check-qtest-i386-y) $(check-qtest-x86_64-y) $(check-qtest-sparc64-y) $(check-qtest-sparc-y)
+	rm -f tests/*.o
+
 -include $(wildcard tests/*.d)
Signature · 1 line
1.7.1
#2 lore raw

PATCH v12 2/7 block export function path_has_protocol

In reply to Wenchao Xia

This function is needed in other module, so export it. There is
already some patch on mail-list try export it, If that patch was applied, pls ignore this one.

---
 block.c |    2 +-
 block.h |    2 ++
 2 files changed, 3 insertions(+), 1 deletions(-)
block.c +1 -1
index c05875f..5c8b542 100644
@@ -199,7 +199,7 @@ static void bdrv_io_limits_intercept(BlockDriverState *bs,
 }
 
 /* check if the path starts with "<protocol>:" */
-static int path_has_protocol(const char *path)
+int path_has_protocol(const char *path)
 {
     const char *p;
 
block.h +2 -0
index 722c620..6805245 100644
@@ -428,6 +428,8 @@ typedef enum {
     BLKDBG_EVENT_MAX,
 } BlkDebugEvent;
 
+int path_has_protocol(const char *path);
+
 #define BLKDBG_EVENT(bs, evt) bdrv_debug_event(bs, evt)
 void bdrv_debug_event(BlockDriverState *bs, BlkDebugEvent event);
 
Signature · 1 line
1.7.1
#3 lore raw

PATCH v12 3/7 stubs add function qemu_set_fd_handler

In reply to Wenchao Xia

This function is needed by libqblock linkage, so add it.

---
 stubs/set-fd-handler.c |    8 ++++++++
 1 files changed, 8 insertions(+), 0 deletions(-)
stubs/set-fd-handler.c +8 -0
index 4807b5d..7b4ca4f 100644
@@ -9,3 +9,11 @@ int qemu_set_fd_handler2(int fd,
 {
     abort();
 }
+
+int qemu_set_fd_handler(int fd,
+                        IOHandler *fd_read,
+                        IOHandler *fd_write,
+                        void *opaque)
+{
+    abort();
+}
Signature · 1 line
1.7.1
#4 lore raw

PATCH v12 4/7 libqblock build system

In reply to Wenchao Xia

Libqblock was placed in new directory ./libqblock, libtool will build
dynamic library there, source files of block layer remains in ./block. So block related source code will generate 3 sets of binary, first is old ones used in qemu, second and third are non PIC and PIC ones in ./libqblock.
GCC compiler flag visibility=hidden was used with special macro, to export
only symbols that was marked as PUBLIC.
For testing, make check-libqblock will build binaries and execute it, make
clean or make check-clean will delete generated binaries.
By default this library will be built and tested if libtool present, out of
tree building is supported.

v10:
Use $(SRC_PATH) in install rules.
Call install libqblock from install target in root Makefile.
Make libqblock and check-libqblock is conditional to a configure option and
libtool now, if libtool is not present it is forced to be disabled.
Removed unnest-vars in libqblock Makfile for that it was done in Makefile.objs. Picked out only needed objects files for libqblock, added stub objects in
libqblock linking.
Use libtool to link check-libqblock.
Removed -fPIC cc flag replacement in tests/Makefile.
Removed seperate directory for libqblock test case.
Added target subdir-libqblock.
Added targets to .phony.
Generate files at root directory instead of ./libqblock.

v11:
Set libqblock initial string to empty instead of “yes” in configure, by default
it will be set depending on libtool’s state, fail when user set it to yes but libtool not present.
Removed link flag in check-libqblock linkage, use libtool to search library. Added libqblock-aio.c, which contains code from main-loop.c, iohandler.c
compatfd.c, removed these three files from compile and link objects.

v12:
libqblock/libqblock-aio.c, this file is kept in this patch which will be
changed in following patch to introduce API needed by libqblock.
tests/Makefile, Removed variable LIBQBLOCK_DIR, Renamed LTLINK_CHECK_LIBQBLOCK
to LTLINK, removed space in LTLINK command "lt LINK $(TARGET_DIR) $@".
tests/Makefile, use only gtester option -k, to avoid silent fail for segment fault. libqblock/Makefile, use stubs-obj-y directly for that symbol conflict solved.

---
 .gitignore                    |    2 +
 Makefile                      |   25 +++++++++++++-
 configure                     |   39 +++++++++++++++++++++++
 libqblock/Makefile            |   69 +++++++++++++++++++++++++++++++++++++++++
 libqblock/libqblock-aio.c     |   62 ++++++++++++++++++++++++++++++++++++
 libqblock/libqblock.c         |    6 +++
 libqblock/libqblock.h         |    1 +
 libqblock/libqblock.pc.in     |   13 ++++++++
 tests/Makefile                |   31 +++++++++++++++++-
 tests/check-libqblock-qcow2.c |    6 +++
 10 files changed, 250 insertions(+), 4 deletions(-)
 create mode 100644 libqblock/Makefile
 create mode 100644 libqblock/libqblock-aio.c
 create mode 100644 libqblock/libqblock-error.c
 create mode 100644 libqblock/libqblock-error.h
 create mode 100644 libqblock/libqblock-types.h
 create mode 100644 libqblock/libqblock.c
 create mode 100644 libqblock/libqblock.h
 create mode 100644 libqblock/libqblock.pc.in
 create mode 100644 tests/check-libqblock-qcow2.c
.gitignore +2 -0
index bd6ba1c..77c1910 100644
@@ -93,3 +93,5 @@ cscope.*
 tags
 TAGS
 *~
+tests/check-libqblock-qcow2
+tests/test_images
Makefile +23 -2
index b04d862..dd01fe7 100644
@@ -195,6 +195,27 @@ qemu-img$(EXESUF): qemu-img.o $(tools-obj-y) $(block-obj-y) libqemustub.a
 qemu-nbd$(EXESUF): qemu-nbd.o $(tools-obj-y) $(block-obj-y) libqemustub.a
 qemu-io$(EXESUF): qemu-io.o cmd.o $(tools-obj-y) $(block-obj-y) libqemustub.a
 
+######################################################################
+# Support building shared library libqblock
+.PHONY: install-libqblock subdir-libqblock
+
+ifeq ($(CONFIG_LIBQBLOCK), y)
+subdir-libqblock: $(GENERATED_HEADERS) $(GENERATED_SOURCES)
+	$(call quiet-command,$(MAKE) $(SUBDIR_MAKEFLAGS) -C libqblock V="$(V)" TARGET_DIR="$*/" all,)
+
+install-libqblock: subdir-libqblock
+	$(call quiet-command,$(MAKE) $(SUBDIR_MAKEFLAGS) -C libqblock V="$(V)" TARGET_DIR="$*/" install-libqblock,)
+else
+LIBQBLOCK_WARN = "Libqblock was not enabled, skip. Make sure libtool was installed and libqblock was enabled."
+subdir-libqblock: $(GENERATED_HEADERS) $(GENERATED_SOURCES)
+	@echo $(LIBQBLOCK_WARN)
+
+install-libqblock:
+	@echo $(LIBQBLOCK_WARN)
+endif
+
+###########################################################################
+
 qemu-bridge-helper$(EXESUF): qemu-bridge-helper.o
 
 vscclient$(EXESUF): $(libcacard-y) $(oslib-obj-y) $(trace-obj-y) libcacard/vscclient.o libqemustub.a
@@ -258,7 +279,7 @@ clean:
 	rm -rf qapi-generated
 	rm -rf qga/qapi-generated
 	$(MAKE) check-clean
-	for d in $(ALL_SUBDIRS) $(QEMULIBS) libcacard; do \
+	for d in $(ALL_SUBDIRS) $(QEMULIBS) libcacard libqblock; do \
 	if test -d $$d; then $(MAKE) -C $$d $@ || exit 1; fi; \
 	rm -f $$d/qemu-options.def; \
         done
@@ -331,7 +352,7 @@ install-confdir:
 install-sysconfig: install-datadir install-confdir
 	$(INSTALL_DATA) $(SRC_PATH)/sysconfigs/target/target-x86_64.conf "$(DESTDIR)$(qemu_confdir)"
 
-install: all $(if $(BUILD_DOCS),install-doc) install-sysconfig install-datadir
+install: all $(if $(BUILD_DOCS),install-doc) install-sysconfig install-datadir $(if $(CONFIG_LIBQBLOCK),install-libqblock)
 	$(INSTALL_DIR) "$(DESTDIR)$(bindir)"
 ifneq ($(TOOLS),)
 	$(INSTALL_PROG) $(STRIP_OPT) $(TOOLS) "$(DESTDIR)$(bindir)"
configure +39 -0
index 801585c..b0bf912 100755
@@ -223,6 +223,7 @@ libiscsi=""
 coroutine=""
 seccomp=""
 glusterfs=""
+libqblock=""
 
 # parse CC options first
 for opt do
@@ -871,6 +872,10 @@ for opt do
   ;;
   --enable-glusterfs) glusterfs="yes"
   ;;
+  --disable-libqblock) libqblock="no"
+  ;;
+  --enable-libqblock) libqblock="yes"
+  ;;
   *) echo "ERROR: unknown option $opt"; show_help="yes"
   ;;
   esac
@@ -1119,6 +1124,8 @@ echo "  --with-coroutine=BACKEND coroutine backend. Supported options:"
 echo "                           gthread, ucontext, sigaltstack, windows"
 echo "  --enable-glusterfs       enable GlusterFS backend"
 echo "  --disable-glusterfs      disable GlusterFS backend"
+echo "  --enable-libqblock       enable dynamic library libqblock"
+echo "  --disable-libqblock      disable dynamic library libqblock"
 echo ""
 echo "NOTE: The object files are built at the place where configure is launched"
 exit 1
@@ -2349,6 +2356,28 @@ EOF
   fi
 fi
 
+##########################################
+# libqblock probe
+if test "$libqblock" == "yes" ; then
+    # if it is set to yes by user, check whether libtool exist
+    if ! has $libtool; then
+        echo
+        echo "ERROR: Libqblock needs libtool, but libtool was not found."
+        echo "Make sure libtool was installed, or disable libqblock."
+        echo
+        exit 1
+    fi
+fi
+
+if test "$libqblock" == "" ; then
+    # libqblock depends on libtool, default to yes if libtool exist
+    if ! has $libtool; then
+        libqblock="no"
+    else
+        libqblock="yes"
+    fi
+fi
+
 #
 # Check for xxxat() functions when we are building linux-user
 # emulator.  This is done because older glibc versions don't
@@ -3101,6 +3130,9 @@ if test "$want_tools" = "yes" ; then
   if [ "$linux" = "yes" -o "$bsd" = "yes" -o "$solaris" = "yes" ] ; then
     tools="qemu-nbd\$(EXESUF) $tools"
   fi
+  if test "$libqblock" = "yes" ; then
+  tools="subdir-libqblock $tools"
+  fi
 fi
 if test "$softmmu" = yes ; then
   if test "$virtfs" != no ; then
@@ -3235,6 +3267,7 @@ echo "build guest agent $guest_agent"
 echo "seccomp support   $seccomp"
 echo "coroutine backend $coroutine_backend"
 echo "GlusterFS support $glusterfs"
+echo "libqblock support $libqblock"
 
 if test "$sdl_too_old" = "yes"; then
 echo "-> Your SDL version is too old - please upgrade to have SDL support"
@@ -3581,6 +3614,10 @@ if test "$glusterfs" = "yes" ; then
   echo "CONFIG_GLUSTERFS=y" >> $config_host_mak
 fi
 
+if test "$libqblock" = "yes" ; then
+  echo "CONFIG_LIBQBLOCK=y" >> $config_host_mak
+fi
+
 # USB host support
 case "$usb" in
 linux)
@@ -4166,12 +4203,14 @@ DIRS="$DIRS pc-bios/optionrom pc-bios/spapr-rtas"
 DIRS="$DIRS roms/seabios roms/vgabios"
 DIRS="$DIRS qapi-generated"
 DIRS="$DIRS libcacard libcacard/libcacard libcacard/trace"
+DIRS="$DIRS libqblock"
 FILES="Makefile tests/Makefile tests/tcg/Makefile qdict-test-data.txt"
 FILES="$FILES tests/tcg/cris/Makefile tests/tcg/cris/.gdbinit"
 FILES="$FILES tests/tcg/lm32/Makefile libcacard/Makefile"
 FILES="$FILES pc-bios/optionrom/Makefile pc-bios/keymaps"
 FILES="$FILES pc-bios/spapr-rtas/Makefile"
 FILES="$FILES roms/seabios/Makefile roms/vgabios/Makefile"
+FILES="$FILES libqblock/Makefile"
 for bios_file in \
     $source_path/pc-bios/*.bin \
     $source_path/pc-bios/*.rom \
libqblock/Makefile new file +69 -0

69 lines not on this page · raw message

libqblock/libqblock-aio.c new file +62 -0

62 lines not on this page · raw message

libqblock/libqblock-error.c new file +0 -0
new file mode 100644
index 0000000..e69de29
libqblock/libqblock-error.h new file +0 -0
new file mode 100644
index 0000000..e69de29
libqblock/libqblock-types.h new file +0 -0
new file mode 100644
index 0000000..e69de29
libqblock/libqblock.c new file +6 -0
new file mode 100644
index 0000000..4c18c41
@@ -0,0 +1,6 @@
+#include "libqblock.h"
+
+int libqb_test(void)
+{
+    return 0;
+}
libqblock/libqblock.h new file +1 -0
new file mode 100644
index 0000000..b0f9daf
@@ -0,0 +1 @@
+__attribute__((visibility("default"))) int libqb_test(void);
libqblock/libqblock.pc.in new file +13 -0
new file mode 100644
index 0000000..d2a7d06
@@ -0,0 +1,13 @@
+prefix=@PREFIX@
+exec_prefix=${prefix}
+libdir=@LIBDIR@
+includedir=@INCLUDEDIR@
+
+Name: qblock
+Description: QEMU block layer library
+Version: @VERSION@
+
+Requires:  rt gthread-2.0 glib-2.0 z curl cap-ng uuid
+Libs: -L${libdir} -lqblock
+Libs.private:
+Cflags: -I${includedir}
tests/Makefile +29 -2
index fc816d0..76de5f4 100644
@@ -88,6 +88,20 @@ check-qtest-$(CONFIG_POSIX)=$(foreach TARGET,$(TARGETS), $(check-qtest-$(TARGET)
 qtest-obj-y = tests/libqtest.o $(oslib-obj-y) libqemustub.a
 $(check-qtest-y): $(qtest-obj-y)
 
+#libqblock build rules
+check-libqblock-$(CONFIG_LIBQBLOCK) = tests/check-libqblock-qcow2$(EXESUF)
+
+$(check-libqblock-y): QEMU_INCLUDES += -I$(SRC_PATH)/tests -I$(SRC_PATH)/libqblock
+LIBQBLOCK_TEST_DIR = tests/test_images
+LIBQBLOCK_LA = libqblock/libqblock.la
+
+LTLINK = $(call quiet-command, $(LIBTOOL) --mode=link --quiet --tag=CC \
+         $(CC) $(QEMU_CFLAGS) $(CFLAGS) $(LDFLAGS) \
+         -o $@ $(sort $(1)) $(LIBS), "lt LINK $(TARGET_DIR)$@")
+
+$(check-libqblock-y): %$(EXESUF): %.o
+	$(call LTLINK, $^ $(LIBQBLOCK_LA))
+
 .PHONY: check-help
 check-help:
 	@echo "Regression testing targets:"
@@ -98,6 +112,7 @@ check-help:
 	@echo " make check-unit           Run qobject tests"
 	@echo " make check-block          Run block tests"
 	@echo " make check-report.html    Generates an HTML test report"
+	@echo " make check-libqblock      Run libqblock tests"
 	@echo " make check-clean          Clean the tests"
 	@echo
 	@echo "Please note that HTML reports do not regenerate if the unit tests"
@@ -130,6 +145,15 @@ $(patsubst %, check-report-qtest-%.xml, $(QTEST_TARGETS)): check-report-qtest-%.
 check-report-unit.xml: $(check-unit-y)
 	$(call quiet-command,gtester -q $(GTESTER_OPTIONS) -o $@ -m=$(SPEED) $^, "GTESTER $@")
 
+# gtester tests with libqblock
+
+# remove quite option to show error if program is terminated by seg fault, otherwise it fails silently.
+GTESTER_OPTIONS_LIBQBLOCK:= -k
+
+.PHONY: $(patsubst %, check-%, $(check-libqblock-y))
+$(patsubst %, check-%, $(check-libqblock-y)): check-%: %
+	$(call quiet-command, LIBQBLOCK_TEST_DIR=$(LIBQBLOCK_TEST_DIR) gtester $(GTESTER_OPTIONS_LIBQBLOCK) -m=$(SPEED) $*,"GTESTER $*")
+
 # Reports and overall runs
 
 check-report.xml: $(patsubst %,check-report-qtest-%.xml, $(QTEST_TARGETS)) check-report-unit.xml
@@ -147,16 +171,19 @@ check-tests/qemu-iotests-quick.sh: tests/qemu-iotests-quick.sh qemu-img$(EXESUF)
 
 # Consolidated targets
 
-.PHONY: check-qtest check-unit check
+.PHONY: check-qtest check-unit check check-libqblock check-clean
 check-qtest: $(patsubst %,check-qtest-%, $(QTEST_TARGETS))
 check-unit: $(patsubst %,check-%, $(check-unit-y))
 check-block: $(patsubst %,check-%, $(check-block-y))
-check: check-unit check-qtest
+check-libqblock: $(patsubst %,check-%, $(check-libqblock-y))
+check: check-libqblock check-unit check-qtest
 
 check-clean:
 	$(MAKE) -C tests/tcg clean
 	rm -f $(check-unit-y)
 	rm -f $(check-qtest-i386-y) $(check-qtest-x86_64-y) $(check-qtest-sparc64-y) $(check-qtest-sparc-y)
 	rm -f tests/*.o
+	rm -f $(check-libqblock-y)
+	rm -rf $(LIBQBLOCK_TEST_DIR)
 
 -include $(wildcard tests/*.d)
tests/check-libqblock-qcow2.c new file +6 -0
new file mode 100644
index 0000000..50a4df3
@@ -0,0 +1,6 @@
+#include "libqblock.h"
+int main(int argc, char **argv)
+{
+    libqb_test();
+    return 0;
+}
Signature · 1 line
1.7.1
#5 lore raw

Re: [Qemu-devel] [PATCH V12 4/7] libqblock build system

Ok, I'm pretty confident that v13 will be the last one. :)

Il 28/11/2012 09:55, Wenchao Xia ha scritto:

configure echo " --with-coroutine=BACKEND coroutine backend. Supported options:" · line 1128

Quoted 6 lines
configure +2 -0
@@ -1119,6 +1124,8 @@ echo "  --with-coroutine=BACKEND coroutine backend. Supported options:"
 echo "                           gthread, ucontext, sigaltstack, windows"
 echo "  --enable-glusterfs       enable GlusterFS backend"
 echo "  --disable-glusterfs      disable GlusterFS backend"
+echo "  --enable-libqblock       enable dynamic library libqblock"
+echo "  --disable-libqblock      disable dynamic library libqblock"

"shared" library, not dynamic.

Quoted 253 lines
configure +32 -0
 echo ""
 echo "NOTE: The object files are built at the place where configure is launched"
 exit 1
@@ -2349,6 +2356,28 @@ EOF
   fi
 fi
 
+##########################################
+# libqblock probe
+if test "$libqblock" == "yes" ; then
+    # if it is set to yes by user, check whether libtool exist
+    if ! has $libtool; then
+        echo
+        echo "ERROR: Libqblock needs libtool, but libtool was not found."
+        echo "Make sure libtool was installed, or disable libqblock."
+        echo
+        exit 1
+    fi
+fi
+
+if test "$libqblock" == "" ; then
+    # libqblock depends on libtool, default to yes if libtool exist
+    if ! has $libtool; then
+        libqblock="no"
+    else
+        libqblock="yes"
+    fi
+fi
+
 #
 # Check for xxxat() functions when we are building linux-user
 # emulator.  This is done because older glibc versions don't
@@ -3101,6 +3130,9 @@ if test "$want_tools" = "yes" ; then
   if [ "$linux" = "yes" -o "$bsd" = "yes" -o "$solaris" = "yes" ] ; then
     tools="qemu-nbd\$(EXESUF) $tools"
   fi
+  if test "$libqblock" = "yes" ; then
+  tools="subdir-libqblock $tools"
+  fi
 fi
 if test "$softmmu" = yes ; then
   if test "$virtfs" != no ; then
@@ -3235,6 +3267,7 @@ echo "build guest agent $guest_agent"
 echo "seccomp support   $seccomp"
 echo "coroutine backend $coroutine_backend"
 echo "GlusterFS support $glusterfs"
+echo "libqblock support $libqblock"
 
 if test "$sdl_too_old" = "yes"; then
 echo "-> Your SDL version is too old - please upgrade to have SDL support"
@@ -3581,6 +3614,10 @@ if test "$glusterfs" = "yes" ; then
   echo "CONFIG_GLUSTERFS=y" >> $config_host_mak
 fi
 
+if test "$libqblock" = "yes" ; then
+  echo "CONFIG_LIBQBLOCK=y" >> $config_host_mak
+fi
+
 # USB host support
 case "$usb" in
 linux)
@@ -4166,12 +4203,14 @@ DIRS="$DIRS pc-bios/optionrom pc-bios/spapr-rtas"
 DIRS="$DIRS roms/seabios roms/vgabios"
 DIRS="$DIRS qapi-generated"
 DIRS="$DIRS libcacard libcacard/libcacard libcacard/trace"
+DIRS="$DIRS libqblock"
 FILES="Makefile tests/Makefile tests/tcg/Makefile qdict-test-data.txt"
 FILES="$FILES tests/tcg/cris/Makefile tests/tcg/cris/.gdbinit"
 FILES="$FILES tests/tcg/lm32/Makefile libcacard/Makefile"
 FILES="$FILES pc-bios/optionrom/Makefile pc-bios/keymaps"
 FILES="$FILES pc-bios/spapr-rtas/Makefile"
 FILES="$FILES roms/seabios/Makefile roms/vgabios/Makefile"
+FILES="$FILES libqblock/Makefile"
 for bios_file in \
     $source_path/pc-bios/*.bin \
     $source_path/pc-bios/*.rom \
libqblock/Makefile new file +69 -0
new file mode 100644
index 0000000..13f9522
@@ -0,0 +1,69 @@
+###########################################################################
+# libqblock Makefile
+##########################################################################
+-include ../config-host.mak
+-include $(SRC_PATH)/rules.mak
+-include $(SRC_PATH)/Makefile.objs
+
+#############################################################################
+# Library settings
+#############################################################################
+# src files are in orignial path
+$(call set-vpath, $(SRC_PATH))
+# generated files are in ../
+$(call set-vpath, ../)
+
+#library objects
+libqblock-obj-y= libqblock/libqblock.o libqblock/libqblock-error.o libqblock/libqblock-aio.o
+
+extra-obj-y= qemu-timer.o qemu-tool.o
+
+QEMU_OBJS= $(libqblock-obj-y) $(block-obj-y) $(oslib-obj-y) $(stub-obj-y) \
+           $(extra-obj-y)
+QEMU_OBJS_LIB= $(patsubst %.o, %.lo, $(QEMU_OBJS))
+
+QEMU_CFLAGS+= -I$(SRC_PATH) -I$(SRC_PATH)/include -I../
+#adding magic macro define for symbol hiding and exposing
+QEMU_CFLAGS+= -fvisibility=hidden -D LIBQB_BUILD
+
+#dependency libraries
+LIBS+= -lz $(LIBS_TOOLS)
+
+#header files to be installed
+libqblock_includedir= $(includedir)/qblock
+libqblock_srcpath= $(SRC_PATH)/libqblock
+libqblock-pub-headers= $(libqblock_srcpath)/libqblock.h \
+                       $(libqblock_srcpath)/libqblock-types.h \
+                       $(libqblock_srcpath)/libqblock-error.h
+
+#################################################################
+# Runtime rules
+#################################################################
+.PHONY: clean all libqblock.la libqblock.pc install-libqblock
+
+clean:
+	rm -f *.lo *.o *.d *.la *.pc
+	rm -rf .libs block trace audio fsdev hw net qapi qga qom slirp ui libqblock stubs backends
+
+all: libqblock.la libqblock.pc
+# Dummy command so that make thinks it has done something
+	@true
+
+libqblock.la: $(QEMU_OBJS_LIB)
+	$(call quiet-command,$(LIBTOOL) --mode=link --quiet --tag=CC $(CC) -rpath $(libdir) -o $@ $^ $(LIBS),"  lt LINK $@")
+
+libqblock.pc: $(libqblock_srcpath)/libqblock.pc.in
+	$(call quiet-command,sed -e 's|@LIBDIR@|$(libdir)|' \
+		-e 's|@INCLUDEDIR@|$(libqblock_includedir)|' \
+	    -e 's|@VERSION@|$(shell cat $(SRC_PATH)/VERSION)|' \
+		-e 's|@PREFIX@|$(prefix)|' \
+		< $(libqblock_srcpath)/libqblock.pc.in > libqblock.pc,\
+	"  GEN   $@")
+
+install-libqblock: libqblock.la libqblock.pc
+	$(INSTALL_DIR) "$(DESTDIR)$(libdir)"
+	$(INSTALL_DIR) "$(DESTDIR)$(libdir)/pkgconfig"
+	$(INSTALL_DIR) "$(DESTDIR)$(libqblock_includedir)"
+	$(LIBTOOL) --mode=install $(INSTALL_DATA) libqblock.la "$(DESTDIR)$(libdir)"
+	$(LIBTOOL) --mode=install $(INSTALL_DATA) libqblock.pc "$(DESTDIR)$(libdir)/pkgconfig"
+	$(LIBTOOL) --mode=install $(INSTALL_DATA) $(libqblock-pub-headers) "$(DESTDIR)$(libqblock_includedir)"
libqblock/libqblock-aio.c new file +62 -0
new file mode 100644
index 0000000..935fb7f
@@ -0,0 +1,62 @@
+/*
+ * QEMU block layer library
+ *
+ * Copyright IBM, Corp. 2012
+ *
+ * Authors:
+ *  Wenchao Xia   <[email protected]>
+ *
+ * This work is licensed under the terms of the GNU LGPL, version 2 or later.
+ * See the COPYING.LIB file in the top-level directory.
+ *
+ */
+
+#include <sys/syscall.h>
+
+#include "qemu-common.h"
+#include "qemu-aio.h"
+#include "main-loop.h"
+#include "compatfd.h"
+
+void qemu_notify_event(void)
+{
+    return;
+}
+
+QEMUBH *qemu_bh_new(QEMUBHFunc *cb, void *opaque)
+{
+    return NULL;
+}
+
+void qemu_aio_flush(void)
+{
+    return;
+}
+
+bool qemu_aio_wait(void)
+{
+    return false;
+}
+
+#ifdef CONFIG_POSIX
+void qemu_aio_set_fd_handler(int fd,
+                             IOHandler *io_read,
+                             IOHandler *io_write,
+                             AioFlushHandler *io_flush,
+                             void *opaque)
+{
+    return;
+}
+#endif
+
+void qemu_aio_set_event_notifier(EventNotifier *notifier,
+                                 EventNotifierHandler *io_read,
+                                 AioFlushEventNotifierHandler *io_flush)
+{
+    return;
+}
+
+bool qemu_signalfd_available(void)
+{
+    return false;
+}
libqblock/libqblock-error.c new file +0 -0
new file mode 100644
index 0000000..e69de29
libqblock/libqblock-error.h new file +0 -0
new file mode 100644
index 0000000..e69de29
libqblock/libqblock-types.h new file +0 -0
new file mode 100644
index 0000000..e69de29
libqblock/libqblock.c new file +6 -0
new file mode 100644
index 0000000..4c18c41
@@ -0,0 +1,6 @@
+#include "libqblock.h"
+
+int libqb_test(void)
+{
+    return 0;
+}
libqblock/libqblock.h new file +1 -0
new file mode 100644
index 0000000..b0f9daf
@@ -0,0 +1 @@
+__attribute__((visibility("default"))) int libqb_test(void);
libqblock/libqblock.pc.in new file +0 -0
new file mode 100644
index 0000000..d2a7d06

libqblock/libqblock.pc.in line 10

Quoted 10 lines
libqblock/libqblock.pc.in new file +10 -0
@@ -0,0 +1,13 @@
+prefix=@PREFIX@
+exec_prefix=${prefix}
+libdir=@LIBDIR@
+includedir=@INCLUDEDIR@
+
+Name: qblock
+Description: QEMU block layer library
+Version: @VERSION@
+
+Requires:  rt gthread-2.0 glib-2.0 z curl cap-ng uuid

This is a bit problematic, because these requires should only be present if the libraries are actually in use.

I suggest that you put

Requires: @REQUIRES@

in this file, and compute the actual requirements in the Makefile:

libqblock-requires-y = rt gthread-2.0 glib-2.0 z
libqblock-requires-$(CONFIG_CURL) += curl
libqblock-requires-$(CONFIG_LIBCAP) += cap-ng
libqblock-requires-$(CONFIG_UUID) += uuid

etc.

Quoted 11 lines
libqblock/libqblock.pc.in new file +3 -0
+Libs: -L${libdir} -lqblock
+Libs.private:
+Cflags: -I${includedir}
tests/Makefile +0 -0
index fc816d0..76de5f4 100644
@@ -88,6 +88,20 @@ check-qtest-$(CONFIG_POSIX)=$(foreach TARGET,$(TARGETS), $(check-qtest-$(TARGET)
 qtest-obj-y = tests/libqtest.o $(oslib-obj-y) libqemustub.a
 $(check-qtest-y): $(qtest-obj-y)
 

tests/Makefile check-qtest-$(CONFIG_POSIX)=$(foreach TARGET,$(TARGETS), $(check-qtest-$(TARGET) · line 100

Quoted 10 lines
tests/Makefile +10 -0
+#libqblock build rules
+check-libqblock-$(CONFIG_LIBQBLOCK) = tests/check-libqblock-qcow2$(EXESUF)
+
+$(check-libqblock-y): QEMU_INCLUDES += -I$(SRC_PATH)/tests -I$(SRC_PATH)/libqblock
+LIBQBLOCK_TEST_DIR = tests/test_images
+LIBQBLOCK_LA = libqblock/libqblock.la
+
+LTLINK = $(call quiet-command, $(LIBTOOL) --mode=link --quiet --tag=CC \
+         $(CC) $(QEMU_CFLAGS) $(CFLAGS) $(LDFLAGS) \
+         -o $@ $(sort $(1)) $(LIBS), "lt LINK $(TARGET_DIR)$@")

Please move LTLINK to rules.mak.

Quoted 15 lines
tests/Makefile +4 -0
+$(check-libqblock-y): %$(EXESUF): %.o
+	$(call LTLINK, $^ $(LIBQBLOCK_LA))
+
 .PHONY: check-help
 check-help:
 	@echo "Regression testing targets:"
@@ -98,6 +112,7 @@ check-help:
 	@echo " make check-unit           Run qobject tests"
 	@echo " make check-block          Run block tests"
 	@echo " make check-report.html    Generates an HTML test report"
+	@echo " make check-libqblock      Run libqblock tests"
 	@echo " make check-clean          Clean the tests"
 	@echo
 	@echo "Please note that HTML reports do not regenerate if the unit tests"

tests/Makefile $(patsubst %, check-report-qtest-%.xml, $(QTEST_TARGETS)): check-report-qtest-%. · line 151

Quoted 7 lines
tests/Makefile +4 -0
@@ -130,6 +145,15 @@ $(patsubst %, check-report-qtest-%.xml, $(QTEST_TARGETS)): check-report-qtest-%.
 check-report-unit.xml: $(check-unit-y)
 	$(call quiet-command,gtester -q $(GTESTER_OPTIONS) -o $@ -m=$(SPEED) $^, "GTESTER $@")
 
+# gtester tests with libqblock
+
+# remove quite option to show error if program is terminated by seg fault, otherwise it fails silently.
+GTESTER_OPTIONS_LIBQBLOCK:= -k

Either remove this, or apply it to all tests. For now, I'd say please remove this.

Also, the corresponding rules for .xml files are missing.

Quoted 42 lines
tests/Makefile +9 -2
+.PHONY: $(patsubst %, check-%, $(check-libqblock-y))
+$(patsubst %, check-%, $(check-libqblock-y)): check-%: %
+	$(call quiet-command, LIBQBLOCK_TEST_DIR=$(LIBQBLOCK_TEST_DIR) gtester $(GTESTER_OPTIONS_LIBQBLOCK) -m=$(SPEED) $*,"GTESTER $*")
+
 # Reports and overall runs
 
 check-report.xml: $(patsubst %,check-report-qtest-%.xml, $(QTEST_TARGETS)) check-report-unit.xml
@@ -147,16 +171,19 @@ check-tests/qemu-iotests-quick.sh: tests/qemu-iotests-quick.sh qemu-img$(EXESUF)
 
 # Consolidated targets
 
-.PHONY: check-qtest check-unit check
+.PHONY: check-qtest check-unit check check-libqblock check-clean
 check-qtest: $(patsubst %,check-qtest-%, $(QTEST_TARGETS))
 check-unit: $(patsubst %,check-%, $(check-unit-y))
 check-block: $(patsubst %,check-%, $(check-block-y))
-check: check-unit check-qtest
+check-libqblock: $(patsubst %,check-%, $(check-libqblock-y))
+check: check-libqblock check-unit check-qtest
 
 check-clean:
 	$(MAKE) -C tests/tcg clean
 	rm -f $(check-unit-y)
 	rm -f $(check-qtest-i386-y) $(check-qtest-x86_64-y) $(check-qtest-sparc64-y) $(check-qtest-sparc-y)
 	rm -f tests/*.o
+	rm -f $(check-libqblock-y)
+	rm -rf $(LIBQBLOCK_TEST_DIR)
 
 -include $(wildcard tests/*.d)
tests/check-libqblock-qcow2.c new file +6 -0
new file mode 100644
index 0000000..50a4df3
@@ -0,0 +1,6 @@
+#include "libqblock.h"
+int main(int argc, char **argv)
+{
+    libqb_test();
+    return 0;
+}
#6 lore raw

Re: [Qemu-devel] [PATCH V12 4/7] libqblock build system

Quoted 12 lines · Paolo Bonzini

Ok, I'm pretty confident that v13 will be the last one. :)

Il 28/11/2012 09:55, Wenchao Xia ha scritto:

configure echo " --with-coroutine=BACKEND coroutine backend. Supported options:" · line 1128

Quoted 6 lines
configure +2 -0
@@ -1119,6 +1124,8 @@ echo "  --with-coroutine=BACKEND coroutine backend. Supported options:"
  echo "                           gthread, ucontext, sigaltstack, windows"
  echo "  --enable-glusterfs       enable GlusterFS backend"
  echo "  --disable-glusterfs      disable GlusterFS backend"
+echo "  --enable-libqblock       enable dynamic library libqblock"
+echo "  --disable-libqblock      disable dynamic library libqblock"

"shared" library, not dynamic.

OK.

Quoted 271 lines
Quoted 253 lines
configure +32 -0
  echo ""
  echo "NOTE: The object files are built at the place where configure is launched"
  exit 1
@@ -2349,6 +2356,28 @@ EOF
    fi
  fi
 
+##########################################
+# libqblock probe
+if test "$libqblock" == "yes" ; then
+    # if it is set to yes by user, check whether libtool exist
+    if ! has $libtool; then
+        echo
+        echo "ERROR: Libqblock needs libtool, but libtool was not found."
+        echo "Make sure libtool was installed, or disable libqblock."
+        echo
+        exit 1
+    fi
+fi
+
+if test "$libqblock" == "" ; then
+    # libqblock depends on libtool, default to yes if libtool exist
+    if ! has $libtool; then
+        libqblock="no"
+    else
+        libqblock="yes"
+    fi
+fi
+
  #
  # Check for xxxat() functions when we are building linux-user
  # emulator.  This is done because older glibc versions don't
@@ -3101,6 +3130,9 @@ if test "$want_tools" = "yes" ; then
    if [ "$linux" = "yes" -o "$bsd" = "yes" -o "$solaris" = "yes" ] ; then
      tools="qemu-nbd\$(EXESUF) $tools"
    fi
+  if test "$libqblock" = "yes" ; then
+  tools="subdir-libqblock $tools"
+  fi
  fi
  if test "$softmmu" = yes ; then
    if test "$virtfs" != no ; then
@@ -3235,6 +3267,7 @@ echo "build guest agent $guest_agent"
  echo "seccomp support   $seccomp"
  echo "coroutine backend $coroutine_backend"
  echo "GlusterFS support $glusterfs"
+echo "libqblock support $libqblock"
 
  if test "$sdl_too_old" = "yes"; then
  echo "-> Your SDL version is too old - please upgrade to have SDL support"
@@ -3581,6 +3614,10 @@ if test "$glusterfs" = "yes" ; then
    echo "CONFIG_GLUSTERFS=y" >> $config_host_mak
  fi
 
+if test "$libqblock" = "yes" ; then
+  echo "CONFIG_LIBQBLOCK=y" >> $config_host_mak
+fi
+
  # USB host support
  case "$usb" in
  linux)
@@ -4166,12 +4203,14 @@ DIRS="$DIRS pc-bios/optionrom pc-bios/spapr-rtas"
  DIRS="$DIRS roms/seabios roms/vgabios"
  DIRS="$DIRS qapi-generated"
  DIRS="$DIRS libcacard libcacard/libcacard libcacard/trace"
+DIRS="$DIRS libqblock"
  FILES="Makefile tests/Makefile tests/tcg/Makefile qdict-test-data.txt"
  FILES="$FILES tests/tcg/cris/Makefile tests/tcg/cris/.gdbinit"
  FILES="$FILES tests/tcg/lm32/Makefile libcacard/Makefile"
  FILES="$FILES pc-bios/optionrom/Makefile pc-bios/keymaps"
  FILES="$FILES pc-bios/spapr-rtas/Makefile"
  FILES="$FILES roms/seabios/Makefile roms/vgabios/Makefile"
+FILES="$FILES libqblock/Makefile"
  for bios_file in \
      $source_path/pc-bios/*.bin \
      $source_path/pc-bios/*.rom \
libqblock/Makefile new file +69 -0
new file mode 100644
index 0000000..13f9522
@@ -0,0 +1,69 @@
+###########################################################################
+# libqblock Makefile
+##########################################################################
+-include ../config-host.mak
+-include $(SRC_PATH)/rules.mak
+-include $(SRC_PATH)/Makefile.objs
+
+#############################################################################
+# Library settings
+#############################################################################
+# src files are in orignial path
+$(call set-vpath, $(SRC_PATH))
+# generated files are in ../
+$(call set-vpath, ../)
+
+#library objects
+libqblock-obj-y= libqblock/libqblock.o libqblock/libqblock-error.o libqblock/libqblock-aio.o
+
+extra-obj-y= qemu-timer.o qemu-tool.o
+
+QEMU_OBJS= $(libqblock-obj-y) $(block-obj-y) $(oslib-obj-y) $(stub-obj-y) \
+           $(extra-obj-y)
+QEMU_OBJS_LIB= $(patsubst %.o, %.lo, $(QEMU_OBJS))
+
+QEMU_CFLAGS+= -I$(SRC_PATH) -I$(SRC_PATH)/include -I../
+#adding magic macro define for symbol hiding and exposing
+QEMU_CFLAGS+= -fvisibility=hidden -D LIBQB_BUILD
+
+#dependency libraries
+LIBS+= -lz $(LIBS_TOOLS)
+
+#header files to be installed
+libqblock_includedir= $(includedir)/qblock
+libqblock_srcpath= $(SRC_PATH)/libqblock
+libqblock-pub-headers= $(libqblock_srcpath)/libqblock.h \
+                       $(libqblock_srcpath)/libqblock-types.h \
+                       $(libqblock_srcpath)/libqblock-error.h
+
+#################################################################
+# Runtime rules
+#################################################################
+.PHONY: clean all libqblock.la libqblock.pc install-libqblock
+
+clean:
+	rm -f *.lo *.o *.d *.la *.pc
+	rm -rf .libs block trace audio fsdev hw net qapi qga qom slirp ui libqblock stubs backends
+
+all: libqblock.la libqblock.pc
+# Dummy command so that make thinks it has done something
+	@true
+
+libqblock.la: $(QEMU_OBJS_LIB)
+	$(call quiet-command,$(LIBTOOL) --mode=link --quiet --tag=CC $(CC) -rpath $(libdir) -o $@ $^ $(LIBS),"  lt LINK $@")
+
+libqblock.pc: $(libqblock_srcpath)/libqblock.pc.in
+	$(call quiet-command,sed -e 's|@LIBDIR@|$(libdir)|' \
+		-e 's|@INCLUDEDIR@|$(libqblock_includedir)|' \
+	    -e 's|@VERSION@|$(shell cat $(SRC_PATH)/VERSION)|' \
+		-e 's|@PREFIX@|$(prefix)|' \
+		< $(libqblock_srcpath)/libqblock.pc.in > libqblock.pc,\
+	"  GEN   $@")
+
+install-libqblock: libqblock.la libqblock.pc
+	$(INSTALL_DIR) "$(DESTDIR)$(libdir)"
+	$(INSTALL_DIR) "$(DESTDIR)$(libdir)/pkgconfig"
+	$(INSTALL_DIR) "$(DESTDIR)$(libqblock_includedir)"
+	$(LIBTOOL) --mode=install $(INSTALL_DATA) libqblock.la "$(DESTDIR)$(libdir)"
+	$(LIBTOOL) --mode=install $(INSTALL_DATA) libqblock.pc "$(DESTDIR)$(libdir)/pkgconfig"
+	$(LIBTOOL) --mode=install $(INSTALL_DATA) $(libqblock-pub-headers) "$(DESTDIR)$(libqblock_includedir)"
libqblock/libqblock-aio.c new file +62 -0
new file mode 100644
index 0000000..935fb7f
@@ -0,0 +1,62 @@
+/*
+ * QEMU block layer library
+ *
+ * Copyright IBM, Corp. 2012
+ *
+ * Authors:
+ *  Wenchao Xia   <[email protected]>
+ *
+ * This work is licensed under the terms of the GNU LGPL, version 2 or later.
+ * See the COPYING.LIB file in the top-level directory.
+ *
+ */
+
+#include <sys/syscall.h>
+
+#include "qemu-common.h"
+#include "qemu-aio.h"
+#include "main-loop.h"
+#include "compatfd.h"
+
+void qemu_notify_event(void)
+{
+    return;
+}
+
+QEMUBH *qemu_bh_new(QEMUBHFunc *cb, void *opaque)
+{
+    return NULL;
+}
+
+void qemu_aio_flush(void)
+{
+    return;
+}
+
+bool qemu_aio_wait(void)
+{
+    return false;
+}
+
+#ifdef CONFIG_POSIX
+void qemu_aio_set_fd_handler(int fd,
+                             IOHandler *io_read,
+                             IOHandler *io_write,
+                             AioFlushHandler *io_flush,
+                             void *opaque)
+{
+    return;
+}
+#endif
+
+void qemu_aio_set_event_notifier(EventNotifier *notifier,
+                                 EventNotifierHandler *io_read,
+                                 AioFlushEventNotifierHandler *io_flush)
+{
+    return;
+}
+
+bool qemu_signalfd_available(void)
+{
+    return false;
+}
libqblock/libqblock-error.c new file +0 -0
new file mode 100644
index 0000000..e69de29
libqblock/libqblock-error.h new file +0 -0
new file mode 100644
index 0000000..e69de29
libqblock/libqblock-types.h new file +0 -0
new file mode 100644
index 0000000..e69de29
libqblock/libqblock.c new file +6 -0
new file mode 100644
index 0000000..4c18c41
@@ -0,0 +1,6 @@
+#include "libqblock.h"
+
+int libqb_test(void)
+{
+    return 0;
+}
libqblock/libqblock.h new file +1 -0
new file mode 100644
index 0000000..b0f9daf
@@ -0,0 +1 @@
+__attribute__((visibility("default"))) int libqb_test(void);
libqblock/libqblock.pc.in new file +0 -0
new file mode 100644
index 0000000..d2a7d06

libqblock/libqblock.pc.in line 10

Quoted 10 lines
libqblock/libqblock.pc.in new file +10 -0
@@ -0,0 +1,13 @@
+prefix=@PREFIX@
+exec_prefix=${prefix}
+libdir=@LIBDIR@
+includedir=@INCLUDEDIR@
+
+Name: qblock
+Description: QEMU block layer library
+Version: @VERSION@
+
+Requires:  rt gthread-2.0 glib-2.0 z curl cap-ng uuid

This is a bit problematic, because these requires should only be present if the libraries are actually in use.

I suggest that you put

Requires: @REQUIRES@

Quoted 9 lines

in this file, and compute the actual requirements in the Makefile:

libqblock-requires-y = rt gthread-2.0 glib-2.0 z
libqblock-requires-$(CONFIG_CURL) += curl
libqblock-requires-$(CONFIG_LIBCAP) += cap-ng
libqblock-requires-$(CONFIG_UUID) += uuid

etc.

I'll try this way, thank u for the detailed tips.

Quoted 11 lines
Quoted 11 lines
libqblock/libqblock.pc.in new file +3 -0
+Libs: -L${libdir} -lqblock
+Libs.private:
+Cflags: -I${includedir}
tests/Makefile +0 -0
index fc816d0..76de5f4 100644
@@ -88,6 +88,20 @@ check-qtest-$(CONFIG_POSIX)=$(foreach TARGET,$(TARGETS), $(check-qtest-$(TARGET)
  qtest-obj-y = tests/libqtest.o $(oslib-obj-y) libqemustub.a
  $(check-qtest-y): $(qtest-obj-y)
 
Quoted 13 lines

tests/Makefile check-qtest-$(CONFIG_POSIX)=$(foreach TARGET,$(TARGETS), $(check-qtest-$(TARGET) · line 100

Quoted 10 lines
tests/Makefile +10 -0
+#libqblock build rules
+check-libqblock-$(CONFIG_LIBQBLOCK) = tests/check-libqblock-qcow2$(EXESUF)
+
+$(check-libqblock-y): QEMU_INCLUDES += -I$(SRC_PATH)/tests -I$(SRC_PATH)/libqblock
+LIBQBLOCK_TEST_DIR = tests/test_images
+LIBQBLOCK_LA = libqblock/libqblock.la
+
+LTLINK = $(call quiet-command, $(LIBTOOL) --mode=link --quiet --tag=CC \
+         $(CC) $(QEMU_CFLAGS) $(CFLAGS) $(LDFLAGS) \
+         -o $@ $(sort $(1)) $(LIBS), "lt LINK $(TARGET_DIR)$@")

Please move LTLINK to rules.mak.

OK.

Quoted 26 lines
Quoted 15 lines
tests/Makefile +4 -0
+$(check-libqblock-y): %$(EXESUF): %.o
+	$(call LTLINK, $^ $(LIBQBLOCK_LA))
+
  .PHONY: check-help
  check-help:
  	@echo "Regression testing targets:"
@@ -98,6 +112,7 @@ check-help:
  	@echo " make check-unit           Run qobject tests"
  	@echo " make check-block          Run block tests"
  	@echo " make check-report.html    Generates an HTML test report"
+	@echo " make check-libqblock      Run libqblock tests"
  	@echo " make check-clean          Clean the tests"
  	@echo
  	@echo "Please note that HTML reports do not regenerate if the unit tests"

tests/Makefile $(patsubst %, check-report-qtest-%.xml, $(QTEST_TARGETS)): check-report-qtest-%. · line 151

Quoted 7 lines
tests/Makefile +4 -0
@@ -130,6 +145,15 @@ $(patsubst %, check-report-qtest-%.xml, $(QTEST_TARGETS)): check-report-qtest-%.
  check-report-unit.xml: $(check-unit-y)
  	$(call quiet-command,gtester -q $(GTESTER_OPTIONS) -o $@ -m=$(SPEED) $^, "GTESTER $@")
 
+# gtester tests with libqblock
+
+# remove quite option to show error if program is terminated by seg fault, otherwise it fails silently.
+GTESTER_OPTIONS_LIBQBLOCK:= -k

Either remove this, or apply it to all tests. For now, I'd say please remove this.

Quoted 2 lines

Also, the corresponding rules for .xml files are missing.

OK.

Quoted 44 lines
Quoted 42 lines
tests/Makefile +9 -2
+.PHONY: $(patsubst %, check-%, $(check-libqblock-y))
+$(patsubst %, check-%, $(check-libqblock-y)): check-%: %
+	$(call quiet-command, LIBQBLOCK_TEST_DIR=$(LIBQBLOCK_TEST_DIR) gtester $(GTESTER_OPTIONS_LIBQBLOCK) -m=$(SPEED) $*,"GTESTER $*")
+
  # Reports and overall runs
 
  check-report.xml: $(patsubst %,check-report-qtest-%.xml, $(QTEST_TARGETS)) check-report-unit.xml
@@ -147,16 +171,19 @@ check-tests/qemu-iotests-quick.sh: tests/qemu-iotests-quick.sh qemu-img$(EXESUF)
 
  # Consolidated targets
 
-.PHONY: check-qtest check-unit check
+.PHONY: check-qtest check-unit check check-libqblock check-clean
  check-qtest: $(patsubst %,check-qtest-%, $(QTEST_TARGETS))
  check-unit: $(patsubst %,check-%, $(check-unit-y))
  check-block: $(patsubst %,check-%, $(check-block-y))
-check: check-unit check-qtest
+check-libqblock: $(patsubst %,check-%, $(check-libqblock-y))
+check: check-libqblock check-unit check-qtest
 
  check-clean:
  	$(MAKE) -C tests/tcg clean
  	rm -f $(check-unit-y)
  	rm -f $(check-qtest-i386-y) $(check-qtest-x86_64-y) $(check-qtest-sparc64-y) $(check-qtest-sparc-y)
  	rm -f tests/*.o
+	rm -f $(check-libqblock-y)
+	rm -rf $(LIBQBLOCK_TEST_DIR)
 
  -include $(wildcard tests/*.d)
tests/check-libqblock-qcow2.c new file +6 -0
new file mode 100644
index 0000000..50a4df3
@@ -0,0 +1,6 @@
+#include "libqblock.h"
+int main(int argc, char **argv)
+{
+    libqb_test();
+    return 0;
+}
Signature · 3 lines
Best Regards

Wenchao Xia
#7 lore raw

PATCH v12 5/7 libqblock API design and type defines

In reply to Wenchao Xia

Public API design header files: libqblock.h, libqblock-error.h. Public type define header files: libqblock-types.h.
Private internal used header files: libqblock-internal, libqblock-aio.h.

For ABI some reserved bytes are used in structure defines.

v11:
Moved API design into this patch.
Added libqblock-aio.h, which contain aio internal function prototype,
although it is not implemented yet.
Added doc about thread not safe in libqblock.h.
Moved including of block_int.h from libqblock.h to libqblock.c
Spell fix.
Use g_malloc0_n instead of g_malloc0.
Renamed FUNC_CLEAN to QB_CLEAN, removed unused macros, use glib function directly,
for malloc, free and strdup.

v12:
Changed QBlockImage new/delete API to new/ref/unref.

---
 libqblock/libqblock-aio.h      |    8 +
 libqblock/libqblock-error.h    |   49 ++++++
 libqblock/libqblock-internal.h |   68 ++++++++
 libqblock/libqblock-types.h    |  245 +++++++++++++++++++++++++++
 libqblock/libqblock.h          |  357 +++++++++++++++++++++++++++++++++++++++-
 5 files changed, 726 insertions(+), 1 deletions(-)
 create mode 100644 libqblock/libqblock-aio.h
 create mode 100644 libqblock/libqblock-internal.h
libqblock/libqblock-aio.h new file +8 -0
new file mode 100644
index 0000000..2d699ae
@@ -0,0 +1,8 @@
+#ifndef LIBQBLOCK_AIO_H
+#define LIBQBLOCK_AIO_H
+
+/* This function initialize lower block system at runtime, should be called
+   once before any real block operation. */
+void libqblock_runtime_init(void);
+
+#endif
libqblock/libqblock-error.h +49 -0

49 lines not on this page · raw message

libqblock/libqblock-internal.h new file +68 -0

68 lines not on this page · raw message

libqblock/libqblock-types.h +245 -0

245 lines not on this page · raw message

libqblock/libqblock.h +356 -1

357 lines not on this page · raw message

Signature · 1 line
1.7.1
#8 lore raw

PATCH v12 6/7 libqblock API implement

In reply to Wenchao Xia

This patch contains implemention for APIs.
Important APIs:
1 QBlockContext. This structure was used to retrieve errors, every thread
must create one first.
2 QBlockImage. It stands for an block image object.
3 QBlockStaticInfo. It contains static information such as location, backing
file, size.
4 Sync I/O. It is similar to C file open, read, write and close operations.

v11:
Moved API design out of this patch.
Spell fix.
Use a new function in libqblock-aio.c to do bdrv init and aio init, removed
this section from library loading call back function, which allows map different aio-context to differenct QBlockContext in the future.
Renamed QBlockState to QBlockImage.
Added reference counter in QBlockImage, which is only used in new/delete pair
function now.
Removed useless parentheses around & argument.
Move virt_size out of format unions, removed virt_size from QBlockStaticInfoAddr. bdrv_read and bdrv_write, Report I/O error when block api return negative value. qb_check_allocation, fixed the length check condition and added comments for
it.
qb_info_image_static_get, renamed info to p_info and info_tmp to info, also
renamed all double pointing parameters with prefix “p_”in all API.
qb_str2fmttype and qb_fmttype2str, added parameter check. qb_setup_info_addr, moved memset into it.
qb_info_image_static_get, added format valid check, removed variable member_addr,
moved memset to qb_setup_info_addr.

v12:
Use new/ref/unref to replace new/delete API for QBlockImage.
Remove libqblock_aio_init(), instead added a function libqblock_runtime_init()
which is automatically called in library loading, accordingly removed the call to libqblock_aio_init() in qb_context_new(), only leave a comments there indicates AIO could goes there later, also removed the global lock for no one use it now.
qemu_signalfd_available(), directly return false.
removed qemu_set_fd_handler() and qemu_set_fd_handler2(), using stubs. qb_check_allocation(), do check only when int is smaller than uint64.

---
 libqblock/libqblock-aio.c   |   41 ++-
 libqblock/libqblock-error.c |   57 ++
 libqblock/libqblock.c       | 1184 ++++++++++++++++++++++++++++++++++++++++++-
 3 files changed, 1275 insertions(+), 7 deletions(-)
libqblock/libqblock-aio.c +35 -6
index 935fb7f..72c8bc4 100644
@@ -11,31 +11,56 @@
  *
  */
 
+/* This file was only used in libqblock, codes are copied from main-loop.c,
+ iohandler.c, compatfd.c now, it may have different implemention in the future.
+*/
+
 #include <sys/syscall.h>
 
+#include "libqblock-aio.h"
+
 #include "qemu-common.h"
 #include "qemu-aio.h"
 #include "main-loop.h"
 #include "compatfd.h"
 
-void qemu_notify_event(void)
+#include "block.h"
+
+/* Aio support, copied from main-loop.c */
+
+static AioContext *qemu_aio_context;
+
+void libqblock_runtime_init(void)
 {
+    qemu_aio_context = aio_context_new();
+    /* bdrv_init must be called after qemu_aio_context was set. */
+    bdrv_init();
     return;
 }
 
+void qemu_notify_event(void)
+{
+    if (!qemu_aio_context) {
+        return;
+    }
+    aio_notify(qemu_aio_context);
+}
+
+/* Functions to operate on the main QEMU AioContext.  */
+
 QEMUBH *qemu_bh_new(QEMUBHFunc *cb, void *opaque)
 {
-    return NULL;
+    return aio_bh_new(qemu_aio_context, cb, opaque);
 }
 
 void qemu_aio_flush(void)
 {
-    return;
+    aio_flush(qemu_aio_context);
 }
 
 bool qemu_aio_wait(void)
 {
-    return false;
+    return aio_poll(qemu_aio_context, true);
 }
 
 #ifdef CONFIG_POSIX
@@ -45,7 +70,8 @@ void qemu_aio_set_fd_handler(int fd,
                              AioFlushHandler *io_flush,
                              void *opaque)
 {
-    return;
+    aio_set_fd_handler(qemu_aio_context, fd, io_read, io_write, io_flush,
+                       opaque);
 }
 #endif
 
@@ -53,9 +79,12 @@ void qemu_aio_set_event_notifier(EventNotifier *notifier,
                                  EventNotifierHandler *io_read,
                                  AioFlushEventNotifierHandler *io_flush)
 {
-    return;
+    aio_set_event_notifier(qemu_aio_context, notifier, io_read, io_flush);
 }
 
+
+/* Signal fd support, original codes are from compatfd.c */
+
 bool qemu_signalfd_available(void)
 {
     return false;
libqblock/libqblock-error.c +57 -0

57 lines not on this page · raw message

libqblock/libqblock.c +1183 -1

1,189 lines not on this page · raw message

Signature · 1 line
1.7.1
#9 lore raw

Re: [Qemu-devel] [PATCH V12 6/7] libqblock API implement

Almost there...

Il 28/11/2012 09:55, Wenchao Xia ha scritto:

libqblock/libqblock-aio.c line 16

Quoted 3 lines
libqblock/libqblock-aio.c +3 -0
+/* This file was only used in libqblock, codes are copied from main-loop.c,
+ iohandler.c, compatfd.c now, it may have different implemention in the future.
+*/

No need for this comment.

libqblock/libqblock-aio.c void qemu_aio_set_event_notifier(EventNotifier *notifier, · line 90

Quoted 6 lines
libqblock/libqblock-aio.c +3 -0
+
+/* Signal fd support, original codes are from compatfd.c */
+
 bool qemu_signalfd_available(void)
 {
     return false;

I think this stub can instead be placed in stubs/.

libqblock/libqblock.c line 146

Quoted 13 lines
libqblock/libqblock.c +13 -0
+int qb_image_new(QBlockContext *context,
+                 QBlockImage **p_qbi)
+{
+    *p_qbi = g_malloc0_n(1, sizeof(QBlockImage));
+    (*p_qbi)->bdrvs = bdrv_new("hda");
+    if ((*p_qbi)->bdrvs == NULL) {
+        QB_FREE(*p_qbi);
+        set_context_err(context, QB_ERR_INTERNAL_ERR,
+                       "failed to create the driver.");
+        return context->err_ret;
+    }
+    return 0;
+}

The first reference should already be provided by qb_image_new.

libqblock/libqblock.c line 60

Quoted 10 lines
libqblock/libqblock.c +9 -0
+__attribute__((constructor))
+static void libqblock_init(void)
+{
+    /* Todo: add an assertion about the ABI. */
+    if (libqb_global_data.init_flag == 0) {
+        libqblock_runtime_init();
+        libqb_global_data.init_flag = 1;
+    }
+}

How can this be called with libqb_global_data.init_flag == 1?

#10 lore raw

Re: [Qemu-devel] [PATCH V12 6/7] libqblock API implement

Quoted 9 lines · Paolo Bonzini

Almost there...

Il 28/11/2012 09:55, Wenchao Xia ha scritto:

libqblock/libqblock-aio.c line 16

Quoted 3 lines
libqblock/libqblock-aio.c +3 -0
+/* This file was only used in libqblock, codes are copied from main-loop.c,
+ iohandler.c, compatfd.c now, it may have different implemention in the future.
+*/

No need for this comment.

will remove.

Quoted 9 lines

libqblock/libqblock-aio.c void qemu_aio_set_event_notifier(EventNotifier *notifier, · line 90

Quoted 6 lines
libqblock/libqblock-aio.c +3 -0
+
+/* Signal fd support, original codes are from compatfd.c */
+
  bool qemu_signalfd_available(void)
  {
      return false;

I think this stub can instead be placed in stubs/.

OK.

Quoted 7 lines
Quoted 7 lines
libqblock/libqblock.c +7 -0
+int qb_image_new(QBlockContext *context,
+                 QBlockImage **p_qbi)
+{
+    *p_qbi = g_malloc0_n(1, sizeof(QBlockImage));
+    (*p_qbi)->bdrvs = bdrv_new("hda");
+    if ((*p_qbi)->bdrvs == NULL) {
+        QB_FREE(*p_qbi);
Quoted 9 lines

libqblock/libqblock.c line 146

Quoted 6 lines
libqblock/libqblock.c +6 -0
+        set_context_err(context, QB_ERR_INTERNAL_ERR,
+                       "failed to create the driver.");
+        return context->err_ret;
+    }
+    return 0;
+}

The first reference should already be provided by qb_image_new.

OK.

Quoted 14 lines

libqblock/libqblock.c line 60

Quoted 10 lines
libqblock/libqblock.c +9 -0
+__attribute__((constructor))
+static void libqblock_init(void)
+{
+    /* Todo: add an assertion about the ABI. */
+    if (libqb_global_data.init_flag == 0) {
+        libqblock_runtime_init();
+        libqb_global_data.init_flag = 1;
+    }
+}

How can this be called with libqb_global_data.init_flag == 1?

OK, I will remove the if, but keep init_flag which can show
library is already initialized.

Signature · 3 lines
Best Regards

Wenchao Xia
#11 lore raw

Re: [Qemu-devel] [PATCH V12 6/7] libqblock API implement

Il 29/11/2012 07:25, Wenchao Xia ha scritto:

Quoted 5 lines
Quoted 3 lines

How can this be called with libqb_global_data.init_flag == 1?

OK, I will remove the if, but keep init_flag which can show
library is already initialized.

Please remove it if there is no usage elsewhere.

For debugging, qemu_aio_context != NULL is just as good to check for initialization.

Paolo

#12 lore raw

PATCH v12 7/7 libqblock test example

In reply to Wenchao Xia

In this example, first it will create some qcow2 images, then try get
information including backing file relationship, then it will do sync IO on the image.

v11:
Use only gtester option -k, to avoid silent fail for segment fault. Little change according to API.

v12:
Little change according to API.

---
 tests/check-libqblock-qcow2.c |  393 ++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 391 insertions(+), 2 deletions(-)
tests/check-libqblock-qcow2.c +391 -2
index 50a4df3..5e14cca 100644
@@ -1,6 +1,395 @@
+/*
+ * QEMU block layer library test
+ *
+ * Copyright IBM, Corp. 2012
+ *
+ * Authors:
+ *  Wenchao Xia   <[email protected]>
+ *
+ * This work is licensed under the terms of the GNU LGPL, version 2 or later.
+ * See the COPYING.LIB file in the top-level directory.
+ *
+ * Limitation:
+ *    1 filename do not support relative path, to save trouble in creating
+ * backing files.
+ */
+
+#include <glib.h>
+#include <stdarg.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <inttypes.h>
+#include <string.h>
+#include <stdlib.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+
+
 #include "libqblock.h"
+#include "libqtest.h"
+
+#define LIBQB_TEST_ENV_DIR "LIBQBLOCK_TEST_DIR"
+#define LIBQB_TEST_DEFAULT_DIR "/tmp"
+#define LIBQB_TEST_DEFAULT_FILENAME "libqblock_qcow2_test_img"
+
+typedef struct LibqbTestSettings {
+    const char *image_filename;
+    uint64_t image_size;
+    unsigned int num_backings;
+    unsigned int io_buf_size;
+    uint64_t io_offset;
+    int print_flag;
+} LibqbTestSettings;
+
+LibqbTestSettings libqb_test_settings;
+
+static void print_loc(const QBlockLocationInfo *loc)
+{
+    if (loc == NULL) {
+        printf("loc is NULL.");
+        return;
+    }
+    switch (loc->prot_type) {
+    case QB_PROTO_NONE:
+        printf("protocol type [none].");
+        break;
+    case QB_PROTO_FILE:
+        printf("protocol type [file], filename [%s].",
+               loc->o_file.filename);
+        break;
+    default:
+        printf("protocol type not supported.");
+        break;
+    }
+}
+
+static void print_info_image_static(QBlockStaticInfo *info)
+{
+    const uint64_t *virt_size = qb_get_virt_size(info);
+    const QBlockLocationInfo *backing_loc = qb_get_backing_loc(info);
+    g_assert(virt_size != NULL);
+
+    printf("=======image location:\n");
+    print_loc(&info->loc);
+    printf("\nvirtual_size %" PRId64 ", format type %d [%s]",
+           *(virt_size),
+           info->fmt.fmt_type, qb_fmttype2str(info->fmt.fmt_type));
+    printf("\nbacking image location:\n");
+    print_loc(backing_loc);
+    printf("\n");
+}
+
+static char *generate_backing_filename(const char *filename, int index)
+{
+    char *backing_filename = NULL;
+
+    backing_filename = g_strdup_printf("%s_backing_%d", filename, index);
+    return backing_filename;
+}
+
+/* get filename in a full path */
+static const char *get_filename(const char *path)
+{
+    const char *filename;
+    filename = strrchr(path, '/');
+    if (filename == NULL) {
+        filename = path;
+    } else {
+        filename++;
+    }
+    return filename;
+}
+
+/* create a chain of files, num_backings must >= 0. */
+static void files_create_qcow2(const char *filename,
+                               int num_backings,
+                               uint64_t virt_size)
+{
+    QBlockContext *context = NULL;
+    QBlockImage *qbi = NULL;
+    QBlockLocationInfo *loc_info = NULL;
+    QBlockFormatInfo *fmt_info = NULL;
+    int ret;
+    int index;
+    int flag;
+    char *backing_filename = NULL, *new_filename = NULL;
+    const char *relative_filename = NULL;
+
+    ret = qb_context_new(&context);
+    g_assert(ret == 0);
+
+    ret = qb_image_new(context, &qbi);
+    g_assert(ret == 0);
+    qb_image_ref(context, &qbi);
+
+    ret = qb_loc_info_new(context, &loc_info);
+    g_assert(ret == 0);
+
+    ret = qb_fmt_info_new(context, &fmt_info);
+    g_assert(ret == 0);
+
+    loc_info->prot_type = QB_PROTO_FILE;
+    fmt_info->fmt_type = QB_FMT_QCOW2;
+    fmt_info->virt_size = virt_size;
+    flag = 0;
+
+    index = 0;
+    while (index < num_backings) {
+        new_filename = generate_backing_filename(filename, index);
+        loc_info->o_file.filename = new_filename;
+        if (backing_filename != NULL) {
+            fmt_info->o_qcow2.backing_loc.prot_type = QB_PROTO_FILE;
+            relative_filename = get_filename(backing_filename);
+            fmt_info->o_qcow2.backing_loc.o_file.filename =
+                                                             relative_filename;
+        }
+        ret = qb_create(context, qbi, loc_info, fmt_info, flag);
+        g_assert(ret == 0);
+        free(backing_filename);
+        backing_filename = new_filename;
+        new_filename = NULL;
+        index++;
+    }
+
+    loc_info->o_file.filename = filename;
+    if (backing_filename != NULL) {
+        fmt_info->o_qcow2.backing_loc.prot_type = QB_PROTO_FILE;
+        relative_filename = get_filename(backing_filename);
+        fmt_info->o_qcow2.backing_loc.o_file.filename =
+                                                         relative_filename;
+    }
+    ret = qb_create(context, qbi, loc_info, fmt_info, flag);
+    g_assert(ret == 0);
+    free(backing_filename);
+
+    qb_fmt_info_delete(context, &fmt_info);
+    qb_loc_info_delete(context, &loc_info);
+    qb_image_unref(context, &qbi);
+    qb_context_delete(&context);
+    return;
+}
+
+static void files_delete(const char *filename,
+                         int num_backings,
+                         uint64_t virt_size)
+{
+    char *new_filename = NULL;
+    int index = 0;
+    while (index < num_backings) {
+        new_filename = generate_backing_filename(filename, index);
+        unlink(new_filename);
+        free(new_filename);
+        index++;
+    }
+    unlink(filename);
+}
+
+static void testcase_info_image_static(void)
+{
+    const char *filename = libqb_test_settings.image_filename;
+    uint64_t image_size = libqb_test_settings.image_size;
+    int num_backings = libqb_test_settings.num_backings;
+    QBlockContext *context = NULL;
+    QBlockImage *qbi = NULL;
+    QBlockLocationInfo *loc_info = NULL;
+    int ret, flag;
+    QBlockStaticInfo *info_st = NULL;
+    int i;
+    char *backing_filename = NULL;
+    const uint64_t *virt_size = NULL;
+    const QBlockLocationInfo *backing_loc = NULL;
+
+    ret = qb_context_new(&context);
+    g_assert(ret == 0);
+
+    ret = qb_image_new(context, &qbi);
+    g_assert(ret == 0);
+    qb_image_ref(context, &qbi);
+
+    ret = qb_loc_info_new(context, &loc_info);
+    g_assert(ret == 0);
+
+    loc_info->prot_type = QB_PROTO_FILE;
+    loc_info->o_file.filename = filename;
+    flag = LIBQBLOCK_O_NO_BACKING;
+    ret = qb_open(context, qbi, loc_info, NULL, flag);
+    g_assert(ret == 0);
+
+    i = num_backings - 1;
+    while (1) {
+        ret = qb_info_image_static_get(context, qbi, &info_st);
+        g_assert(ret == 0);
+        if (libqb_test_settings.print_flag > 0) {
+            print_info_image_static(info_st);
+        }
+        qb_close(context, qbi);
+
+        /* checking the information */
+        g_assert(info_st->loc.prot_type == loc_info->prot_type);
+        ret = strcmp(info_st->loc.o_file.filename,
+                     loc_info->o_file.filename);
+
+        g_assert(ret == 0);
+        g_assert(info_st->fmt.fmt_type == QB_FMT_QCOW2);
+        virt_size = qb_get_virt_size(info_st);
+        g_assert(virt_size != NULL);
+        g_assert(*virt_size == image_size);
+        backing_loc = qb_get_backing_loc(info_st);
+        /* qcow2 have always backing file property */
+        g_assert(backing_loc != NULL);
+        if (i >= 0) {
+            /* it should have backing file */
+            g_assert(backing_loc->prot_type == QB_PROTO_FILE);
+            backing_filename = generate_backing_filename(filename, i);
+            ret = strcmp(backing_filename, backing_loc->o_file.filename);
+            g_assert(ret == 0);
+            g_free(backing_filename);
+        } else {
+            g_assert(backing_loc->prot_type == QB_FMT_NONE);
+        }
+        i--;
+        /* see if there is backing file need to check. */
+        if (backing_loc->prot_type == QB_FMT_NONE) {
+            g_assert(i < 0);
+            break;
+        }
+        qb_loc_info_delete(context, &loc_info);
+        loc_info = qb_loc_info_dup(backing_loc);
+        ret = qb_open(context, qbi, loc_info, NULL, flag);
+        g_assert(ret == 0);
+        qb_info_image_static_delete(context, &info_st);
+    }
+    qb_info_image_static_delete(context, &info_st);
+
+    qb_loc_info_delete(context, &loc_info);
+    qb_image_unref(context, &qbi);
+    qb_context_delete(&context);
+    return;
+}
+
+/* assuming the image is new created */
+static void testcase_sync_io(void)
+{
+    const char *filename = libqb_test_settings.image_filename;
+    uint64_t io_buf_size = libqb_test_settings.io_buf_size;
+    uint64_t io_offset = libqb_test_settings.io_offset;;
+    unsigned char *buf_r, *buf_w;
+    uint64_t i;
+    unsigned int rand_value;
+
+    buf_r = g_malloc0(io_buf_size);
+    buf_w = g_malloc0(io_buf_size);
+    for (i = 0; i < io_buf_size; i++) {
+        rand_value = g_test_rand_int_range(0, 255);
+        buf_w[i] = (unsigned char)rand_value;
+    }
+
+    QBlockContext *context = NULL;
+    QBlockImage *qbi = NULL;
+    QBlockLocationInfo *loc_info = NULL;
+    int ret, flag;
+
+    ret = qb_context_new(&context);
+    g_assert(ret == 0);
+
+    ret = qb_image_new(context, &qbi);
+    g_assert(ret == 0);
+    qb_image_ref(context, &qbi);
+
+    ret = qb_loc_info_new(context, &loc_info);
+    g_assert(ret == 0);
+
+
+    loc_info->prot_type = QB_PROTO_FILE;
+    loc_info->o_file.filename = filename;
+    flag = LIBQBLOCK_O_RDWR;
+    ret = qb_open(context, qbi, loc_info, NULL, flag);
+    g_assert(ret == 0);
+
+    int status;
+    int64_t len;
+    ret = qb_check_allocation(context, qbi, io_offset, io_buf_size,
+                                       &status, &len);
+    g_assert(ret == 0);
+    g_assert(status == 0);
+    g_assert(len == io_buf_size);
+
+    ret = qb_write(context, qbi, buf_w, io_buf_size, io_offset);
+    g_assert(ret == io_buf_size);
+
+    ret = qb_read(context, qbi, buf_r, io_buf_size, io_offset);
+    g_assert(ret == io_buf_size);
+
+    int cmp = memcmp(buf_r, buf_w, io_buf_size);
+    g_assert(cmp == 0);
+
+    ret = qb_check_allocation(context, qbi, io_offset, io_buf_size,
+                                       &status, &len);
+    g_assert(ret == 0);
+    g_assert(status == 1);
+    g_assert(len == io_buf_size);
+
+    qb_close(context, qbi);
+
+    qb_loc_info_delete(context, &loc_info);
+    qb_image_unref(context, &qbi);
+    qb_context_delete(&context);
+    g_free(buf_r);
+    g_free(buf_w);
+    return;
+}
+
 int main(int argc, char **argv)
 {
-    libqb_test();
-    return 0;
+    const char *root_dir;
+    const char *name = LIBQB_TEST_DEFAULT_FILENAME;
+    int ret;
+    int dir_created = 0;
+    char *filename;
+
+    root_dir = getenv(LIBQB_TEST_ENV_DIR);
+    if (root_dir == NULL) {
+        root_dir = LIBQB_TEST_DEFAULT_DIR;
+    }
+
+    ret = access(root_dir, F_OK);
+    if (ret != 0) {
+        /* try mkdir */
+        ret = mkdir(root_dir, 0755);
+        g_assert(ret == 0);
+        dir_created = 1;
+    }
+
+    filename = g_strdup_printf("%s/%s", root_dir, name);
+
+    g_test_init(&argc, &argv, NULL);
+    g_test_rand_int();
+
+    libqb_test_settings.image_filename = filename;
+    libqb_test_settings.image_size =
+                            1024 * 1024 * g_test_rand_int_range(1, 1024);
+    libqb_test_settings.num_backings = g_test_rand_int_range(0, 10);
+    libqb_test_settings.io_buf_size = g_test_rand_int_range(1, 1024 * 1024);
+    libqb_test_settings.io_offset = g_test_rand_int_range(0,
+            libqb_test_settings.image_size - libqb_test_settings.io_buf_size);
+
+    files_create_qcow2(libqb_test_settings.image_filename,
+                       libqb_test_settings.num_backings,
+                       libqb_test_settings.image_size);
+
+    g_test_add_func("/libqblock/sync_info_image_static",
+                      testcase_info_image_static);
+    g_test_add_func("/libqblock/sync_io", testcase_sync_io);
+
+    ret = g_test_run();
+    files_delete(libqb_test_settings.image_filename,
+                 libqb_test_settings.num_backings,
+                 libqb_test_settings.image_size);
+
+    g_free(filename);
+    if (dir_created) {
+        ret = rmdir(root_dir);
+        g_assert(ret == 0);
+    }
+    return ret;
 }
Signature · 1 line
1.7.1
#13 lore raw

Re: [Qemu-devel] [PATCH V12 7/7] libqblock test example

Il 28/11/2012 09:55, Wenchao Xia ha scritto:

tests/check-libqblock-qcow2.c line 123

Quoted 3 lines
tests/check-libqblock-qcow2.c +3 -0
+    ret = qb_image_new(context, &qbi);
+    g_assert(ret == 0);
+    qb_image_ref(context, &qbi);

Remember, when changing qb_image_new, to remove these _ref calls.

Paolo

#14 lore raw

Re: [Qemu-devel] [PATCH V12 7/7] libqblock test example

Quoted 9 lines · Paolo Bonzini

Il 28/11/2012 09:55, Wenchao Xia ha scritto:

tests/check-libqblock-qcow2.c line 123

Quoted 3 lines
tests/check-libqblock-qcow2.c +3 -0
+    ret = qb_image_new(context, &qbi);
+    g_assert(ret == 0);
+    qb_image_ref(context, &qbi);

Remember, when changing qb_image_new, to remove these _ref calls.

Paolo

Sure to remove it.

Signature · 3 lines
Best Regards

Wenchao Xia