[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [Xen-devel] [PATCH 2/2] libfsimage - use for pygrub
# HG changeset patch # User john.levon@xxxxxxx # Date 1161961342 25200 # Node ID c2a9cf36d6850ca07b069a43d588241f7b3d640e # Parent 5ff4ddea38e2cd18a10cb66040879ab7eca6797a Use libfsimage for reading filesystem images. Signed-off-by: John Levon <john.levon@xxxxxxx> diff --git a/tools/pygrub/setup.py b/tools/pygrub/setup.py --- a/tools/pygrub/setup.py +++ b/tools/pygrub/setup.py @@ -5,46 +5,25 @@ import sys extra_compile_args = [ "-static-libgcc", "-fno-strict-aliasing", "-Wall", "-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='katzj@xxxxxxxxxx', license='GPL', - package_dir={'grub': 'src'}, + package_dir={'grub': 'src', 'fsimage': 'src'}, scripts = ["src/pygrub"], packages=pkgs, - ext_modules = fsys_mods + ext_modules = [ fsimage ] ) - diff --git a/tools/pygrub/src/fsimage/fsimage.c b/tools/pygrub/src/fsimage/fsimage.c new file mode 100644 --- /dev/null +++ b/tools/pygrub/src/fsimage/fsimage.c @@ -0,0 +1,299 @@ +/* + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + * + * Copyright 2006 Sun Microsystems, Inc. All rights reserved. + * Use is subject to license terms. + */ + +#include <Python.h> + +#include <fsimage.h> +#include <stdlib.h> + +#if (PYTHON_API_VERSION >= 1011) +#define PY_PAD 0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L +#else +#define PY_PAD 0L,0L,0L,0L +#endif + +typedef struct fsimage_fs { + PyObject_HEAD + fsi_t *fs; +} fsimage_fs_t; + +typedef struct fsimage_file { + PyObject_HEAD + fsimage_fs_t *fs; + fsi_file_t *file; +} fsimage_file_t; + +struct foo { + int ref; + int size; + long hash; + int state; +}; + +static PyObject * +fsimage_file_read(fsimage_file_t *file, PyObject *args, PyObject *kwargs) +{ + static char *kwlist[] = { "size", "offset", NULL }; + int bufsize; + int size = 0; + uint64_t offset = 0; + ssize_t bytesread = 0; + PyObject * buffer; + + if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|iL", kwlist, + &size, &offset)) + return (NULL); + + bufsize = size ? size : 4096; + + if ((buffer = PyString_FromStringAndSize(NULL, bufsize)) == NULL) + return (NULL); + + while (1) { + int err; + void *buf = PyString_AS_STRING(buffer) + bytesread; + + err = fsi_pread_file(file->file, buf, bufsize, + bytesread + offset); + + if (err == -1) { + Py_DECREF(buffer); + PyErr_SetFromErrno(PyExc_IOError); + return (NULL); + } else if (err == 0) { + break; + } + + bytesread += err; + + if (size != 0) { + bufsize -= bytesread; + if (bufsize == 0) + break; + } else { + if (_PyString_Resize(&buffer, bytesread + bufsize) < 0) + return (NULL); + } + } + + _PyString_Resize(&buffer, bytesread); + return (buffer); +} + +PyDoc_STRVAR(fsimage_file_read__doc__, + "read(file, [size=size, offset=off])\n" + "\n" + "Read size bytes (or all bytes if not set) from the given " + "file. If offset is specified as well, read from the given " + "offset.\n"); + +static struct PyMethodDef fsimage_file_methods[] = { + { "read", (PyCFunction) fsimage_file_read, + METH_VARARGS|METH_KEYWORDS, fsimage_file_read__doc__ }, + { NULL, NULL, 0, NULL } +}; + +static PyObject * +fsimage_file_getattr(fsimage_file_t *file, char *name) +{ + return (Py_FindMethod(fsimage_file_methods, (PyObject *)file, name)); +} + +static void +fsimage_file_dealloc(fsimage_file_t *file) +{ + if (file->file != NULL) + fsi_close_file(file->file); + Py_XDECREF(file->fs); + PyMem_DEL(file); +} + +static char fsimage_file_type__doc__[] = "Filesystem image file"; +PyTypeObject fsimage_file_type = { + PyObject_HEAD_INIT(&PyType_Type) + 0, /* ob_size */ + "fsimage.file", /* tp_name */ + sizeof(fsimage_file_t), /* tp_size */ + 0, /* tp_itemsize */ + (destructor) fsimage_file_dealloc, /* tp_dealloc */ + 0, /* tp_print */ + (getattrfunc) fsimage_file_getattr, /* tp_getattr */ + 0, /* tp_setattr */ + 0, /* tp_compare */ + 0, /* tp_repr */ + 0, /* tp_as_number */ + 0, /* tp_as_sequence */ + 0, /* tp_as_mapping */ + 0, /* tp_hash */ + 0, /* tp_call */ + 0, /* tp_str */ + 0, /* tp_getattro */ + 0, /* tp_setattro */ + 0, /* tp_as_buffer */ + Py_TPFLAGS_DEFAULT, /* tp_flags */ + fsimage_file_type__doc__, + PY_PAD +}; + +static PyObject * +fsimage_fs_open_file(fsimage_fs_t *fs, PyObject *args, PyObject *kwargs) +{ + static char *kwlist[] = { "name", NULL }; + fsimage_file_t *file; + char *name; + + if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s", kwlist, &name)) + return (NULL); + + file = (fsimage_file_t *)PyObject_NEW(fsimage_file_t, &fsimage_file_type); + + if (file == NULL) + return (NULL); + + file->fs = fs; + + Py_INCREF(file->fs); + if ((file->file = fsi_open_file(fs->fs, name)) == NULL) { + Py_DECREF(file->fs); + file->fs = NULL; + PyErr_SetFromErrno(PyExc_IOError); + return (NULL); + } + + return ((PyObject *)file); +} + +static PyObject * +fsimage_fs_file_exists(fsimage_fs_t *fs, PyObject *args, PyObject *kwargs) +{ + static char *kwlist[] = { "name", NULL }; + char *name; + + if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s", kwlist, &name)) + return (NULL); + + if (fsi_file_exists(fs->fs, name)) { + Py_INCREF(Py_True); + return (Py_True); + } + + Py_INCREF(Py_False); + return (Py_False); +} + +PyDoc_STRVAR(fsimage_fs_open_file__doc__, + "open_file(fs, filename) - lookup name in the given fs and return the file"); +PyDoc_STRVAR(fsimage_fs_file_exists__doc__, + "file_exists(fs, name) - lookup name in the given fs and return " + "True if it exists"); + +static struct PyMethodDef fsimage_fs_methods[] = { + { "open_file", (PyCFunction) fsimage_fs_open_file, + METH_VARARGS|METH_KEYWORDS, fsimage_fs_open_file__doc__ }, + { "file_exists", (PyCFunction) fsimage_fs_file_exists, + METH_VARARGS|METH_KEYWORDS, fsimage_fs_file_exists__doc__ }, + { NULL, NULL, 0, NULL } +}; + +static PyObject * +fsimage_fs_getattr(fsimage_fs_t *fs, char *name) +{ + return (Py_FindMethod(fsimage_fs_methods, (PyObject *)fs, name)); +} + +static void +fsimage_fs_dealloc (fsimage_fs_t *fs) +{ + if (fs->fs != NULL) + fsi_close_fsimage(fs->fs); + PyMem_DEL(fs); +} + +PyDoc_STRVAR(fsimage_fs_type__doc__, "Filesystem image"); + +PyTypeObject fsimage_fs_type = { + PyObject_HEAD_INIT(&PyType_Type) + 0, /* ob_size */ + "fsimage.fs", /* tp_name */ + sizeof(fsimage_fs_t), /* tp_size */ + 0, /* tp_itemsize */ + (destructor) fsimage_fs_dealloc, /* tp_dealloc */ + 0, /* tp_print */ + (getattrfunc) fsimage_fs_getattr, /* tp_getattr */ + 0, /* tp_setattr */ + 0, /* tp_compare */ + 0, /* tp_repr */ + 0, /* tp_as_number */ + 0, /* tp_as_sequence */ + 0, /* tp_as_mapping */ + 0, /* tp_hash */ + 0, /* tp_call */ + 0, /* tp_str */ + 0, /* tp_getattro */ + 0, /* tp_setattro */ + 0, /* tp_as_buffer */ + Py_TPFLAGS_DEFAULT, /* tp_flags */ + fsimage_fs_type__doc__, + PY_PAD +}; + +static PyObject * +fsimage_open(PyObject *o, PyObject *args, PyObject *kwargs) +{ + static char *kwlist[] = { "name", "offset", NULL }; + char * name; + uint64_t offset = 0; + fsimage_fs_t *fs; + + if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|L", kwlist, + &name, &offset)) + return (NULL); + + if ((fs = PyObject_NEW(fsimage_fs_t, &fsimage_fs_type)) == NULL) + return (NULL); + + if ((fs->fs = fsi_open_fsimage(name, offset)) == NULL) { + PyErr_SetFromErrno(PyExc_IOError); + return (NULL); + } + + return (PyObject *)fs; +} + +PyDoc_STRVAR(fsimage_open__doc__, + "open(name, [offset=off]) - Open the given file as a filesystem image.\n" + "\n" + "name - name of file to open.\n" + "offset - offset of file system within file image.\n"); + +static struct PyMethodDef fsimage_module_methods[] = { + { "open", (PyCFunction)fsimage_open, + METH_VARARGS|METH_KEYWORDS, fsimage_open__doc__ }, + { NULL, NULL, 0, NULL } +}; + +PyMODINIT_FUNC +initfsimage(void) +{ + Py_InitModule("fsimage", fsimage_module_methods); +} diff --git a/tools/pygrub/src/fsys/__init__.py b/tools/pygrub/src/fsys/__init__.py deleted file mode 100644 --- a/tools/pygrub/src/fsys/__init__.py +++ /dev/null @@ -1,64 +0,0 @@ -# -# Copyright 2005 Red Hat, Inc. -# Jeremy Katz <katzj@xxxxxxxxxx> -# -# 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. -# - -import os -import sys - -fstypes = {} - -def register_fstype(x): - if x.name in fstypes.keys(): - return - fstypes[x.name] = x - -class FileSystemType(object): - """A simple representation for a file system that gives a fs name - and a method for sniffing a file to see if it's of the given fstype.""" - def __init__(self): - self.name = "" - - def sniff_magic(self, fn, offset = 0): - """Look at the filesystem at fn for the appropriate magic starting at - offset offset.""" - raise RuntimeError, "sniff_magic not implemented" - - def open_fs(self, fn, offset = 0): - """Open the given filesystem and return a filesystem object.""" - raise RuntimeError, "open_fs not implemented" - -class FileSystem(object): - def open(self, name, flags = 0, block_size = 0): - """Open the fsys on name with given flags and block_size.""" - raise RuntimeError, "open not implemented" - - def close(self): - """Close the fsys.""" - raise RuntimeError, "close not implemented" - - def open_file(self, file, flags = None): - """Open the file 'name' with the given flags. The returned object - should look similar to a native file object.""" - raise RuntimeError, "open_file not implemented" - - def file_exist(self, file): - """Check to see if the give file is existed. - Return true if file existed, return false otherwise.""" - raise RuntimeError, "file_exist not implemented" - -mydir = sys.modules['grub.fsys'].__path__[0] -for f in os.listdir(mydir): - if not os.path.isdir("%s/%s" %(mydir, f)): - continue - try: - exec "import grub.fsys.%s" %(f,) - except ImportError, e: - pass diff --git a/tools/pygrub/src/fsys/ext2/__init__.py b/tools/pygrub/src/fsys/ext2/__init__.py deleted file mode 100644 --- a/tools/pygrub/src/fsys/ext2/__init__.py +++ /dev/null @@ -1,38 +0,0 @@ -# Copyright 2005 Red Hat, Inc. -# Jeremy Katz <katzj@xxxxxxxxxx> -# -# 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()) - diff --git a/tools/pygrub/src/fsys/ext2/ext2module.c b/tools/pygrub/src/fsys/ext2/ext2module.c deleted file mode 100644 --- a/tools/pygrub/src/fsys/ext2/ext2module.c +++ /dev/null @@ -1,387 +0,0 @@ -/* - * ext2module.c - simple python binding for libext2fs - * - * Copyright 2005 Red Hat, Inc. - * Jeremy Katz <katzj@xxxxxxxxxx> - * - * 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. - */ - -#include <Python.h> - -#include <ext2fs/ext2fs.h> -#include <stdlib.h> -#include <stdio.h> - -#if (PYTHON_API_VERSION >= 1011) -#define PY_PAD 0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L -#else -#define PY_PAD 0L,0L,0L,0L -#endif - - -/* global error object */ -PyObject *Ext2Error; - -typedef struct _Ext2Fs Ext2Fs; -struct _Ext2Fs { - PyObject_HEAD; - ext2_filsys fs; -}; - -typedef struct _Ext2File Ext2File; -struct _Ext2File { - PyObject_HEAD; - ext2_file_t file; -}; - -/* ext2 file object */ - -static PyObject * -ext2_file_close (Ext2File *file, PyObject *args) -{ - if (file->file != NULL) - ext2fs_file_close(file->file); - Py_INCREF(Py_None); - return Py_None; -} - -static PyObject * -ext2_file_read (Ext2File *file, PyObject *args) -{ - int err, size = 0; - unsigned int n, total = 0; - PyObject * buffer = NULL; - - if (file->file == NULL) { - PyErr_SetString(PyExc_ValueError, "Cannot read from closed file"); - return NULL; - } - - if (!PyArg_ParseTuple(args, "|i", &size)) - return NULL; - - buffer = PyString_FromStringAndSize((char *) NULL, (size) ? size : 4096); - if (buffer == NULL) - return buffer; - - while (1) { - err = ext2fs_file_read(file->file, PyString_AS_STRING(buffer) + total, - (size) ? size : 4096, &n); - if (err) { - if (buffer != NULL) { Py_DECREF(buffer); } - Py_DECREF(buffer); - PyErr_SetString(PyExc_ValueError, "read error"); - return NULL; - } - - total += n; - if (n == 0) - break; - - if (size && size == total) - break; - - if (!size) { - _PyString_Resize(&buffer, total + 4096); - } - } - - _PyString_Resize(&buffer, total); - return buffer; -} - -static void -ext2_file_dealloc (Ext2File * file) -{ - if (file->file != NULL) - ext2fs_file_close(file->file); - PyMem_DEL(file); -} - -static struct PyMethodDef Ext2FileMethods[] = { - { "close", - (PyCFunction) ext2_file_close, - METH_VARARGS, NULL }, - { "read", - (PyCFunction) ext2_file_read, - METH_VARARGS, NULL }, - { NULL, NULL, 0, NULL } -}; - -static PyObject * -ext2_file_getattr (Ext2File * file, char * name) -{ - return Py_FindMethod (Ext2FileMethods, (PyObject *) file, name); -} - -static char Ext2FileType__doc__[] = "This is the ext2 filesystem object"; -PyTypeObject Ext2FileType = { - PyObject_HEAD_INIT(&PyType_Type) - 0, /* ob_size */ - "Ext2File", /* tp_name */ - sizeof(Ext2File), /* tp_size */ - 0, /* tp_itemsize */ - (destructor) ext2_file_dealloc, /* tp_dealloc */ - 0, /* tp_print */ - (getattrfunc) ext2_file_getattr, /* tp_getattr */ - 0, /* tp_setattr */ - 0, /* tp_compare */ - 0, /* tp_repr */ - 0, /* tp_as_number */ - 0, /* tp_as_sequence */ - 0, /* tp_as_mapping */ - 0, /* tp_hash */ - 0, /* tp_call */ - 0, /* tp_str */ - 0, /* tp_getattro */ - 0, /* tp_setattro */ - 0, /* tp_as_buffer */ - 0L, /* tp_flags */ - Ext2FileType__doc__, - PY_PAD -}; - -static PyObject * -ext2_file_open (Ext2Fs *fs, char * name, int flags) -{ - int err; - ext2_file_t f; - ext2_ino_t ino; - Ext2File * file; - - file = (Ext2File *) PyObject_NEW(Ext2File, &Ext2FileType); - file->file = NULL; - - err = ext2fs_namei_follow(fs->fs, EXT2_ROOT_INO, EXT2_ROOT_INO, name, &ino); - if (err) { - PyErr_SetString(PyExc_ValueError, "unable to open file"); - return NULL; - } - - err = ext2fs_file_open(fs->fs, ino, flags, &f); - if (err) { - PyErr_SetString(PyExc_ValueError, "unable to open file"); - return NULL; - } - - file->file = f; - return (PyObject *) file; -} - -static PyObject * -ext2_file_exist (Ext2Fs *fs, char * name) -{ - int err; - ext2_ino_t ino; - Ext2File * file; - - file = (Ext2File *) PyObject_NEW(Ext2File, &Ext2FileType); - file->file = NULL; - - err = ext2fs_namei_follow(fs->fs, EXT2_ROOT_INO, EXT2_ROOT_INO, name, &ino); - if (err) { - Py_INCREF(Py_False); - return Py_False; - } - Py_INCREF(Py_True); - return Py_True; -} - -/* ext2fs object */ - -static PyObject * -ext2_fs_close (Ext2Fs *fs, PyObject *args) -{ - if (fs->fs != NULL) - ext2fs_close(fs->fs); - Py_INCREF(Py_None); - return Py_None; -} - -static PyObject * -ext2_fs_open (Ext2Fs *fs, PyObject *args, PyObject *kwargs) -{ - static char *kwlist[] = { "name", "flags", "superblock", - "block_size", "offset", NULL }; - char * name; - int flags = 0, superblock = 0, offset = 0, err; - unsigned int block_size = 0; - ext2_filsys efs; -#ifdef HAVE_EXT2FS_OPEN2 - char offsetopt[30]; -#endif - - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|iiii", kwlist, - &name, &flags, &superblock, - &block_size, &offset)) - return NULL; - - if (fs->fs != NULL) { - PyErr_SetString(PyExc_ValueError, "already have an fs object"); - return NULL; - } - -#ifdef HAVE_EXT2FS_OPEN2 - if (offset == 0) { - offsetopt[0] = '\0'; - } - else { - snprintf(offsetopt, 29, "offset=%d", offset); - } - - err = ext2fs_open2(name, offsetopt, flags, superblock, block_size, - unix_io_manager, &efs); -#else - if (offset != 0) { - PyErr_SetString(PyExc_ValueError, "offset argument not supported"); - return NULL; - } - - err = ext2fs_open(name, flags, superblock, block_size, - unix_io_manager, &efs); -#endif - if (err) { - PyErr_SetString(PyExc_ValueError, "unable to open filesystem"); - return NULL; - } - - fs->fs = efs; - - Py_INCREF(Py_None); - return Py_None; -} - -static PyObject * -ext2_fs_open_file (Ext2Fs *fs, PyObject *args, PyObject *kwargs) -{ - static char *kwlist[] = { "name", "flags", NULL }; - char * name; - int flags = 0; - - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|i", kwlist, - &name, &flags)) - return NULL; - - return ext2_file_open(fs, name, flags); -} - -static PyObject * -ext2_fs_file_exist (Ext2Fs *fs, PyObject *args, PyObject *kwargs) -{ - static char *kwlist[] = { "name", NULL }; - char * name; - - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s", kwlist, &name)) - return NULL; - - return ext2_file_exist(fs, name); -} - -static void -ext2_fs_dealloc (Ext2Fs * fs) -{ - if (fs->fs != NULL) - ext2fs_close(fs->fs); - PyMem_DEL(fs); -} - -static struct PyMethodDef Ext2FsMethods[] = { - { "close", - (PyCFunction) ext2_fs_close, - METH_VARARGS, NULL }, - { "open", - (PyCFunction) ext2_fs_open, - METH_VARARGS|METH_KEYWORDS, NULL }, - { "open_file", - (PyCFunction) ext2_fs_open_file, - METH_VARARGS|METH_KEYWORDS, NULL }, - { "file_exist", - (PyCFunction) ext2_fs_file_exist, - METH_VARARGS|METH_KEYWORDS, NULL }, - { NULL, NULL, 0, NULL } -}; - -static PyObject * -ext2_fs_getattr (Ext2Fs * fs, char * name) -{ - return Py_FindMethod (Ext2FsMethods, (PyObject *) fs, name); -} - -static char Ext2FsType__doc__[] = "This is the ext2 filesystem object"; -PyTypeObject Ext2FsType = { - PyObject_HEAD_INIT(&PyType_Type) - 0, /* ob_size */ - "Ext2Fs", /* tp_name */ - sizeof(Ext2Fs), /* tp_size */ - 0, /* tp_itemsize */ - (destructor) ext2_fs_dealloc, /* tp_dealloc */ - 0, /* tp_print */ - (getattrfunc) ext2_fs_getattr, /* tp_getattr */ - 0, /* tp_setattr */ - 0, /* tp_compare */ - 0, /* tp_repr */ - 0, /* tp_as_number */ - 0, /* tp_as_sequence */ - 0, /* tp_as_mapping */ - 0, /* tp_hash */ - 0, /* tp_call */ - 0, /* tp_str */ - 0, /* tp_getattro */ - 0, /* tp_setattro */ - 0, /* tp_as_buffer */ - 0L, /* tp_flags */ - Ext2FsType__doc__, - PY_PAD -}; - -static PyObject * -ext2_fs_new(PyObject *o, PyObject *args, PyObject *kwargs) -{ - static char *kwlist[] = { "name", "flags", "superblock", - "block_size", "offset", NULL }; - char * name; - int flags = 0, superblock = 0, offset; - unsigned int block_size = 0; - Ext2Fs *pfs; - - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|iiii", kwlist, - &name, &flags, &superblock, &block_size, - &offset)) - return NULL; - - pfs = (Ext2Fs *) PyObject_NEW(Ext2Fs, &Ext2FsType); - if (pfs == NULL) - return NULL; - pfs->fs = NULL; - - if (!ext2_fs_open(pfs, - Py_BuildValue("siiii", name, flags, superblock, - block_size, offset), NULL)) - return NULL; - - return (PyObject *)pfs; -} - -static struct PyMethodDef Ext2ModuleMethods[] = { - { "Ext2Fs", (PyCFunction) ext2_fs_new, METH_VARARGS|METH_KEYWORDS, NULL }, - { NULL, NULL, 0, NULL } -}; - -void init_pyext2(void) { - PyObject *m; - - m = Py_InitModule("_pyext2", Ext2ModuleMethods); - /* - * PyObject *d; - * d = PyModule_GetDict(m); - * o = PyObject_NEW(PyObject, yExt2FsConstructorType); - * PyDict_SetItemString(d, "PyExt2Fs", o); - * Py_DECREF(o); - */ -} diff --git a/tools/pygrub/src/fsys/ext2/test.py b/tools/pygrub/src/fsys/ext2/test.py deleted file mode 100644 --- a/tools/pygrub/src/fsys/ext2/test.py +++ /dev/null @@ -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() diff --git a/tools/pygrub/src/fsys/reiser/__init__.py b/tools/pygrub/src/fsys/reiser/__init__.py deleted file mode 100644 --- a/tools/pygrub/src/fsys/reiser/__init__.py +++ /dev/null @@ -1,40 +0,0 @@ -# -# Copyright (C) 2005 Nguyen Anh Quynh <aquynh@xxxxxxxxx> -# -# 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()) - diff --git a/tools/pygrub/src/fsys/reiser/reisermodule.c b/tools/pygrub/src/fsys/reiser/reisermodule.c deleted file mode 100644 --- a/tools/pygrub/src/fsys/reiser/reisermodule.c +++ /dev/null @@ -1,345 +0,0 @@ -/* - * reisermodule.c - simple python binding for libreiserfs{2,3} - * - * Copyright (C) 2005 Nguyen Anh Quynh <aquynh@xxxxxxxxx> - * - * 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. - */ - -#include <Python.h> - -#include <fcntl.h> -#include <stdlib.h> -#include <stdio.h> - -#include <dal/file_dal.h> -#include <reiserfs/reiserfs.h> - -#if (PYTHON_API_VERSION >= 1011) -#define PY_PAD 0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L -#else -#define PY_PAD 0L,0L,0L,0L -#endif - - -/* global error object */ -PyObject *ReiserError; - -typedef struct { - PyObject_HEAD - reiserfs_fs_t *fs; - dal_t *dal; -} ReiserFs; - -typedef struct _ReiserFile ReiserFile; -struct _ReiserFile { - PyObject_HEAD - reiserfs_file_t *file; -}; - -void file_dal_close(dal_t *dal) { - - if (!dal) return; - - close((int)(unsigned long)dal->dev); - dal_free(dal); -} - -/* reiser file object */ - -static PyObject * -reiser_file_close (ReiserFile *file, PyObject *args) -{ - if (file->file != NULL) - { - reiserfs_file_close(file->file); - file->file = NULL; - } - Py_INCREF(Py_None); - return Py_None; -} - -static PyObject * -reiser_file_read (ReiserFile *file, PyObject *args) -{ - int size = 0; - size_t n, total = 0; - PyObject * buffer = NULL; - - if (file->file == NULL) { - PyErr_SetString(PyExc_ValueError, "Cannot read from closed file"); - return NULL; - } - - if (!PyArg_ParseTuple(args, "|i", &size)) - return NULL; - - buffer = PyString_FromStringAndSize((char *) NULL, (size) ? size : 4096); - if (buffer == NULL) - return buffer; - - while (1) { - n = reiserfs_file_read(file->file, PyString_AS_STRING(buffer) + total, - (size) ? size : 4096); - if (n == 0) - break; - - total += n; - - if (size && size == total) - break; - - if (!size) { - _PyString_Resize(&buffer, total + 4096); - } - } - - _PyString_Resize(&buffer, total); - return buffer; -} - -static void -reiser_file_dealloc (ReiserFile * file) -{ - if (file->file != NULL) { - reiserfs_file_close(file->file); - file->file = NULL; - } - PyObject_DEL(file); -} - -static struct PyMethodDef ReiserFileMethods[] = { - { "close", (PyCFunction) reiser_file_close, METH_VARARGS, NULL }, - { "read", (PyCFunction) reiser_file_read, METH_VARARGS, NULL }, - { NULL, NULL, 0, NULL } -}; - -static PyObject * -reiser_file_getattr (ReiserFile * file, char * name) -{ - return Py_FindMethod (ReiserFileMethods, (PyObject *) file, name); -} - -static char ReiserFileType__doc__[] = "This is the reiser filesystem object"; -PyTypeObject ReiserFileType = { - PyObject_HEAD_INIT(&PyType_Type) - 0, /* ob_size */ - "ReiserFile", /* tp_name */ - sizeof(ReiserFile), /* tp_size */ - 0, /* tp_itemsize */ - (destructor) reiser_file_dealloc, /* tp_dealloc */ - 0, /* tp_print */ - (getattrfunc) reiser_file_getattr, /* tp_getattr */ - 0, /* tp_setattr */ - 0, /* tp_compare */ - 0, /* tp_repr */ - 0, /* tp_as_number */ - 0, /* tp_as_sequence */ - 0, /* tp_as_mapping */ - 0, /* tp_hash */ - 0, /* tp_call */ - 0, /* tp_str */ - 0, /* tp_getattro */ - 0, /* tp_setattro */ - 0, /* tp_as_buffer */ - Py_TPFLAGS_DEFAULT, /* tp_flags */ - ReiserFileType__doc__, - PY_PAD -}; - -static PyObject * -reiser_file_open (ReiserFs *fs, char *name, int flags) -{ - ReiserFile *file; - reiserfs_file_t *f; - - file = (ReiserFile *) PyObject_NEW(ReiserFile, &ReiserFileType); - - f = reiserfs_file_open(fs->fs, name, flags); - file->file = f; - - if (!f) { - PyErr_SetString(PyExc_ValueError, "unable to open file"); - return NULL; - } - - return (PyObject *) file; -} - -static PyObject * -reiser_file_exist (ReiserFs *fs, char *name) -{ - reiserfs_file_t *f; - - f = reiserfs_file_open(fs->fs, name, O_RDONLY); - - if (!f) { - Py_INCREF(Py_False); - return Py_False; - } - reiserfs_file_close(f); - Py_INCREF(Py_True); - return Py_True; -} - -/* reiserfs object */ - -static PyObject * -reiser_fs_close (ReiserFs *fs, PyObject *args) -{ - if (fs->fs != NULL) - { - reiserfs_fs_close(fs->fs); - file_dal_close(fs->dal); - fs->fs = NULL; - } - Py_INCREF(Py_None); - return Py_None; -} - -static PyObject * -reiser_fs_open (ReiserFs *fs, PyObject *args) -{ - char *name; - size_t block_size = DEFAULT_BLOCK_SIZE; - dal_t *dal; - reiserfs_fs_t *rfs; - - if (!PyArg_ParseTuple(args, "s|i", &name, &block_size)) - return NULL; - - if (fs->fs != NULL) { - PyErr_SetString(PyExc_ValueError, "already have an fs object"); - return NULL; - } - - if (!(dal = file_dal_open(name, block_size, O_RDONLY))) { - PyErr_SetString(PyExc_ValueError, "Couldn't create device abstraction"); - return NULL; - } - - if (!(rfs = reiserfs_fs_open_fast(dal, dal))) { - file_dal_close(dal); - PyErr_SetString(PyExc_ValueError, "unable to open file"); - return NULL; - } - - fs->fs = rfs; - fs->dal = dal; - - Py_INCREF(Py_None); - return Py_None; -} - -static PyObject * -reiser_fs_open_file (ReiserFs *fs, PyObject *args) -{ - char *name; - int flags = 0; - - if (!PyArg_ParseTuple(args, "s|i", &name, &flags)) - return NULL; - - return reiser_file_open(fs, name, flags); -} - -static PyObject * -reiser_fs_file_exist (ReiserFs *fs, PyObject *args) -{ - char * name; - - if (!PyArg_ParseTuple(args, "s", &name)) - return NULL; - - return reiser_file_exist(fs, name); -} - -static void -reiser_fs_dealloc (ReiserFs * fs) -{ - if (fs->fs != NULL) - { - reiserfs_fs_close(fs->fs); - file_dal_close(fs->dal); - fs->fs = NULL; - } - PyObject_DEL(fs); -} - -static struct PyMethodDef ReiserFsMethods[] = { - { "close", (PyCFunction) reiser_fs_close, METH_VARARGS, NULL }, - { "open", (PyCFunction) reiser_fs_open, METH_VARARGS, NULL }, - { "open_file", (PyCFunction) reiser_fs_open_file, METH_VARARGS, NULL }, - { "file_exist", (PyCFunction) reiser_fs_file_exist, METH_VARARGS, NULL }, - { NULL, NULL, 0, NULL } -}; - -static PyObject * -reiser_fs_getattr (ReiserFs * fs, char * name) -{ - return Py_FindMethod (ReiserFsMethods, (PyObject *) fs, name); -} - -static char ReiserFsType__doc__[] = "This is the reiser filesystem object"; - -PyTypeObject ReiserFsType = { - PyObject_HEAD_INIT(&PyType_Type) - 0, /* ob_size */ - "ReiserFs", /* tp_name */ - sizeof(ReiserFs), /* tp_size */ - 0, /* tp_itemsize */ - (destructor) reiser_fs_dealloc, /* tp_dealloc */ - 0, /* tp_print */ - (getattrfunc) reiser_fs_getattr, /* tp_getattr */ - 0, /* tp_setattr */ - 0, /* tp_compare */ - 0, /* tp_repr */ - 0, /* tp_as_number */ - 0, /* tp_as_sequence */ - 0, /* tp_as_mapping */ - 0, /* tp_hash */ - 0, /* tp_call */ - 0, /* tp_str */ - 0, /* tp_getattro */ - 0, /* tp_setattro */ - 0, /* tp_as_buffer */ - Py_TPFLAGS_DEFAULT, /* tp_flags */ - ReiserFsType__doc__, - PY_PAD -}; - -static PyObject * -reiser_fs_new(PyObject *o, PyObject *args) -{ - char *name; - size_t block_size = DEFAULT_BLOCK_SIZE; - ReiserFs *pfs; - - if (!PyArg_ParseTuple(args, "s|i", &name, &block_size)) - return NULL; - - pfs = (ReiserFs *) PyObject_NEW(ReiserFs, &ReiserFsType); - if (pfs == NULL) - return NULL; - - pfs->fs = NULL; - - if (!reiser_fs_open(pfs, Py_BuildValue("si", name, block_size))) - return NULL; - - return (PyObject *)pfs; -} - -static struct PyMethodDef ReiserModuleMethods[] = { - { "ReiserFs", (PyCFunction) reiser_fs_new, METH_VARARGS}, - { NULL, NULL, 0} -}; - -void init_pyreiser(void) { - Py_InitModule("_pyreiser", ReiserModuleMethods); -} diff --git a/tools/pygrub/src/pygrub b/tools/pygrub/src/pygrub --- a/tools/pygrub/src/pygrub +++ b/tools/pygrub/src/pygrub @@ -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") _______________________________________________ Xen-devel mailing list Xen-devel@xxxxxxxxxxxxxxxxxxx http://lists.xensource.com/xen-devel
|
Lists.xenproject.org is hosted with RackSpace, monitoring our |