From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.33) id 1BbxbF-0008GJ-Ri for qemu-devel@nongnu.org; Sun, 20 Jun 2004 04:22:50 -0400 Received: from exim by lists.gnu.org with spam-scanned (Exim 4.33) id 1BbxbD-0008CI-T1 for qemu-devel@nongnu.org; Sun, 20 Jun 2004 04:22:49 -0400 Received: from [199.232.76.173] (helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.33) id 1BbxbD-0008BP-Os for qemu-devel@nongnu.org; Sun, 20 Jun 2004 04:22:47 -0400 Received: from [66.54.152.27] (helo=jive.SoftHome.net) by monty-python.gnu.org with smtp (Exim 4.34) id 1BbxZt-0005Nj-Lh for qemu-devel@nongnu.org; Sun, 20 Jun 2004 04:21:26 -0400 From: Mulyadi Santosa Subject: Re: Re: [Qemu-devel] Mounting a disk image under Linux Date: Sun, 20 Jun 2004 15:20:24 +0700 References: <1087396951.40d05c57252a0@www.raysa.org> <1087675230.9477.296.camel@sherbert> <20040619234835.GA14521@jbrown.mylinuxbox.org> In-Reply-To: <20040619234835.GA14521@jbrown.mylinuxbox.org> MIME-Version: 1.0 Content-Type: Multipart/Mixed; boundary="Boundary-00=_IjU1AUtMQhl7o9P" Message-Id: <200406201520.24153.a_mulyadi@softhome.net> Reply-To: a_mulyadi@softhome.net, qemu-devel@nongnu.org List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org --Boundary-00=_IjU1AUtMQhl7o9P Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit Content-Disposition: inline Hello all I take Jim's program and add some checking of fdisk output. Basically, it just skip the output : "You must set cylinders. You can do this from the extra functions menu" I got this message when run fdisk -lu, so I skip them until I get following lines: "Disk /mnt/qemu/myimage: 0 MB, 0 bytes" And the rest of the program is pretty similar regards Mulyadi --Boundary-00=_IjU1AUtMQhl7o9P Content-Type: text/x-csrc; charset="iso-8859-1"; name="lomount.c" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="lomount.c" #include #include #include #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; int total_sec; int pressed_key=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, "/sbin/fdisk -lu %s > %s 2>&1", diskimage, TEMPFILE); system(buf); temp = fopen(TEMPFILE, "r"); do { fgets(buf, BUF, temp); /* skip until we get "Disk" */ } while (strncasecmp(buf, "Disk", 4)!=0); fgets(buf, BUF, temp); //skip once more fscanf(temp, "Units = sectors of %d * %d = %d bytes\n", &sec1, &sec2, &total_sec); #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, "mount -oloop,offset=%d %s %s", pnum, diskimage, argv2); printf("Do you want to execute mount command? [Y/n] "); pressed_key=getchar(); /* if user press Enter (that means YES), or 'y' or 'Y' then execute the command */ if ( (pressed_key==10) || (pressed_key==89) || (pressed_key==121) ) { system(buf); } return 0; } --Boundary-00=_IjU1AUtMQhl7o9P--