Skip to contentSkip to conversation

Loopback device

PATCH Marco Gerards 4 participants 12 messages Jan 21, 2005

lore
Share & tools
lore
#0 lore raw

Hi,

Here is a patch to add loopback support to GRUB. Although a lot of people told me they would find it useful, I just wrote it out of boredom without thinking about its use, just to figure out how hard it would be.

I could commit it if anyone really wants it. Can I commit it?

Thanks,
Marco

2005-01-21 Marco Gerards <[email protected]>

Add the loopback device, a device via which files can be accessed as devices.

* conf/i386-pc.rmk (grub_emu_SOURCES): Add `disk/loopback.c'. (pkgdata_MODULES): Add loopback.mod. (loopback_SOURCES): New variable.
(loopback_CFLAGS): Likewise.
* conf/powerpc-ieee1275.rmk (grub_emu_SOURCES): Add `disk/loopback.c'.
(pkgdata_MODULES): Add loopback.mod. (loopback_SOURCES): New variable.
(loopback_CFLAGS): Likewise.
* disk/loopback.c: new file.
* include/grub/normal.h (grub_loop_init): New prototype. (grub_loop_fini): New prototype.
(util/grub-emu.c): Initialize and de-initialize loopback support.

Index: conf/i386-pc.rmk
===================================================================
RCS file: /cvsroot/grub/grub2/conf/i386-pc.rmk,v
retrieving revision 1.23

i386-pc.rmk +6 -2
diff -u -p -u -p -r1.23 i386-pc.rmk
@@ -73,7 +73,7 @@ grub_emu_SOURCES = kern/main.c kern/devi
 	commands/terminal.c commands/boot.c commands/cmp.c commands/cat.c		\
 	util/i386/pc/biosdisk.c fs/fat.c fs/ext2.c fs/ufs.c fs/minix.c fs/hfs.c fs/jfs.c fs/iso9660.c \
 	normal/cmdline.c normal/command.c normal/main.c normal/menu.c normal/arg.c	\
-	util/console.c util/grub-emu.c util/misc.c util/i386/pc/getroot.c
+	util/console.c util/grub-emu.c util/misc.c util/i386/pc/getroot.c disk/loopback.c
 grub_emu_LDFLAGS = -lncurses
 
 # For genmoddep.
@@ -83,7 +83,7 @@ genmoddep_SOURCES = util/genmoddep.c
 pkgdata_MODULES = _chain.mod _linux.mod linux.mod fat.mod ufs.mod ext2.mod minix.mod \
 	hfs.mod jfs.mod normal.mod hello.mod vga.mod font.mod _multiboot.mod ls.mod \
 	boot.mod cmp.mod cat.mod terminal.mod fshelp.mod chain.mod multiboot.mod \
-	amiga.mod apple.mod pc.mod
+	amiga.mod apple.mod pc.mod loopback.mod
 
 # For _chain.mod.
 _chain_mod_SOURCES = loader/i386/pc/chainloader.c
@@ -190,3 +190,7 @@ apple_mod_CFLAGS = $(COMMON_CFLAGS)
 # For pc.mod
 pc_mod_SOURCES = partmap/pc.c
 pc_mod_CFLAGS = $(COMMON_CFLAGS)
+
+# For loopback.mod
+loopback_SOURCES = disk/loopback.c
+loopback_CFLAGS = $(COMMON_CFLAGS)

Index: conf/powerpc-ieee1275.rmk
===================================================================
RCS file: /cvsroot/grub/grub2/conf/powerpc-ieee1275.rmk,v retrieving revision 1.19

powerpc-ieee1275.rmk +7 -2
diff -u -p -u -p -r1.19 powerpc-ieee1275.rmk
@@ -41,7 +41,7 @@ grub_emu_SOURCES = kern/main.c kern/devi
 	normal/cmdline.c normal/command.c normal/main.c normal/menu.c	\
 	normal/arg.c kern/partition.c	\
 	util/console.c util/grub-emu.c util/misc.c util/i386/pc/getroot.c \
-	kern/env.c commands/ls.c		\
+	kern/env.c disk/loopback.c commands/ls.c		\
 	commands/terminal.c commands/boot.c commands/cmp.c commands/cat.c
 grub_emu_LDFLAGS = -lncurses
 
@@ -64,7 +64,8 @@ genmoddep_SOURCES = util/genmoddep.c
 # Modules.
 pkgdata_MODULES = _linux.mod linux.mod fat.mod ufs.mod ext2.mod minix.mod \
 	hfs.mod jfs.mod normal.mod hello.mod font.mod \
-	boot.mod cmp.mod cat.mod terminal.mod fshelp.mod amiga.mod apple.mod pc.mod
+	boot.mod cmp.mod cat.mod terminal.mod fshelp.mod amiga.mod apple.mod \
+	pc.mod loopback.mod
 
 # For fshelp.mod.
 fshelp_mod_SOURCES = fs/fshelp.c
@@ -151,3 +152,7 @@ apple_mod_CFLAGS = $(COMMON_CFLAGS)
 # For pc.mod
 pc_mod_SOURCES = partmap/pc.c
 pc_mod_CFLAGS = $(COMMON_CFLAGS)
+
+# For loopback.mod
+loopback_SOURCES = disk/loopback.c
+loopback_CFLAGS = $(COMMON_CFLAGS)

Index: disk/loopback.c
===================================================================
RCS file: disk/loopback.c

loopback.c new file +262 -0

262 lines not on this page · raw message

Index: include/grub/normal.h
===================================================================
RCS file: /cvsroot/grub/grub2/include/grub/normal.h,v retrieving revision 1.9

grub/normal.h +2 -0
diff -u -p -u -p -r1.9 normal.h
@@ -158,6 +158,8 @@ void grub_cmp_init (void);
 void grub_cmp_fini (void);
 void grub_terminal_init (void);
 void grub_terminal_fini (void);
+void grub_loop_init (void);
+void grub_loop_fini (void);
 #endif
 
 #endif /* ! GRUB_NORMAL_HEADER */

Index: util/grub-emu.c
===================================================================
RCS file: /cvsroot/grub/grub2/util/grub-emu.c,v
retrieving revision 1.11

grub-emu.c +2 -1
diff -u -p -u -p -r1.11 grub-emu.c
@@ -176,13 +176,14 @@ main (int argc, char *argv[])
   grub_cmp_init ();
   grub_cat_init ();
   grub_terminal_init ();
-
+  grub_loop_init ();
   /* XXX: Should normal mode be started by default?  */
   grub_normal_init ();
 
   /* Start GRUB!  */
   grub_main ();
 
+  grub_loop_fini ();
   grub_util_biosdisk_fini ();
   grub_normal_fini ();
   grub_ufs_fini ();
#1 lore raw
Quoted 8 lines · Marco Gerards

Hi,

Here is a patch to add loopback support to GRUB. Although a lot of people told me they would find it useful, I just wrote it out of boredom without thinking about its use, just to figure out how hard it would be.

I could commit it if anyone really wants it. Can I commit it?

Thanks, that's handy.

Note I'm not sure it's completely necessary though as I have been using grub on a file for ages by just doing losetup /dev/loop0 disk.img before running grub.

Signature · 2 lines
Pádraig Brady - http://www.pixelbeat.org
--
#2 lore raw
Quoted 8 lines · [email protected] writes:
Quoted 6 lines · Marco Gerards

Hi,
Here is a patch to add loopback support to GRUB. Although a lot of people told me they would find it useful, I just wrote it out of boredom without thinking about its use, just to figure out how hard it would be.
I could commit it if anyone really wants it. Can I commit it?

Quoted 6 lines

Thanks, that's handy.

Note I'm not sure it's completely necessary though as I have been using grub on a file for ages by just doing losetup /dev/loop0 disk.img before running grub.

This patch is more for when you are booting.

So when you booted GRUB from your harddrive you can use the commands:

loopback loop (hd0,4)/home/marco/image/floppy.ext2 linux (loop)/vmlinuz root=/dev/hda5
boot

To load a kernel from a floppy image on your harddrive. Same for iso9660, etc.

I have no idea if your example would work. I never used such things. :)

--
Marco

#3 lore raw
In reply to Marco Gerards
Quoted 4 lines · Marco Gerards

Here is a patch to add loopback support to GRUB. Although a lot of people told me they would find it useful, I just wrote it out of boredom without thinking about its use, just to figure out how hard it would be.

That's great. That is the very thing I wanted to have.

BTW, I think it is better to make it possible to choose a device type (i.e. if it has a partition table or not). I'm always annoyed with that Linux cannot deal with a hard disk image.

Okuji

#4 lore raw
Quoted 11 lines · Yoshinori K. Okuji
Quoted 4 lines · Marco Gerards

Here is a patch to add loopback support to GRUB. Although a lot of people told me they would find it useful, I just wrote it out of boredom without thinking about its use, just to figure out how hard it would be.

That's great. That is the very thing I wanted to have.

BTW, I think it is better to make it possible to choose a device type (i.e. if it has a partition table or not). I'm always annoyed with that Linux cannot deal with a hard disk image.

I could add an argument for that. Such thing is not hard. But I don't have a harddisk image around to test this.

Can someone suggest a good image (bochs or so) that has multiple partitions and filesystems so I can test this? I prefer ext2.

Thanks,
Marco

#5 lore raw
Quoted 2 lines · Marco Gerards

Can someone suggest a good image (bochs or so) that has multiple partitions and filesystems so I can test this? I prefer ext2.

You can make it yourself very easily. Try parted.

Okuji

#6 lore raw
In reply to Marco Gerards

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Marco Gerards wrote:

| I could add an argument for that.  Such thing is not hard.  But I
| don't have a harddisk image around to test this.
|
| Can someone suggest a good image (bochs or so) that has multiple
| partitions and filesystems so I can test this?  I prefer ext2.

That could be easily be generated :

$ dd if=/dev/zero of=your_image bs=512 count=...
$ /sbin/parted your_image
mklabel ("help mklabel" to see the available types of partition table
available)
mkpartfs

I used that a lot to test grub support for SPT & SUN partition tables.

Vincent Pelletier
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.5 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

iD8DBQFB8Ra0FEQoKRQyjtURAqK1AJ9bMNXbRZGSsKuCUbUwqudtY6TDWgCgtlcg 4tKcWdW+dFcy85Rd9vtu9jM=
=o8fC
-----END PGP SIGNATURE-----

#7 lore raw
In reply to Yoshinori K. Okuji
Quoted 11 lines · Yoshinori K. Okuji
Quoted 4 lines · Marco Gerards

Here is a patch to add loopback support to GRUB. Although a lot of people told me they would find it useful, I just wrote it out of boredom without thinking about its use, just to figure out how hard it would be.

That's great. That is the very thing I wanted to have.

BTW, I think it is better to make it possible to choose a device type (i.e. if it has a partition table or not). I'm always annoyed with that Linux cannot deal with a hard disk image.

Yes that's annoying. Have a look at:
ftp://ftp.hq.nasa.gov/pub/ig/ccd/enhanced_loopback/

I get around the problem with this script:

#!/bin/sh
FILE=$1
PART=$2
DEST=$3

UNITS=fdisk -lu $FILE 2>/dev/null | grep $FILE$PART | tr -s ' ' | cut -f2 -d' ' OFFSET=expr 512 '*' $UNITS
mount -o loop,offset=$OFFSET $FILE $DEST

Signature · 2 lines
Pádraig Brady - http://www.pixelbeat.org
--
#8 lore raw
In reply to Yoshinori K. Okuji

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Yoshinori K. Okuji wrote:
| I'm always annoyed with that
| Linux cannot deal with a hard disk image.

Same here.
Maybe should we try to write something ?

Vincent Pelletier

PS: thanks Okuji & Marco for the savanah access :). -----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.5 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

iD8DBQFB8RXdFEQoKRQyjtURAtqJAJ9fpUIKbQCwRgXzRnHIc4VHo7azJwCff5uG u9ePasK7WVWBM15Fhlr7mBI=
=F/r5
-----END PGP SIGNATURE-----

#9 lore raw
In reply to Yoshinori K. Okuji
Quoted 11 lines · Yoshinori K. Okuji
Quoted 4 lines · Marco Gerards

Here is a patch to add loopback support to GRUB. Although a lot of people told me they would find it useful, I just wrote it out of boredom without thinking about its use, just to figure out how hard it would be.

That's great. That is the very thing I wanted to have.

BTW, I think it is better to make it possible to choose a device type (i.e. if it has a partition table or not). I'm always annoyed with that Linux cannot deal with a hard disk image.

I did that and the new patch is included with this email.

Unfortunately it does not work well, but I believe that is a filesystem problem. Can I commit this patch? I will put the filesystem problem on the wiki so it is documented to have a look at it later.

When the PPC port is mostly finished I plan to have a look at the bugs in GRUB and do some testing. I think there are some regressions in the filesystems, there are a few known bugs and perhaps I can write some testing tools to make it easier to test filesystems.

Thanks,
Marco

2005-01-21 Marco Gerards <[email protected]>

Add the loopback device, a device via which files can be accessed as devices.

* conf/i386-pc.rmk (grub_emu_SOURCES): Add `disk/loopback.c'. (pkgdata_MODULES): Add loopback.mod. (loopback_SOURCES): New variable.
(loopback_CFLAGS): Likewise.
* conf/powerpc-ieee1275.rmk (grub_emu_SOURCES): Add `disk/loopback.c'.
(pkgdata_MODULES): Add loopback.mod. (loopback_SOURCES): New variable.
(loopback_CFLAGS): Likewise.
* disk/loopback.c: new file.
* include/grub/normal.h (grub_loop_init): New prototype. (grub_loop_fini): New prototype.
(util/grub-emu.c): Initialize and de-initialize loopback support.
* include/grub/disk.h (grub_disk_dev_id): Add `GRUB_DISK_DEVICE_LOOPBACK_ID'.

Index: conf/i386-pc.rmk
===================================================================
RCS file: /cvsroot/grub/grub2/conf/i386-pc.rmk,v
retrieving revision 1.23

i386-pc.rmk +6 -2
diff -u -p -u -p -r1.23 i386-pc.rmk
@@ -73,7 +73,7 @@ grub_emu_SOURCES = kern/main.c kern/devi
 	commands/terminal.c commands/boot.c commands/cmp.c commands/cat.c		\
 	util/i386/pc/biosdisk.c fs/fat.c fs/ext2.c fs/ufs.c fs/minix.c fs/hfs.c fs/jfs.c fs/iso9660.c \
 	normal/cmdline.c normal/command.c normal/main.c normal/menu.c normal/arg.c	\
-	util/console.c util/grub-emu.c util/misc.c util/i386/pc/getroot.c
+	util/console.c util/grub-emu.c util/misc.c util/i386/pc/getroot.c disk/loopback.c
 grub_emu_LDFLAGS = -lncurses
 
 # For genmoddep.
@@ -83,7 +83,7 @@ genmoddep_SOURCES = util/genmoddep.c
 pkgdata_MODULES = _chain.mod _linux.mod linux.mod fat.mod ufs.mod ext2.mod minix.mod \
 	hfs.mod jfs.mod normal.mod hello.mod vga.mod font.mod _multiboot.mod ls.mod \
 	boot.mod cmp.mod cat.mod terminal.mod fshelp.mod chain.mod multiboot.mod \
-	amiga.mod apple.mod pc.mod
+	amiga.mod apple.mod pc.mod loopback.mod
 
 # For _chain.mod.
 _chain_mod_SOURCES = loader/i386/pc/chainloader.c
@@ -190,3 +190,7 @@ apple_mod_CFLAGS = $(COMMON_CFLAGS)
 # For pc.mod
 pc_mod_SOURCES = partmap/pc.c
 pc_mod_CFLAGS = $(COMMON_CFLAGS)
+
+# For loopback.mod
+loopback_SOURCES = disk/loopback.c
+loopback_CFLAGS = $(COMMON_CFLAGS)

Index: conf/powerpc-ieee1275.rmk
===================================================================
RCS file: /cvsroot/grub/grub2/conf/powerpc-ieee1275.rmk,v retrieving revision 1.19

powerpc-ieee1275.rmk +7 -2
diff -u -p -u -p -r1.19 powerpc-ieee1275.rmk
@@ -41,7 +41,7 @@ grub_emu_SOURCES = kern/main.c kern/devi
 	normal/cmdline.c normal/command.c normal/main.c normal/menu.c	\
 	normal/arg.c kern/partition.c	\
 	util/console.c util/grub-emu.c util/misc.c util/i386/pc/getroot.c \
-	kern/env.c commands/ls.c		\
+	kern/env.c disk/loopback.c commands/ls.c		\
 	commands/terminal.c commands/boot.c commands/cmp.c commands/cat.c
 grub_emu_LDFLAGS = -lncurses
 
@@ -64,7 +64,8 @@ genmoddep_SOURCES = util/genmoddep.c
 # Modules.
 pkgdata_MODULES = _linux.mod linux.mod fat.mod ufs.mod ext2.mod minix.mod \
 	hfs.mod jfs.mod normal.mod hello.mod font.mod \
-	boot.mod cmp.mod cat.mod terminal.mod fshelp.mod amiga.mod apple.mod pc.mod
+	boot.mod cmp.mod cat.mod terminal.mod fshelp.mod amiga.mod apple.mod \
+	pc.mod loopback.mod
 
 # For fshelp.mod.
 fshelp_mod_SOURCES = fs/fshelp.c
@@ -151,3 +152,7 @@ apple_mod_CFLAGS = $(COMMON_CFLAGS)
 # For pc.mod
 pc_mod_SOURCES = partmap/pc.c
 pc_mod_CFLAGS = $(COMMON_CFLAGS)
+
+# For loopback.mod
+loopback_SOURCES = disk/loopback.c
+loopback_CFLAGS = $(COMMON_CFLAGS)

Index: disk/loopback.c
===================================================================
RCS file: disk/loopback.c

loopback.c new file +272 -0

272 lines not on this page · raw message

Index: include/grub/disk.h
===================================================================
RCS file: /cvsroot/grub/grub2/include/grub/disk.h,v retrieving revision 1.5

grub/disk.h +1 -0
diff -u -p -u -p -r1.5 disk.h
@@ -31,6 +31,7 @@ enum grub_disk_dev_id
   {
     GRUB_DISK_DEVICE_BIOSDISK_ID,
     GRUB_DISK_DEVICE_OFDISK_ID,
+    GRUB_DISK_DEVICE_LOOPBACK_ID
   };
 
 struct grub_disk;

Index: include/grub/normal.h
===================================================================
RCS file: /cvsroot/grub/grub2/include/grub/normal.h,v retrieving revision 1.9

grub/normal.h +2 -0
diff -u -p -u -p -r1.9 normal.h
@@ -158,6 +158,8 @@ void grub_cmp_init (void);
 void grub_cmp_fini (void);
 void grub_terminal_init (void);
 void grub_terminal_fini (void);
+void grub_loop_init (void);
+void grub_loop_fini (void);
 #endif
 
 #endif /* ! GRUB_NORMAL_HEADER */

Index: util/grub-emu.c
===================================================================
RCS file: /cvsroot/grub/grub2/util/grub-emu.c,v
retrieving revision 1.11

grub-emu.c +4 -2
diff -u -p -u -p -r1.11 grub-emu.c
@@ -176,13 +176,15 @@ main (int argc, char *argv[])
   grub_cmp_init ();
   grub_cat_init ();
   grub_terminal_init ();
-
+  grub_loop_init ();
+  
   /* XXX: Should normal mode be started by default?  */
   grub_normal_init ();
-
+  
   /* Start GRUB!  */
   grub_main ();
 
+  grub_loop_fini ();
   grub_util_biosdisk_fini ();
   grub_normal_fini ();
   grub_ufs_fini ();
#10 lore raw
Quoted 1 line · Marco Gerards

I did that and the new patch is included with this email.

Thank you.

Quoted 4 lines

Unfortunately it does not work well, but I believe that is a filesystem problem. Can I commit this patch? I will put the filesystem problem on the wiki so it is documented to have a look at it later.

Please.

Okuji

#11 lore raw
Quoted 6 lines · Yoshinori K. Okuji
Quoted 4 lines

Unfortunately it does not work well, but I believe that is a filesystem problem. Can I commit this patch? I will put the filesystem problem on the wiki so it is documented to have a look at it later.

Please.

Committed and updated the wiki.

--
Marco