qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: "Jim C. Brown" <jbrown106@phreaker.net>
To: qemu-devel@nongnu.org
Subject: Re: [Qemu-devel] Mounting a disk image under Linux
Date: Sat, 19 Jun 2004 19:48:35 -0400	[thread overview]
Message-ID: <20040619234835.GA14521@jbrown.mylinuxbox.org> (raw)
In-Reply-To: <1087675230.9477.296.camel@sherbert>

[-- Attachment #1: Type: text/plain, Size: 1440 bytes --]

On Sat, Jun 19, 2004 at 09:00:30PM +0100, Gianni Tedesco wrote:
> On Sat, 2004-06-19 at 13:31 -0400, Jim C. Brown wrote:
> > lomount -t vfat -diskimage /opt/qemu/tempImage -partition 1 /mnt/tempImage
> > 
> > It works great but has a bug if you try to mount a partition which doesnt
> > exist.
> 
> Sounds great. Perhaps it could be included with qemu, (at least in souce
> dist!)?

Sure. I have no problems with that.

> 
> Do you want to post it on the list to fix it? Or have you just not had
> time?

I wrote it in under an hour, right before I went to bed. I've attached the
source code here but be warned: its very ugly. Very little error checking.
Expects fdisk to work right (which in turns means it assumes fdisk is being
passed a valid image with a valid parition table).

When I have some free time I'll try to clean it up. Right now it's full
of ugly hacks. (It was originally meant to be a shell script not a C program,
only my shell programming skills did not extend so far.)

> 
> -- 
> // Gianni Tedesco (gianni at scaramanga dot co dot uk)
> lynx --source www.scaramanga.co.uk/scaramanga.asc | gpg --import
> 8646BE7D: 6D9F 2287 870E A2C9 8F60 3A3C 91B5 7669 8646 BE7D



> _______________________________________________
> Qemu-devel mailing list
> Qemu-devel@nongnu.org
> http://lists.nongnu.org/mailman/listinfo/qemu-devel


-- 
Infinite complexity begets infinite beauty.
Infinite precision begets infinite perfection.

[-- Attachment #2: lomount.c --]
[-- Type: text/plain, Size: 2703 bytes --]

#include <stdio.h>
#include <stdlib.h>

#define BUF 4096
#define TEMPFILE "/tmp/temp.minix"

int partnum(char * diskimage, FILE * temp, int * pnum)
{
	int num=0, c, i;
	char buf[BUF], buf2[BUF];
	strncpy(buf, diskimage, BUF);
	strncat(buf, "%d", BUF-strlen(buf));
	fscanf(temp, buf, pnum); /* skip start of line */
#ifdef DEBUG
	printf("pnum = %d\n", *pnum);
#endif
	c = fgetc(temp);
	while (c == ' ' || c == '*' || c == '@')
	{
		c = fgetc(temp);
#ifdef DEBUG
		printf("c = %d ", c);
#endif
	}
	/*ungetc(c, temp);
	fscanf(temp, "%d", &num);*/
#ifdef DEBUG
	printf("c = %d\n", c);
#endif
	buf2[0] = c;
	c = fgetc(temp);
	i = 1;
	while (c == '0' || c == '1' || c == '2' || c == '3' || c == '4' || c == '5' || c == '6' || c == '7' || c == '8' || c == '9')
	{
		buf2[i] = c;
		c = fgetc(temp);
		i++;
	}
	buf2[i] = '\0';
	num = atoi(buf2);
#ifdef DEBUG
	printf("buf2 = %s num = %d\n", buf2, num);
#endif
	fgets(buf, BUF, temp); /* skip rest of line */
	return num;
}

int main(int argc, char ** argv)
{
	FILE * temp;
	char buf[BUF], argv2[BUF], diskimage[BUF];
	int partition = 1, sec1, sec2, num = 0, pnum = 0, len = BUF, i, f = 0;
	for (i = 1; i < argc; i ++)
	{
		if (strncmp(argv[i], "-diskimage", BUF)==0)
		{
			strncpy(diskimage, argv[i+1], BUF);
			i++; f = 1;
		}
		else if (strncmp(argv[i], "-partition", BUF)==0)
		{
			partition = atoi(argv[i+1]);
			i++;
			if (partition < 1) partition = 1;
		}
		else
		{
			strncat(argv2, argv[i], len);
			strncat(argv2, " ", len-1);
			len -= strlen(argv[i]);
			len--;
		}
	}
	if (!f)
	{
		printf("You must specify -diskimage and -partition\n");
		printf("ex. lomount -t fs-type -diskimage hda.img -partition 1 /mnt\n");
		return 0;
	}
	snprintf(buf, BUF, "fdisk -lu %s > %s 2>&1", diskimage, TEMPFILE);
	system(buf);
	temp = fopen(TEMPFILE, "r");
	fgets(buf, BUF, temp); /* skip first line */
#ifdef DEBUG
	printf("spare line: %s\n", buf);
#endif
	fgets(buf, BUF, temp); /* skip second line */
#ifdef DEBUG
	printf("spare line: %s\n", buf);
#endif
	fgets(buf, BUF, temp); /* skip third line */
#ifdef DEBUG
	printf("spare line: %s\n", buf);
#endif
	fgets(buf, BUF, temp); /* skip fourth line */
#ifdef DEBUG
	printf("spare line: %s\n", buf);
#endif
	fscanf(temp, "Units = sectors of %d * %d bytes\n", &sec1, &sec2);
#ifdef DEBUG
	printf("sec1: %d sec2: %d\n", sec1, sec2);
#endif
	fgets(buf, BUF, temp); /* skip sixth line */
#ifdef DEBUG
	printf("spare line: %s\n", buf);
#endif
	while (pnum != partition)
	{
		num = partnum(diskimage, temp, &pnum);
	}
	fclose(temp);
	pnum = sec1 * sec2 * num;
#ifdef DEBUG
	printf("offset = %d\n", pnum);
#endif
	snprintf(buf, BUF, "echo mount -oloop,offset=%d %s %s", pnum, diskimage, argv2);
	return system(buf);
}

  reply	other threads:[~2004-06-20  4:07 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-06-16 14:42 [Qemu-devel] some help Ludovic Gele
2004-06-16 19:13 ` [Qemu-devel] Can't see network under qemu 0.5.5 Leonard T. Erwine
2004-06-17  8:22   ` Ludovic Gele
2004-06-17 15:39     ` Marc Vertes
2004-06-18 20:58       ` Leonard T. Erwine
2004-06-18 20:34     ` Leonard T. Erwine
2004-06-16 20:26 ` [Qemu-devel] Useful Q&A Benjamin Brown
2004-06-16 20:35   ` Bartosz Fabianowski
2004-06-17 10:42     ` Antony T Curtis
2004-06-16 21:02   ` Fabrice Bellard
2004-06-16 21:16     ` malc
2004-06-16 21:41     ` Benjamin Brown
2004-06-17  8:25       ` Johannes Schindelin
2004-06-19 17:35         ` Benjamin Brown
2004-06-19 16:34       ` [Qemu-devel] Mounting a disk image under Linux Benjamin Brown
2004-06-19 16:39         ` Grzegorz Kulewski
2004-06-19 16:40         ` Gianni Tedesco
2004-06-19 17:16           ` Benjamin Brown
2004-06-19 17:31         ` Jim C. Brown
2004-06-19 20:00           ` Gianni Tedesco
2004-06-19 23:48             ` Jim C. Brown [this message]
2004-06-20  8:20               ` Mulyadi Santosa

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20040619234835.GA14521@jbrown.mylinuxbox.org \
    --to=jbrown106@phreaker.net \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).