Git-Url: http://git.frugalware.org/gitweb/gitweb.cgi?p=frugalwareutils.git;a=commitdiff;h=3350544f1de8dbc34aa04ccec5b3994c34e24c7c
commit 3350544f1de8dbc34aa04ccec5b3994c34e24c7c Author: James Buren <r...@frugalware.org> Date: Fri Feb 24 00:26:43 2012 -0600 initial draft of new libfwgrubconfig diff --git a/libfwgrubconfig/new.c b/libfwgrubconfig/new.c new file mode 100644 index 0000000..eec56cc --- /dev/null +++ b/libfwgrubconfig/new.c @@ -0,0 +1,139 @@ +/* + * libfwgrubconfig.c for frugalwareutils + * + * Copyright (c) 2006, 2007, 2008, 2009, 2010, 2011 by Miklos Vajna <vmik...@frugalware.org> + * + * 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; either version 2 of the License, or + * (at your option) any later version. + * + * 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. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, + * USA. + */ + +#include <stdlib.h> +#include <stdio.h> +#include <string.h> +#include <regex.h> +#include <unistd.h> +#include <limits.h> +#include "libfwgrubconfig.h" + +#define SPACE " \t\r\n\v\f" +#define DIGIT "0123456789" +#define FWGRUB_LOGDEV "/dev/tty4" + +static +char *guess_mbr_device(void) +{ + FILE *f; + char line[LINE_MAX], *p; + int i, j; + regex_t re; + static char root[4096]; + + f = fopen("/proc/partitions","rb"); + + if(!f) + return 0; + + if(regcomp(&re,"^[shv]d[a-z]$",REG_EXTENDED | REG_NOSUB | REG_NEWLINE)) + { + fclose(f); + + return 0; + } + + for( i = 1, *root = 0 ; fgets(line,sizeof line,f) ; ++i ) + { + if(i < 3) + continue; + + p = line + strspn(line,SPACE); + + for( j = 0 ; j < 3 ; ++j ) + { + p += strspn(p,DIGIT); + + p += strspn(p,SPACE); + } + + if(!regexec(&re,p,0,0,0)) + { + strcpy(root,"/dev/"); + strcpy(root,p); + p = strchr(root,'\n'); + if(p) + *p = 0; + break; + } + } + + fclose(f); + + regfree(&re); + + if(!*root) + return 0; + + return root; +} + +/** Installs grub to a given target + * @param mode FWGRUB_INSTALL_MBR, FWGRUB_INSTALL_EFI + * @return 0 on succcess, 1 on error + */ + +int fwgrub_install(enum fwgrub_install_mode mode) +{ + char cmd[4096], *mbr; + pid_t pid; + int status; + + /* First, define the common parts of the install command. */ + strcpy(cmd,"grub-install --recheck --no-floppy --boot-directory=/boot "); + + /* Now, define additional arguments based on installation mode. */ + switch(mode) + { + case FWGRUB_INSTALL_MBR: + mbr = guess_mbr_device(); + if(!mbr) + return 1; + strcat(cmd,mbr); + break; + + case FWGRUB_INSTALL_EFI: + strcat(cmd,"--root-directory=/boot/efi --bootloader-id=frugalware"); + if(mkdir("/boot/efi",0755)) + return 1; + break; + } + + /* Setup logging. */ + strcat(cmd,FWGRUB_LOGDEV " 2>&1"); + + pid = fork(); + + if(!pid) + { + execl("/bin/sh","/bin/sh","-c",cmd,(void *) 0); + + _exit(EXIT_FAILURE); + } + else if(id == -1) + return 1; + + if(waitpid(pid,&status,0) == -1 || !WIFEXITED(status) || WEXITSTATUS(status)) + return 1; + + return 0; +} diff --git a/libfwgrubconfig/new.h b/libfwgrubconfig/new.h new file mode 100644 index 0000000..f676d41 --- /dev/null +++ b/libfwgrubconfig/new.h @@ -0,0 +1,29 @@ +/* + * libfwgrubconfig.h for frugalwareutils + * + * Copyright (c) 2006 by Miklos Vajna <vmik...@frugalware.org> + * + * 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; either version 2 of the License, or + * (at your option) any later version. + * + * 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. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, + * USA. + */ + +enum fwgrub_install_mode +{ + FWGRUB_INSTALL_MBR, + FWGRUB_INSTALL_EFI +}; + +int fwgrub_install(enum fwgrub_install_mode mode); +void fwgrub_create_menu(FILE *fp); _______________________________________________ Frugalware-git mailing list Frugalware-git@frugalware.org http://frugalware.org/mailman/listinfo/frugalware-git