debuggers.hg
changeset 597:2fa0baf66747
bitkeeper revision 1.304.1.5 (3f0bdfabhNAbf77xMA6z5OyrFKHExA)
Add VBD create command
Fix parser so that it should hopefully now always print usage instead of throwing unamusing exceptions
Add VBD create command
Fix parser so that it should hopefully now always print usage instead of throwing unamusing exceptions
author | rac61@labyrinth.cl.cam.ac.uk |
---|---|
date | Wed Jul 09 09:26:03 2003 +0000 (2003-07-09) |
parents | 4fb0fd9f744b |
children | 05473a4cb01e |
files | .rootkeys tools/control/src/org/xenoserver/cmdline/Main.java tools/control/src/org/xenoserver/cmdline/ParseGroup.java tools/control/src/org/xenoserver/cmdline/ParseVbdCreate.java tools/control/src/org/xenoserver/control/CommandVbdCreate.java tools/control/src/org/xenoserver/control/CommandVbdCreatePhysical.java |
line diff
1.1 --- a/.rootkeys Wed Jul 09 09:20:54 2003 +0000 1.2 +++ b/.rootkeys Wed Jul 09 09:26:03 2003 +0000 1.3 @@ -30,6 +30,7 @@ 3f05631djnPcaqmzMFSa8RLmGCZ-0Q tools/con 1.4 3f05631dswxJX_TpcuG6tBstyHSetg tools/control/src/org/xenoserver/cmdline/ParsePhysicalGrant.java 1.5 3f05631dMY7PMkwSY7zBFelGJ8goVg tools/control/src/org/xenoserver/cmdline/ParsePhysicalList.java 1.6 3f05631dYDFXv6mwNFAgz3ta9kShJA tools/control/src/org/xenoserver/cmdline/ParsePhysicalRevoke.java 1.7 +3f0bdfabfXM4tMbvmV06di5U-5FfqA tools/control/src/org/xenoserver/cmdline/ParseVbdCreate.java 1.8 3f098761TRsbDk9woUM846Q6_F7EmA tools/control/src/org/xenoserver/cmdline/ParseVdCreate.java 1.9 3f099009pmH9MFkRYwP2V1DfsqEwdg tools/control/src/org/xenoserver/cmdline/ParseVdDelete.java 1.10 3f098761zh9WTV6LpRqcet3gqlXdtg tools/control/src/org/xenoserver/cmdline/ParseVdFree.java 1.11 @@ -46,6 +47,8 @@ 3f0987611uZwg-o64yi0p_2aXCYEug tools/con 1.12 3f05631ev3UK5FRi5vgR08zDp3OZYw tools/control/src/org/xenoserver/control/CommandPhysicalGrant.java 1.13 3f05631e_G6wzHhEnpihX0pIkEsbMw tools/control/src/org/xenoserver/control/CommandPhysicalList.java 1.14 3f05631eGWxq7bojQbMa-tGxsENIhw tools/control/src/org/xenoserver/control/CommandPhysicalRevoke.java 1.15 +3f0bdfab88VYiD26FXCDmmAAGJ8zWA tools/control/src/org/xenoserver/control/CommandVbdCreate.java 1.16 +3f0bdfabI14M5_odjCIwQbXCdauReA tools/control/src/org/xenoserver/control/CommandVbdCreatePhysical.java 1.17 3f098761c5-idlmf9vWEMOlDw0VCHg tools/control/src/org/xenoserver/control/CommandVdCreate.java 1.18 3f0990096KcyQw77qJmjTu941smS8A tools/control/src/org/xenoserver/control/CommandVdDelete.java 1.19 3f0990093VJUL7QjxGigR5GPXf_Fkw tools/control/src/org/xenoserver/control/CommandVdRefresh.java
2.1 --- a/tools/control/src/org/xenoserver/cmdline/Main.java Wed Jul 09 09:20:54 2003 +0000 2.2 +++ b/tools/control/src/org/xenoserver/cmdline/Main.java Wed Jul 09 09:26:03 2003 +0000 2.3 @@ -1,58 +1,70 @@ 2.4 package org.xenoserver.cmdline; 2.5 2.6 import java.util.LinkedList; 2.7 +import java.util.NoSuchElementException; 2.8 2.9 import org.xenoserver.control.CommandFailedException; 2.10 import org.xenoserver.control.Defaults; 2.11 2.12 +/** 2.13 + * Main class for the command-line xenctl interface. 2.14 + */ 2.15 public class Main { 2.16 static final ParseHelp help = new ParseHelp(); 2.17 - static final CommandParser domaincommands[] = 2.18 + private static final CommandParser domaincommands[] = 2.19 { new ParseDomainNew(), 2.20 new ParseDomainStart(), 2.21 new ParseDomainStop(), 2.22 new ParseDomainDestroy(), 2.23 new ParseDomainList() 2.24 }; 2.25 - static final CommandParser partitioncommands[] = 2.26 + private static final CommandParser partitioncommands[] = 2.27 { new ParsePartitionsAdd(), 2.28 new ParsePartitionsList() 2.29 }; 2.30 - static final CommandParser physicalcommands[] = 2.31 + private static final CommandParser physicalcommands[] = 2.32 { new ParsePhysicalGrant(), 2.33 new ParsePhysicalRevoke(), 2.34 new ParsePhysicalList() 2.35 }; 2.36 - static final CommandParser vdcommands[] = 2.37 + private static final CommandParser vdcommands[] = 2.38 { new ParseVdCreate(), 2.39 new ParseVdDelete(), 2.40 new ParseVdRefresh(), 2.41 new ParseVdShow(), 2.42 new ParseVdFree() 2.43 }; 2.44 - static final CommandParser commands[] = 2.45 + private static final CommandParser vbdcommands[] = 2.46 + { new ParseVbdCreate() 2.47 + }; 2.48 + private static final CommandParser commands[] = 2.49 { help, 2.50 new ParseGroup( "domain", domaincommands ), 2.51 new ParseGroup( "partitions", partitioncommands ), 2.52 new ParseGroup( "physical", physicalcommands ), 2.53 - new ParseGroup( "vd", vdcommands ) 2.54 + new ParseGroup( "vd", vdcommands ), 2.55 + new ParseGroup( "vbd", vbdcommands ) 2.56 }; 2.57 + /** The top-level parser. */ 2.58 static final CommandParser parser = new ParseGroup( null, commands ); 2.59 2.60 public static void main(String[] args) { 2.61 Defaults d = new Defaults(); 2.62 int ec = -1; 2.63 LinkedList arglist = new LinkedList(); 2.64 - for ( int i=0; i<args.length; i++ ) 2.65 + for ( int i=0; i<args.length; i++ ) { 2.66 arglist.add( args[i] ); 2.67 + } 2.68 2.69 if (args.length == 0) { 2.70 - help.parse(d, arglist); 2.71 + help.parse(null, null); 2.72 } else { 2.73 try 2.74 { 2.75 parser.parse(d, arglist); 2.76 ec = 0; 2.77 + } catch (NoSuchElementException e) { 2.78 + help.parse(null, null); 2.79 } catch (ParseFailedException e) { 2.80 System.err.println( e.getMessage() ); 2.81 } catch (CommandFailedException e) {
3.1 --- a/tools/control/src/org/xenoserver/cmdline/ParseGroup.java Wed Jul 09 09:20:54 2003 +0000 3.2 +++ b/tools/control/src/org/xenoserver/cmdline/ParseGroup.java Wed Jul 09 09:26:03 2003 +0000 3.3 @@ -5,69 +5,90 @@ import java.util.LinkedList; 3.4 import org.xenoserver.control.CommandFailedException; 3.5 import org.xenoserver.control.Defaults; 3.6 3.7 +/** 3.8 + * Parses a group of commands; taking the first argument, it searches its 3.9 + * array of commands until it finds a match, and then, removing the matched 3.10 + * argument from the command line, invokes it. This allows hierarchical 3.11 + * parsing. 3.12 + */ 3.13 public class ParseGroup extends CommandParser { 3.14 - private final String name; 3.15 - private final CommandParser[] commands; 3.16 - 3.17 - /** 3.18 - * Constructor for ParseGroup. 3.19 - * @param name Name of this group of commands 3.20 - * @param commands Array of commands to include 3.21 - */ 3.22 - public ParseGroup(String name, CommandParser[] commands) { 3.23 - this.name = name; 3.24 - this.commands = commands; 3.25 - } 3.26 + /** Name of this group, i.e. the prefix to the command line */ 3.27 + private final String name; 3.28 + /** The commands this group will attempt to match its arguments against. */ 3.29 + private final CommandParser[] commands; 3.30 3.31 - public void parse(Defaults d, LinkedList args) throws ParseFailedException, CommandFailedException { 3.32 - int i; 3.33 - String c = (String) args.removeFirst(); 3.34 - for (i = 0; i < commands.length; i++) { 3.35 - if (commands[i].getName().equals(c)) { 3.36 - if (!args.isEmpty() && args.getFirst().equals("-?")) { 3.37 - commands[i].printHelpText(null); 3.38 - } else { 3.39 - commands[i].parse(d, args); 3.40 - } 3.41 - break; 3.42 - } 3.43 + /** 3.44 + * Constructor for ParseGroup. 3.45 + * @param name Name of this group of commands 3.46 + * @param commands Array of commands to include 3.47 + */ 3.48 + public ParseGroup(String name, CommandParser[] commands) { 3.49 + this.name = name; 3.50 + this.commands = commands; 3.51 } 3.52 - if (i == commands.length) { 3.53 - throw new ParseFailedException("Unknown command " + c); 3.54 - } 3.55 - } 3.56 3.57 - public String getName() { 3.58 - return name; 3.59 - } 3.60 + public void parse(Defaults d, LinkedList args) 3.61 + throws ParseFailedException, CommandFailedException { 3.62 + if (args.isEmpty()) { 3.63 + Main.help.parse(null,null); 3.64 + return; 3.65 + } 3.66 + 3.67 + int i; 3.68 + String c = (String) args.removeFirst(); 3.69 + for (i = 0; i < commands.length; i++) { 3.70 + if (commands[i].getName().equals(c)) { 3.71 + if (!args.isEmpty() && args.getFirst().equals("-?")) { 3.72 + commands[i].printHelpText(null); 3.73 + } else { 3.74 + commands[i].parse(d, args); 3.75 + } 3.76 + break; 3.77 + } 3.78 + } 3.79 + if (i == commands.length) { 3.80 + throw new ParseFailedException("Unknown command " + c); 3.81 + } 3.82 + } 3.83 3.84 - public String getUsage() { 3.85 - return null; 3.86 - } 3.87 + public String getName() { 3.88 + return name; 3.89 + } 3.90 3.91 - public String getHelpText() { 3.92 - return null; 3.93 - } 3.94 + public String getUsage() { 3.95 + return null; 3.96 + } 3.97 3.98 - public void printUsage(String prefix) { 3.99 - if ( prefix == null ) 3.100 - prefix = name; 3.101 - else 3.102 - prefix += " " + name; 3.103 - for ( int i=0; i<commands.length; i++ ) 3.104 - commands[i].printUsage(prefix); 3.105 - } 3.106 + public String getHelpText() { 3.107 + return null; 3.108 + } 3.109 + 3.110 + public void printUsage(String prefix) { 3.111 + if (prefix == null) { 3.112 + prefix = name; 3.113 + } else { 3.114 + prefix += " " + name; 3.115 + } 3.116 + for (int i = 0; i < commands.length; i++) { 3.117 + commands[i].printUsage(prefix); 3.118 + } 3.119 + } 3.120 3.121 - public void printHelpText(LinkedList args) { 3.122 - if ( name != null ) 3.123 - System.out.print( name + " " ); 3.124 - int i; 3.125 - String c = (String) args.removeFirst(); 3.126 - for (i = 0; i < commands.length; i++) { 3.127 - if (commands[i].getName().equals(c)) { 3.128 - commands[i].printHelpText(args); 3.129 - break; 3.130 - } 3.131 + public void printHelpText(LinkedList args) { 3.132 + if (args == null) { 3.133 + Main.help.parse(null,null); 3.134 + return; 3.135 + } 3.136 + if (name != null) { 3.137 + System.out.print(name + " "); 3.138 + } 3.139 + int i; 3.140 + String c = (String) args.removeFirst(); 3.141 + for (i = 0; i < commands.length; i++) { 3.142 + if (commands[i].getName().equals(c)) { 3.143 + commands[i].printHelpText(args); 3.144 + break; 3.145 + } 3.146 + } 3.147 } 3.148 - } 3.149 }
4.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 4.2 +++ b/tools/control/src/org/xenoserver/cmdline/ParseVbdCreate.java Wed Jul 09 09:26:03 2003 +0000 4.3 @@ -0,0 +1,80 @@ 4.4 +package org.xenoserver.cmdline; 4.5 + 4.6 +import java.util.LinkedList; 4.7 + 4.8 +import org.xenoserver.control.CommandFailedException; 4.9 +import org.xenoserver.control.CommandVbdCreate; 4.10 +import org.xenoserver.control.CommandVbdCreatePhysical; 4.11 +import org.xenoserver.control.Defaults; 4.12 +import org.xenoserver.control.Mode; 4.13 +import org.xenoserver.control.Partition; 4.14 +import org.xenoserver.control.PartitionManager; 4.15 +import org.xenoserver.control.VirtualDisk; 4.16 +import org.xenoserver.control.VirtualDiskManager; 4.17 + 4.18 +public class ParseVbdCreate extends CommandParser { 4.19 + public void parse(Defaults d, LinkedList args) 4.20 + throws ParseFailedException, CommandFailedException { 4.21 + String vd_key = getStringParameter(args, 'k', ""); 4.22 + String partition_name = getStringParameter(args, 'p', ""); 4.23 + int domain_id = getIntParameter(args, 'n', 0); 4.24 + int vbd_num = getIntParameter(args, 'v', -1); 4.25 + boolean write = getFlagParameter(args, 'w'); 4.26 + 4.27 + if (vd_key.equals("") && partition_name.equals("")) { 4.28 + throw new ParseFailedException("Expected -k<key> or -p<partition>"); 4.29 + } 4.30 + if (domain_id == 0) { 4.31 + throw new ParseFailedException("Expected -n<domain_id>"); 4.32 + } 4.33 + if (vbd_num == -1) { 4.34 + throw new ParseFailedException("Expected -v<vbd_num>"); 4.35 + } 4.36 + 4.37 + Mode mode; 4.38 + if (write) { 4.39 + mode = Mode.READ_WRITE; 4.40 + } else { 4.41 + mode = Mode.READ_ONLY; 4.42 + } 4.43 + 4.44 + loadState(); 4.45 + String output; 4.46 + if (vd_key.equals("")) { 4.47 + Partition p = PartitionManager.IT.getPartition(partition_name); 4.48 + if ( p == null ) { 4.49 + throw new CommandFailedException("No partition " + partition_name + " exists" ); 4.50 + } 4.51 + 4.52 + output = new CommandVbdCreatePhysical( p, domain_id, vbd_num, mode ).execute(); 4.53 + } else { 4.54 + VirtualDisk vd = VirtualDiskManager.IT.getVirtualDisk(vd_key); 4.55 + if (vd == null) { 4.56 + throw new CommandFailedException( 4.57 + "No virtual disk with key " + vd_key); 4.58 + } 4.59 + 4.60 + output = 4.61 + new CommandVbdCreate(vd, domain_id, vbd_num, mode).execute(); 4.62 + } 4.63 + if (output != null) { 4.64 + System.out.println(output); 4.65 + } 4.66 + saveState(); 4.67 + } 4.68 + 4.69 + public String getName() { 4.70 + return "create"; 4.71 + } 4.72 + 4.73 + public String getUsage() { 4.74 + return "-n<domain_id> {-k<key>|-p<partition} -v<vbd_num> [-w]"; 4.75 + } 4.76 + 4.77 + public String getHelpText() { 4.78 + return "Create a new virtual block device binding the virtual disk with\n" 4.79 + + "the specified key to the domain and VBD number given. Add -w to\n" 4.80 + + "allow read-write access."; 4.81 + } 4.82 + 4.83 +}
5.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 5.2 +++ b/tools/control/src/org/xenoserver/control/CommandVbdCreate.java Wed Jul 09 09:26:03 2003 +0000 5.3 @@ -0,0 +1,65 @@ 5.4 +package org.xenoserver.control; 5.5 + 5.6 +import java.io.FileWriter; 5.7 +import java.io.IOException; 5.8 + 5.9 +/** 5.10 + * Create a virtual block device. 5.11 + */ 5.12 +public class CommandVbdCreate extends Command { 5.13 + /** Virtual disk to map to. */ 5.14 + private VirtualDisk vd; 5.15 + /** Domain to create VBD for. */ 5.16 + private int domain_id; 5.17 + /** VBD number to use. */ 5.18 + private int vbd_num; 5.19 + /** Access mode to grant. */ 5.20 + private Mode mode; 5.21 + 5.22 + /** 5.23 + * Constructor for CommandVbdCreate. 5.24 + * @param vd VirtualDisk to map to. 5.25 + * @param domain_id Domain to map for. 5.26 + * @param vbd_num VBD number within domain. 5.27 + * @param mode Access mode to grant. 5.28 + */ 5.29 + public CommandVbdCreate( 5.30 + VirtualDisk vd, 5.31 + int domain_id, 5.32 + int vbd_num, 5.33 + Mode mode) { 5.34 + this.vd = vd; 5.35 + this.domain_id = domain_id; 5.36 + this.vbd_num = vbd_num; 5.37 + this.mode = mode; 5.38 + } 5.39 + 5.40 + /** 5.41 + * @see org.xenoserver.control.Command#execute() 5.42 + */ 5.43 + public String execute() throws CommandFailedException { 5.44 + VirtualBlockDevice vbd; 5.45 + 5.46 + vbd = 5.47 + VirtualDiskManager.IT.createVirtualBlockDevice( 5.48 + vd, 5.49 + domain_id, 5.50 + vbd_num, 5.51 + mode); 5.52 + String command = vd.dumpForXen(vbd); 5.53 + 5.54 + try { 5.55 + FileWriter fw = new FileWriter("/proc/xeno/dom0/vhd"); 5.56 + fw.write(command); 5.57 + fw.flush(); 5.58 + fw.close(); 5.59 + } catch (IOException e) { 5.60 + throw new CommandFailedException("Could not write VBD details to /proc/xeno/dom0/vhd", e); 5.61 + } 5.62 + 5.63 + return "Created virtual block device " 5.64 + + vbd_num 5.65 + + " for domain " 5.66 + + domain_id; 5.67 + } 5.68 +}
6.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 6.2 +++ b/tools/control/src/org/xenoserver/control/CommandVbdCreatePhysical.java Wed Jul 09 09:26:03 2003 +0000 6.3 @@ -0,0 +1,66 @@ 6.4 +package org.xenoserver.control; 6.5 + 6.6 +import java.io.FileWriter; 6.7 +import java.io.IOException; 6.8 + 6.9 +/** 6.10 + * Create a virtual block device. 6.11 + */ 6.12 +public class CommandVbdCreatePhysical extends Command { 6.13 + /** Virtual disk to map to. */ 6.14 + private Partition partition; 6.15 + /** Domain to create VBD for. */ 6.16 + private int domain_id; 6.17 + /** VBD number to use. */ 6.18 + private int vbd_num; 6.19 + /** Access mode to grant. */ 6.20 + private Mode mode; 6.21 + 6.22 + /** 6.23 + * Constructor for CommandVbdCreate. 6.24 + * @param partition Partition to map to. 6.25 + * @param domain_id Domain to map for. 6.26 + * @param vbd_num VBD number within domain. 6.27 + * @param mode Access mode to grant. 6.28 + */ 6.29 + public CommandVbdCreatePhysical( 6.30 + Partition partition, 6.31 + int domain_id, 6.32 + int vbd_num, 6.33 + Mode mode) { 6.34 + this.partition = partition; 6.35 + this.domain_id = domain_id; 6.36 + this.vbd_num = vbd_num; 6.37 + this.mode = mode; 6.38 + } 6.39 + 6.40 + /** 6.41 + * @see org.xenoserver.control.Command#execute() 6.42 + */ 6.43 + public String execute() throws CommandFailedException { 6.44 + VirtualDisk vd = new VirtualDisk("vbd:"+partition.getName()); 6.45 + vd.addPartition(partition,partition.getNumSects()); 6.46 + 6.47 + VirtualBlockDevice vbd = new VirtualBlockDevice( 6.48 + vd, 6.49 + domain_id, 6.50 + vbd_num, 6.51 + mode); 6.52 + 6.53 + String command = vd.dumpForXen(vbd); 6.54 + 6.55 + try { 6.56 + FileWriter fw = new FileWriter("/proc/xeno/dom0/vhd"); 6.57 + fw.write(command); 6.58 + fw.flush(); 6.59 + fw.close(); 6.60 + } catch (IOException e) { 6.61 + throw new CommandFailedException("Could not write VBD details to /proc/xeno/dom0/vhd", e); 6.62 + } 6.63 + 6.64 + return "Created virtual block device " 6.65 + + vbd_num 6.66 + + " for domain " 6.67 + + domain_id; 6.68 + } 6.69 +}