On Wed, Jul 27, 2011 at 04:02:13PM -0400, Ben Lipton wrote: > Automatically sets it to the smaller of: > * Size of the swap partition on source machine > * 10% of instance disk > > Signed-off-by: Ben Lipton <[email protected]> > --- > p2v-transfer/p2v_transfer.py | 59 ++++++++++++++----- > p2v-transfer/test/p2v_transfer_test.py | 99 > ++++++++++++++++++++++++++++---- > 2 files changed, 133 insertions(+), 25 deletions(-) > > diff --git a/p2v-transfer/p2v_transfer.py b/p2v-transfer/p2v_transfer.py > index b2fc12f..432051f 100755 > --- a/p2v-transfer/p2v_transfer.py > +++ b/p2v-transfer/p2v_transfer.py > @@ -192,34 +192,63 @@ def _WaitForCompletion(channel): > time.sleep(.01) > > > -def PartitionTargetDisks(client, swap_cyls): > - """Partition and format the disks on the target machine. > +def GetDiskSize(client): > + """Determine how much disk is available, how much swap space to include. > > - Sends commands over the SSH connection to partition and format the > - disk of the target instance. > + For swap size, returns the minimum of: > + - amount of swap space on the source machine > + - 10% of the target drive > > @type client: paramiko.SSHClient > @param client: SSH client object used to connect to the instance. > - @type swap_cyls: int > - @param swap_cyls: Desired size of swap space, in cylinders > + @rtype: (int, int) > + @return: Total size in megabytes, swap size in megabytes > > """ > - # Find out how many cylinders are available on target > - total_cyls = 0 > - stdin, stdout, stderr = client.exec_command("sfdisk -l /dev/xvda") > + # Find out how many MB are available on target > + stdin, stdout, stderr = client.exec_command("fdisk -l /dev/xvda") > for line in stdout: > if line.startswith("Disk /dev/xvda:"): > words = line.split() > - total_cyls = int(words[2]) > + total_megs = int(words[2]) > break > stdout.close()
This would be better read via blockdev --getsize64 /dev/xvda, which returns the number of bytes. > - nonswap_cyls = total_cyls - swap_cyls > - sfdisk_command = """sfdisk /dev/xvda <<EOF > + swap_output = subprocess.Popen(["swapon", "-s"], > + stdout=subprocess.PIPE, > + stderr=subprocess.PIPE).communicate()[0] > + swap_megs = 0 > + for line in swap_output.splitlines()[1:]: > + words = line.split() > + try: > + size_kb = int(words[2]) > + swap_megs += size_kb / 1024 > + except ValueError: > + pass # This line doesn't have data on it Wait, is this done on the bootstrap OS? That won't likely make sense, I think, because the swap size on it is usually non-existent. Rather, the swap size from the original machine would be more interesting? iustin
