diff --git a/regressions/Makefile.am b/regressions/Makefile.am
index d47e3fa..00f1ddb 100644
--- a/regressions/Makefile.am
+++ b/regressions/Makefile.am
@@ -45,6 +45,7 @@ TESTS = \
 	test-guestfish-tilde.sh \
 	test-inspect-fstab.sh \
 	test-launch-race.pl \
+	test-list-filesystems.sh \
 	test-list-md-devices.sh \
 	test-luks.sh \
 	test-luks-list.sh \
diff --git a/regressions/test-list-filesystems.sh b/regressions/test-list-filesystems.sh
new file mode 100755
index 0000000..036f79c
--- /dev/null
+++ b/regressions/test-list-filesystems.sh
@@ -0,0 +1,77 @@
+#!/bin/bash -
+# libguestfs
+# Copyright (C) 2011 Red Hat Inc.
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+
+# Test guestfish list-mddevices command
+
+set -e
+
+# Create 2 disks partitioned as:
+# sda1: 20M ext3
+# sda2: 20M MD (md127)
+#
+# sdb1: 20M PV (vg0)
+# sdb2: 20M MD (md127)
+#
+# md127 : 20M ext4
+# vg0 : 16M LV (lv0)
+# lv0 : 16M vfat
+output=$(
+../fish/guestfish <<EOF
+# Add 2 empty disks
+sparse fs-test1.img 50M
+sparse fs-test2.img 50M
+run
+
+part-init /dev/sda mbr
+part-add /dev/sda p 64 41023
+part-add /dev/sda p 41024 81983
+part-init /dev/sdb mbr
+part-add /dev/sdb p 64 41023
+part-add /dev/sdb p 41024 81983
+
+# Create vg0 and lv0 on /dev/sdb1
+pvcreate /dev/sdb1
+vgcreate vg0 /dev/sdb1
+lvcreate lv0 vg0 16
+
+# Create an md device from sda2 and sdb2
+debug sh "/usr/bin/yes | mdadm -C /dev/md/test --level=1 --raid-devices=2 /dev/vda2 /dev/vdb2 >/dev/null 2>&1"
+
+# Create filesystems
+mkfs ext3 /dev/sda1
+mkfs ext4 /dev/md127
+mkfs vfat /dev/vg0/lv0
+
+list-filesystems
+EOF
+)
+
+expected="
+/dev/vda1: ext3
+/dev/md127: ext4
+/dev/vg0/lv0: vfat"
+
+# Check the output of list-filesystems
+if [ "$output" != "$expected" ]; then
+    echo "$0: error: output of list-filesystems did not match expected output"
+    printf "%s\n" "$output"
+    exit 1;
+fi
+
+
+rm -f fs-test1.img fs-test2.img
diff --git a/src/listfs.c b/src/listfs.c
index 42bbaef..25ac481 100644
--- a/src/listfs.c
+++ b/src/listfs.c
@@ -44,13 +44,13 @@ static void check_with_vfs_type (guestfs_h *g, const char *dev, char ***ret, siz
 char **
 guestfs__list_filesystems (guestfs_h *g)
 {
-  size_t i;
-  char **ret;
-  size_t ret_size;
+  char **ret = NULL;
+  size_t ret_size = 0;
 
-  ret = safe_malloc (g, sizeof (char *));
-  ret[0] = NULL;
-  ret_size = 0;
+  char **devices = NULL;
+  char **partitions = NULL;
+  char **mds = NULL;
+  char **lvs = NULL;
 
   /* Look to see if any devices directly contain filesystems
    * (RHBZ#590167).  However vfs-type will fail to tell us anything
@@ -58,21 +58,14 @@ guestfs__list_filesystems (guestfs_h *g)
    * get the list of partitions and exclude the corresponding devices
    * by using part-to-dev.
    */
-  char **devices;
   devices = guestfs_list_devices (g);
-  if (devices == NULL) {
-    guestfs___free_string_list (ret);
-    return NULL;
-  }
-  char **partitions;
+  if (devices == NULL) goto error;
   partitions = guestfs_list_partitions (g);
-  if (partitions == NULL) {
-    guestfs___free_string_list (devices);
-    guestfs___free_string_list (ret);
-    return NULL;
-  }
+  if (partitions == NULL) goto error;
+  mds = guestfs_list_md_devices (g);
+  if (mds == NULL) goto error;
 
-  for (i = 0; partitions[i] != NULL; ++i) {
+  for (size_t i = 0; partitions[i] != NULL; ++i) {
     char *dev = guestfs_part_to_dev (g, partitions[i]);
     if (dev)
       remove_from_list (devices, dev);
@@ -80,30 +73,39 @@ guestfs__list_filesystems (guestfs_h *g)
   }
 
   /* Use vfs-type to check for filesystems on devices. */
-  for (i = 0; devices[i] != NULL; ++i)
+  for (size_t i = 0; devices[i] != NULL; ++i)
     check_with_vfs_type (g, devices[i], &ret, &ret_size);
-  guestfs___free_string_list (devices);
 
   /* Use vfs-type to check for filesystems on partitions. */
-  for (i = 0; partitions[i] != NULL; ++i)
+  for (size_t i = 0; partitions[i] != NULL; ++i)
     check_with_vfs_type (g, partitions[i], &ret, &ret_size);
-  guestfs___free_string_list (partitions);
+
+  /* Use vfs-type to check for filesystems on md devices. */
+  for (size_t i = 0; mds[i] != NULL; ++i)
+    check_with_vfs_type (g, mds[i], &ret, &ret_size);
 
   if (guestfs___feature_available (g, "lvm2")) {
     /* Use vfs-type to check for filesystems on LVs. */
-    char **lvs;
     lvs = guestfs_lvs (g);
-    if (lvs == NULL) {
-      guestfs___free_string_list (ret);
-      return NULL;
-    }
+    if (lvs == NULL) goto error;
 
-    for (i = 0; lvs[i] != NULL; ++i)
+    for (size_t i = 0; lvs[i] != NULL; ++i)
       check_with_vfs_type (g, lvs[i], &ret, &ret_size);
-    guestfs___free_string_list (lvs);
   }
 
+  guestfs___free_string_list (devices);
+  guestfs___free_string_list (partitions);
+  guestfs___free_string_list (mds);
+  guestfs___free_string_list (lvs);
   return ret;
+
+error:
+  if (devices) guestfs___free_string_list (devices);
+  if (partitions) guestfs___free_string_list (partitions);
+  if (mds) guestfs___free_string_list (mds);
+  if (lvs) guestfs___free_string_list (lvs);
+  if (ret) guestfs___free_string_list (ret);
+  return NULL;
 }
 
 /* If 'item' occurs in 'list', remove and free it. */
