Skip to contentSkip to conversation

Add support for whole disks to pygrub

PATCH New Jeremy Katz 1 participant 1 message Aug 1, 2005

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Add support for whole disks to pygrub

The attached patch adds the support that was only stubbed in to be able to support having reading the boot loader config when you're using a whole disk (eg, file:/root/disk.img,xvda,w) as opposed to just a partition.

This reads the partition table in the MBR to find the active partition and then passes that offset down into the filesystem reading code.

Jeremy

tools/pygrub/setup.py +1 -1
diff -r 55a5ad2f028d tools/pygrub/setup.py
@@ -23,7 +23,7 @@
     fsys_pkgs.append("grub.fsys.reiser")
 
 setup(name='pygrub',
-      version='0.2',
+      version='0.3',
       description='Boot loader that looks a lot like grub for Xen',
       author='Jeremy Katz',
       author_email='[email protected]',
tools/pygrub/src/fsys/ext2/__init__.py +1 -1
diff -r 55a5ad2f028d tools/pygrub/src/fsys/ext2/__init__.py
@@ -32,7 +32,7 @@
     def open_fs(self, fn, offset = 0):
         if not self.sniff_magic(fn, offset):
             raise ValueError, "Not an ext2 filesystem"
-        return Ext2Fs(fn)
+        return Ext2Fs(fn, offset = offset)
 
 register_fstype(Ext2FileSystemType())
 
tools/pygrub/src/fsys/ext2/ext2module.c +20 -13
diff -r 55a5ad2f028d tools/pygrub/src/fsys/ext2/ext2module.c
@@ -208,22 +208,28 @@
 ext2_fs_open (Ext2Fs *fs, PyObject *args, PyObject *kwargs)
 {
     static char *kwlist[] = { "name", "flags", "superblock", 
-                              "block_size", NULL };
+                              "block_size", "offset", NULL };
     char * name;
-    int flags = 0, superblock = 0, err;
+    int flags = 0, superblock = 0, offset = 0, err;
     unsigned int block_size = 0;
     ext2_filsys efs;
-
-    if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|iii", kwlist, 
-                                     &name, &flags, &superblock, &block_size))
-                                     return NULL;
+    char offsetopt[30];
+
+    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;
     }
 
-    err = ext2fs_open(name, flags, superblock, block_size, 
+    if (offset != 0) {
+        snprintf(offsetopt, 29, "offset=%d", offset);
+    }
+
+    err = ext2fs_open2(name, offsetopt, flags, superblock, block_size, 
                       unix_io_manager, &efs);
     if (err) {
         PyErr_SetString(PyExc_ValueError, "unable to open file");
@@ -323,14 +329,15 @@
 ext2_fs_new(PyObject *o, PyObject *args, PyObject *kwargs) 
 {
     static char *kwlist[] = { "name", "flags", "superblock", 
-                              "block_size", NULL };
+                              "block_size", "offset", NULL };
     char * name;
-    int flags = 0, superblock = 0;
+    int flags = 0, superblock = 0, offset;
     unsigned int block_size = 0;
     Ext2Fs *pfs;
 
-    if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|iii", kwlist, 
-                                     &name, &flags, &superblock, &block_size))
+    if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|iiii", kwlist, 
+                                     &name, &flags, &superblock, &block_size,
+                                     &offset))
         return NULL;
 
     pfs = (Ext2Fs *) PyObject_NEW(Ext2Fs, &Ext2FsType);
@@ -339,8 +346,8 @@
     pfs->fs = NULL;
 
     if (!ext2_fs_open(pfs, 
-                      Py_BuildValue("siii", name, flags, superblock, block_size),
-                      NULL))
+                      Py_BuildValue("siiii", name, flags, superblock, 
+                                    block_size, offset), NULL))
         return NULL;
 
     return (PyObject *)pfs;
tools/pygrub/src/pygrub +26 -8
diff -r 55a5ad2f028d tools/pygrub/src/pygrub
@@ -24,7 +24,7 @@
 import grub.GrubConf
 import grub.fsys
 
-PYGRUB_VER = 0.02
+PYGRUB_VER = 0.3
 
 
 def draw_window():
@@ -77,9 +77,21 @@
     buf = os.read(fd, 512)
     os.close(fd)
 
-    if len(buf) >= 512 and struct.unpack("H", buf[0x1fe: 0x200]) == (0xaaff):
+    if len(buf) >= 512 and struct.unpack("H", buf[0x1fe: 0x200]) == (0xaa55,):
         return True
     return False
+
+SECTOR_SIZE=512
+def get_active_offset(file):
+    """Find the offset for the start of the first active partition in the
+    disk image file."""
+    fd = os.open(file, os.O_RDONLY)
+    buf = os.read(fd, 512)
+    for poff in (446, 462, 478, 494): # partition offsets
+        # active partition has 0x80 as the first byte
+        if struct.unpack("<c", buf[p:p+1]) == ('\x80',):
+            return struct.unpack("<", buf[p+8:p+12])[0] * SECTOR_SIZE
+    return -1
 
 def get_config(fn):
     if not os.access(fn, os.R_OK):
@@ -87,14 +99,17 @@
 
     cf = grub.GrubConf.GrubConfigFile()
 
+    offset = 0
     if is_disk_image(fn):
-        raise RuntimeError, "appears to be a full disk image... unable to handle this yet"
+        offset = get_active_offset(fn)
+        if offset == -1:
+            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):
-            fs = fstype.open_fs(fn)
+        if fstype.sniff_magic(fn, offset):
+            fs = fstype.open_fs(fn, offset)
             break
 
     if fs is not None:
@@ -244,14 +259,17 @@
     if img.initrd:
         print "  initrd: %s" %(img.initrd[1],)
 
+    offset = 0
     if is_disk_image(file):
-        raise RuntimeError, "unable to handle full disk images yet"
+        offset = get_active_offset(fn)
+        if offset == -1:
+            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):
-            fs = fstype.open_fs(file)
+        if fstype.sniff_magic(file, offset):
+            fs = fstype.open_fs(file, offset)
             break
 
     if fs is None:

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel