ia64/xen-unstable

changeset 535:75772169ac6c

bitkeeper revision 1.296 (3f0aab4cKX_VINngzj4KEwSKL_NmtA)

Merge labyrinth.cl.cam.ac.uk:/auto/groups/xeno/users/rac61/xeno.bk
into labyrinth.cl.cam.ac.uk:/auto/groups/xeno/users/sos22/xeno.bk
author sos22@labyrinth.cl.cam.ac.uk
date Tue Jul 08 11:30:20 2003 +0000 (2003-07-08)
parents 49232d5003e6 2251957dbcf3
children 00a8c9490645
files BitKeeper/etc/ignore tools/control/src/org/xenoserver/cmdline/CommandParser.java tools/control/src/org/xenoserver/cmdline/ParsePartitionsAdd.java tools/control/src/org/xenoserver/cmdline/ParsePartitionsList.java tools/control/src/org/xenoserver/cmdline/ParsePhysicalGrant.java tools/control/src/org/xenoserver/cmdline/ParsePhysicalList.java tools/control/src/org/xenoserver/cmdline/ParsePhysicalRevoke.java tools/control/src/org/xenoserver/cmdline/ParseVdCreate.java tools/control/src/org/xenoserver/cmdline/ParseVdDelete.java tools/control/src/org/xenoserver/cmdline/ParseVdFree.java tools/control/src/org/xenoserver/cmdline/ParseVdShow.java tools/control/src/org/xenoserver/control/CommandPartitionAdd.java tools/control/src/org/xenoserver/control/CommandPhysicalList.java tools/control/src/org/xenoserver/control/CommandVdCreate.java tools/control/src/org/xenoserver/control/CommandVdDelete.java tools/control/src/org/xenoserver/control/CommandVdRefresh.java tools/control/src/org/xenoserver/control/Extent.java tools/control/src/org/xenoserver/control/InetAddressPattern.java tools/control/src/org/xenoserver/control/Library.java tools/control/src/org/xenoserver/control/Mode.java tools/control/src/org/xenoserver/control/Partition.java tools/control/src/org/xenoserver/control/PartitionManager.java tools/control/src/org/xenoserver/control/Settings.java tools/control/src/org/xenoserver/control/StringPattern.java tools/control/src/org/xenoserver/control/VirtualBlockDevice.java tools/control/src/org/xenoserver/control/VirtualDisk.java tools/control/src/org/xenoserver/control/VirtualDiskManager.java tools/control/src/org/xenoserver/control/XML.java tools/control/src/org/xenoserver/control/XMLHelper.java
line diff
     1.1 --- a/BitKeeper/etc/ignore	Tue Jul 08 11:23:42 2003 +0000
     1.2 +++ b/BitKeeper/etc/ignore	Tue Jul 08 11:30:20 2003 +0000
     1.3 @@ -427,3 +427,4 @@ xenolinux-2.4.16-sparse/arch/xeno/driver
     1.4  xenolinux-2.4.16-sparse/arch/xeno/drivers/block/block.c.orig
     1.5  xenolinux-2.4.16-sparse/scripts/kconfig.tk
     1.6  xenolinux-2.4.21-sparse/include/linux/blk.h~
     1.7 +tools/control/.checkstyle
     2.1 --- a/tools/control/src/org/xenoserver/cmdline/CommandParser.java	Tue Jul 08 11:23:42 2003 +0000
     2.2 +++ b/tools/control/src/org/xenoserver/cmdline/CommandParser.java	Tue Jul 08 11:30:20 2003 +0000
     2.3 @@ -89,10 +89,10 @@ public abstract class CommandParser {
     2.4    }
     2.5  
     2.6    protected void loadState() {
     2.7 -    XML.load_state( PartitionManager.it, VirtualDiskManager.it, Settings.STATE_INPUT_FILE );
     2.8 +    XML.loadState( PartitionManager.IT, VirtualDiskManager.IT, Settings.STATE_INPUT_FILE );
     2.9    }
    2.10    
    2.11    protected void saveState() {
    2.12 -    XML.dump_state( PartitionManager.it, VirtualDiskManager.it, Settings.STATE_OUTPUT_FILE );
    2.13 +    XML.saveState( PartitionManager.IT, VirtualDiskManager.IT, Settings.STATE_OUTPUT_FILE );
    2.14    }
    2.15  }
     3.1 --- a/tools/control/src/org/xenoserver/cmdline/ParsePartitionsAdd.java	Tue Jul 08 11:23:42 2003 +0000
     3.2 +++ b/tools/control/src/org/xenoserver/cmdline/ParsePartitionsAdd.java	Tue Jul 08 11:30:20 2003 +0000
     3.3 @@ -19,19 +19,19 @@ public class ParsePartitionsAdd extends 
     3.4      if (partition_name.equals(""))
     3.5        throw new ParseFailedException("Expected -p<partition_name>");
     3.6        
     3.7 -    long chunksize = Library.parse_size( size ) / Settings.SECTOR_SIZE;
     3.8 +    long chunksize = Library.parseSize( size ) / Settings.SECTOR_SIZE;
     3.9      if ( chunksize <= 0 )
    3.10        throw new CommandFailedException("Chunk size " + size + " is smaller than sector size.");
    3.11      
    3.12      // Initialise the partition manager and look up the partition
    3.13      loadState();
    3.14 -    Partition p = PartitionManager.it.get_partition(partition_name);
    3.15 +    Partition p = PartitionManager.IT.getPartition(partition_name);
    3.16      
    3.17      if ( p == null )
    3.18        throw new CommandFailedException("Partition " + partition_name + " does not exist.");
    3.19      
    3.20      // Check if this partition belongs to the VDM
    3.21 -    if (p.getIsXeno() && !force)
    3.22 +    if (p.isXeno() && !force)
    3.23        throw new CommandFailedException("Refusing to add partition as it is already allocated to the virtual disk manager. Use -f if you are sure.");
    3.24      
    3.25      String output = new CommandPartitionAdd( p, chunksize ).execute();
     4.1 --- a/tools/control/src/org/xenoserver/cmdline/ParsePartitionsList.java	Tue Jul 08 11:23:42 2003 +0000
     4.2 +++ b/tools/control/src/org/xenoserver/cmdline/ParsePartitionsList.java	Tue Jul 08 11:30:20 2003 +0000
     4.3 @@ -12,26 +12,26 @@ import org.xenoserver.control.PartitionM
     4.4  public class ParsePartitionsList extends CommandParser {
     4.5    public void parse(Defaults d, LinkedList args) throws ParseFailedException, CommandFailedException {
     4.6      loadState();
     4.7 -    Iterator i = PartitionManager.it.iterator();
     4.8 +    Iterator i = PartitionManager.IT.iterator();
     4.9      int idx = 1;
    4.10      System.out.println( "     maj:min " + "    blocks " + "start sect " +
    4.11                          " num sects " + "name" );
    4.12      while (i.hasNext()) {
    4.13        Partition p = (Partition) i.next();
    4.14  
    4.15 -      if (p.getIsXeno()) {
    4.16 +      if (p.isXeno()) {
    4.17          System.out.print("[ ");
    4.18        } else {
    4.19          System.out.print("  ");
    4.20        }
    4.21 -      System.out.print(Library.format(idx++, 2, 0) + " ");
    4.22 -      System.out.print(Library.format(p.getMajor(),3,0) + ":" + 
    4.23 -          Library.format(p.getMinor(),3,1) + " " +
    4.24 -          Library.format(p.getBlocks(),10,0) + " " +
    4.25 -          Library.format(p.getStartSect(),10,0) + " " +
    4.26 -          Library.format(p.getNumSects(),10,0) + " " +
    4.27 -          Library.format(p.getName(),7,1));   
    4.28 -      if (p.getIsXeno()) {
    4.29 +      System.out.print(Library.format(idx++, 2, false) + " ");
    4.30 +      System.out.print(Library.format(p.getMajor(),3,false) + ":" + 
    4.31 +          Library.format(p.getMinor(),3,true) + " " +
    4.32 +          Library.format(p.getBlocks(),10,false) + " " +
    4.33 +          Library.format(p.getStartSect(),10,false) + " " +
    4.34 +          Library.format(p.getNumSects(),10,false) + " " +
    4.35 +          Library.format(p.getName(),7,true));   
    4.36 +      if (p.isXeno()) {
    4.37          System.out.println("]");
    4.38        } else {
    4.39          System.out.println();
     5.1 --- a/tools/control/src/org/xenoserver/cmdline/ParsePhysicalGrant.java	Tue Jul 08 11:23:42 2003 +0000
     5.2 +++ b/tools/control/src/org/xenoserver/cmdline/ParsePhysicalGrant.java	Tue Jul 08 11:30:20 2003 +0000
     5.3 @@ -30,13 +30,13 @@ public class ParsePhysicalGrant extends 
     5.4        
     5.5      // Initialise the partition manager and look up the partition
     5.6      loadState();
     5.7 -    Partition p = PartitionManager.it.get_partition(partition_name);
     5.8 +    Partition p = PartitionManager.IT.getPartition(partition_name);
     5.9      
    5.10      if ( p == null )
    5.11        throw new CommandFailedException("Partition " + partition_name + " does not exist.");
    5.12      
    5.13      // Check if this partition belongs to the VDM
    5.14 -    if (p.getIsXeno() && !force)
    5.15 +    if (p.isXeno() && !force)
    5.16        throw new CommandFailedException("Refusing to grant physical access as the given partition is allocated to the virtual disk manager. Use -f if you are sure.");
    5.17       
    5.18      // Convert the partition into a physical extent
     6.1 --- a/tools/control/src/org/xenoserver/cmdline/ParsePhysicalList.java	Tue Jul 08 11:23:42 2003 +0000
     6.2 +++ b/tools/control/src/org/xenoserver/cmdline/ParsePhysicalList.java	Tue Jul 08 11:30:20 2003 +0000
     6.3 @@ -35,23 +35,23 @@ public class ParsePhysicalList extends C
     6.4        Entry entry = (Entry) i.next(); 
     6.5        Extent e = (Extent) entry.getKey();
     6.6        String mode = entry.getValue().toString();
     6.7 -      Partition p = PartitionManager.it.get_partition( e );
     6.8 +      Partition p = PartitionManager.IT.getPartition( e );
     6.9        if ( p != null ) {
    6.10 -        System.out.println(Library.format(p.getMajor(),3,0) + ":" + 
    6.11 -          Library.format(p.getMinor(),3,1) + " " +
    6.12 -          Library.format(p.getBlocks(),10,0) + " " +
    6.13 -          Library.format(p.getStartSect(),10,0) + " " +
    6.14 -          Library.format(p.getNumSects(),10,0) + " " +
    6.15 -          Library.format(p.getName(),7,1) + " " +
    6.16 -          Library.format(mode,2,1));   
    6.17 +        System.out.println(Library.format(p.getMajor(),3,false) + ":" + 
    6.18 +          Library.format(p.getMinor(),3,true) + " " +
    6.19 +          Library.format(p.getBlocks(),10,false) + " " +
    6.20 +          Library.format(p.getStartSect(),10,false) + " " +
    6.21 +          Library.format(p.getNumSects(),10,false) + " " +
    6.22 +          Library.format(p.getName(),7,true) + " " +
    6.23 +          Library.format(mode,2,true));   
    6.24        } else {
    6.25 -        System.out.println(Library.format(e.getMajor(),3,0) + ":" +
    6.26 -          Library.format(e.getMinor(),3,1) + " " +
    6.27 +        System.out.println(Library.format(e.getMajor(),3,false) + ":" +
    6.28 +          Library.format(e.getMinor(),3,true) + " " +
    6.29            "          " + " " +
    6.30 -          Library.format(e.getOffset(),10,0) + " " +
    6.31 -          Library.format(e.getSize(),10,0) + " " +
    6.32 +          Library.format(e.getOffset(),10,false) + " " +
    6.33 +          Library.format(e.getSize(),10,false) + " " +
    6.34            "       " + " " +
    6.35 -          Library.format(mode,2,1));
    6.36 +          Library.format(mode,2,true));
    6.37        }
    6.38      }
    6.39    }
     7.1 --- a/tools/control/src/org/xenoserver/cmdline/ParsePhysicalRevoke.java	Tue Jul 08 11:23:42 2003 +0000
     7.2 +++ b/tools/control/src/org/xenoserver/cmdline/ParsePhysicalRevoke.java	Tue Jul 08 11:30:20 2003 +0000
     7.3 @@ -21,7 +21,7 @@ public class ParsePhysicalRevoke extends
     7.4        
     7.5      // Initialise the partition manager and look up the partition
     7.6      loadState();
     7.7 -    Partition p = PartitionManager.it.get_partition(partition_name);
     7.8 +    Partition p = PartitionManager.IT.getPartition(partition_name);
     7.9      
    7.10      if ( p == null )
    7.11        throw new CommandFailedException("Partition " + partition_name + " does not exist.");
     8.1 --- a/tools/control/src/org/xenoserver/cmdline/ParseVdCreate.java	Tue Jul 08 11:23:42 2003 +0000
     8.2 +++ b/tools/control/src/org/xenoserver/cmdline/ParseVdCreate.java	Tue Jul 08 11:30:20 2003 +0000
     8.3 @@ -25,7 +25,7 @@ public class ParseVdCreate extends Comma
     8.4      else
     8.5        expiry = new Date(Date.parse(expiry_s));
     8.6        
     8.7 -    long size = Library.parse_size(size_s);
     8.8 +    long size = Library.parseSize(size_s);
     8.9      
    8.10      loadState();
    8.11      String output = new CommandVdCreate(name,size/Settings.SECTOR_SIZE,expiry).execute();
     9.1 --- a/tools/control/src/org/xenoserver/cmdline/ParseVdDelete.java	Tue Jul 08 11:23:42 2003 +0000
     9.2 +++ b/tools/control/src/org/xenoserver/cmdline/ParseVdDelete.java	Tue Jul 08 11:30:20 2003 +0000
     9.3 @@ -15,7 +15,7 @@ public class ParseVdDelete extends Comma
     9.4        throw new ParseFailedException("Expected -k<key>");
     9.5      
     9.6      loadState();
     9.7 -    if ( VirtualDiskManager.it.get_virtual_disk_key(vd_key) == null )
     9.8 +    if ( VirtualDiskManager.IT.getVirtualDisk(vd_key) == null )
     9.9        throw new CommandFailedException("Virtual disk " + vd_key + " does not exist");
    9.10        
    9.11      String output = new CommandVdDelete(vd_key).execute();
    10.1 --- a/tools/control/src/org/xenoserver/cmdline/ParseVdFree.java	Tue Jul 08 11:23:42 2003 +0000
    10.2 +++ b/tools/control/src/org/xenoserver/cmdline/ParseVdFree.java	Tue Jul 08 11:30:20 2003 +0000
    10.3 @@ -16,17 +16,17 @@ public class ParseVdFree extends Command
    10.4      boolean verbose = getFlagParameter(args, 'v');
    10.5      
    10.6      loadState();
    10.7 -    VirtualDisk free = VirtualDiskManager.it.getFreeVirtualDisk();
    10.8 +    VirtualDisk free = VirtualDiskManager.IT.getFreeDisk();
    10.9      System.out.println( "Free disk has " + free.getExtentCount() + " extents totalling "
   10.10 -                      + Library.format_size(free.getSize()*Settings.SECTOR_SIZE,8,1) );
   10.11 +                      + Library.formatSize(free.getSize()*Settings.SECTOR_SIZE,8,true) );
   10.12      if ( verbose ) {
   10.13 -      Iterator i = free.iterator();
   10.14 +      Iterator i = free.extents();
   10.15        System.out.println("  disk       offset         size");
   10.16        while (i.hasNext()) {
   10.17          Extent e = (Extent) i.next();
   10.18 -        System.out.println( Library.format(e.getDisk(), 6, 0) + " "
   10.19 -                          + Library.format(e.getOffset(), 12, 0) + " "
   10.20 -                          + Library.format(e.getSize(), 12, 0) );
   10.21 +        System.out.println( Library.format(e.getDisk(), 6, false) + " "
   10.22 +                          + Library.format(e.getOffset(), 12, false) + " "
   10.23 +                          + Library.format(e.getSize(), 12, false) );
   10.24        }
   10.25      }
   10.26    }
    11.1 --- a/tools/control/src/org/xenoserver/cmdline/ParseVdShow.java	Tue Jul 08 11:23:42 2003 +0000
    11.2 +++ b/tools/control/src/org/xenoserver/cmdline/ParseVdShow.java	Tue Jul 08 11:30:20 2003 +0000
    11.3 @@ -13,41 +13,42 @@ import org.xenoserver.control.VirtualDis
    11.4  
    11.5  public class ParseVdShow extends CommandParser {
    11.6    public void parse(Defaults d, LinkedList args) throws ParseFailedException, CommandFailedException {
    11.7 -    int vd_num = getIntParameter(args,'n',-1);
    11.8 +    String key = getStringParameter(args,'k',"");
    11.9      
   11.10      loadState();
   11.11      
   11.12 -    if ( vd_num < 0 ) {
   11.13 -      System.out.println("num key        expiry                       name                 size");
   11.14 -      for (int i=0;i<VirtualDiskManager.it.getVirtualDiskCount();i++) {
   11.15 -        VirtualDisk vd = VirtualDiskManager.it.getVirtualDisk(i);
   11.16 -        System.out.print( Library.format(i,3,0) + " " + vd.getKey() + " " );
   11.17 +    if ( key.equals("") ) {
   11.18 +      System.out.println("key        expiry                       name                 size");
   11.19 +      Iterator i = VirtualDiskManager.IT.getVirtualDisks();
   11.20 +      while ( i.hasNext() ) {
   11.21 +        VirtualDisk vd = (VirtualDisk) i.next();
   11.22 +        System.out.print( vd.getKey() + " " );
   11.23          if ( vd.getExpiry() != null )
   11.24            System.out.print( vd.getExpiry().toString() );
   11.25          else
   11.26            System.out.print( "                            " );
   11.27 -        System.out.println( " " + Library.format(vd.getName(),16,1) + " "
   11.28 -                          + Library.format_size(vd.getSize()*Settings.SECTOR_SIZE,8,0) );
   11.29 +        System.out.println( " " + Library.format(vd.getName(),16,true) + " "
   11.30 +                          + Library.formatSize(vd.getSize()*Settings.SECTOR_SIZE,8,false) );
   11.31        }
   11.32      } else {
   11.33 -      VirtualDisk vd = VirtualDiskManager.it.getVirtualDisk(vd_num);
   11.34 +      VirtualDisk vd = VirtualDiskManager.IT.getVirtualDisk(key);
   11.35        if ( vd == null )
   11.36 -        throw new CommandFailedException("There is no virtual disk " + vd_num );
   11.37 +        throw new CommandFailedException("There is no virtual disk " + key );
   11.38          
   11.39        System.out.println("  name: " + vd.getName());
   11.40        System.out.println("   key: " + vd.getKey());
   11.41 -      System.out.println("  size: " + Library.format_size(vd.getSize()*Settings.SECTOR_SIZE,8,1));
   11.42 +      System.out.println("  size: " + Library.formatSize(vd.getSize()*Settings.SECTOR_SIZE,8,true));
   11.43        if ( vd.getExpiry() != null )
   11.44          System.out.println("expiry: " + vd.getExpiry());
   11.45        System.out.println();
   11.46   
   11.47 -      Iterator i = vd.iterator();
   11.48 +      Iterator i = vd.extents();
   11.49        System.out.println("  disk       offset         size");
   11.50        while (i.hasNext()) {
   11.51          Extent e = (Extent) i.next();
   11.52 -        System.out.println( Library.format(e.getDisk(), 6, 0) + " "
   11.53 -                          + Library.format(e.getOffset(), 12, 0) + " "
   11.54 -                          + Library.format(e.getSize(), 12, 0) );
   11.55 +        System.out.println( Library.format(e.getDisk(), 6, false) + " "
   11.56 +                          + Library.format(e.getOffset(), 12, false) + " "
   11.57 +                          + Library.format(e.getSize(), 12, false) );
   11.58        }
   11.59      }
   11.60    }
    12.1 --- a/tools/control/src/org/xenoserver/control/CommandPartitionAdd.java	Tue Jul 08 11:23:42 2003 +0000
    12.2 +++ b/tools/control/src/org/xenoserver/control/CommandPartitionAdd.java	Tue Jul 08 11:30:20 2003 +0000
    12.3 @@ -15,8 +15,8 @@ public class CommandPartitionAdd extends
    12.4    }
    12.5  
    12.6    public String execute() throws CommandFailedException {
    12.7 -    VirtualDiskManager.it.add_xeno_partition(partition,chunksize);
    12.8 -    PartitionManager.it.add_xeno_partition(partition);
    12.9 +    VirtualDiskManager.IT.addPartition(partition,chunksize);
   12.10 +    PartitionManager.IT.addXenoPartition(partition);
   12.11      return "Added partition " + partition.getName();
   12.12    }
   12.13  }
    13.1 --- a/tools/control/src/org/xenoserver/control/CommandPhysicalList.java	Tue Jul 08 11:23:42 2003 +0000
    13.2 +++ b/tools/control/src/org/xenoserver/control/CommandPhysicalList.java	Tue Jul 08 11:30:20 2003 +0000
    13.3 @@ -12,86 +12,95 @@ import java.util.StringTokenizer;
    13.4   * to modes.
    13.5   */
    13.6  public class CommandPhysicalList extends Command {
    13.7 -  private int domain_id;
    13.8 -  private Defaults d;
    13.9 -  private Map map = new HashMap();
   13.10 +    private int domain_id;
   13.11 +    private Defaults d;
   13.12 +    private Map map = new HashMap();
   13.13 +
   13.14 +    /**
   13.15 +     * Constructor for CommandDomainList.
   13.16 +     * @param d Defaults object to use.
   13.17 +     */
   13.18 +    public CommandPhysicalList(Defaults d, int domain_id) {
   13.19 +        this.d = d;
   13.20 +        this.domain_id = domain_id;
   13.21 +    }
   13.22  
   13.23 -  /**
   13.24 -   * Constructor for CommandDomainList.
   13.25 -   * @param d Defaults object to use.
   13.26 -   */
   13.27 -  public CommandPhysicalList(Defaults d, int domain_id) {
   13.28 -    this.d = d;
   13.29 -    this.domain_id = domain_id;
   13.30 -  }
   13.31 +    /**
   13.32 +     * Retrieves the list of extents.
   13.33 +     * @return null, call extents() to get the list.
   13.34 +     */
   13.35 +    public String execute() throws CommandFailedException {
   13.36 +        Runtime r = Runtime.getRuntime();
   13.37 +        String outline;
   13.38 +        BufferedReader in;
   13.39 +        String output = null;
   13.40 +
   13.41 +        try {
   13.42 +            Process start_p;
   13.43 +            String start_cmdarray[] = new String[2];
   13.44 +            int start_rc;
   13.45 +            start_cmdarray[0] = d.XIToolsDir + "xi_phys_probe";
   13.46 +            start_cmdarray[1] = Integer.toString(domain_id);
   13.47 +
   13.48 +            if (Settings.TEST) {
   13.49 +                output = reportCommand(start_cmdarray);
   13.50 +            } else {
   13.51 +                start_p = r.exec(start_cmdarray);
   13.52 +                start_rc = start_p.waitFor();
   13.53 +                if (start_rc != 0) {
   13.54 +                    throw CommandFailedException.XICommandFailed(
   13.55 +                        "Could not get extent list",
   13.56 +                        start_cmdarray);
   13.57 +                }
   13.58  
   13.59 -  /**
   13.60 -   * Retrieves the list of extents.
   13.61 -   * @return null, call extents() to get the list.
   13.62 -   */
   13.63 -  public String execute() throws CommandFailedException {
   13.64 -    Runtime r = Runtime.getRuntime();
   13.65 -    String outline;
   13.66 -    BufferedReader in;
   13.67 -    String output = null;
   13.68 +                in =
   13.69 +                    new BufferedReader(
   13.70 +                        new InputStreamReader(start_p.getInputStream()));
   13.71  
   13.72 -    try {
   13.73 -      Process start_p;
   13.74 -      String start_cmdarray[] = new String[2];
   13.75 -      int start_rc;
   13.76 -      start_cmdarray[0] = d.XIToolsDir + "xi_phys_probe";
   13.77 -      start_cmdarray[1] = Integer.toString( domain_id );
   13.78 +                outline = in.readLine();
   13.79 +                while (outline != null) {
   13.80 +                    int disk = -1;
   13.81 +                    long offset = -1;
   13.82 +                    long size = -1;
   13.83 +                    
   13.84 +                    StringTokenizer st = new StringTokenizer(outline);
   13.85 +                    if (st.hasMoreTokens()) {
   13.86 +                        disk = Short.parseShort(st.nextToken(), 16);
   13.87 +                    }
   13.88 +                    if (st.hasMoreTokens()) {
   13.89 +                        offset = Long.parseLong(st.nextToken(), 16);
   13.90 +                    }
   13.91 +                    if (st.hasMoreTokens()) {
   13.92 +                        size = Long.parseLong(st.nextToken(), 16);
   13.93 +                    }
   13.94 +                    if (st.hasMoreTokens()) {
   13.95 +                        String mode = st.nextToken();
   13.96 +                        Extent extent = new Extent(disk,offset,size);
   13.97 +                        if (mode.equals("rw"))
   13.98 +                            map.put(extent, Mode.READ_WRITE);
   13.99 +                        else if (mode.equals("r"))
  13.100 +                            map.put(extent, Mode.READ_ONLY);
  13.101 +                        else
  13.102 +                            throw new CommandFailedException(
  13.103 +                                "Could not parse access mode " + mode);
  13.104 +                    }
  13.105  
  13.106 -      if (Settings.TEST) {
  13.107 -        output = reportCommand(start_cmdarray);
  13.108 -      } else {
  13.109 -        start_p = r.exec(start_cmdarray);
  13.110 -        start_rc = start_p.waitFor();
  13.111 -        if (start_rc != 0) {
  13.112 -          throw CommandFailedException.XICommandFailed("Could not get extent list", start_cmdarray);
  13.113 +                    outline = in.readLine();
  13.114 +                }
  13.115 +
  13.116 +            }
  13.117 +        } catch (CommandFailedException e) {
  13.118 +            throw e;
  13.119 +        } catch (Exception e) {
  13.120 +            throw new CommandFailedException(
  13.121 +                "Could not get extent list(" + e + ")",
  13.122 +                e);
  13.123          }
  13.124  
  13.125 -        in =
  13.126 -          new BufferedReader(new InputStreamReader(start_p.getInputStream()));
  13.127 -
  13.128 -        outline = in.readLine();
  13.129 -        while (outline != null) {
  13.130 -          Extent extent = new Extent();
  13.131 -
  13.132 -          StringTokenizer st = new StringTokenizer(outline);
  13.133 -          if (st.hasMoreTokens()) {
  13.134 -            extent.disk = Short.parseShort(st.nextToken(),16);
  13.135 -          }
  13.136 -          if (st.hasMoreTokens()) {
  13.137 -            extent.offset = Long.parseLong(st.nextToken(),16);
  13.138 -          }
  13.139 -          if (st.hasMoreTokens()) {
  13.140 -            extent.size = Long.parseLong(st.nextToken(),16);
  13.141 -          }
  13.142 -          if (st.hasMoreTokens()) {
  13.143 -            String mode = st.nextToken();
  13.144 -            if ( mode.equals( "rw" ) )
  13.145 -              map.put( extent, Mode.READ_WRITE );
  13.146 -            else if ( mode.equals ( "r" ) )
  13.147 -              map.put( extent, Mode.READ_ONLY );
  13.148 -            else
  13.149 -              throw new CommandFailedException("Could not parse access mode " + mode);
  13.150 -          }
  13.151 -
  13.152 -          outline = in.readLine();
  13.153 -        }
  13.154 -
  13.155 -      }
  13.156 -    } catch (CommandFailedException e) {
  13.157 -      throw e;
  13.158 -    } catch (Exception e) {
  13.159 -      throw new CommandFailedException("Could not get extent list(" + e + ")", e);
  13.160 +        return output;
  13.161      }
  13.162  
  13.163 -    return output;
  13.164 -  }
  13.165 -  
  13.166 -  public Map extents() {
  13.167 -    return map;
  13.168 -  }
  13.169 +    public Map extents() {
  13.170 +        return map;
  13.171 +    }
  13.172  }
    14.1 --- a/tools/control/src/org/xenoserver/control/CommandVdCreate.java	Tue Jul 08 11:23:42 2003 +0000
    14.2 +++ b/tools/control/src/org/xenoserver/control/CommandVdCreate.java	Tue Jul 08 11:30:20 2003 +0000
    14.3 @@ -20,7 +20,7 @@ public class CommandVdCreate extends Com
    14.4    }
    14.5  
    14.6    public String execute() throws CommandFailedException {
    14.7 -    VirtualDisk vd = VirtualDiskManager.it.create_virtual_disk(name,size,expiry);
    14.8 +    VirtualDisk vd = VirtualDiskManager.IT.createVirtualDisk(name,size,expiry);
    14.9      if ( vd == null )
   14.10        throw new CommandFailedException( "Not enough free space to create disk" );
   14.11      return "Virtual Disk created with key: " + vd.getKey();
    15.1 --- a/tools/control/src/org/xenoserver/control/CommandVdDelete.java	Tue Jul 08 11:23:42 2003 +0000
    15.2 +++ b/tools/control/src/org/xenoserver/control/CommandVdDelete.java	Tue Jul 08 11:30:20 2003 +0000
    15.3 @@ -12,7 +12,7 @@ public class CommandVdDelete extends Com
    15.4    }
    15.5  
    15.6    public String execute() throws CommandFailedException {
    15.7 -    VirtualDiskManager.it.delete_virtual_disk(key);
    15.8 +    VirtualDiskManager.IT.deleteVirtualDisk(key);
    15.9      return "Deleted virtual disk " + key;
   15.10    }
   15.11  }
    16.1 --- a/tools/control/src/org/xenoserver/control/CommandVdRefresh.java	Tue Jul 08 11:23:42 2003 +0000
    16.2 +++ b/tools/control/src/org/xenoserver/control/CommandVdRefresh.java	Tue Jul 08 11:30:20 2003 +0000
    16.3 @@ -9,7 +9,7 @@ public class CommandVdRefresh extends Co
    16.4    /**
    16.5     * Constructor for CommandVdRefresh.
    16.6     * @param key Key to refresh.
    16.7 -   * @param expiry New expiry.
    16.8 +   * @param expiry New expiry (null for no expiry).
    16.9     */
   16.10    public CommandVdRefresh(String key, Date expiry) {
   16.11      this.key = key;
   16.12 @@ -17,7 +17,10 @@ public class CommandVdRefresh extends Co
   16.13    }
   16.14  
   16.15    public String execute() throws CommandFailedException {
   16.16 -    VirtualDiskManager.it.refresh_virtual_disk(key,expiry);
   16.17 +    VirtualDisk vd = VirtualDiskManager.IT.getVirtualDisk(key);
   16.18 +    if ( vd == null )
   16.19 +      throw new CommandFailedException( "No such virtual disk " + key );
   16.20 +    vd.refreshExpiry(expiry);
   16.21      return "Refreshed virtual disk " + key;
   16.22    }
   16.23  }
    17.1 --- a/tools/control/src/org/xenoserver/control/Extent.java	Tue Jul 08 11:23:42 2003 +0000
    17.2 +++ b/tools/control/src/org/xenoserver/control/Extent.java	Tue Jul 08 11:30:20 2003 +0000
    17.3 @@ -5,83 +5,61 @@
    17.4  
    17.5  package org.xenoserver.control;
    17.6  
    17.7 -public class
    17.8 -Extent
    17.9 -{
   17.10 -  int disk;
   17.11 -  long offset;                                           /* offset into disk */
   17.12 -  long size;                      /* size of this extent in 512 byte sectors */
   17.13 -
   17.14 -  public int
   17.15 -  getDisk()
   17.16 -  {
   17.17 -    return disk;
   17.18 -  }
   17.19 -
   17.20 -  public long
   17.21 -  getOffset()
   17.22 -  {
   17.23 -    return offset;
   17.24 -  }
   17.25 +/**
   17.26 + * Represents an extent on physical disk.
   17.27 + */
   17.28 +public class Extent {
   17.29 +    /** Disk number; 16-bit major:minor pair with no partition number. */
   17.30 +    private int disk;
   17.31 +    /** Offset into disk in sectors. */
   17.32 +    private long offset;
   17.33 +    /** Size of extent in sectors. */
   17.34 +    private long size;
   17.35  
   17.36 -  public long
   17.37 -  getSize()
   17.38 -  {
   17.39 -    return size;
   17.40 -  }
   17.41 -  
   17.42 -  public int
   17.43 -  getMajor()
   17.44 -  {
   17.45 -    return disk >> 8;
   17.46 -  }
   17.47 -  
   17.48 -  public int
   17.49 -  getMinor()
   17.50 -  {
   17.51 -    return disk & 0xFF;
   17.52 -  }
   17.53 -  
   17.54 -  public int hashCode() {
   17.55 -    final int PRIME = 1000003;
   17.56 -    int result = 0;
   17.57 -    result = PRIME * result + disk;
   17.58 -    result = PRIME * result + (int) (offset >>> 32);
   17.59 -    result = PRIME * result + (int) (offset & 0xFFFFFFFF);
   17.60 -    result = PRIME * result + (int) (size >>> 32);
   17.61 -    result = PRIME * result + (int) (size & 0xFFFFFFFF);
   17.62 -
   17.63 -    return result;
   17.64 -  }
   17.65 -
   17.66 -  public boolean equals(Object oth) {
   17.67 -    if (this == oth) {
   17.68 -      return true;
   17.69 +    /**
   17.70 +     * Constructor for Extent.
   17.71 +     * @param disk Disk number.
   17.72 +     * @param offset Offset into disk.
   17.73 +     * @param size Size of extent.
   17.74 +     */
   17.75 +    Extent(int disk, long offset, long size) {
   17.76 +        this.disk = disk;
   17.77 +        this.offset = offset;
   17.78 +        this.size = size;
   17.79      }
   17.80  
   17.81 -    if (oth == null) {
   17.82 -      return false;
   17.83 +    /**
   17.84 +     * @return Disk number.
   17.85 +     */
   17.86 +    public int getDisk() {
   17.87 +        return disk;
   17.88      }
   17.89  
   17.90 -    if (oth.getClass() != getClass()) {
   17.91 -      return false;
   17.92 +    /**
   17.93 +     * @return Offset into disk.
   17.94 +     */
   17.95 +    public long getOffset() {
   17.96 +        return offset;
   17.97      }
   17.98  
   17.99 -    Extent other = (Extent) oth;
  17.100 -
  17.101 -    if (this.disk != other.disk) {
  17.102 -      return false;
  17.103 +    /**
  17.104 +     * @return Size of extent.
  17.105 +     */
  17.106 +    public long getSize() {
  17.107 +        return size;
  17.108      }
  17.109  
  17.110 -    if (this.offset != other.offset) {
  17.111 -      return false;
  17.112 +    /**
  17.113 +     * @return Major number of disk.
  17.114 +     */
  17.115 +    public int getMajor() {
  17.116 +        return disk >> 8;
  17.117      }
  17.118  
  17.119 -    if (this.size != other.size) {
  17.120 -      return false;
  17.121 +    /**
  17.122 +     * @return Minor number of disk, not including partition.
  17.123 +     */
  17.124 +    public int getMinor() {
  17.125 +        return disk & 0xFF;
  17.126      }
  17.127 -
  17.128 -    return true;
  17.129 -  }
  17.130 -
  17.131  }
    18.1 --- a/tools/control/src/org/xenoserver/control/InetAddressPattern.java	Tue Jul 08 11:23:42 2003 +0000
    18.2 +++ b/tools/control/src/org/xenoserver/control/InetAddressPattern.java	Tue Jul 08 11:30:20 2003 +0000
    18.3 @@ -3,58 +3,77 @@ package org.xenoserver.control;
    18.4  import java.net.InetAddress;
    18.5  import java.net.UnknownHostException;
    18.6  
    18.7 -public class InetAddressPattern
    18.8 -{
    18.9 -  InetAddress base;
   18.10 -  boolean addDom;
   18.11 -
   18.12 -  static InetAddressPattern parse (String t)
   18.13 -  {
   18.14 -    InetAddressPattern result = new InetAddressPattern ();
   18.15 -    char[] ca = t.toCharArray ();
   18.16 -    int len = ca.length;
   18.17 +/**
   18.18 + * This utility class expands configuration file patterns.
   18.19 + */
   18.20 +public class InetAddressPattern {
   18.21 +    /** The base InetAddress for this pattern. */
   18.22 +    private InetAddress base;
   18.23 +    /** If true, add the domain number to the base address. */
   18.24 +    private boolean addDom;
   18.25  
   18.26 -    try {
   18.27 -      if (len == 0) {
   18.28 -	result.base = null;
   18.29 -	result.addDom = false;
   18.30 -      } else if (ca[len - 1] == '+') {
   18.31 -	result.base = InetAddress.getByName(t.substring(0, len - 1));
   18.32 -	result.addDom = true;
   18.33 -      } else {
   18.34 -	result.base = InetAddress.getByName(t);
   18.35 -	result.addDom = false;
   18.36 -      }
   18.37 -    } catch (UnknownHostException uhe) {
   18.38 -      result.base = null;
   18.39 -      result.addDom = false;
   18.40 +    /**
   18.41 +     * Parse a pattern string into an InetAddressPattern.
   18.42 +     * @param t The pattern string.
   18.43 +     * @return The parsed pattern object.
   18.44 +     */
   18.45 +    static InetAddressPattern parse(String t) {
   18.46 +        InetAddressPattern result = new InetAddressPattern();
   18.47 +        char[] ca = t.toCharArray();
   18.48 +        int len = ca.length;
   18.49 +
   18.50 +        try {
   18.51 +            if (len == 0) {
   18.52 +                result.base = null;
   18.53 +                result.addDom = false;
   18.54 +            } else if (ca[len - 1] == '+') {
   18.55 +                result.base = InetAddress.getByName(t.substring(0, len - 1));
   18.56 +                result.addDom = true;
   18.57 +            } else {
   18.58 +                result.base = InetAddress.getByName(t);
   18.59 +                result.addDom = false;
   18.60 +            }
   18.61 +        } catch (UnknownHostException uhe) {
   18.62 +            result.base = null;
   18.63 +            result.addDom = false;
   18.64 +        }
   18.65 +
   18.66 +        return result;
   18.67      }
   18.68  
   18.69 -    return result;
   18.70 -  }
   18.71 +    /**
   18.72 +     * Resolve the pattern for the given domain.
   18.73 +     * @param domain_id The domain ID.
   18.74 +     * @return The resolved string.
   18.75 +     */
   18.76 +    String resolve(int domain_id) {
   18.77 +        byte[] b = base.getAddress();
   18.78 +        if (addDom) {
   18.79 +            if (((int) b[3]) + domain_id > 255) {
   18.80 +                if (((int) b[2]) + domain_id > 255) {
   18.81 +                    if (((int) b[1]) + domain_id > 255) {
   18.82 +                        b[0]++;
   18.83 +                    }
   18.84 +                    b[1]++;
   18.85 +                }
   18.86 +                b[2]++;
   18.87 +            }
   18.88 +            b[3] += domain_id;
   18.89 +        }
   18.90 +        return ""
   18.91 +            + (b[0] + (b[0] < 0 ? 256 : 0))
   18.92 +            + "."
   18.93 +            + (b[1] + (b[1] < 0 ? 256 : 0))
   18.94 +            + "."
   18.95 +            + (b[2] + (b[2] < 0 ? 256 : 0))
   18.96 +            + "."
   18.97 +            + (b[3] + (b[3] < 0 ? 256 : 0));
   18.98 +    }
   18.99  
  18.100 -  public String resolve (int domain_id) {
  18.101 -    byte b[] = base.getAddress ();
  18.102 -    if (addDom) {
  18.103 -      if (((int)b[3]) + domain_id > 255) {
  18.104 -	if (((int)b[2]) + domain_id > 255) {
  18.105 -	  if (((int)b[1]) + domain_id > 255) {
  18.106 -	    b[0] ++;
  18.107 -	  }
  18.108 -	  b[1] ++;
  18.109 -	}
  18.110 -	b[2] ++;
  18.111 -      }
  18.112 -      b[3] += domain_id;
  18.113 +    /**
  18.114 +     * @see java.lang.Object#toString()
  18.115 +     */
  18.116 +    public String toString() {
  18.117 +        return "[" + base + (addDom ? "+dom_id" : "") + "]";
  18.118      }
  18.119 -    return "" + 
  18.120 -      (b[0] + (b[0] < 0 ? 256 : 0)) + "." + 
  18.121 -      (b[1] + (b[1] < 0 ? 256 : 0)) + "." + 
  18.122 -      (b[2] + (b[2] < 0 ? 256 : 0)) + "." + 
  18.123 -      (b[3] + (b[3] < 0 ? 256 : 0));
  18.124 -  }
  18.125 -
  18.126 -  public String toString () {
  18.127 -    return "[" + base + (addDom ? "+dom_id" : "") + "]";
  18.128 -  }
  18.129  }
    19.1 --- a/tools/control/src/org/xenoserver/control/Library.java	Tue Jul 08 11:23:42 2003 +0000
    19.2 +++ b/tools/control/src/org/xenoserver/control/Library.java	Tue Jul 08 11:30:20 2003 +0000
    19.3 @@ -5,162 +5,144 @@
    19.4  
    19.5  package org.xenoserver.control;
    19.6  
    19.7 -public class
    19.8 -Library
    19.9 -{
   19.10 -  /*
   19.11 -   * convert a number to a fixed width string
   19.12 -   */
   19.13 -  public static String
   19.14 -  format (long input, int width, int prefix)
   19.15 -  {
   19.16 -    String sss = Long.toString(input);
   19.17 -    String space = "                                ";
   19.18 +/**
   19.19 + * Library functions.
   19.20 + */
   19.21 +public class Library {
   19.22 +    /**
   19.23 +     * Convert a number to a fixed width string.
   19.24 +     * @param input The number to convert.
   19.25 +     * @param width The width desired.
   19.26 +     * @param leftAlign True to left-align the number.
   19.27 +     * @return The formatted string.
   19.28 +     */
   19.29 +    public static String format(long input, int width, boolean leftAlign) {
   19.30 +        String sss = Long.toString(input);
   19.31 +        String space = "                                ";
   19.32  
   19.33 -    if (width < sss.length())
   19.34 -    {
   19.35 -      width = sss.length();
   19.36 +        if (width < sss.length()) {
   19.37 +            width = sss.length();
   19.38 +        }
   19.39 +
   19.40 +        if (leftAlign) {
   19.41 +            return sss + space.substring(0, width - sss.length());
   19.42 +        } else {
   19.43 +            return space.substring(0, width - sss.length()) + sss;
   19.44 +        }
   19.45      }
   19.46  
   19.47 -    if (prefix == 0)
   19.48 -    {
   19.49 -      return space.substring(0, width - sss.length()) + sss;
   19.50 -    }
   19.51 -    else
   19.52 -    {
   19.53 -      return sss + space.substring(0, width - sss.length());
   19.54 -    }
   19.55 -  }
   19.56 -
   19.57 -  /*
   19.58 -   * convert a string to a fixed width string
   19.59 -   */
   19.60 -  public static String
   19.61 -  format (String input, int width, int prefix)
   19.62 -  {
   19.63 -    String space = "                                ";
   19.64 -
   19.65 -    if (width < input.length())
   19.66 -    {
   19.67 -      width = input.length();
   19.68 -    }
   19.69 +    /**
   19.70 +     * Convert a string to a fixed-width string.
   19.71 +     * @param input Input string.
   19.72 +     * @param width Width desired.
   19.73 +     * @param leftAlign True to left-align the string.
   19.74 +     * @return The formatted string.
   19.75 +     */
   19.76 +    public static String format(String input, int width, boolean leftAlign) {
   19.77 +        String space = "                                ";
   19.78  
   19.79 -    if (prefix == 0)
   19.80 -    {
   19.81 -      return space.substring(0, width - input.length()) + input;
   19.82 -    }
   19.83 -    else
   19.84 -    {
   19.85 -      return input + space.substring(0, width - input.length());
   19.86 -    }
   19.87 -  }
   19.88 +        if (width < input.length()) {
   19.89 +            width = input.length();
   19.90 +        }
   19.91  
   19.92 -  /*
   19.93 -   * convert a number (string format) into 
   19.94 -   * the corresponding integer value.
   19.95 -   */
   19.96 -  public static long
   19.97 -  parse_size(String size)
   19.98 -  {
   19.99 -    String substring = size;
  19.100 -    int    suffix = 1;
  19.101 -    long   value = 0;
  19.102 -
  19.103 -    if (size == null)
  19.104 -    {
  19.105 -      return 0;
  19.106 +        if (leftAlign) {
  19.107 +            return input + space.substring(0, width - input.length());
  19.108 +        } else {
  19.109 +            return space.substring(0, width - input.length()) + input;
  19.110 +        }
  19.111      }
  19.112  
  19.113 -    if ((substring = check(size, 'm')) != null)
  19.114 -    {
  19.115 -      suffix = 1024 * 1024;
  19.116 -    }
  19.117 -    else if ((substring = check(size, 'M')) != null)
  19.118 -    {
  19.119 -      suffix = 1024 * 1024;
  19.120 -    }
  19.121 -    else if ((substring = check(size, 'k')) != null)
  19.122 -    {
  19.123 -      suffix = 1024;
  19.124 -    }
  19.125 -    else if ((substring = check(size, 'K')) != null)
  19.126 -    {
  19.127 -      suffix = 1024;
  19.128 -    }
  19.129 -    else if ((substring = check(size, 'g')) != null)
  19.130 -    {
  19.131 -      suffix = 1024 * 1024 * 1024;
  19.132 -    }
  19.133 -    else if ((substring = check(size, 'G')) != null)
  19.134 -    {
  19.135 -      suffix = 1024 * 1024 * 1024;
  19.136 -    }
  19.137 -    else
  19.138 -    {
  19.139 -      substring = size;
  19.140 -    }
  19.141 +    /**
  19.142 +     * Parse a size which may have a k/m/g suffix into a number.
  19.143 +     * @param size The size string to parse.
  19.144 +     * @return The equivalent number.
  19.145 +     */
  19.146 +    public static long parseSize(String size) {
  19.147 +        String substring = size;
  19.148 +        int suffix = 1;
  19.149 +        long value = 0;
  19.150 +
  19.151 +        if (size == null) {
  19.152 +            return 0;
  19.153 +        }
  19.154  
  19.155 -    try
  19.156 -    {
  19.157 -      value = Long.decode(substring).longValue() * suffix;
  19.158 -    }
  19.159 -    catch (NumberFormatException e)
  19.160 -    {
  19.161 -      value = 0;
  19.162 +        if ((substring = check(size, 'm')) != null) {
  19.163 +            suffix = 1024 * 1024;
  19.164 +        } else if ((substring = check(size, 'M')) != null) {
  19.165 +            suffix = 1024 * 1024;
  19.166 +        } else if ((substring = check(size, 'k')) != null) {
  19.167 +            suffix = 1024;
  19.168 +        } else if ((substring = check(size, 'K')) != null) {
  19.169 +            suffix = 1024;
  19.170 +        } else if ((substring = check(size, 'g')) != null) {
  19.171 +            suffix = 1024 * 1024 * 1024;
  19.172 +        } else if ((substring = check(size, 'G')) != null) {
  19.173 +            suffix = 1024 * 1024 * 1024;
  19.174 +        } else {
  19.175 +            substring = size;
  19.176 +        }
  19.177 +
  19.178 +        try {
  19.179 +            value = Long.decode(substring).longValue() * suffix;
  19.180 +        } catch (NumberFormatException e) {
  19.181 +            value = 0;
  19.182 +        }
  19.183 +
  19.184 +        return value;
  19.185      }
  19.186  
  19.187 -    return value;
  19.188 -  }
  19.189 -
  19.190 -  public static String
  19.191 -  check(String size, char suffix)
  19.192 -  {
  19.193 -    int index = size.indexOf(suffix);
  19.194 +    /**
  19.195 +     * Check if the given size has the specified suffix.
  19.196 +     * @param size Size string.
  19.197 +     * @param suffix Test suffix.
  19.198 +     * @return Number part of string, or null if suffix does not match.
  19.199 +     */
  19.200 +    private static String check(String size, char suffix) {
  19.201 +        int index = size.indexOf(suffix);
  19.202  
  19.203 -    if (index != -1)
  19.204 -    {
  19.205 -      return size.substring(0, index);
  19.206 -    }
  19.207 -    else
  19.208 -    {
  19.209 -      return null;
  19.210 +        if (index != -1) {
  19.211 +            return size.substring(0, index);
  19.212 +        } else {
  19.213 +            return null;
  19.214 +        }
  19.215      }
  19.216 -  }
  19.217 -  
  19.218 -  /**
  19.219 -   * Formats a number of bytes in whichever way makes most sense based
  19.220 -   * on magnitude and width.
  19.221 -   * 
  19.222 -   * @param size Number of bytes.
  19.223 -   * @param width Width of field - at least 5, plz.
  19.224 -   * @param prefix Set to 1 for left justify
  19.225 -   * @return The formatted string.
  19.226 -   */
  19.227 -  public static String format_size(long size,int width,int prefix) {
  19.228 -    char[] suffixes = { ' ', 'k', 'M', 'G' };
  19.229 -    int suffix = 0;
  19.230 -    long before = size;
  19.231 -    float after = 0;
  19.232 -    
  19.233 -    while ( before > 10000 ) {
  19.234 -      after = ((float)(before % 1024)) / 1024;
  19.235 -      before /= 1024;
  19.236 -      suffix++; 
  19.237 +
  19.238 +    /**
  19.239 +     * Formats a number of bytes in whichever way makes most sense based
  19.240 +     * on magnitude and width.
  19.241 +     * 
  19.242 +     * @param size Number of bytes.
  19.243 +     * @param width Width of field - at least 5, plz.
  19.244 +     * @param leftAlign True for left-align.
  19.245 +     * @return The formatted string.
  19.246 +     */
  19.247 +    public static String formatSize(long size, int width, boolean leftAlign) {
  19.248 +        char[] suffixes = { ' ', 'k', 'M', 'G' };
  19.249 +        int suffix = 0;
  19.250 +        long before = size;
  19.251 +        float after = 0;
  19.252 +
  19.253 +        while (before > 10000) {
  19.254 +            after = ((float) (before % 1024)) / 1024;
  19.255 +            before /= 1024;
  19.256 +            suffix++;
  19.257 +        }
  19.258 +
  19.259 +        StringBuffer num = new StringBuffer(width);
  19.260 +        num.append(Long.toString(before));
  19.261 +        if (after != 0) {
  19.262 +            int space = width - num.length() - 2;
  19.263 +            if (space > 0) {
  19.264 +                num.append('.');
  19.265 +                if (space > 3) {
  19.266 +                    space = 3;
  19.267 +                }
  19.268 +                num.append(
  19.269 +                    Integer.toString((int) (after * Math.pow(10, space))));
  19.270 +            }
  19.271 +        }
  19.272 +        num.append(suffixes[suffix]);
  19.273 +
  19.274 +        return format(num.toString(), width, leftAlign);
  19.275      }
  19.276 -    
  19.277 -    StringBuffer num = new StringBuffer(width);
  19.278 -    num.append( Long.toString( before ) );
  19.279 -    if ( after != 0 ) {
  19.280 -      int space = width - num.length() - 2;
  19.281 -      if ( space > 0 ) {
  19.282 -        num.append( '.' );
  19.283 -        if ( space > 3 )
  19.284 -          space = 3;
  19.285 -        num.append( Integer.toString( (int) (after * Math.pow(10,space))));
  19.286 -      }
  19.287 -    }
  19.288 -    num.append( suffixes[suffix] );
  19.289 -    
  19.290 -    return format(num.toString(),width,prefix);
  19.291 -  }
  19.292  }
    20.1 --- a/tools/control/src/org/xenoserver/control/Mode.java	Tue Jul 08 11:23:42 2003 +0000
    20.2 +++ b/tools/control/src/org/xenoserver/control/Mode.java	Tue Jul 08 11:30:20 2003 +0000
    20.3 @@ -7,16 +7,30 @@
    20.4  
    20.5  package org.xenoserver.control;
    20.6  
    20.7 -public class 
    20.8 -Mode 
    20.9 -{
   20.10 -  private final String name;
   20.11 -
   20.12 -  private Mode(String name) { this.name = name; }
   20.13 +/**
   20.14 + * Enumeration to represent an access mode.
   20.15 + */
   20.16 +public class Mode {
   20.17 +    /** name of this mode */
   20.18 +    private final String name;
   20.19  
   20.20 -  public String toString()  { return name; }
   20.21 +    /**
   20.22 +     * Construct a mode
   20.23 +     * @param name Name to use.
   20.24 +     */
   20.25 +    private Mode(String name) {
   20.26 +        this.name = name;
   20.27 +    }
   20.28  
   20.29 -  public static final Mode READ_ONLY  = new Mode("ro");
   20.30 -  public static final Mode READ_WRITE = new Mode("rw");
   20.31 +    /**
   20.32 +     * @see java.lang.Object#toString()
   20.33 +     */
   20.34 +    public String toString() {
   20.35 +        return name;
   20.36 +    }
   20.37 +
   20.38 +    /** Single read-only mode instance. */
   20.39 +    public static final Mode READ_ONLY = new Mode("ro");
   20.40 +    /** Single read-write mode instance. */
   20.41 +    public static final Mode READ_WRITE = new Mode("rw");
   20.42  }
   20.43 -
    21.1 --- a/tools/control/src/org/xenoserver/control/Partition.java	Tue Jul 08 11:23:42 2003 +0000
    21.2 +++ b/tools/control/src/org/xenoserver/control/Partition.java	Tue Jul 08 11:30:20 2003 +0000
    21.3 @@ -7,184 +7,179 @@ package org.xenoserver.control;
    21.4  
    21.5  import java.io.PrintWriter;
    21.6  
    21.7 -public class
    21.8 -Partition
    21.9 -{
   21.10 -  int major;
   21.11 -  int minor;
   21.12 -  long blocks;
   21.13 -  long start_sect;
   21.14 -  long nr_sects;
   21.15 -  String name;
   21.16 -  boolean xeno;
   21.17 -
   21.18 -  Partition
   21.19 -  duplicate ()
   21.20 -  {
   21.21 -    Partition p = new Partition();
   21.22 +/**
   21.23 + * Represents a single real partition.
   21.24 + */
   21.25 +public class Partition {
   21.26 +    /** Major device number as seen by Linux. */
   21.27 +    private int major;
   21.28 +    /** Minor device number as seen by Linux. */
   21.29 +    private int minor;
   21.30 +    /** Number of blocks in the partition. */
   21.31 +    private long blocks;
   21.32 +    /** Start sector of the partition. */
   21.33 +    private long start_sect;
   21.34 +    /** Number of sectors in the partition. */
   21.35 +    private long nr_sects;
   21.36 +    /** Name of the partition. */
   21.37 +    private String name;
   21.38 +    /** True if this partition is a XenoPartition. */
   21.39 +    private boolean xeno;
   21.40  
   21.41 -    p.major = major;
   21.42 -    p.minor = minor;
   21.43 -    p.blocks = blocks;
   21.44 -    p.start_sect = start_sect;
   21.45 -    p.nr_sects = nr_sects;
   21.46 -    p.name = name;
   21.47 -    p.xeno = xeno;
   21.48 -
   21.49 -    return p;
   21.50 -  }
   21.51 -
   21.52 -  void
   21.53 -  dump_xml(PrintWriter out)
   21.54 -  {
   21.55 -    out.println ("  <partition>\n" +
   21.56 -		 "    <major>" + major + "</major>\n" +
   21.57 -		 "    <minor>" + minor + "</minor>\n" +
   21.58 -		 "    <blocks>" + blocks + "</blocks>\n" +
   21.59 -		 "    <start_sect>" + start_sect + "</start_sect>\n" +
   21.60 -		 "    <nr_sects>" + nr_sects + "</nr_sects>\n" +
   21.61 -		 "    <name>" + name + "</name>\n" +
   21.62 -		 "  </partition>");
   21.63 -  }
   21.64 -
   21.65 -  public int
   21.66 -  getMajor()
   21.67 -  {
   21.68 -    return major;
   21.69 -  }
   21.70 +    /**
   21.71 +     * Mark this partition as a XenoPartition.
   21.72 +     */
   21.73 +    void makeXeno() {
   21.74 +        xeno = true;
   21.75 +    }
   21.76  
   21.77 -  public int
   21.78 -  getMinor()
   21.79 -  {
   21.80 -    return minor;
   21.81 -  }
   21.82 -
   21.83 -  public long
   21.84 -  getBlocks()
   21.85 -  {
   21.86 -    return blocks;
   21.87 -  }
   21.88 -  
   21.89 -  public long
   21.90 -  getStartSect()
   21.91 -  {
   21.92 -    return start_sect;
   21.93 -  }
   21.94 +    /**
   21.95 +     * Constructor for Partition.
   21.96 +     * @param major Major number
   21.97 +     * @param minor Minor number
   21.98 +     * @param blocks Size in blocks
   21.99 +     * @param start_sect Start sector
  21.100 +     * @param nr_sects Number of sectors
  21.101 +     * @param name Name of partition
  21.102 +     * @param xeno True if XenoPartition
  21.103 +     */
  21.104 +    Partition(
  21.105 +        int major,
  21.106 +        int minor,
  21.107 +        long blocks,
  21.108 +        long start_sect,
  21.109 +        long nr_sects,
  21.110 +        String name,
  21.111 +        boolean xeno) {
  21.112 +        this.major = major;
  21.113 +        this.minor = minor;
  21.114 +        this.blocks = blocks;
  21.115 +        this.start_sect = start_sect;
  21.116 +        this.nr_sects = nr_sects;
  21.117 +        this.name = name;
  21.118 +        this.xeno = xeno;
  21.119 +    }
  21.120  
  21.121 -  public long
  21.122 -  getNumSects()
  21.123 -  {
  21.124 -    return nr_sects;
  21.125 -  }
  21.126 -  
  21.127 -  public String
  21.128 -  getName()
  21.129 -  {
  21.130 -    return name;
  21.131 -  }
  21.132 +    /**
  21.133 +     * Dump this partition as XML.
  21.134 +     * @param out The writer to dump to.
  21.135 +     */
  21.136 +    void dumpAsXML(PrintWriter out) {
  21.137 +        out.println(
  21.138 +            "  <partition>\n"
  21.139 +                + "    <major>"
  21.140 +                + major
  21.141 +                + "</major>\n"
  21.142 +                + "    <minor>"
  21.143 +                + minor
  21.144 +                + "</minor>\n"
  21.145 +                + "    <blocks>"
  21.146 +                + blocks
  21.147 +                + "</blocks>\n"
  21.148 +                + "    <start_sect>"
  21.149 +                + start_sect
  21.150 +                + "</start_sect>\n"
  21.151 +                + "    <nr_sects>"
  21.152 +                + nr_sects
  21.153 +                + "</nr_sects>\n"
  21.154 +                + "    <name>"
  21.155 +                + name
  21.156 +                + "</name>\n"
  21.157 +                + "  </partition>");
  21.158 +    }
  21.159  
  21.160 -  public boolean
  21.161 -  getIsXeno()
  21.162 -  {
  21.163 -    return xeno;
  21.164 -  }
  21.165 -
  21.166 -  public int hashCode() {
  21.167 -    final int PRIME = 1000003;
  21.168 -    int result = 0;
  21.169 -    result = PRIME * result + major;
  21.170 -    result = PRIME * result + minor;
  21.171 -    result = PRIME * result + (int) (blocks >>> 32);
  21.172 -    result = PRIME * result + (int) (blocks & 0xFFFFFFFF);
  21.173 -    result = PRIME * result + (int) (start_sect >>> 32);
  21.174 -    result = PRIME * result + (int) (start_sect & 0xFFFFFFFF);
  21.175 -    result = PRIME * result + (int) (nr_sects >>> 32);
  21.176 -    result = PRIME * result + (int) (nr_sects & 0xFFFFFFFF);
  21.177 -    if (name != null) {
  21.178 -      result = PRIME * result + name.hashCode();
  21.179 +    /**
  21.180 +     * @return Major device number.
  21.181 +     */
  21.182 +    public int getMajor() {
  21.183 +        return major;
  21.184      }
  21.185  
  21.186 -    return result;
  21.187 -  }
  21.188 -
  21.189 -  public boolean equals(Object oth) {
  21.190 -    if (this == oth) {
  21.191 -      return true;
  21.192 +    /**
  21.193 +     * @return Minor device number.
  21.194 +     */
  21.195 +    public int getMinor() {
  21.196 +        return minor;
  21.197      }
  21.198  
  21.199 -    if (oth == null) {
  21.200 -      return false;
  21.201 -    }
  21.202 -
  21.203 -    if (oth.getClass() != getClass()) {
  21.204 -      return false;
  21.205 +    /**
  21.206 +     * @return Number of blocks.
  21.207 +     */
  21.208 +    public long getBlocks() {
  21.209 +        return blocks;
  21.210      }
  21.211  
  21.212 -    Partition other = (Partition) oth;
  21.213 -
  21.214 -    if (this.major != other.major) {
  21.215 -      return false;
  21.216 +    /**
  21.217 +     * @return Starting sector.
  21.218 +     */
  21.219 +    public long getStartSect() {
  21.220 +        return start_sect;
  21.221      }
  21.222  
  21.223 -    if (this.minor != other.minor) {
  21.224 -      return false;
  21.225 +    /**
  21.226 +     * @return Number of sectors.
  21.227 +     */
  21.228 +    public long getNumSects() {
  21.229 +        return nr_sects;
  21.230      }
  21.231  
  21.232 -    if (this.blocks != other.blocks) {
  21.233 -      return false;
  21.234 +    /**
  21.235 +     * @return Name of partition.
  21.236 +     */
  21.237 +    public String getName() {
  21.238 +        return name;
  21.239      }
  21.240  
  21.241 -    if (this.start_sect != other.start_sect) {
  21.242 -      return false;
  21.243 +    /**
  21.244 +     * @return True if this is a XenoPartition.
  21.245 +     */
  21.246 +    public boolean isXeno() {
  21.247 +        return xeno;
  21.248      }
  21.249  
  21.250 -    if (this.nr_sects != other.nr_sects) {
  21.251 -      return false;
  21.252 +    /**
  21.253 +     * Is this partition identical to the other?
  21.254 +     * @param other Other partition to compare to.
  21.255 +     * @return True if they are identical.
  21.256 +     */
  21.257 +    public boolean identical(Partition other) {
  21.258 +        return this.major == other.major
  21.259 +            && this.minor == other.minor
  21.260 +            && this.blocks == other.blocks
  21.261 +            && this.start_sect == other.start_sect
  21.262 +            && this.nr_sects == other.nr_sects
  21.263 +            && this.name.equals(other.name);
  21.264      }
  21.265 -    if (this.name == null) {
  21.266 -      if (other.name != null) {
  21.267 -        return false;
  21.268 -      }
  21.269 -    } else {
  21.270 -      if (!this.name.equals(other.name)) {
  21.271 -        return false;
  21.272 -      }
  21.273 +
  21.274 +    /**
  21.275 +     * @return An Extent covering this partiton.
  21.276 +     */
  21.277 +    public Extent toExtent() {
  21.278 +        return new Extent(getDisk(),start_sect,nr_sects);
  21.279      }
  21.280  
  21.281 -    return true;
  21.282 -  }
  21.283 -
  21.284 -  /**
  21.285 -   * @return An Extent covering this partiton.
  21.286 -   */
  21.287 -  public Extent toExtent()
  21.288 -  {
  21.289 -    Extent e = new Extent();
  21.290 -    // Build 16-bit disk number.. high 8 bits are the major
  21.291 -    e.disk = major << 8;
  21.292 -    // Low 8 bits are the minor, but bottom 5 need to be cleared
  21.293 -    // as they are the partition number, not the disk number
  21.294 -    e.disk |= ( minor & 0xE0 );
  21.295 -    e.offset = start_sect;
  21.296 -    e.size = nr_sects;
  21.297 -    return e;
  21.298 -  }
  21.299 -  
  21.300 -  /**
  21.301 -   * @param e Extent to compare this partition to.
  21.302 -   * @return True if this partition covers the same disk area as the given extent.
  21.303 -   */
  21.304 -  public boolean matchesExtent( Extent e )
  21.305 -  {
  21.306 -    if ( e.getMajor() != major )
  21.307 -      return false;
  21.308 -    if ( e.getMinor() != minor )
  21.309 -      return false;
  21.310 -    if ( e.offset != start_sect )
  21.311 -      return false;
  21.312 -    if ( e.size != nr_sects )
  21.313 -      return false;
  21.314 -      
  21.315 -    return true;
  21.316 -  }
  21.317 +    /**
  21.318 +     * @param e Extent to compare this partition to.
  21.319 +     * @return True if this partition covers the same disk area as the given extent.
  21.320 +     */
  21.321 +    public boolean matchesExtent(Extent e) {
  21.322 +        return e.getDisk() == getDisk()
  21.323 +            && e.getOffset() == start_sect
  21.324 +            && e.getSize() == nr_sects;
  21.325 +    }
  21.326 +    
  21.327 +    /**
  21.328 +     * @return Disk number for this partition.
  21.329 +     */
  21.330 +    public int getDisk() {
  21.331 +        if ( name.startsWith("hd") ) {
  21.332 +            // High 8 bits are major, low 8 bits are minor, with bottom 6 clear
  21.333 +            return (major << 8) | (minor & 0xC0); 
  21.334 +        } else if ( name.startsWith("sd") ) {
  21.335 +            // High 8 bits are major, low 8 bits are minor, with bottom 4 clear
  21.336 +            return (major << 8) | (minor & 0xF0);
  21.337 +        } else {
  21.338 +            throw new IllegalArgumentException("Don't know how to convert " + name + "into a disk number");
  21.339 +        }
  21.340 +    }
  21.341  }
    22.1 --- a/tools/control/src/org/xenoserver/control/PartitionManager.java	Tue Jul 08 11:23:42 2003 +0000
    22.2 +++ b/tools/control/src/org/xenoserver/control/PartitionManager.java	Tue Jul 08 11:30:20 2003 +0000
    22.3 @@ -17,170 +17,155 @@ import java.util.Vector;
    22.4   * PartitionManager manages the partitions on the machine. It is a Singleton
    22.5   * which automatically initialises itself on first class reference.
    22.6   */
    22.7 -public class
    22.8 -PartitionManager
    22.9 -{
   22.10 -  static final String proc_template =
   22.11 -    "major minor  #blocks  start_sect   nr_sects name";
   22.12 -    
   22.13 -  public static final PartitionManager it = new PartitionManager(Settings.PARTITIONS_FILE);
   22.14 -    
   22.15 -  Vector partition_map;
   22.16 +public class PartitionManager {
   22.17 +    /** The proc header string, used to check that this is a suitable proc file. */
   22.18 +    private static final String PROC_TEMPLATE =
   22.19 +        "major minor  #blocks  start_sect   nr_sects name";
   22.20  
   22.21 -  /*
   22.22 -   * Initialize partition manager with source file.
   22.23 -   * Normally we read from /proc/partitions, but we can
   22.24 -   * specify an alternative file for debugging
   22.25 -   */
   22.26 -  private PartitionManager (String filename)
   22.27 -  {
   22.28 -    String str;
   22.29 -    BufferedReader in;
   22.30 +    /** The single PartitionManager reference. */
   22.31 +    public static final PartitionManager IT =
   22.32 +        new PartitionManager(Settings.PARTITIONS_FILE);
   22.33  
   22.34 -    partition_map = new Vector(100,10);
   22.35 -
   22.36 -    try
   22.37 -    {
   22.38 -      in = new BufferedReader(new FileReader(filename));
   22.39 +    /** The list of partitions. */
   22.40 +    private Vector partition_map;
   22.41  
   22.42 -      str = in.readLine();                                  /* skip headings */
   22.43 -      if (str.length() < proc_template.length() ||
   22.44 -	  !str.substring(0, proc_template.length()).equals(proc_template))
   22.45 -      {
   22.46 -	System.err.println ("Error: Incorrect /proc/partitions.");
   22.47 -	System.err.println ("       Is this Xeno?");
   22.48 -	System.exit (1);
   22.49 -      }
   22.50 -
   22.51 -      str = in.readLine();                                /* skip blank line */
   22.52 -
   22.53 -      str = in.readLine();
   22.54 -      while (str != null)
   22.55 -      {
   22.56 -	Partition partition = new Partition();
   22.57 +    /**
   22.58 +     * Initialize partition manager with source file.
   22.59 +     * Normally we read from /proc/partitions, but we can
   22.60 +     * specify an alternative file for debugging.
   22.61 +     * @param filename The file to read partition data from.
   22.62 +     */
   22.63 +    private PartitionManager(String filename) {
   22.64 +        String str;
   22.65 +        BufferedReader in;
   22.66  
   22.67 -	partition.major = Integer.parseInt(str.substring(0,5).trim());
   22.68 -	partition.minor = Integer.parseInt(str.substring(5,10).trim());
   22.69 -	partition.blocks = Integer.parseInt(str.substring(10,21).trim());
   22.70 -	partition.start_sect = Integer.parseInt(str.substring(21,32).trim());
   22.71 -	partition.nr_sects = Integer.parseInt(str.substring(32,43).trim());
   22.72 -	partition.name = str.substring(43).trim();
   22.73 -	partition.xeno = false;
   22.74 +        partition_map = new Vector(100, 10);
   22.75  
   22.76 -	partition_map.add(partition);
   22.77 -	str = in.readLine();
   22.78 -      }
   22.79 -    }
   22.80 -    catch (IOException io)
   22.81 -    {
   22.82 -      System.err.println ("PartitionManager: error reading partition file [" 
   22.83 -			  + filename + "]");
   22.84 -      System.err.println (io);
   22.85 -    }
   22.86 -  }
   22.87 +        try {
   22.88 +            in = new BufferedReader(new FileReader(filename));
   22.89  
   22.90 -  public Partition
   22.91 -  get_partition (String name)
   22.92 -  {
   22.93 -    Partition partition = null;
   22.94 -    for (Enumeration e = partition_map.elements() ; e.hasMoreElements() ;) 
   22.95 -    {
   22.96 -      partition = (Partition) e.nextElement();
   22.97 -      if (partition.name.equals(name))
   22.98 -      {
   22.99 -  return partition;
  22.100 -      }
  22.101 -    }
  22.102 -    return null;
  22.103 -  }
  22.104 +            str = in.readLine(); /* skip headings */
  22.105 +            if (str.length() < PROC_TEMPLATE.length()
  22.106 +                || !str.substring(0, PROC_TEMPLATE.length()).equals(
  22.107 +                    PROC_TEMPLATE)) {
  22.108 +                System.err.println("Error: Incorrect /proc/partitions.");
  22.109 +                System.err.println("       Is this Xeno?");
  22.110 +                System.exit(1);
  22.111 +            }
  22.112 +
  22.113 +            str = in.readLine(); /* skip blank line */
  22.114  
  22.115 -  /**
  22.116 -   * Finds the partition that matches the given extent, if any.
  22.117 -   * @param extent The extent to compare to.
  22.118 -   * @return The first matching partition, or null if none.
  22.119 -   */
  22.120 -  public Partition
  22.121 -  get_partition (Extent extent)
  22.122 -  {
  22.123 -    Partition partition = null;
  22.124 -    for (Enumeration e = partition_map.elements() ; e.hasMoreElements() ;) 
  22.125 -    {
  22.126 -      partition = (Partition) e.nextElement();
  22.127 -      if (partition.matchesExtent(extent))
  22.128 -      {
  22.129 -  return partition;
  22.130 -      }
  22.131 -    }
  22.132 -    return null;
  22.133 -  }
  22.134 -
  22.135 -  Partition
  22.136 -  get_partition (int index)
  22.137 -  {
  22.138 -    return (Partition) partition_map.get(index);
  22.139 -  }
  22.140 +            str = in.readLine();
  22.141 +            while (str != null) {
  22.142 +                Partition partition =
  22.143 +                    new Partition(
  22.144 +                        Integer.parseInt(str.substring(0, 5).trim()),
  22.145 +                        Integer.parseInt(str.substring(5, 10).trim()),
  22.146 +                        Integer.parseInt(str.substring(10, 21).trim()),
  22.147 +                        Integer.parseInt(str.substring(21, 32).trim()),
  22.148 +                        Integer.parseInt(str.substring(32, 43).trim()),
  22.149 +                        str.substring(43).trim(),
  22.150 +                        false);
  22.151  
  22.152 -  void
  22.153 -  add_xeno_partition (Partition p)
  22.154 -  {
  22.155 -    for (Enumeration e = partition_map.elements() ; e.hasMoreElements() ;) 
  22.156 -    {
  22.157 -      Partition partition = (Partition) e.nextElement();
  22.158 -      if (partition.equals(p))
  22.159 -      {
  22.160 -	partition.xeno = true;
  22.161 -      }
  22.162 -    }
  22.163 -  }
  22.164 -
  22.165 -  /*
  22.166 -   * dump the xeno partition list as xml
  22.167 -   */
  22.168 -  void
  22.169 -  dump_xml (PrintWriter out)
  22.170 -  {
  22.171 -    out.println("<partitions>");
  22.172 -    for (Enumeration e = partition_map.elements() ; e.hasMoreElements() ;) 
  22.173 -    {
  22.174 -      Partition partition = (Partition) e.nextElement();
  22.175 -      if (partition.xeno == true)
  22.176 -      {
  22.177 -	partition.dump_xml(out);
  22.178 -      }
  22.179 +                partition_map.add(partition);
  22.180 +                str = in.readLine();
  22.181 +            }
  22.182 +        } catch (IOException io) {
  22.183 +            System.err.println(
  22.184 +                "PartitionManager: error reading partition file ["
  22.185 +                    + filename
  22.186 +                    + "]");
  22.187 +            System.err.println(io);
  22.188 +        }
  22.189      }
  22.190  
  22.191 -    out.println("</partitions>");
  22.192 -
  22.193 -    return;
  22.194 -  }
  22.195 +    /**
  22.196 +     * Find a partition with the specified name.
  22.197 +     * @param name The name to search for.
  22.198 +     * @return The partition found, or null if no such partition.
  22.199 +     */
  22.200 +    public Partition getPartition(String name) {
  22.201 +        Partition partition = null;
  22.202 +        for (Enumeration e = partition_map.elements(); e.hasMoreElements();) {
  22.203 +            partition = (Partition) e.nextElement();
  22.204 +            if (partition.getName().equals(name)) {
  22.205 +                return partition;
  22.206 +            }
  22.207 +        }
  22.208 +        return null;
  22.209 +    }
  22.210  
  22.211 -  /**
  22.212 -   * get the number of partitions 
  22.213 -   */
  22.214 -
  22.215 -  int
  22.216 -  getPartitionCount ()
  22.217 -  {
  22.218 -    return partition_map.size();
  22.219 -  }
  22.220 +    /**
  22.221 +     * Finds the partition that matches the given extent, if any.
  22.222 +     * @param extent The extent to compare to.
  22.223 +     * @return The first matching partition, or null if none.
  22.224 +     */
  22.225 +    public Partition getPartition(Extent extent) {
  22.226 +        Partition partition = null;
  22.227 +        for (Enumeration e = partition_map.elements(); e.hasMoreElements();) {
  22.228 +            partition = (Partition) e.nextElement();
  22.229 +            if (partition.matchesExtent(extent)) {
  22.230 +                return partition;
  22.231 +            }
  22.232 +        }
  22.233 +        return null;
  22.234 +    }
  22.235 +    
  22.236 +    /**
  22.237 +     * Find the ith partition in the partition list.
  22.238 +     * @param i Index number.
  22.239 +     * @return The partition, or null if out of range.
  22.240 +     */
  22.241 +    public Partition getPartition(int i) {
  22.242 +        if ( i >= partition_map.size() ) {
  22.243 +          return null;
  22.244 +        }
  22.245 +        return (Partition) partition_map.elementAt( i );
  22.246 +    }
  22.247  
  22.248 -  /**
  22.249 -   * get the details about a particular partition
  22.250 -   *
  22.251 -   */
  22.252 -  Partition
  22.253 -  getPartition (int index)
  22.254 -  {
  22.255 -    Partition partition = (Partition) partition_map.get(index);
  22.256 -    return partition;
  22.257 -  }
  22.258 - 
  22.259 -  /**
  22.260 -   * Get an iterator over all the partitions.
  22.261 -   * @return An iterator over Partition objects.
  22.262 -   */
  22.263 -  public Iterator iterator()
  22.264 -  {
  22.265 -    return partition_map.iterator();
  22.266 -  }
  22.267 +    /**
  22.268 +     * Adds the given partition as a XenoPartition.
  22.269 +     * @param p The partition to add.
  22.270 +     */
  22.271 +    void addXenoPartition(Partition p) {
  22.272 +        for (Enumeration e = partition_map.elements(); e.hasMoreElements();) {
  22.273 +            Partition partition = (Partition) e.nextElement();
  22.274 +            if (partition.identical(p)) {
  22.275 +                partition.makeXeno();
  22.276 +            }
  22.277 +        }
  22.278 +    }
  22.279 +
  22.280 +    /**
  22.281 +     * Dump the XenoPartition list as XML.
  22.282 +     * @param out Writer to dump to.
  22.283 +     */
  22.284 +    void dumpAsXML(PrintWriter out) {
  22.285 +        out.println("<partitions>");
  22.286 +        for (Enumeration e = partition_map.elements(); e.hasMoreElements();) {
  22.287 +            Partition partition = (Partition) e.nextElement();
  22.288 +            if (partition.isXeno()) {
  22.289 +                partition.dumpAsXML(out);
  22.290 +            }
  22.291 +        }
  22.292 +
  22.293 +        out.println("</partitions>");
  22.294 +
  22.295 +        return;
  22.296 +    }
  22.297 +
  22.298 +    /**
  22.299 +     * @return The number of partitions. 
  22.300 +     */
  22.301 +    public int getPartitionCount() {
  22.302 +        return partition_map.size();
  22.303 +    }
  22.304 +
  22.305 +    /**
  22.306 +     * Get an iterator over all the partitions.
  22.307 +     * @return An iterator over Partition objects.
  22.308 +     */
  22.309 +    public Iterator iterator() {
  22.310 +        return partition_map.iterator();
  22.311 +    }
  22.312  }
    23.1 --- a/tools/control/src/org/xenoserver/control/Settings.java	Tue Jul 08 11:23:42 2003 +0000
    23.2 +++ b/tools/control/src/org/xenoserver/control/Settings.java	Tue Jul 08 11:30:20 2003 +0000
    23.3 @@ -7,38 +7,63 @@ import java.util.StringTokenizer;
    23.4   * The Settings class is a repository for global settings such as the IP of
    23.5   * the machine and the location of the defaults file.
    23.6   */
    23.7 -public final class Settings
    23.8 -{
    23.9 -  public static final String DEFAULTS_FILE = System.getProperty ("DEFAULTS_FILE", "domctl.xml");
   23.10 -  public static final String DEFAULTS_PATH = System.getProperty ("DEFAULTS_PATH", ".:/etc:/var/lib/xen");
   23.11 -  public static final String LOCAL_IP = System.getProperty ("LOCAL_IP");
   23.12 -  public static final String LOCAL_MASK = System.getProperty ("LOCAL_MASK");
   23.13 -  public static final String LOCAL_GW = System.getProperty ("LOCAL_ROUTE");
   23.14 -  public static final boolean TEST = (System.getProperty ("TEST") != null);
   23.15 -  public static final String XI_HELPER = System.getProperty ("XI_HELPER", "xi_helper");
   23.16 -  public static final String PARTITIONS_FILE = System.getProperty("PARTITIONS_FILE", "/proc/partitions");
   23.17 -  public static final String STATE_INPUT_FILE = System.getProperty("STATE_INPUT_FILE", "/var/lib/xen/vdstate.xml");
   23.18 -  public static final String STATE_OUTPUT_FILE = System.getProperty("STATE_OUTPUT_FILE", "/var/lib/xen/vdstate.xml");
   23.19 -  public static final int SECTOR_SIZE = Integer.parseInt( System.getProperty("SECTOR_SIZE", "512") );
   23.20 +public final class Settings {
   23.21 +    /** Filename for the defaults file. */
   23.22 +    public static final String DEFAULTS_FILE =
   23.23 +        System.getProperty("DEFAULTS_FILE", "domctl.xml");
   23.24 +    /** Path to search for the defaults file. */
   23.25 +    public static final String DEFAULTS_PATH =
   23.26 +        System.getProperty("DEFAULTS_PATH", ".:/etc:/var/lib/xen");
   23.27 +    /** IP address of domain 0. */
   23.28 +    public static final String LOCAL_IP = System.getProperty("LOCAL_IP");
   23.29 +    /** Network mask of domain 0. */
   23.30 +    public static final String LOCAL_MASK = System.getProperty("LOCAL_MASK");
   23.31 +    /** Gateway of domain 0. */
   23.32 +    public static final String LOCAL_GW = System.getProperty("LOCAL_ROUTE");
   23.33 +    /** If set, do not call any xi_ commands, just print their command lines. */
   23.34 +    public static final boolean TEST = (System.getProperty("TEST") != null);
   23.35 +    /** Name of xi_helper utility. */
   23.36 +    public static final String XI_HELPER =
   23.37 +        System.getProperty("XI_HELPER", "xi_helper");
   23.38 +    /** File to parse to get partition info. */
   23.39 +    public static final String PARTITIONS_FILE =
   23.40 +        System.getProperty("PARTITIONS_FILE", "/proc/partitions");
   23.41 +    /** File to load virtual disk state from. */
   23.42 +    public static final String STATE_INPUT_FILE =
   23.43 +        System.getProperty("STATE_INPUT_FILE", "/var/lib/xen/vdstate.xml");
   23.44 +    /** File to save virtual disk state to. */
   23.45 +    public static final String STATE_OUTPUT_FILE =
   23.46 +        System.getProperty("STATE_OUTPUT_FILE", "/var/lib/xen/vdstate.xml");
   23.47 +    /** Size of a sector in bytes. */
   23.48 +    public static final int SECTOR_SIZE =
   23.49 +        Integer.parseInt(System.getProperty("SECTOR_SIZE", "512"));
   23.50  
   23.51 -  public static File getDefaultsFile() {
   23.52 -    StringTokenizer tok = new StringTokenizer (DEFAULTS_PATH, ":");
   23.53 -    File result = null;
   23.54 -    File probe;
   23.55 +    /**
   23.56 +     * Search for the defaults file in the path configured in DEFAULTS_PATH.
   23.57 +     * @return Reference to the defaults file.
   23.58 +     */
   23.59 +    public static File getDefaultsFile() {
   23.60 +        StringTokenizer tok = new StringTokenizer(DEFAULTS_PATH, ":");
   23.61 +        File result = null;
   23.62 +        File probe;
   23.63  
   23.64 -    while (tok.hasMoreTokens ()) {
   23.65 -      String probe_dir = tok.nextToken ();
   23.66 -      probe = new File (probe_dir, DEFAULTS_FILE);
   23.67 -      if (probe.exists ()) {
   23.68 -	result = probe;
   23.69 -	break;
   23.70 -      }
   23.71 +        while (tok.hasMoreTokens()) {
   23.72 +            String probe_dir = tok.nextToken();
   23.73 +            probe = new File(probe_dir, DEFAULTS_FILE);
   23.74 +            if (probe.exists()) {
   23.75 +                result = probe;
   23.76 +                break;
   23.77 +            }
   23.78 +        }
   23.79 +
   23.80 +        if (result == null) {
   23.81 +            System.err.println(
   23.82 +                "Could not find "
   23.83 +                    + DEFAULTS_FILE
   23.84 +                    + " in path "
   23.85 +                    + DEFAULTS_PATH);
   23.86 +        }
   23.87 +
   23.88 +        return result;
   23.89      }
   23.90 -
   23.91 -    if (result == null) {
   23.92 -      System.err.println ("Could not find " + DEFAULTS_FILE + " in path " + DEFAULTS_PATH);
   23.93 -    }
   23.94 -
   23.95 -    return result;
   23.96 -  }
   23.97  }
    24.1 --- a/tools/control/src/org/xenoserver/control/StringPattern.java	Tue Jul 08 11:23:42 2003 +0000
    24.2 +++ b/tools/control/src/org/xenoserver/control/StringPattern.java	Tue Jul 08 11:30:20 2003 +0000
    24.3 @@ -1,59 +1,78 @@
    24.4  package org.xenoserver.control;
    24.5  
    24.6 -public class StringPattern
    24.7 -{
    24.8 -  String base;
    24.9 -  int bn;
   24.10 -  boolean addDom;
   24.11 -  boolean appendDom;
   24.12 -
   24.13 -  static StringPattern parse (String t)
   24.14 -  {
   24.15 -    StringPattern result = new StringPattern ();
   24.16 -    char[] ca = t.toCharArray ();
   24.17 -    int idx = 0;
   24.18 -    int len = ca.length;
   24.19 +/**
   24.20 + * This utility class expands configuration file patterns.
   24.21 + */
   24.22 +public class StringPattern {
   24.23 +    /** The base string for this pattern. */
   24.24 +    private String base;
   24.25 +    /** The base number for this pattern. */
   24.26 +    private int bn;
   24.27 +    /** If true, add the domain number to the base number and append. */
   24.28 +    private boolean addDom;
   24.29 +    /** If true, append the domain number to the base string. */
   24.30 +    private boolean appendDom;
   24.31  
   24.32 -    if (len == 0) {
   24.33 -      result.base = "";
   24.34 -      result.bn = 0;
   24.35 -      result.addDom = false;
   24.36 -    } else if (ca[len - 1] == '+') {
   24.37 -      idx = len - 2;
   24.38 -      if ((idx >= 0) && (ca[idx] >= '0') && (ca[idx] <= '9')) {
   24.39 -	while ((idx >= 0) && (ca[idx] >= '0') && (ca[idx] <= '9')) {
   24.40 -	  idx --;
   24.41 -	}
   24.42 -	result.base = t.substring(0, idx + 1);
   24.43 -	result.bn = Integer.parseInt (t.substring (idx + 1, len - 1));
   24.44 -	result.addDom = true;
   24.45 -      } else {
   24.46 -	result.base = t.substring(0, len - 1);
   24.47 -	result.appendDom = true;
   24.48 -      }
   24.49 -    } else {
   24.50 -      result.base = t;
   24.51 +    /**
   24.52 +     * Parse a string into a pattern.
   24.53 +     * @param t The pattern string to parse.
   24.54 +     * @return A usable pattern object.
   24.55 +     */
   24.56 +    static StringPattern parse(String t) {
   24.57 +        StringPattern result = new StringPattern();
   24.58 +        char[] ca = t.toCharArray();
   24.59 +        int idx = 0;
   24.60 +        int len = ca.length;
   24.61 +
   24.62 +        if (len == 0) {
   24.63 +            result.base = "";
   24.64 +            result.bn = 0;
   24.65 +            result.addDom = false;
   24.66 +        } else if (ca[len - 1] == '+') {
   24.67 +            idx = len - 2;
   24.68 +            if ((idx >= 0) && (ca[idx] >= '0') && (ca[idx] <= '9')) {
   24.69 +                while ((idx >= 0) && (ca[idx] >= '0') && (ca[idx] <= '9')) {
   24.70 +                    idx--;
   24.71 +                }
   24.72 +                result.base = t.substring(0, idx + 1);
   24.73 +                result.bn = Integer.parseInt(t.substring(idx + 1, len - 1));
   24.74 +                result.addDom = true;
   24.75 +            } else {
   24.76 +                result.base = t.substring(0, len - 1);
   24.77 +                result.appendDom = true;
   24.78 +            }
   24.79 +        } else {
   24.80 +            result.base = t;
   24.81 +        }
   24.82 +
   24.83 +        return result;
   24.84      }
   24.85  
   24.86 -    return result;
   24.87 -  }
   24.88 +    /**
   24.89 +     * Resolve the pattern for the given domain number.
   24.90 +     * @param domain The domain number to use.
   24.91 +     * @return The expanded pattern for the given domain.
   24.92 +     */
   24.93 +    String resolve(int domain) {
   24.94 +        if (addDom) {
   24.95 +            return base + (bn + domain);
   24.96 +        } else if (appendDom) {
   24.97 +            return base + domain;
   24.98 +        } else {
   24.99 +            return base;
  24.100 +        }
  24.101 +    }
  24.102  
  24.103 -  public String resolve (int domain_id) {
  24.104 -    if (addDom) {
  24.105 -      return base + (bn + domain_id);
  24.106 -    } else if (appendDom) {
  24.107 -      return base + domain_id;
  24.108 -    } else {
  24.109 -      return base;
  24.110 +    /**
  24.111 +     * @see java.lang.Object#toString()
  24.112 +     */
  24.113 +    public String toString() {
  24.114 +        return (
  24.115 +            "["
  24.116 +                + base
  24.117 +                + (addDom ? "+" + bn : "")
  24.118 +                + ((addDom || appendDom) ? "+ID" : "")
  24.119 +                + "]");
  24.120      }
  24.121 -  }
  24.122 -
  24.123 -  public String toString () {
  24.124 -    return ("[" + 
  24.125 -	    base + 
  24.126 -	    (addDom ? "+" + bn : "") + 
  24.127 -	    ((addDom || appendDom) ? "+ID" : "") + 
  24.128 -	    "]");
  24.129 -  }
  24.130  
  24.131  }
    25.1 --- a/tools/control/src/org/xenoserver/control/VirtualBlockDevice.java	Tue Jul 08 11:23:42 2003 +0000
    25.2 +++ b/tools/control/src/org/xenoserver/control/VirtualBlockDevice.java	Tue Jul 08 11:30:20 2003 +0000
    25.3 @@ -7,55 +7,98 @@ package org.xenoserver.control;
    25.4  
    25.5  import java.io.PrintWriter;
    25.6  
    25.7 +/**
    25.8 + * A virtual block device; a mapping from a domain-specific number to a virtual
    25.9 + * disk with associated access mode.
   25.10 + */
   25.11  public class VirtualBlockDevice {
   25.12 -  String key;
   25.13 -  int domain;
   25.14 -  int vbdnum;
   25.15 -  Mode mode; /* rw or ro */
   25.16 -
   25.17 -  String dump(boolean title) {
   25.18 -    StringBuffer sb = new StringBuffer();
   25.19 +    /** The virtual disk which this block device maps onto. */
   25.20 +    private VirtualDisk vd;
   25.21 +    /** The domain in which this block device exists. */
   25.22 +    private int domain;
   25.23 +    /** The block device number in that domain. */
   25.24 +    private int vbdNum;
   25.25 +    /** The access mode within that domain. */
   25.26 +    private Mode mode;
   25.27  
   25.28 -    if (title) {
   25.29 -      sb.append("  key         dom vbd mode\n");
   25.30 -    } else {
   25.31 -      sb.append(
   25.32 -        "  "
   25.33 -          + key
   25.34 -          + "  "
   25.35 -          + Library.format(domain, 3, 0)
   25.36 -          + " "
   25.37 -          + Library.format(vbdnum, 3, 0)
   25.38 -          + " "
   25.39 -          + mode.toString()
   25.40 -          + "\n");
   25.41 +    /**
   25.42 +     * Constructor for VirtualBlockDevice.
   25.43 +     * @param vd The virtual disk to map to.
   25.44 +     * @param domain The domain to create the device in.
   25.45 +     * @param vbdNum The number for the device.
   25.46 +     * @param mode The access mode.
   25.47 +     */
   25.48 +    VirtualBlockDevice(
   25.49 +        VirtualDisk vd,
   25.50 +        int domain,
   25.51 +        int vbdNum,
   25.52 +        Mode mode) {
   25.53 +        this.vd = vd;
   25.54 +        this.domain = domain;
   25.55 +        this.vbdNum = vbdNum;
   25.56 +        this.mode = mode;
   25.57      }
   25.58  
   25.59 -    return sb.toString();
   25.60 -  }
   25.61 +    // TODO: get rid of this method
   25.62 +    String dump(boolean title) {
   25.63 +        StringBuffer sb = new StringBuffer();
   25.64  
   25.65 -  void dump_xml(PrintWriter out) {
   25.66 -    out.println("  <virtual_block_device>");
   25.67 -    out.println("    <key>" + key + "</key>");
   25.68 -    out.println("    <domain>" + domain + "</domain>");
   25.69 -    out.println("    <vbdnum>" + vbdnum + "</vbdnum>");
   25.70 -    out.println("    <mode>" + mode + "</mode>");
   25.71 -    out.println("  </virtual_block_device>");
   25.72 -  }
   25.73 +        if (title) {
   25.74 +            sb.append("  key         dom vbd mode\n");
   25.75 +        } else {
   25.76 +            sb.append(
   25.77 +                "  "
   25.78 +                    + vd.getKey()
   25.79 +                    + "  "
   25.80 +                    + Library.format(domain, 3, false)
   25.81 +                    + " "
   25.82 +                    + Library.format(vbdNum, 3, false)
   25.83 +                    + " "
   25.84 +                    + mode.toString()
   25.85 +                    + "\n");
   25.86 +        }
   25.87 +
   25.88 +        return sb.toString();
   25.89 +    }
   25.90  
   25.91 -  public String getKey() {
   25.92 -    return key;
   25.93 -  }
   25.94 +    /**
   25.95 +     * Dump the virtual block device as XML.
   25.96 +     * @param out The writer to dump to.
   25.97 +     */
   25.98 +    void dumpAsXML(PrintWriter out) {
   25.99 +        out.println("  <virtual_block_device>");
  25.100 +        out.println("    <key>" + vd.getKey() + "</key>");
  25.101 +        out.println("    <domain>" + domain + "</domain>");
  25.102 +        out.println("    <vbdnum>" + vbdNum + "</vbdnum>");
  25.103 +        out.println("    <mode>" + mode + "</mode>");
  25.104 +        out.println("  </virtual_block_device>");
  25.105 +    }
  25.106  
  25.107 -  public int getDomain() {
  25.108 -    return domain;
  25.109 -  }
  25.110 +    /**
  25.111 +     * @return This device's virtual disk.
  25.112 +     */
  25.113 +    public VirtualDisk getVirtualDisk() {
  25.114 +        return vd;
  25.115 +    }
  25.116  
  25.117 -  public int getVBDNum() {
  25.118 -    return vbdnum;
  25.119 -  }
  25.120 +    /**
  25.121 +     * @return The domain this device exists in.
  25.122 +     */
  25.123 +    public int getDomain() {
  25.124 +        return domain;
  25.125 +    }
  25.126  
  25.127 -  public Mode getMode() {
  25.128 -    return mode;
  25.129 -  }
  25.130 +    /**
  25.131 +     * @return The device number within its domain.
  25.132 +     */
  25.133 +    public int getVbdNum() {
  25.134 +        return vbdNum;
  25.135 +    }
  25.136 +
  25.137 +    /**
  25.138 +     * @return This device's access mode.
  25.139 +     */
  25.140 +    public Mode getMode() {
  25.141 +        return mode;
  25.142 +    }
  25.143  }
    26.1 --- a/tools/control/src/org/xenoserver/control/VirtualDisk.java	Tue Jul 08 11:23:42 2003 +0000
    26.2 +++ b/tools/control/src/org/xenoserver/control/VirtualDisk.java	Tue Jul 08 11:30:20 2003 +0000
    26.3 @@ -10,172 +10,224 @@ import java.util.Date;
    26.4  import java.util.Iterator;
    26.5  import java.util.Vector;
    26.6  
    26.7 +/**
    26.8 + * A single virtual disk. This may be used by multiple virtual block devices.
    26.9 + */
   26.10  public class VirtualDisk {
   26.11 -  String name;
   26.12 -  String key;
   26.13 -  Date expiry;
   26.14 -  Vector extents;
   26.15 +    /** The name of this virtual disk. */
   26.16 +    private String name;
   26.17 +    /** The key of this virtual disk (unique). */
   26.18 +    private String key;
   26.19 +    /** The expiry time of this virtual disk, or null for never. */
   26.20 +    private Date expiry;
   26.21 +    /** The extent list for this virtual disk. */
   26.22 +    private Vector extents;
   26.23  
   26.24 -  VirtualDisk(String name, Date expiry, String key) {
   26.25 -    this.name = name;
   26.26 -    if ( key == null )
   26.27 -      this.key = generate_key();
   26.28 -    else
   26.29 -      this.key = key;
   26.30 -    this.expiry = expiry;
   26.31 -    extents = new Vector();
   26.32 -  }
   26.33 +    /**
   26.34 +     * Construct a new virtual disk, specifying all parameters.
   26.35 +     * @param name Name of the new disk.
   26.36 +     * @param expiry Expiry time, or null for never.
   26.37 +     * @param key Key for the new disk, or null to autogenerate.
   26.38 +     */
   26.39 +    VirtualDisk(String name, Date expiry, String key) {
   26.40 +        this.name = name;
   26.41 +        if (key == null) {
   26.42 +            this.key = generateKey();
   26.43 +        } else {
   26.44 +            this.key = key;
   26.45 +        }
   26.46 +        this.expiry = expiry;
   26.47 +        extents = new Vector();
   26.48 +    }
   26.49  
   26.50 -  VirtualDisk(String name) {
   26.51 -    this(name, null, null);
   26.52 -  }
   26.53 -
   26.54 -  VirtualDisk(String name, Date expiry) {
   26.55 -    this(name, expiry, null);
   26.56 -  }
   26.57 +    /**
   26.58 +     * Construct a new virtual disk, with automatically generated key and no expiry.
   26.59 +     * @param name Name of the new disk.
   26.60 +     */
   26.61 +    VirtualDisk(String name) {
   26.62 +        this(name, null, null);
   26.63 +    }
   26.64  
   26.65 -  /*
   26.66 -   * generate a unique key for this virtual disk.
   26.67 -   * for now, just generate a 10 digit number
   26.68 -   */
   26.69 -  String generate_key() {
   26.70 -    return Long.toString(1000000000l + (long) (Math.random() * 8999999999l));
   26.71 -  }
   26.72 +    /**
   26.73 +     * Construct a new virtual disk, with automatically generated key.
   26.74 +     * @param name Name of the new disk.
   26.75 +     * @param expiry Expiry time, or null for never.
   26.76 +     */
   26.77 +    VirtualDisk(String name, Date expiry) {
   26.78 +        this(name, expiry, null);
   26.79 +    }
   26.80  
   26.81 -  void set_expiry(Date expiry) {
   26.82 -    this.expiry = expiry;
   26.83 -  }
   26.84 +    /**
   26.85 +     * Generate a unique key for this virtual disk.
   26.86 +     * For now, just generate a 10 digit number.
   26.87 +     * @return A unique key.
   26.88 +     */
   26.89 +    private static String generateKey() {
   26.90 +        return Long.toString(
   26.91 +            1000000000L + (long) (Math.random() * 8999999999L));
   26.92 +    }
   26.93  
   26.94 -  public void add_extent(Extent extent) {
   26.95 -    extents.add(extent);
   26.96 -  }
   26.97 +    /**
   26.98 +     * Add an extent to this disk.
   26.99 +     * @param extent The extent to add.
  26.100 +     */
  26.101 +    void addExtent(Extent extent) {
  26.102 +        extents.add(extent);
  26.103 +    }
  26.104  
  26.105 -  public Extent remove_extent() {
  26.106 -    Extent e;
  26.107 +    /**
  26.108 +     * Remove the first extent from this disk.
  26.109 +     * @return The extent removed, or null if there are no extents.
  26.110 +     */
  26.111 +    Extent removeExtent() {
  26.112 +        Extent e;
  26.113  
  26.114 -    if (extents.size() > 0) {
  26.115 -      e = (Extent) extents.remove(0);
  26.116 -    } else {
  26.117 -      e = null;
  26.118 +        if (extents.size() > 0) {
  26.119 +            e = (Extent) extents.remove(0);
  26.120 +        } else {
  26.121 +            e = null;
  26.122 +        }
  26.123 +
  26.124 +        return e;
  26.125      }
  26.126  
  26.127 -    return e;
  26.128 -  }
  26.129 -
  26.130 -  String dump_xen(VirtualBlockDevice vbd) {
  26.131 -    StringBuffer sb = new StringBuffer();
  26.132 -
  26.133 -    sb.append(
  26.134 -      "domain:"
  26.135 -        + vbd.domain
  26.136 -        + " "
  26.137 -        + vbd.mode.toString()
  26.138 -        + " "
  26.139 -        + "segment:"
  26.140 -        + vbd.vbdnum
  26.141 -        + " "
  26.142 -        + "extents:"
  26.143 -        + extents.size()
  26.144 -        + " ");
  26.145 -    for (int loop = 0; loop < extents.size(); loop++) {
  26.146 -      Extent e = (Extent) extents.get(loop);
  26.147 -      sb.append(
  26.148 -        "(disk:"
  26.149 -          + e.disk
  26.150 -          + " "
  26.151 -          + "offset:"
  26.152 -          + e.offset
  26.153 -          + " "
  26.154 -          + "size:"
  26.155 -          + e.size
  26.156 -          + ")");
  26.157 -    }
  26.158 -    return sb.toString();
  26.159 -  }
  26.160 +    /**
  26.161 +     * Form a string suitable for passing into the XenoLinux proc interface mapping
  26.162 +     * the given VBD to this virtual disk.
  26.163 +     * @param vbd The virtual block device to map.
  26.164 +     * @return A XenoLinux /proc string.
  26.165 +     */
  26.166 +    String dumpForXen(VirtualBlockDevice vbd) {
  26.167 +        StringBuffer sb = new StringBuffer();
  26.168  
  26.169 -  void dump_xml(PrintWriter out) {
  26.170 -    out.println("  <virtual_disk>");
  26.171 -    out.println("    <name>" + name + "</name>");
  26.172 -    out.println("    <key>" + key + "</key>");
  26.173 -    if (expiry == null) {
  26.174 -      out.println("    <expiry>0</expiry>");
  26.175 -    } else {
  26.176 -      out.println("    <expiry>" + expiry.getTime() + "</expiry>");
  26.177 +        sb.append(
  26.178 +            "domain:"
  26.179 +                + vbd.getDomain()
  26.180 +                + " "
  26.181 +                + vbd.getMode().toString()
  26.182 +                + " "
  26.183 +                + "segment:"
  26.184 +                + vbd.getVbdNum()
  26.185 +                + " "
  26.186 +                + "extents:"
  26.187 +                + extents.size()
  26.188 +                + " ");
  26.189 +        for (int loop = 0; loop < extents.size(); loop++) {
  26.190 +            Extent e = (Extent) extents.get(loop);
  26.191 +            sb.append(
  26.192 +                "(disk:"
  26.193 +                    + e.getDisk()
  26.194 +                    + " "
  26.195 +                    + "offset:"
  26.196 +                    + e.getOffset()
  26.197 +                    + " "
  26.198 +                    + "size:"
  26.199 +                    + e.getSize()
  26.200 +                    + ")");
  26.201 +        }
  26.202 +        return sb.toString();
  26.203      }
  26.204 -    out.println("    <extents>");
  26.205 -    for (int loop = 0; loop < extents.size(); loop++) {
  26.206 -      Extent e = (Extent) extents.get(loop);
  26.207 -      out.println("      <extent>");
  26.208 -      out.println("        <disk>" + e.disk + "</disk>");
  26.209 -      out.println("        <size>" + e.size + "</size>");
  26.210 -      out.println("        <offset>" + e.offset + "</offset>");
  26.211 -      out.println("      </extent>");
  26.212 -    }
  26.213 -    out.println("    </extents>");
  26.214 -    out.println("  </virtual_disk>");
  26.215  
  26.216 -    return;
  26.217 -  }
  26.218 -
  26.219 -  /*
  26.220 -   * Add a partition as a XenoPartition.
  26.221 -   * Chop the partition in to extents and of size "size" sectors
  26.222 -   * and add them to the virtual disk.
  26.223 -   */
  26.224 +    /**
  26.225 +     * Dump the virtual disk as XML.
  26.226 +     * @param out The writer to dump to.
  26.227 +     */
  26.228 +    void dumpAsXML(PrintWriter out) {
  26.229 +        out.println("  <virtual_disk>");
  26.230 +        out.println("    <name>" + name + "</name>");
  26.231 +        out.println("    <key>" + key + "</key>");
  26.232 +        if (expiry == null) {
  26.233 +            out.println("    <expiry>0</expiry>");
  26.234 +        } else {
  26.235 +            out.println("    <expiry>" + expiry.getTime() + "</expiry>");
  26.236 +        }
  26.237 +        out.println("    <extents>");
  26.238 +        for (int loop = 0; loop < extents.size(); loop++) {
  26.239 +            Extent e = (Extent) extents.get(loop);
  26.240 +            out.println("      <extent>");
  26.241 +            out.println("        <disk>" + e.getDisk() + "</disk>");
  26.242 +            out.println("        <size>" + e.getSize() + "</size>");
  26.243 +            out.println("        <offset>" + e.getOffset() + "</offset>");
  26.244 +            out.println("      </extent>");
  26.245 +        }
  26.246 +        out.println("    </extents>");
  26.247 +        out.println("  </virtual_disk>");
  26.248  
  26.249 -  void add_new_partition(Partition partition, long size) {
  26.250 -    int loop;
  26.251 -
  26.252 -    for (loop = 0; loop < partition.nr_sects / size; loop++) {
  26.253 -      Extent extent = new Extent();
  26.254 -
  26.255 -      extent.disk = partition.major << 8;
  26.256 -      extent.disk = extent.disk | (partition.minor >> 5) << 5;
  26.257 -      extent.size = size;
  26.258 -      extent.offset = partition.start_sect + (size * loop);
  26.259 -
  26.260 -      add_extent(extent);
  26.261 +        return;
  26.262      }
  26.263  
  26.264 -    return;
  26.265 -  }
  26.266 +    /**
  26.267 +     * Add a partition as a XenoPartition.
  26.268 +     * Chop the partition in to extents and add them to this virtual disk.
  26.269 +     * @param partition The partition to add.
  26.270 +     * @param extentSize The number of sectors to use for each extent. 
  26.271 +     */
  26.272 +    void addPartition(Partition partition, long extentSize) {
  26.273 +        int loop;
  26.274  
  26.275 -  public String getName() {
  26.276 -    return name;
  26.277 -  }
  26.278 +        for (loop = 0; loop < partition.getNumSects() / extentSize; loop++) {
  26.279 +            Extent extent =
  26.280 +                new Extent(
  26.281 +                    partition.getDisk(),
  26.282 +                    extentSize,
  26.283 +                    partition.getStartSect() + (extentSize * loop));
  26.284  
  26.285 -  public String getKey() {
  26.286 -    return key;
  26.287 -  }
  26.288 +            addExtent(extent);
  26.289 +        }
  26.290 +    }
  26.291  
  26.292 -  public Date getExpiry() {
  26.293 -    return expiry;
  26.294 -  }
  26.295 +    /**
  26.296 +     * @return The name of this virtual disk.
  26.297 +     */
  26.298 +    public String getName() {
  26.299 +        return name;
  26.300 +    }
  26.301  
  26.302 -  public int getExtentCount() {
  26.303 -    return extents.size();
  26.304 -  }
  26.305 +    /**
  26.306 +     * @return The key of this virtual disk.
  26.307 +     */
  26.308 +    public String getKey() {
  26.309 +        return key;
  26.310 +    }
  26.311  
  26.312 -  public Extent getExtent(int index) {
  26.313 -    return (Extent) extents.get(index);
  26.314 -  }
  26.315 +    /**
  26.316 +     * @return The expiry time of this virtual disk.
  26.317 +     */
  26.318 +    public Date getExpiry() {
  26.319 +        return expiry;
  26.320 +    }
  26.321 +
  26.322 +    /**
  26.323 +     * @return The number of extents in this virtual disk.
  26.324 +     */
  26.325 +    public int getExtentCount() {
  26.326 +        return extents.size();
  26.327 +    }
  26.328  
  26.329 -  /**
  26.330 -   * @return Total size of this virtual disk in sectors.
  26.331 -   */
  26.332 -  public long getSize() {
  26.333 -    long size = 0;
  26.334 -    Iterator i = extents.iterator();
  26.335 -    while ( i.hasNext() ) {
  26.336 -      size += ((Extent) i.next()).getSize();
  26.337 +    /**
  26.338 +     * @return Total size of this virtual disk in sectors.
  26.339 +     */
  26.340 +    public long getSize() {
  26.341 +        long size = 0;
  26.342 +        Iterator i = extents.iterator();
  26.343 +        while (i.hasNext()) {
  26.344 +            size += ((Extent) i.next()).getSize();
  26.345 +        }
  26.346 +        return size;
  26.347      }
  26.348 -    return size;
  26.349 -  }
  26.350 -  
  26.351 -  /**
  26.352 -   * @return An iterator over all extents in the disk.
  26.353 -   */
  26.354 -  public Iterator iterator() {
  26.355 -    return extents.iterator();
  26.356 -  }
  26.357 +
  26.358 +    /**
  26.359 +     * @return An iterator over all extents in the disk.
  26.360 +     */
  26.361 +    public Iterator extents() {
  26.362 +        return extents.iterator();
  26.363 +    }
  26.364 +
  26.365 +    /**
  26.366 +     * Reset the expiry time for this virtual disk.
  26.367 +     * @param expiry The new expiry time, or null for never.
  26.368 +     */
  26.369 +    public void refreshExpiry(Date expiry) {
  26.370 +        this.expiry = expiry;
  26.371 +    }
  26.372  }
    27.1 --- a/tools/control/src/org/xenoserver/control/VirtualDiskManager.java	Tue Jul 08 11:23:42 2003 +0000
    27.2 +++ b/tools/control/src/org/xenoserver/control/VirtualDiskManager.java	Tue Jul 08 11:30:20 2003 +0000
    27.3 @@ -7,230 +7,216 @@ package org.xenoserver.control;
    27.4  
    27.5  import java.io.PrintWriter;
    27.6  import java.util.Date;
    27.7 -import java.util.Enumeration;
    27.8 -import java.util.Hashtable;
    27.9 -import java.util.Vector;
   27.10 +import java.util.Iterator;
   27.11 +import java.util.LinkedHashMap;
   27.12  
   27.13  /**
   27.14   * VirtualDiskManager manages the list of virtual disks on the machine. It is
   27.15   * a Singleton which automatically initialises itself on first class reference.
   27.16   */
   27.17  public class VirtualDiskManager {
   27.18 -  public static final VirtualDiskManager it = new VirtualDiskManager();
   27.19 -  VirtualDisk free_disk;
   27.20 -  Vector virtual_disks;
   27.21 -  Hashtable virtual_block_devices;
   27.22 -  Hashtable key_hash;
   27.23 -
   27.24 -  private VirtualDiskManager() {
   27.25 -    free_disk = new VirtualDisk("free");
   27.26 +    /** The single VDM reference. */
   27.27 +    public static final VirtualDiskManager IT = new VirtualDiskManager();
   27.28 +    /** The free-space disk. */
   27.29 +    private VirtualDisk freeDisk;
   27.30 +    /** The map of keys to virtual disks. */
   27.31 +    private LinkedHashMap virtualDisks = new LinkedHashMap(100);
   27.32 +    /** The map of (domain,vbdnum) to virtual block devices. */
   27.33 +    private LinkedHashMap virtualBlockDevices = new LinkedHashMap(100);
   27.34  
   27.35 -    virtual_disks = new Vector(10, 5);
   27.36 -    flush_virtual_block_devices();
   27.37 -    key_hash = new Hashtable(100);
   27.38 -  }
   27.39 -
   27.40 -  public VirtualDisk get_virtual_disk_key(String key) {
   27.41 -    return ((VirtualDisk) key_hash.get(key));
   27.42 -  }
   27.43 +    /**
   27.44 +     * VDM constructor, private as it's a singleton.
   27.45 +     */
   27.46 +    private VirtualDiskManager() {
   27.47 +        freeDisk = new VirtualDisk("free");
   27.48 +    }
   27.49  
   27.50 -  public void add_xeno_partition(Partition partition, long size) {
   27.51 -    free_disk.add_new_partition(partition, size);
   27.52 -    return;
   27.53 -  }
   27.54 -
   27.55 -  /*
   27.56 -   * create a new virtual disk
   27.57 -   */
   27.58 -
   27.59 -  public VirtualDisk create_virtual_disk(String name, long size, Date expiry) {
   27.60 -    VirtualDisk vd = new VirtualDisk(name, expiry);
   27.61 +    /**
   27.62 +     * Get the virtual disk with the specified key.
   27.63 +     * @param key The key to look for.
   27.64 +     * @return The virtual disk, or null if not found.
   27.65 +     */
   27.66 +    public VirtualDisk getVirtualDisk(String key) {
   27.67 +        return ((VirtualDisk) virtualDisks.get(key));
   27.68 +    }
   27.69  
   27.70 -    if ( free_disk.getSize() < size )
   27.71 -      return null;
   27.72 -    
   27.73 -    while (size > 0) {
   27.74 -      Extent e;
   27.75 -
   27.76 -      e = free_disk.remove_extent();
   27.77 -      if (e == null) {
   27.78 -        return null;
   27.79 -      }
   27.80 -      size -= e.size;
   27.81 -      vd.add_extent(e);
   27.82 +    /**
   27.83 +     * Add a new partition to the free space list in the disk manager.
   27.84 +     * @param partition The partition to add.
   27.85 +     * @param chunkSize The chunk size to split the partition into, in sectors. 
   27.86 +     */
   27.87 +    public void addPartition(Partition partition, long chunkSize) {
   27.88 +        freeDisk.addPartition(partition, chunkSize);
   27.89      }
   27.90  
   27.91 -    add_virtual_disk(vd);
   27.92 -
   27.93 -    return vd;
   27.94 -  }
   27.95 +    /**
   27.96 +     * Create a new virtual disk.
   27.97 +     * @param name The disk name to use.
   27.98 +     * @param size The number of sectors to allocate.
   27.99 +     * @param expiry The expiry time, or null for never.
  27.100 +     * @return null if not enough space is available
  27.101 +     */
  27.102 +    public VirtualDisk createVirtualDisk(String name, long size, Date expiry) {
  27.103 +        if (freeDisk.getSize() < size) {
  27.104 +            return null;
  27.105 +        }
  27.106  
  27.107 -  /*
  27.108 -   * delete a new virtual disk.  extents go back into the free pool
  27.109 -   */
  27.110 -
  27.111 -  public void delete_virtual_disk(String key) {
  27.112 -    VirtualDisk vd;
  27.113 +        VirtualDisk vd = new VirtualDisk(name, expiry);
  27.114  
  27.115 -    vd = (VirtualDisk) key_hash.get(key);
  27.116 -    if (vd != null) {
  27.117 -      Extent e;
  27.118 +        while (size > 0) {
  27.119 +            Extent e;
  27.120  
  27.121 -      key_hash.remove(key);
  27.122 -      virtual_disks.remove(vd);
  27.123 +            e = freeDisk.removeExtent();
  27.124 +            if (e == null) {
  27.125 +                return null;
  27.126 +            }
  27.127 +            size -= e.getSize();
  27.128 +            vd.addExtent(e);
  27.129 +        }
  27.130  
  27.131 -      e = vd.remove_extent();
  27.132 -      while (e != null) {
  27.133 -        free_disk.add_extent(e);
  27.134 -        e = vd.remove_extent();
  27.135 -      }
  27.136 +        insertVirtualDisk(vd);
  27.137 +
  27.138 +        return vd;
  27.139      }
  27.140 -    return;
  27.141 -  }
  27.142  
  27.143 -  /*
  27.144 -   * reset the expiry time for a virtual disk
  27.145 -   */
  27.146 -
  27.147 -  public void refresh_virtual_disk(String key, Date expiry) {
  27.148 -    VirtualDisk vd = (VirtualDisk) key_hash.get(key);
  27.149 -    if (vd != null) {
  27.150 -      vd.set_expiry(expiry);
  27.151 -    }
  27.152 -  }
  27.153 +    /**
  27.154 +     * Delete a virtual disk, and put its extents back into the free pool.
  27.155 +     * @param key The key of the disk to delete.
  27.156 +     */
  27.157 +    public void deleteVirtualDisk(String key) {
  27.158 +        VirtualDisk vd;
  27.159  
  27.160 -  /*
  27.161 -   * create a new virtual block device
  27.162 -   */
  27.163 -  public VirtualBlockDevice create_virtual_block_device(
  27.164 -    String key,
  27.165 -    int domain,
  27.166 -    int vbd_num,
  27.167 -    String mode) {
  27.168 -    VirtualBlockDevice vbd = new VirtualBlockDevice();
  27.169 -    VirtualDisk vd = get_virtual_disk_key(key);
  27.170 +        vd = (VirtualDisk) virtualDisks.get(key);
  27.171 +        if (vd != null) {
  27.172 +            Extent e;
  27.173 +
  27.174 +            virtualDisks.remove(key);
  27.175  
  27.176 -    if (vd == null) {
  27.177 -      System.err.println(
  27.178 -        "create virtual block device error: unknown key " + "[" + key + "]");
  27.179 -      return null;
  27.180 +            e = vd.removeExtent();
  27.181 +            while (e != null) {
  27.182 +                freeDisk.addExtent(e);
  27.183 +                e = vd.removeExtent();
  27.184 +            }
  27.185 +        }
  27.186      }
  27.187  
  27.188 -    vbd.key = key;
  27.189 -    vbd.domain = domain;
  27.190 -    vbd.vbdnum = vbd_num;
  27.191 +    /**
  27.192 +     * Create a new virtual block device.
  27.193 +     * @param vd The virtual disk to expose.
  27.194 +     * @param domain The domain to create the device for.
  27.195 +     * @param vbdNum The block device number to use.
  27.196 +     * @param mode The mode to create the device with.
  27.197 +     * @return The newly created virtual block device.
  27.198 +     */
  27.199 +    public VirtualBlockDevice createVirtualBlockDevice(
  27.200 +        VirtualDisk vd,
  27.201 +        int domain,
  27.202 +        int vbdNum,
  27.203 +        Mode mode) {
  27.204 +        VirtualBlockDevice vbd =
  27.205 +            new VirtualBlockDevice(vd, domain, vbdNum, mode);
  27.206 +
  27.207 +        insertVirtualBlockDevice(vbd);
  27.208 +
  27.209 +        return vbd;
  27.210 +    }
  27.211  
  27.212 -    if (mode.equals(Mode.READ_ONLY.toString())
  27.213 -      || mode.equals("RO")
  27.214 -      || mode.equals("ro")) {
  27.215 -      vbd.mode = Mode.READ_ONLY;
  27.216 -    } else if (
  27.217 -      mode.equals(Mode.READ_WRITE.toString())
  27.218 -        || mode.equals("RW")
  27.219 -        || mode.equals("rw")) {
  27.220 -      vbd.mode = Mode.READ_WRITE;
  27.221 -    } else {
  27.222 -      System.err.println(
  27.223 -        "create virtual block device error: unknown mode " + "[" + mode + "]");
  27.224 -      return null;
  27.225 +    /**
  27.226 +     * Delete a virtual block device.
  27.227 +     * @param domain Domain owning the device.
  27.228 +     * @param vbdNum The vbd number within the domain.
  27.229 +     */
  27.230 +    public void deleteVirtualBlockDevice(int domain, int vbdNum) {
  27.231 +        Object hash = hashVBD(domain, vbdNum);
  27.232 +        virtualBlockDevices.remove(hash);
  27.233 +    }
  27.234 +
  27.235 +    /**
  27.236 +     * Flush all virtual block devices.
  27.237 +     */
  27.238 +    public void flushVirtualBlockDevices() {
  27.239 +        /* isn't automatic garbage collection wonderful? */
  27.240 +        virtualBlockDevices = new LinkedHashMap(100);
  27.241 +    }
  27.242 +
  27.243 +    /**
  27.244 +     * Insert a new virtual disk into the map.
  27.245 +     * @param vd The disk to insert.
  27.246 +     */
  27.247 +    void insertVirtualDisk(VirtualDisk vd) {
  27.248 +        virtualDisks.put(vd.getKey(), vd);
  27.249 +    }
  27.250 +
  27.251 +    /**
  27.252 +     * Insert a new virtual block device into the map.
  27.253 +     * @param vbd The device to insert.
  27.254 +     */
  27.255 +    void insertVirtualBlockDevice(VirtualBlockDevice vbd) {
  27.256 +        Object hash = hashVBD(vbd.getDomain(), vbd.getVbdNum());
  27.257 +        virtualBlockDevices.put(hash, vbd);
  27.258      }
  27.259  
  27.260 -    add_virtual_block_device(vbd);
  27.261 -
  27.262 -    return vbd;
  27.263 -  }
  27.264 -
  27.265 -  /*
  27.266 -   * delete a virtual block device 
  27.267 -   */
  27.268 -  public void delete_virtual_block_device(int domain, int vbd_num) {
  27.269 -    Object hash = get_vbd_hash(domain, vbd_num);
  27.270 -    virtual_block_devices.remove(hash);
  27.271 -  }
  27.272 -
  27.273 -  /*
  27.274 -   * flush all virtual block devices
  27.275 -   */
  27.276 -  public void flush_virtual_block_devices() {
  27.277 -    /* isn't automatic garbage collection wonderful? */
  27.278 -    virtual_block_devices = new Hashtable(100);
  27.279 -  }
  27.280 -
  27.281 -  public void add_virtual_disk(VirtualDisk vd) {
  27.282 -    virtual_disks.add(vd);
  27.283 -    key_hash.put(vd.getKey(), vd);
  27.284 -  }
  27.285 +    /**
  27.286 +     * Hash a virtual block device.
  27.287 +     * @param domain The VBD's domain.
  27.288 +     * @param vbdNum The VBD's number within the domain.
  27.289 +     * @return A suitable hash key.
  27.290 +     */
  27.291 +    Object hashVBD(int domain, int vbdNum) {
  27.292 +        return new Integer(domain * 16 + vbdNum);
  27.293 +    }
  27.294  
  27.295 -  public void add_virtual_block_device(VirtualBlockDevice vbd) {
  27.296 -    Object hash = get_vbd_hash(vbd.domain, vbd.vbdnum);
  27.297 -    virtual_block_devices.put(hash, vbd);
  27.298 -  }
  27.299 -
  27.300 -  Object get_vbd_hash(int domain, int vbd_num) {
  27.301 -    return new Integer(domain * 16 + vbd_num);
  27.302 -  }
  27.303 -
  27.304 -  public void add_free(VirtualDisk vd) {
  27.305 -    free_disk = vd;
  27.306 -  }
  27.307 -
  27.308 -  public String dump_virtualblockdevices() {
  27.309 -    StringBuffer sb = new StringBuffer();
  27.310 -    boolean first = true;
  27.311 -
  27.312 -    for (Enumeration enumeration = virtual_block_devices.elements();
  27.313 -      enumeration.hasMoreElements();
  27.314 -      ) {
  27.315 -      VirtualBlockDevice vbd = (VirtualBlockDevice) enumeration.nextElement();
  27.316 -      if (first) {
  27.317 -        sb.append(vbd.dump(true));
  27.318 -        first = false;
  27.319 -      }
  27.320 -
  27.321 -      sb.append(vbd.dump(false));
  27.322 +    /**
  27.323 +     * Set a new free disk.
  27.324 +     * @param vd The free disk to set.
  27.325 +     */
  27.326 +    void setFreeDisk(VirtualDisk vd) {
  27.327 +        freeDisk = vd;
  27.328      }
  27.329  
  27.330 -    return sb.toString();
  27.331 -  }
  27.332 +    /**
  27.333 +     * Dump the data in the VirtualDiskManager in XML form.
  27.334 +     * @param out The output writer to dump to.
  27.335 +     */
  27.336 +    void dumpAsXML(PrintWriter out) {
  27.337 +        out.println("<free>");
  27.338 +        freeDisk.dumpAsXML(out);
  27.339 +        out.println("</free>");
  27.340 +        out.println("<virtual_disks>");
  27.341 +        Iterator i = virtualDisks.values().iterator();
  27.342 +        while (i.hasNext()) {
  27.343 +            VirtualDisk vd = (VirtualDisk) i.next();
  27.344 +            vd.dumpAsXML(out);
  27.345 +        }
  27.346 +        out.println("</virtual_disks>");
  27.347 +        out.println("<virtual_block_devices>");
  27.348 +        i = virtualBlockDevices.values().iterator();
  27.349 +        while (i.hasNext()) {
  27.350 +            VirtualBlockDevice vbd = (VirtualBlockDevice) i.next();
  27.351 +            vbd.dumpAsXML(out);
  27.352 +        }
  27.353  
  27.354 -  public void dump_xml(PrintWriter out) {
  27.355 -    out.println("<free>");
  27.356 -    free_disk.dump_xml(out);
  27.357 -    out.println("</free>");
  27.358 -    out.println("<virtual_disks>");
  27.359 -    for (int i = 0; i < virtual_disks.size(); i++) {
  27.360 -      VirtualDisk vd = (VirtualDisk) virtual_disks.get(i);
  27.361 -      vd.dump_xml(out);
  27.362 -    }
  27.363 -    out.println("</virtual_disks>");
  27.364 -    out.println("<virtual_block_devices>");
  27.365 -    for (Enumeration enumeration = virtual_block_devices.elements();
  27.366 -      enumeration.hasMoreElements();
  27.367 -      ) {
  27.368 -      VirtualBlockDevice vbd = (VirtualBlockDevice) enumeration.nextElement();
  27.369 -      vbd.dump_xml(out);
  27.370 +        out.println("</virtual_block_devices>");
  27.371      }
  27.372  
  27.373 -    out.println("</virtual_block_devices>");
  27.374 -
  27.375 -    return;
  27.376 -  }
  27.377 -
  27.378 -  /*************************************************************************/
  27.379 -
  27.380 -  public int getVirtualDiskCount() {
  27.381 -    return virtual_disks.size();
  27.382 -  }
  27.383 +    /**
  27.384 +     * @return The free disk.
  27.385 +     */
  27.386 +    public VirtualDisk getFreeDisk() {
  27.387 +        return freeDisk;
  27.388 +    }
  27.389  
  27.390 -  public VirtualDisk getVirtualDisk(int index) {
  27.391 -    return (VirtualDisk) virtual_disks.get(index);
  27.392 -  }
  27.393 +    /**
  27.394 +     * @return An iterator over the virtual block devices.
  27.395 +     */
  27.396 +    public Iterator getVirtualBlockDevices() {
  27.397 +        return virtualBlockDevices.values().iterator();
  27.398 +    }
  27.399  
  27.400 -  public VirtualDisk getFreeVirtualDisk() {
  27.401 -    return free_disk;
  27.402 -  }
  27.403 -
  27.404 -  public Enumeration getVirtualBlockDevices() {
  27.405 -    return virtual_block_devices.elements();
  27.406 -  }
  27.407 +    /**
  27.408 +     * @return An iterator over the virtual disks.
  27.409 +     */
  27.410 +    public Iterator getVirtualDisks() {
  27.411 +        return virtualDisks.values().iterator();
  27.412 +    }
  27.413  }
    28.1 --- a/tools/control/src/org/xenoserver/control/XML.java	Tue Jul 08 11:23:42 2003 +0000
    28.2 +++ b/tools/control/src/org/xenoserver/control/XML.java	Tue Jul 08 11:30:20 2003 +0000
    28.3 @@ -19,107 +19,102 @@ import javax.xml.parsers.ParserConfigura
    28.4  import org.w3c.dom.Document;
    28.5  import org.xml.sax.SAXException;
    28.6  import org.xml.sax.SAXParseException;
    28.7 - 
    28.8 -public class
    28.9 -XML
   28.10 -{
   28.11 -  static Document document = null;
   28.12  
   28.13 -  /*
   28.14 -   * dump partition manager and virtual disk manager state to filename
   28.15 -   */
   28.16 -
   28.17 -  public static void
   28.18 -  dump_state (PartitionManager pm, VirtualDiskManager vdm, String filename)
   28.19 -  {
   28.20 -    PrintWriter out;
   28.21 +/**
   28.22 + * Utility class for loading and saving state through XML files.
   28.23 + */
   28.24 +public class XML {
   28.25 +    /** The document we are reading/writing. */
   28.26 +    private static Document document = null;
   28.27  
   28.28 -    try
   28.29 -    {
   28.30 -      out = new PrintWriter(new BufferedWriter(new FileWriter(filename)));
   28.31 -    }
   28.32 -    catch (IOException e)
   28.33 -    {
   28.34 -      System.err.println ("XML.dump_state error [" + filename + "]");
   28.35 -      System.err.println (e);
   28.36 -      return;
   28.37 -    }
   28.38 +    /**
   28.39 +     * Save the state of the managers to the configuration file given.
   28.40 +     * @param pm The PartitionManager to save.
   28.41 +     * @param vdm The VirtualDiskManager to save.
   28.42 +     * @param filename The configuration filename to save to.
   28.43 +     */
   28.44 +    public static void saveState(
   28.45 +        PartitionManager pm,
   28.46 +        VirtualDiskManager vdm,
   28.47 +        String filename) {
   28.48 +        PrintWriter out;
   28.49  
   28.50 -    out.println("<?xml version=\"1.0\"?>");
   28.51 -    out.println("<vdmanager>");
   28.52 -    pm.dump_xml(out);
   28.53 -    vdm.dump_xml(out);
   28.54 -    out.println("</vdmanager>");
   28.55 -
   28.56 -    out.close();
   28.57 -    return;
   28.58 -  }
   28.59 +        try {
   28.60 +            out = new PrintWriter(new BufferedWriter(new FileWriter(filename)));
   28.61 +        } catch (IOException e) {
   28.62 +            System.err.println("XML.dump_state error [" + filename + "]");
   28.63 +            System.err.println(e);
   28.64 +            return;
   28.65 +        }
   28.66  
   28.67 -  /*
   28.68 -   * load partition manager and virtual disk manager state from filename
   28.69 -   */
   28.70 -  public static void
   28.71 -  load_state (PartitionManager pm, VirtualDiskManager vdm, String filename)
   28.72 -  {
   28.73 -    if (document == null)
   28.74 -    {
   28.75 -      load_file (filename);
   28.76 +        out.println("<?xml version=\"1.0\"?>");
   28.77 +        out.println("<vdmanager>");
   28.78 +        pm.dumpAsXML(out);
   28.79 +        vdm.dumpAsXML(out);
   28.80 +        out.println("</vdmanager>");
   28.81 +
   28.82 +        out.close();
   28.83 +        return;
   28.84      }
   28.85  
   28.86 -    XMLHelper.parse(pm, vdm, document);
   28.87 -  }
   28.88 -
   28.89 -  /*
   28.90 -   * load XML from disk
   28.91 -   */
   28.92 -  static void
   28.93 -  load_file (String filename)
   28.94 -  {
   28.95 -    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
   28.96 -    // factory.setNamespaceAware(true);
   28.97 -    // factory.setValidating(true);
   28.98 +    /**
   28.99 +     * Load the state of the managers from the given configuration file.
  28.100 +     * @param pm The PartitionManager to load.
  28.101 +     * @param vdm The VirtualDomainManager to load.
  28.102 +     * @param filename The filename to read the configuration from.
  28.103 +     */
  28.104 +    public static void loadState(
  28.105 +        PartitionManager pm,
  28.106 +        VirtualDiskManager vdm,
  28.107 +        String filename) {
  28.108 +        if (document == null) {
  28.109 +            loadFile(filename);
  28.110 +        }
  28.111  
  28.112 -    try
  28.113 -    {
  28.114 -      File file = new File(filename);
  28.115 -
  28.116 -      DocumentBuilder builder = factory.newDocumentBuilder();
  28.117 -      document = builder.parse(file);
  28.118 +        XMLHelper.parse(pm, vdm, document);
  28.119      }
  28.120 -    catch (SAXParseException spe)               /* error generated by parser */
  28.121 -    {
  28.122 -      System.err.println ("xml parser exception on line " + 
  28.123 -			  spe.getLineNumber() + 
  28.124 -			  " for uri " + spe.getSystemId());
  28.125 -      System.err.println (spe.getMessage());
  28.126 +
  28.127 +    /**
  28.128 +     * Load an XML file and parse it into a Document.
  28.129 +     * @param filename The file to load.
  28.130 +     */
  28.131 +    private static void loadFile(String filename) {
  28.132 +        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  28.133 +
  28.134 +        try {
  28.135 +            File file = new File(filename);
  28.136  
  28.137 -      Exception x = spe;
  28.138 -      if (spe.getException() != null)
  28.139 -	x = spe.getException();
  28.140 -      x.printStackTrace();
  28.141 -      System.exit(1);
  28.142 -    }
  28.143 -    catch (SAXException sxe)
  28.144 -    {
  28.145 -      Exception e = sxe;
  28.146 -      if (sxe.getException() != null)
  28.147 -	e = sxe.getException();
  28.148 -      e.printStackTrace();
  28.149 -      System.exit(1);
  28.150 +            DocumentBuilder builder = factory.newDocumentBuilder();
  28.151 +            document = builder.parse(file);
  28.152 +        } catch (SAXParseException spe) /* error generated by parser */ {
  28.153 +            System.err.println(
  28.154 +                "xml parser exception on line "
  28.155 +                    + spe.getLineNumber()
  28.156 +                    + " for uri "
  28.157 +                    + spe.getSystemId());
  28.158 +            System.err.println(spe.getMessage());
  28.159 +
  28.160 +            Exception x = spe;
  28.161 +            if (spe.getException() != null) {
  28.162 +                x = spe.getException();
  28.163 +            }
  28.164 +            x.printStackTrace();
  28.165 +            System.exit(1);
  28.166 +        } catch (SAXException sxe) {
  28.167 +            Exception e = sxe;
  28.168 +            if (sxe.getException() != null) {
  28.169 +                e = sxe.getException();
  28.170 +            }
  28.171 +            e.printStackTrace();
  28.172 +            System.exit(1);
  28.173 +        } catch (ParserConfigurationException pce) {
  28.174 +            pce.printStackTrace();
  28.175 +        } catch (FileNotFoundException fnfe) {
  28.176 +            System.err.println(
  28.177 +                "warning: state file not found [" + filename + "]");
  28.178 +        } catch (IOException ioe) {
  28.179 +            ioe.printStackTrace();
  28.180 +        }
  28.181 +        return;
  28.182      }
  28.183 -    catch (ParserConfigurationException pce)
  28.184 -    {
  28.185 -      pce.printStackTrace();
  28.186 -    }
  28.187 -    catch (FileNotFoundException fnfe)
  28.188 -    {
  28.189 -      System.err.println ("warning: state file not found [" +
  28.190 -			  filename + "]");
  28.191 -    }
  28.192 -    catch (IOException ioe)
  28.193 -    {
  28.194 -      ioe.printStackTrace();
  28.195 -    }
  28.196 -    return;
  28.197 -  }
  28.198  }
    29.1 --- a/tools/control/src/org/xenoserver/control/XMLHelper.java	Tue Jul 08 11:23:42 2003 +0000
    29.2 +++ b/tools/control/src/org/xenoserver/control/XMLHelper.java	Tue Jul 08 11:30:20 2003 +0000
    29.3 @@ -8,201 +8,216 @@ package org.xenoserver.control;
    29.4  import java.util.Date;
    29.5  
    29.6  import org.w3c.dom.Document;
    29.7 -import org.w3c.dom.Element;
    29.8  import org.w3c.dom.Node;
    29.9  import org.w3c.dom.NodeList;
   29.10  
   29.11 -public class
   29.12 -XMLHelper
   29.13 -{
   29.14 -  static void
   29.15 -  dump_document (Document document)
   29.16 -  {
   29.17 -    dump_element(document.getDocumentElement(), 0);
   29.18 -  }
   29.19 +/**
   29.20 + * XMLHelper contains helper methods used to parse the XML state files.
   29.21 + */
   29.22 +class XMLHelper {
   29.23 +    /**
   29.24 +     * Find a subnode with the specified name.
   29.25 +     * @param name Name to look for.
   29.26 +     * @param node Node from which to start search.
   29.27 +     * @return The first subnode found, or null if none.
   29.28 +     */
   29.29 +    private static Node getSubNode(String name, Node node) {
   29.30 +        if (node.getNodeType() != Node.ELEMENT_NODE) {
   29.31 +            System.err.println("Error: Search node not of element type");
   29.32 +            return null;
   29.33 +        }
   29.34 +
   29.35 +        if (!node.hasChildNodes()) {
   29.36 +            return null;
   29.37 +        }
   29.38  
   29.39 -  static void
   29.40 -  dump_element (Element element, int indent)
   29.41 -  {
   29.42 -    NodeList nl = element.getChildNodes();
   29.43 +        NodeList list = node.getChildNodes();
   29.44 +        for (int i = 0; i < list.getLength(); i++) {
   29.45 +            Node subnode = list.item(i);
   29.46 +            if (subnode.getNodeType() == Node.ELEMENT_NODE) {
   29.47 +                if (subnode.getNodeName() == name) {
   29.48 +                    return subnode;
   29.49 +                }
   29.50 +            }
   29.51 +        }
   29.52 +        return null;
   29.53 +    }
   29.54  
   29.55 -    System.out.println ("<" + element.getTagName() + ">");
   29.56 -    dump_nodelist(nl, indent + 1);
   29.57 -    System.out.println("</" + element.getTagName() + ">");
   29.58 -  }
   29.59 +    /**
   29.60 +     * Get all the text of a given node.
   29.61 +     * @param node The node to examine.
   29.62 +     * @return The node's text.
   29.63 +     */
   29.64 +    private static String getText(Node node) {
   29.65 +        StringBuffer result = new StringBuffer();
   29.66 +        if (node == null || !node.hasChildNodes()) {
   29.67 +            return "";
   29.68 +        }
   29.69  
   29.70 -  static void
   29.71 -  dump_nodelist (NodeList nl, int indent)
   29.72 -  {
   29.73 -    for (int loop = 0; loop < nl.getLength(); loop++)
   29.74 -    {
   29.75 -      Node node = nl.item(loop);
   29.76 -      switch (node.getNodeType())
   29.77 -      {
   29.78 -	case Node.ELEMENT_NODE : 
   29.79 -	{
   29.80 -	  dump_element((Element)node, indent);
   29.81 -	  break;
   29.82 -	}
   29.83 -	case Node.TEXT_NODE :
   29.84 -	{
   29.85 -	  System.out.println("TEXT: " + node.getNodeValue());
   29.86 -	  break;
   29.87 -	}
   29.88 -	default :
   29.89 -	{
   29.90 -	  System.out.println("NODE: " + node.getNodeType());
   29.91 -	}
   29.92 -      }
   29.93 +        NodeList list = node.getChildNodes();
   29.94 +        for (int i = 0; i < list.getLength(); i++) {
   29.95 +            Node subnode = list.item(i);
   29.96 +            if (subnode.getNodeType() == Node.TEXT_NODE) {
   29.97 +                result.append(subnode.getNodeValue());
   29.98 +            }
   29.99 +        }
  29.100 +        return result.toString();
  29.101      }
  29.102 -  }
  29.103 +
  29.104 +    /**
  29.105 +     * Parse the given configuration document and configure the managers.
  29.106 +     * @param pm PartitionManager instance to configure.
  29.107 +     * @param vdm VirtualDomainManager instance to configure.
  29.108 +     * @param document Document to parse.
  29.109 +     */
  29.110 +    static void parse(
  29.111 +        PartitionManager pm,
  29.112 +        VirtualDiskManager vdm,
  29.113 +        Document document) {
  29.114 +        if (document == null) {
  29.115 +            return;
  29.116 +        }
  29.117 +
  29.118 +        /* parse partitions */
  29.119 +        parsePartitions(pm, document.getElementsByTagName("partition"));
  29.120  
  29.121 -  static Node 
  29.122 -  get_subnode (String name, Node node) 
  29.123 -  {
  29.124 -    if (node.getNodeType() != Node.ELEMENT_NODE) 
  29.125 -    {
  29.126 -      System.err.println("Error: Search node not of element type");
  29.127 -      return null;
  29.128 +        /* parse virtual disks */
  29.129 +        NodeList list = document.getElementsByTagName("virtual_disk");
  29.130 +        for (int i = 0; i < list.getLength(); i++) {
  29.131 +            Node subnode = list.item(i);
  29.132 +            String parent = subnode.getParentNode().getNodeName();
  29.133 +            VirtualDisk vd = parseVirtualDisk(subnode);
  29.134 +
  29.135 +            if (parent.equals("free")) {
  29.136 +                vdm.setFreeDisk(vd);
  29.137 +            } else if (parent.equals("virtual_disks")) {
  29.138 +                vdm.insertVirtualDisk(vd);
  29.139 +            } else {
  29.140 +                System.out.println(
  29.141 +                    "XML parse error: unknown parent for virtual_disk "
  29.142 +                        + "["
  29.143 +                        + parent
  29.144 +                        + "]");
  29.145 +            }
  29.146 +        }
  29.147 +
  29.148 +        /* parse virtual block devices */
  29.149 +        parseVirtualBlockDevices(
  29.150 +            vdm,
  29.151 +            document.getElementsByTagName("virtual_block_device"));
  29.152 +
  29.153 +        return;
  29.154      }
  29.155  
  29.156 -    if (!node.hasChildNodes()) return null;
  29.157 -
  29.158 -    NodeList list = node.getChildNodes();
  29.159 -    for (int i=0; i < list.getLength(); i++) 
  29.160 -    {
  29.161 -      Node subnode = list.item(i);
  29.162 -      if (subnode.getNodeType() == Node.ELEMENT_NODE) 
  29.163 -      {
  29.164 -	if (subnode.getNodeName() == name) return subnode;
  29.165 -      }
  29.166 -    }
  29.167 -    return null;
  29.168 -  }
  29.169 -
  29.170 -  static String 
  29.171 -  get_text (Node node) 
  29.172 -  {
  29.173 -    StringBuffer result = new StringBuffer();
  29.174 -    if (node==null || !node.hasChildNodes()) return "";
  29.175 +    /**
  29.176 +     * Parse a node representing a virtual disk.
  29.177 +     * @param node The node to parse.
  29.178 +     * @return The VirtualDisk this node represents.
  29.179 +     */
  29.180 +    private static VirtualDisk parseVirtualDisk(Node node) {
  29.181 +        VirtualDisk vd;
  29.182 +        Date date = new Date();
  29.183 +        NodeList list;
  29.184  
  29.185 -    NodeList list = node.getChildNodes();
  29.186 -    for (int i=0; i < list.getLength(); i++) 
  29.187 -    {
  29.188 -      Node subnode = list.item(i);
  29.189 -      if (subnode.getNodeType() == Node.TEXT_NODE) 
  29.190 -      {
  29.191 -	result.append(subnode.getNodeValue());
  29.192 -      }
  29.193 -    }
  29.194 -    return result.toString();
  29.195 -  }
  29.196 +        long timestamp =
  29.197 +            Long.parseLong(
  29.198 +                XMLHelper.getText(XMLHelper.getSubNode("expiry", node)));
  29.199 +        if (timestamp == 0) {
  29.200 +            date = null;
  29.201 +        } else {
  29.202 +            date.setTime(timestamp);
  29.203 +        }
  29.204 +        vd =
  29.205 +            new VirtualDisk(
  29.206 +                XMLHelper.getText(XMLHelper.getSubNode("name", node)),
  29.207 +                date,
  29.208 +                XMLHelper.getText(XMLHelper.getSubNode("key", node)));
  29.209  
  29.210 -  static void
  29.211 -  parse (PartitionManager pm, VirtualDiskManager vdm, Document document)
  29.212 -  {
  29.213 -    if (document == null) return;
  29.214 -
  29.215 -    /* parse partitions */
  29.216 -    parse_partitions(pm, document.getElementsByTagName("partition"));
  29.217 +        list = XMLHelper.getSubNode("extents", node).getChildNodes();
  29.218 +        for (int i = 0; i < list.getLength(); i++) {
  29.219 +            Node enode = list.item(i);
  29.220  
  29.221 -    /* parse virtual disks */
  29.222 -    NodeList list = document.getElementsByTagName("virtual_disk");
  29.223 -    for (int i = 0; i < list.getLength(); i++)
  29.224 -    {
  29.225 -      Node subnode = list.item(i);
  29.226 -      String parent = subnode.getParentNode().getNodeName();
  29.227 -      VirtualDisk vd =  parse_virtual_disk(subnode);
  29.228 +            if (enode.getNodeType() == Node.ELEMENT_NODE
  29.229 +                && enode.getNodeName().equals("extent")) {
  29.230 +                Extent extent =
  29.231 +                    new Extent(
  29.232 +                        Integer.parseInt(
  29.233 +                            XMLHelper.getText(
  29.234 +                                XMLHelper.getSubNode("disk", enode))),
  29.235 +                        Long.parseLong(
  29.236 +                            XMLHelper.getText(
  29.237 +                                XMLHelper.getSubNode("size", enode))),
  29.238 +                        Long.parseLong(
  29.239 +                            XMLHelper.getText(
  29.240 +                                XMLHelper.getSubNode("offset", enode))));
  29.241 +                vd.addExtent(extent);
  29.242 +            }
  29.243 +        }
  29.244  
  29.245 -      if (parent.equals("free"))
  29.246 -      {
  29.247 -	vdm.add_free(vd);
  29.248 -      }
  29.249 -      else if (parent.equals("virtual_disks"))
  29.250 -      {
  29.251 -	vdm.add_virtual_disk(vd);
  29.252 -      }
  29.253 -      else
  29.254 -      {
  29.255 -	System.out.println ("XML parse error: unknown parent for virtual_disk "
  29.256 -			    + "[" + parent + "]");
  29.257 -      }
  29.258 +        return vd;
  29.259      }
  29.260  
  29.261 -    /* parse virtual block devices */
  29.262 -    parse_virtual_block_devices(vdm, document.getElementsByTagName("virtual_block_device"));
  29.263 -
  29.264 -    return;
  29.265 -  }
  29.266 +    /**
  29.267 +     * Parse a list of partition nodes.
  29.268 +     * @param pm The partition manager to configure.
  29.269 +     * @param nl The list of partition nodes.
  29.270 +     */
  29.271 +    private static void parsePartitions(PartitionManager pm, NodeList nl) {
  29.272 +        Partition partition;
  29.273  
  29.274 -  static VirtualDisk
  29.275 -  parse_virtual_disk(Node node)
  29.276 -  {
  29.277 -    VirtualDisk vd;
  29.278 -    Date date = new Date();
  29.279 -    NodeList list;
  29.280 +        for (int loop = 0; loop < nl.getLength(); loop++) {
  29.281 +            Node node = nl.item(loop);
  29.282  
  29.283 -    long timestamp = Long.parseLong(XMLHelper.get_text(XMLHelper.get_subnode("expiry", node)));
  29.284 -    if ( timestamp == 0 )
  29.285 -      date = null;
  29.286 -    else
  29.287 -      date.setTime( timestamp );
  29.288 -    vd = new VirtualDisk(XMLHelper.get_text(XMLHelper.get_subnode("name", node)),
  29.289 -			 date,
  29.290 -			 XMLHelper.get_text(XMLHelper.get_subnode("key", node)));
  29.291 +            partition =
  29.292 +                new Partition(
  29.293 +                    Integer.parseInt(
  29.294 +                        XMLHelper.getText(XMLHelper.getSubNode("major", node))),
  29.295 +                    Integer.parseInt(
  29.296 +                        XMLHelper.getText(XMLHelper.getSubNode("minor", node))),
  29.297 +                    Integer.parseInt(
  29.298 +                        XMLHelper.getText(
  29.299 +                            XMLHelper.getSubNode("blocks", node))),
  29.300 +                    Integer.parseInt(
  29.301 +                        XMLHelper.getText(
  29.302 +                            XMLHelper.getSubNode("start_sect", node))),
  29.303 +                    Integer.parseInt(
  29.304 +                        XMLHelper.getText(
  29.305 +                            XMLHelper.getSubNode("nr_sects", node))),
  29.306 +                    XMLHelper.getText(XMLHelper.getSubNode("name", node)),
  29.307 +                    true);
  29.308  
  29.309 -    list = XMLHelper.get_subnode("extents", node).getChildNodes();
  29.310 -    for (int i = 0; i < list.getLength(); i++)
  29.311 -    {
  29.312 -      Node enode = list.item(i);
  29.313 -
  29.314 -      if (enode.getNodeType() == Node.ELEMENT_NODE &&
  29.315 -	  enode.getNodeName().equals("extent"))
  29.316 -      {
  29.317 -	Extent extent = new Extent();
  29.318 -
  29.319 -	extent.disk = Integer.parseInt(XMLHelper.get_text(XMLHelper.get_subnode("disk", enode)));
  29.320 -	extent.size = Long.parseLong(XMLHelper.get_text(XMLHelper.get_subnode("size", enode)));
  29.321 -	extent.offset = Long.parseLong(XMLHelper.get_text(XMLHelper.get_subnode("offset", enode)));
  29.322 -	vd.add_extent(extent);
  29.323 -      }
  29.324 +            pm.addXenoPartition(partition);
  29.325 +        }
  29.326      }
  29.327  
  29.328 -    return vd;
  29.329 -  }
  29.330 -
  29.331 -  static void
  29.332 -  parse_partitions (PartitionManager pm, NodeList nl)
  29.333 -  {
  29.334 -    Partition partition;
  29.335 -
  29.336 -    for (int loop = 0; loop < nl.getLength(); loop++)
  29.337 -    {
  29.338 -      Node node = nl.item(loop);
  29.339 +    /**
  29.340 +     * Parse a list of virtual block device nodes.
  29.341 +     * @param vdm The VirtualDiskManager to configure. 
  29.342 +     * @param nl The node list.
  29.343 +     */
  29.344 +    private static void parseVirtualBlockDevices(
  29.345 +        VirtualDiskManager vdm,
  29.346 +        NodeList nl) {
  29.347 +        for (int loop = 0; loop < nl.getLength(); loop++) {
  29.348 +            Node node = nl.item(loop);
  29.349 +            Mode mode;
  29.350  
  29.351 -      partition = new Partition();
  29.352 -      partition.major = Integer.parseInt(XMLHelper.get_text(XMLHelper.get_subnode("major", node)));
  29.353 -      partition.minor = Integer.parseInt(XMLHelper.get_text(XMLHelper.get_subnode("minor", node)));
  29.354 -      partition.blocks = Integer.parseInt(XMLHelper.get_text(XMLHelper.get_subnode("blocks", node)));
  29.355 -      partition.start_sect = Integer.parseInt(XMLHelper.get_text(XMLHelper.get_subnode("start_sect", node)));
  29.356 -      partition.nr_sects = Integer.parseInt(XMLHelper.get_text(XMLHelper.get_subnode("nr_sects", node)));
  29.357 -      partition.name = XMLHelper.get_text(XMLHelper.get_subnode("name", node));
  29.358 -
  29.359 -      pm.add_xeno_partition(partition);
  29.360 -    }
  29.361 -  }
  29.362 +            if (XMLHelper
  29.363 +                .getText(XMLHelper.getSubNode("mode", node))
  29.364 +                .equals("rw")) {
  29.365 +                mode = Mode.READ_WRITE;
  29.366 +            } else {
  29.367 +                mode = Mode.READ_ONLY;
  29.368 +            }
  29.369  
  29.370 -  static void
  29.371 -  parse_virtual_block_devices (VirtualDiskManager vdm, NodeList nl)
  29.372 -  {
  29.373 -    for (int loop = 0; loop < nl.getLength(); loop++)
  29.374 -    {
  29.375 -      Node node = nl.item(loop);
  29.376 -
  29.377 -      vdm.create_virtual_block_device(XMLHelper.get_text(XMLHelper.get_subnode("key", node)),
  29.378 -				      Integer.parseInt(XMLHelper.get_text(XMLHelper.get_subnode("domain", node))),
  29.379 -				      Integer.parseInt(XMLHelper.get_text(XMLHelper.get_subnode("vbdnum", node))),
  29.380 -				      XMLHelper.get_text(XMLHelper.get_subnode("mode", node)));
  29.381 +            vdm.createVirtualBlockDevice(
  29.382 +                vdm.getVirtualDisk(
  29.383 +                    XMLHelper.getText(XMLHelper.getSubNode("key", node))),
  29.384 +                Integer.parseInt(
  29.385 +                    XMLHelper.getText(XMLHelper.getSubNode("domain", node))),
  29.386 +                Integer.parseInt(
  29.387 +                    XMLHelper.getText(XMLHelper.getSubNode("vbdnum", node))),
  29.388 +                mode);
  29.389 +        }
  29.390      }
  29.391 -  }
  29.392  }