stuff...

  • Increase font size
  • Default font size
  • Decrease font size
Home Software Linux Which device should I use on grub?

Which device should I use on grub?

E-mail Print PDF
User Rating: / 0
PoorBest 

Hi,

I recently wrote a simple program that parses a linux-device string and returns another string translated on how grub use it. That is, given a /dev/disk -partition string returns a string that it's grub-readable.

For instance, if you call the program with /dev/sda3, the program will print out (hd0,2).

Most users get confused at the beggining with linux and grub on how grub uses 0s and /dev uses letters and numbers. So, this is a simple, very simple program that make the job for those users (and for free!).

/*
 *   This program is free software; you can 
 *   redistribute it and/or modify it under
 *   the terms of the GNU General Public License
 *   as published by the Free Software Foundation;
 *   version 2 of the License.
 *
 *   This program is distributed in the hope 
 *   that it will be useful, but WITHOUT ANY 
 *   WARRANTY; without even the implied warranty
 *   of MERCHANTABILITY or FITNESS FOR A 
 *   PARTICULAR PURPOSE.  See the GNU General 
 *   Public License for more details.
 *
 *   Author: Sebastian Treu
 *   Mail: sebastian.treu (at) gmail.com
 *   Date: 20 / 09 / 2009
*/              

#include <stdio.h>
#include 


#define V_LEN 3
#define INIT 0

int main(int argc, char *argv[]) {

  if ( argc != 2) 
  {
    printf("Usage:\n----\n\n ./grub2dev <device>\n\n");
    exit(1);
  }
  /* variables needed for pcre */  
  int v[V_LEN];
  pcre *regex;
  const char *error;
  char *device = argv[argc-1];
  int errpos;
  
  /* string length */
  int len  = 0;
  
  regex = pcre_compile("/dev/sd|hd[a-z][1-9]\0",0,&error,&errpos,NULL);
    
  if ( regex == NULL ) 
  {
    printf("Error: prce_compile fail.\n");
    exit(1);
  }  
  
  /* get arguments length */  
  for(len; device[len] != 0; len++);
  
  if ( pcre_exec(regex,NULL,argv[argc-1],len,INIT,0,v,V_LEN) < 1 ) 
  {
    printf("<device> is not well specified.\n");
    exit(1);
  }
  
  /* Nuestro cadena <device> es correcta */
  int hd, part;
  
  hd   = device[len-2] - 'a';
  part = device[len-1] - '1';
  
  printf("\nGrub Notation:\n")
  printf("(hd%d,%d) for  (%s)\n",hd,part,device);
  printf("\n This is Free Software. (c) Sebastian Treu 2009\n");
}

You can compile it with:

gcc dev2grub.c -o dev2grub -lpcre

Run the program as:

./dev2grub /dev/sda2

Of course, this can be done with bash scripts.





Free web hostingWeb hosting
Last Updated on Monday, 25 January 2010 01:29  

Add comment

Be polite.


Security code
Refresh


polls