Skip to contentSkip to conversation

introduce libfsimage for reading filesystem images

PATCH In review John Levon 2 participants 3 messages Nov 8, 2006

lore
Share & tools
lore

John Levon

author
#0 lore raw

PATCH 1/2 introduce libfsimage for reading filesystem images

# HG changeset patch
# User [email protected]
# Date 1162945967 28800
# Node ID f93406ba0c73b3193e216fec7fcd876ef0f081f0
# Parent d27d399bb3882320345e403cfb221dcf7681146b
Add libfsimage, a C library for reading files from filesystem images. Initial support is provided for Solaris UFS, ext2 (both using libext2fs and not), and reiserfs.

tools/Makefile +1 -0
@@ -18,6 +18,7 @@ SUBDIRS-y += xenstat
 SUBDIRS-y += xenstat
 SUBDIRS-$(CONFIG_Linux) += libaio
 SUBDIRS-$(CONFIG_Linux) += blktap
+SUBDIRS-y += libfsimage
 
 # These don't cross-compile
 ifeq ($(XEN_COMPILE_ARCH),$(XEN_TARGET_ARCH))
tools/libfsimage/Makefile new file +13 -0
new file mode 100644
@@ -0,0 +1,13 @@
+XEN_ROOT = ../..
+include $(XEN_ROOT)/tools/Rules.mk
+
+SUBDIRS-y = common ufs reiserfs
+SUBDIRS-y += $(shell ./check-libext2fs)
+
+.PHONY: all
+all install clean:
+	@set -e; for subdir in $(SUBDIRS-y); do \
+		$(MAKE) -C $$subdir $@; \
+	done
+
+distclean: clean
tools/libfsimage/Rules.mk new file +32 -0
new file mode 100644
@@ -0,0 +1,32 @@
+include $(XEN_ROOT)/tools/Rules.mk
+
+DEPS = .*.d
+
+CFLAGS += -I$(XEN_ROOT)/tools/libfsimage/common/ -Werror -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -Wp,-MD,.$(@F).d
+LDFLAGS += -L../common/
+
+PIC_OBJS := $(patsubst %.c,%.opic,$(LIB_SRCS-y))
+
+FSDIR-$(CONFIG_Linux) = $(LIBDIR)/fs/$(FS)
+FSDIR-$(CONFIG_SunOS)-x86_64 = lib/fs/$(FS)/64
+FSDIR-$(CONFIG_SunOS)-x86_32 = lib/fs/$(FS)/
+FSDIR-$(CONFIG_SunOS) = $(FSDIR-$(CONFIG_SunOS)-$(XEN_TARGET_ARCH))
+FSDIR = $(FSDIR-y)
+
+FSLIB = fsimage.so
+
+.PHONY: fs-all
+fs-all: $(FSLIB)
+
+.PHONY: fs-install
+fs-install: fs-all
+	$(INSTALL_DIR) $(DESTDIR)/usr/$(FSDIR)
+	$(INSTALL_PROG) $(FSLIB) $(DESTDIR)/usr/$(FSDIR)
+
+$(FSLIB): $(PIC_OBJS)
+	$(CC) $(CFLAGS) $(LDFLAGS) $(SHLIB_CFLAGS) -o $@ $^ -lfsimage $(FS_LIBDEPS)
+
+clean distclean:
+	rm -f $(PIC_OBJS) $(FSLIB)
+
+-include $(DEPS)
tools/libfsimage/check-libext2fs new file +21 -0
new file mode 100755
@@ -0,0 +1,21 @@
+#!/bin/bash
+
+cat >ext2-test.c <<EOF
+#include <ext2fs/ext2fs.h>
+
+int main()
+{
+	ext2fs_open2;
+}
+EOF
+
+gcc -o ext2-test ext2-test.c -lext2fs >/dev/null 2>&1
+if [ $? = 0 ]; then
+	echo ext2fs-lib
+else
+	echo ext2fs
+fi
+
+rm -f ext2-test ext2-test.c
+
+exit 0
tools/libfsimage/common/Makefile new file +46 -0

46 lines not on this page · raw message

tools/libfsimage/common/fsimage.c new file +140 -0

140 lines not on this page · raw message

tools/libfsimage/common/fsimage.h new file +52 -0

52 lines not on this page · raw message

tools/libfsimage/common/fsimage_grub.c new file +277 -0

277 lines not on this page · raw message

tools/libfsimage/common/fsimage_grub.h new file +92 -0

92 lines not on this page · raw message

tools/libfsimage/common/fsimage_plugin.c new file +214 -0

214 lines not on this page · raw message

tools/libfsimage/common/fsimage_plugin.h new file +65 -0

65 lines not on this page · raw message

tools/libfsimage/common/fsimage_priv.h new file +62 -0

62 lines not on this page · raw message

tools/libfsimage/common/mapfile-GNU new file +37 -0
new file mode 100644
@@ -0,0 +1,37 @@
+VERSION {
+	libfsimage.so.1.1 {
+		global:
+			fsi_open_fsimage;
+			fsi_close_fsimage;
+			fsi_file_exists;
+			fsi_open_file;
+			fsi_close_file;
+			fsi_read_file;
+			fsi_pread_file;
+	
+			fsip_fs_set_data;
+			fsip_fs_free;
+			fsip_file_alloc;
+			fsip_file_free;
+			fsip_fs;
+			fsip_fs_offset;
+			fsip_fs_data;
+			fsip_file_data;
+	
+			fsig_init;
+			fsig_devread;
+			fsig_substring;
+			fsig_fs_buf;
+			fsig_file_alloc;
+			fsig_file_buf;
+			fsig_filepos;
+			fsig_filemax;
+			fsig_int1;
+			fsig_int2;
+			fsig_errnum;
+			fsig_disk_read_junk;
+	
+		local:
+			*;
+	};
+}
tools/libfsimage/common/mapfile-SunOS new file +35 -0
new file mode 100644
@@ -0,0 +1,35 @@
+libfsimage.so.1.1 {
+	global:
+		fsi_open_fsimage;
+		fsi_close_fsimage;
+		fsi_file_exists;
+		fsi_open_file;
+		fsi_close_file;
+		fsi_read_file;
+		fsi_pread_file;
+
+		fsip_fs_set_data;
+		fsip_fs_free;
+		fsip_file_alloc;
+		fsip_file_free;
+		fsip_fs;
+		fsip_fs_data;
+		fsip_fs_offset;
+		fsip_file_data;
+
+		fsig_init;
+		fsig_devread;
+		fsig_substring;
+		fsig_fs_buf;
+		fsig_file_alloc;
+		fsig_file_buf;
+		fsig_filepos;
+		fsig_filemax;
+		fsig_int1;
+		fsig_int2;
+		fsig_errnum;
+		fsig_disk_read_junk;
+
+	local:
+		*;
+};
tools/libfsimage/ext2fs-lib/Makefile new file +15 -0
new file mode 100644
@@ -0,0 +1,15 @@
+XEN_ROOT = ../../..
+
+LIB_SRCS-y = ext2fs-lib.c
+
+FS = ext2fs-lib
+
+FS_LIBDEPS = -lext2fs
+
+.PHONY: all
+all: fs-all
+
+.PHONY: install
+install: fs-install
+
+include $(XEN_ROOT)/tools/libfsimage/Rules.mk
tools/libfsimage/ext2fs-lib/ext2fs-lib.c new file +171 -0

171 lines not on this page · raw message

tools/libfsimage/ext2fs/Makefile new file +13 -0
new file mode 100644
@@ -0,0 +1,13 @@
+XEN_ROOT = ../../..
+
+LIB_SRCS-y = fsys_ext2fs.c
+
+FS = ext2fs
+
+.PHONY: all
+all: fs-all
+
+.PHONY: install
+install: fs-install
+
+include $(XEN_ROOT)/tools/libfsimage/Rules.mk
tools/libfsimage/ext2fs/fsys_ext2fs.c new file +804 -0

804 lines not on this page · raw message

tools/libfsimage/reiserfs/Makefile new file +13 -0
new file mode 100644
@@ -0,0 +1,13 @@
+XEN_ROOT = ../../..
+
+LIB_SRCS-y = fsys_reiserfs.c
+
+FS = reiserfs
+
+.PHONY: all
+all: fs-all
+
+.PHONY: install
+install: fs-install
+
+include $(XEN_ROOT)/tools/libfsimage/Rules.mk
tools/libfsimage/reiserfs/fsys_reiserfs.c new file +1254 -0

891 lines not on this page · raw message

tools/libfsimage/ufs/Makefile new file +13 -0
new file mode 100644

Lines of this file are not shown. View the raw message.

tools/libfsimage/ufs/fsys_ufs.c new file +276 -0

276 lines not on this page · raw message

tools/libfsimage/ufs/ufs.h new file +228 -0

228 lines not on this page · raw message

The rest of this diff is not shown. View the raw message.

Part of the diff is not shown here. View the raw message.

author
#1 lore raw

PATCH 2/2 introduce libfsimage for reading filesystem images - use it for pygrub

# HG changeset patch
# User [email protected]
# Date 1162946030 28800
# Node ID dd5c694de656d7873e3c8f73210a886e1142db12 # Parent f93406ba0c73b3193e216fec7fcd876ef0f081f0 Use libfsimage for reading filesystem images.

tools/pygrub/setup.py +10 -31
@@ -5,46 +5,25 @@ import sys
 
 extra_compile_args  = [ "-fno-strict-aliasing", "-Werror" ]
 
-fsys_mods = []
-fsys_pkgs = []
+XEN_ROOT = "../.."
 
-if os.path.exists("/usr/include/ext2fs/ext2_fs.h"):
-    ext2defines = []
-    cc = new_compiler()
-    cc.add_library("ext2fs")
-    if hasattr(cc, "has_function") and cc.has_function("ext2fs_open2"):
-        ext2defines.append( ("HAVE_EXT2FS_OPEN2", None) )
-    else:
-        sys.stderr.write("WARNING: older version of e2fsprogs installed, not building full\n")
-        sys.stderr.write("         disk support for ext2.\n")
-        
-    ext2 = Extension("grub.fsys.ext2._pyext2",
-                     extra_compile_args = extra_compile_args,
-                     libraries = ["ext2fs"],
-                     define_macros = ext2defines,
-                     sources = ["src/fsys/ext2/ext2module.c"])
-    fsys_mods.append(ext2)
-    fsys_pkgs.append("grub.fsys.ext2")
+fsimage = Extension("fsimage",
+    extra_compile_args = extra_compile_args,
+    include_dirs = [ XEN_ROOT + "/tools/libfsimage/common/" ],
+    library_dirs = [ XEN_ROOT + "/tools/libfsimage/common/" ],
+    libraries = ["fsimage"],
+    sources = ["src/fsimage/fsimage.c"])
 
-if os.path.exists("/usr/include/reiserfs/reiserfs.h"):
-    reiser = Extension("grub.fsys.reiser._pyreiser",
-                     extra_compile_args = extra_compile_args,
-                     libraries = ["reiserfs"],
-                     sources = ["src/fsys/reiser/reisermodule.c"])
-    fsys_mods.append(reiser)
-    fsys_pkgs.append("grub.fsys.reiser")
+pkgs = [ 'grub' ]
 
-pkgs = ['grub', 'grub.fsys']
-pkgs.extend(fsys_pkgs)
 setup(name='pygrub',
       version='0.3',
       description='Boot loader that looks a lot like grub for Xen',
       author='Jeremy Katz',
       author_email='[email protected]',
       license='GPL',
-      package_dir={'grub': 'src'},
+      package_dir={'grub': 'src', 'fsimage': 'src'},
       scripts = ["src/pygrub"],
       packages=pkgs,
-      ext_modules = fsys_mods
+      ext_modules = [ fsimage ]
       )
-               
tools/pygrub/src/fsimage/fsimage.c new file +299 -0

299 lines not on this page · raw message

tools/pygrub/src/fsys/__init__.py deleted +0 -64

64 lines not on this page · raw message

tools/pygrub/src/fsys/ext2/__init__.py deleted +0 -38
deleted file mode 100644
@@ -1,38 +0,0 @@
-# Copyright 2005 Red Hat, Inc.
-# Jeremy Katz <[email protected]>
-#
-# This software may be freely redistributed under the terms of the GNU
-# general public license.
-#
-# 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., 675 Mass Ave, Cambridge, MA 02139, USA.
-#
-
-from grub.fsys import register_fstype, FileSystemType
-from _pyext2 import *
-
-import os, struct
-
-class Ext2FileSystemType(FileSystemType):
-    def __init__(self):
-        FileSystemType.__init__(self)
-        self.name = "ext2"
-
-    def sniff_magic(self, fn, offset = 0):
-        fd = os.open(fn, os.O_RDONLY)
-        os.lseek(fd, offset, 0)
-        buf = os.read(fd, 2048)
-        os.close(fd)        
-        if len(buf) > 1082 and \
-               struct.unpack("<H", buf[1080:1082]) == (0xef53,):
-            return True
-        return False
-
-    def open_fs(self, fn, offset = 0):
-        if not self.sniff_magic(fn, offset):
-            raise ValueError, "Not an ext2 filesystem"
-        return Ext2Fs(fn, offset = offset)
-
-register_fstype(Ext2FileSystemType())
-
tools/pygrub/src/fsys/ext2/ext2module.c deleted +0 -387

387 lines not on this page · raw message

tools/pygrub/src/fsys/ext2/test.py deleted +0 -15
deleted file mode 100644
@@ -1,15 +0,0 @@
-#!/usr/bin/python
-
-
-import _pyext2
-import struct, os, sys
-
-fs = _pyext2.Ext2Fs("test.img")
-
-f = fs.open_file("/boot/vmlinuz-2.6.11-1.1177_FC4")
-buf = f.read()
-o = open("vmlinuz", "wb+")
-o.write(buf)
-o.close()
-
-f.close()
tools/pygrub/src/fsys/reiser/__init__.py deleted +0 -40
deleted file mode 100644
@@ -1,40 +0,0 @@
-# 
-# Copyright (C) 2005 Nguyen Anh Quynh <[email protected]>
-#
-# This software may be freely redistributed under the terms of the GNU
-# general public license.
-#
-# 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., 675 Mass Ave, Cambridge, MA 02139, USA.
-#
-
-from grub.fsys import register_fstype, FileSystemType
-from _pyreiser import *
-
-import os
-
-FSMAGIC2 = 'ReIsEr2'
-FSMAGIC3 = 'ReIsEr3'
-
-class ReiserFileSystemType(FileSystemType):
-    def __init__(self):
-        FileSystemType.__init__(self)
-        self.name = "reiser"
-
-    def sniff_magic(self, fn, offset = 0):
-        fd = os.open(fn, os.O_RDONLY)
-        os.lseek(fd, 0x10000, 0)
-        buf = os.read(fd, 0x40)
-        os.close(fd)
-        if len(buf) == 0x40 and (buf[0x34:0x3B] in [FSMAGIC2, FSMAGIC3]) :
-            return True
-        return False
-
-    def open_fs(self, fn, offset = 0):
-        if not self.sniff_magic(fn, offset):
-            raise ValueError, "Not a reiserfs filesystem"
-        return ReiserFs(fn)
-
-register_fstype(ReiserFileSystemType())
-
tools/pygrub/src/fsys/reiser/reisermodule.c deleted +0 -345

345 lines not on this page · raw message

tools/pygrub/src/pygrub +6 -17
@@ -22,8 +22,8 @@ import getopt
 
 sys.path = [ '/usr/lib/python' ] + sys.path
 
+import fsimage
 import grub.GrubConf
-import grub.fsys
 
 PYGRUB_VER = 0.5
 
@@ -313,25 +313,21 @@ class Grub:
                 raise RuntimeError, "Unable to find active partition on disk"
 
         # open the image and read the grub config
-        fs = None
-        for fstype in grub.fsys.fstypes.values():
-            if fstype.sniff_magic(fn, offset):
-                fs = fstype.open_fs(fn, offset)
-                break
+        fs = fsimage.open(fn, offset)
 
         if fs is not None:
             grubfile = None
             for f in ("/boot/grub/menu.lst", "/boot/grub/grub.conf",
                       "/grub/menu.lst", "/grub/grub.conf"):
-                if fs.file_exist(f):
+                if fs.file_exists(f):
                     grubfile = f
                     break
             if grubfile is None:
                 raise RuntimeError, "we couldn't find grub config file in the image provided."
             f = fs.open_file(grubfile)
             buf = f.read()
-            f.close()
-            fs.close()
+            del f
+            del fs
             # then parse the grub config
             self.cf.parse(buf)
         else:
@@ -511,14 +507,7 @@ if __name__ == "__main__":
             raise RuntimeError, "Unable to find active partition on disk"
 
     # read the kernel and initrd onto the hostfs
-    fs = None
-    for fstype in grub.fsys.fstypes.values():
-        if fstype.sniff_magic(file, offset):
-            fs = fstype.open_fs(file, offset)
-            break
-
-    if fs is None:
-        raise RuntimeError, "Unable to open filesystem"
+    fs = fsimage.open(file, offset)
 
     kernel = fs.open_file(img.kernel[1],).read()
     (tfd, fn) = tempfile.mkstemp(prefix="vmlinuz.", dir="/var/lib/xen")
#2 lore raw
In reply to John Levon
Quoted 8 lines · John Levon

# HG changeset patch
# User [email protected]
# Date 1162945967 28800
# Node ID f93406ba0c73b3193e216fec7fcd876ef0f081f0
# Parent d27d399bb3882320345e403cfb221dcf7681146b
Add libfsimage, a C library for reading files from filesystem images. Initial support is provided for Solaris UFS, ext2 (both using libext2fs and not), and reiserfs.

This seems pretty sane to me from a quick look. I agree that having the flexibility to use some of the grub code is definitely an advantage as there _is_ more support there than general libraries. Make it so that we can continue to use libraries where it makes sense (ext[23] if nothing else) should avoid the concerns sct had as well.

Jeremy