|
From: <ls...@us...> - 2007-05-12 10:29:56
|
Revision: 3188
http://jnode.svn.sourceforge.net/jnode/?rev=3188&view=rev
Author: lsantha
Date: 2007-05-12 03:29:55 -0700 (Sat, 12 May 2007)
Log Message:
-----------
First working GRUB installer.
Added Paths:
-----------
trunk/fs/src/fs/org/jnode/fs/jfat/command/GrubJFatFormatter.java
trunk/fs/src/fs/org/jnode/fs/jfat/command/JGrubInstallCommand.java
Added: trunk/fs/src/fs/org/jnode/fs/jfat/command/GrubJFatFormatter.java
===================================================================
--- trunk/fs/src/fs/org/jnode/fs/jfat/command/GrubJFatFormatter.java (rev 0)
+++ trunk/fs/src/fs/org/jnode/fs/jfat/command/GrubJFatFormatter.java 2007-05-12 10:29:55 UTC (rev 3188)
@@ -0,0 +1,107 @@
+/*
+ * $Id: GrubJFatFormatter.java Tanmoy $
+ *
+ * JNode.org
+ * Copyright (C) 2007 JNode.org
+ *
+ * This library is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as published
+ * by the Free Software Foundation; either version 2.1 of the License, or
+ * (at your option) any later version.
+ *
+ * This library 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 Lesser General Public
+ * License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this library; If not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+package org.jnode.fs.jfat.command;
+
+import java.io.*;
+import org.apache.log4j.Logger;
+import org.jnode.fs.FileSystemException;
+
+
+/**
+ * File :GrubFatFormatter.java
+ * <p/>
+ * The very important file for the Grub Installation. Here the
+ * methods for setting the Stage2 to the partition is kept.
+ *
+ * @author Tango Devian
+ */
+public class GrubJFatFormatter {
+ private static final Logger log = Logger.getLogger(GrubJFatFormatter.class);
+ private static final String GRUB_STAGE_2 = "/devices/sg0/boot/grub/grub.s2";
+ private static final String GRUB_MENU_LST = "/devices/sg0/boot/grub/menu.lst";
+
+ /**
+ * @throws org.jnode.fs.FileSystemException
+ *
+ * @throws org.jnode.fs.FileSystemException
+ *
+ * @throws org.jnode.fs.FileSystemException
+ *
+ * @throws java.io.IOException
+ * @throws java.io.IOException
+ * @see org.jnode.fs.fat.FatFormatter#format(org.jnode.driver.block.BlockDeviceAPI)
+ */
+ public void format(String path) throws FileSystemException, IOException {
+ // writting of the stage2 and menu.LST
+ try {
+ File destDir = new File(path + "/boot/grub/");
+ if(!destDir.exists()){
+ System.out.print("Creating directory: " + destDir.getAbsolutePath() + " ... ");
+ destDir.mkdirs();
+ System.out.println("done.");
+ }
+
+ System.out.print("Writing stage 2 ... ");
+ copyFAT(GRUB_STAGE_2, destDir.getAbsolutePath(), "stage2");
+ System.out.println("done.");
+
+ System.out.print("Writing menu.lst ... ");
+ copyFAT(GRUB_MENU_LST, destDir.getAbsolutePath(), "menu.lst");
+ System.out.println("done.");
+
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+
+ private static void copyFile(File srcFile, File destFile) throws IOException {
+
+ InputStream in = new FileInputStream(srcFile);
+ OutputStream out = new FileOutputStream(destFile);
+
+ byte[] buffer = new byte[1024];
+ int bytesRead;
+ while ((bytesRead = in.read(buffer)) >= 0) {
+ out.write(buffer, 0, bytesRead);
+ }
+ out.close();
+ in.close();
+
+ }
+
+ private static void copyFAT(String srcFileCopy, String destFileCopy, String destFileName) throws IOException {
+
+ // make sure the source file is indeed a readable file
+ File srcFile = new File(srcFileCopy);
+ if (!srcFile.isFile() || !srcFile.canRead()) {
+ throw new IllegalArgumentException("Not a readable file: " + srcFile.getName());
+ }
+
+ // make sure the second argument is a directory
+ File destDir = new File(destFileCopy);
+
+ // create File object for destination file
+ File destFile = new File(destDir, destFileName);
+
+ // copy file, optionally creating a checksum
+ copyFile(srcFile, destFile);
+ }
+}
Added: trunk/fs/src/fs/org/jnode/fs/jfat/command/JGrubInstallCommand.java
===================================================================
--- trunk/fs/src/fs/org/jnode/fs/jfat/command/JGrubInstallCommand.java (rev 0)
+++ trunk/fs/src/fs/org/jnode/fs/jfat/command/JGrubInstallCommand.java 2007-05-12 10:29:55 UTC (rev 3188)
@@ -0,0 +1,99 @@
+/*
+ * $Id: FatConstants.java 2224 Tanmoy $
+ *
+ * JNode.org
+ * Copyright (C) 2003-2006 JNode.org
+ *
+ * This library is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as published
+ * by the Free Software Foundation; either version 2.1 of the License, or
+ * (at your option) any later version.
+ *
+ * This library 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 Lesser General Public
+ * License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this library; If not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+
+package org.jnode.fs.jfat.command;
+
+import java.io.*;
+import javax.naming.NameNotFoundException;
+import org.jnode.driver.Device;
+import org.jnode.driver.DeviceManager;
+import org.jnode.driver.DeviceNotFoundException;
+import org.jnode.driver.DriverException;
+import org.jnode.naming.InitialNaming;
+import org.jnode.shell.Command;
+import org.jnode.shell.CommandLine;
+import org.jnode.shell.help.Help;
+import org.jnode.shell.help.Parameter;
+import org.jnode.shell.help.ParsedArguments;
+import org.jnode.shell.help.argument.DeviceArgument;
+import org.jnode.shell.help.argument.FileArgument;
+
+/**
+ * <p/>
+ * The Grub Installer command for the JNODE.
+ * jnode/>grub hdb /devices/hdb0
+ *
+ * @HDA_TARGET /dev/hda0 or /dev/fd0
+ * <p/>
+ * TODO: Adding more options for supporting JGRUB with user specified File
+ * System wise.
+ * Adding more command support for grub insallation.
+ * @author Tango Devian
+ */
+public class JGrubInstallCommand implements Command {
+ static final DeviceArgument ARG_DEVICE = new DeviceArgument("device", "device where grub will be installed");
+ static final FileArgument ARG_DIR = new FileArgument("directory", "the directory for stage2 and menu.lst");
+ static final Help.Info HELP_INFO = new Help.Info("grub", "Install GRUB to the specified location.",
+ new Parameter(ARG_DEVICE, Parameter.MANDATORY),
+ new Parameter(ARG_DIR, Parameter.MANDATORY));
+
+ /**
+ * @param args
+ * @throws Exception
+ */
+ public static void main(String[] args) throws Exception {
+ new JGrubInstallCommand().execute(new CommandLine(args), System.in, System.out, System.err);
+ }
+
+ /**
+ *
+ */
+ public void execute(CommandLine commandLine, InputStream in, PrintStream out, PrintStream err) throws Exception {
+ try {
+ ParsedArguments cmdLine = HELP_INFO.parse(commandLine.toStringArray());
+ String device = ARG_DEVICE.getValue(cmdLine);
+ File destDir = ARG_DIR.getFile(cmdLine);
+ DeviceManager dm = InitialNaming.lookup(DeviceManager.NAME);
+ Device dev = dm.getDevice(device);
+ String destDirName = destDir.getAbsolutePath();
+ out.println("Installing GRUB to: " + device + ", " + destDirName);
+ try {
+ new MBRFormatter().format(dev);
+ new GrubJFatFormatter().format(destDirName);
+ out.println("Restarting device: " + device);
+ dm.stop(dev);
+ dm.start(dev);
+ out.println("GRUB has been successflly installed to " + device + ".");
+ } catch (FileNotFoundException e) {
+ err.println("File not found: " + e.getMessage());
+ } catch (IOException e) {
+ err.println("I/O exception: " + e.getMessage());
+ }
+ } catch (NameNotFoundException e) {
+ err.println("Name not found: " + e.getMessage());
+ } catch (DeviceNotFoundException e) {
+ err.println("Device not found: " + e.getMessage());
+ } catch (DriverException e) {
+ err.println("The DriverException Occuered ..." + e.getMessage());
+ }
+ }
+}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|