starting aws ebs instance with larger root volume

946 views
Skip to first unread message

Karl

unread,
Jul 3, 2011, 10:48:08 PM7/3/11
to ruby-fog
My current AMI has a root ebs volume of only 15 gigs. Is it possible
to tell fog (aws) to start the instance with more disk space? I was
looking at the following post which made it sound like it might be
possible:

http://blog.bioteam.net/2010/07/how-to-resize-an-amazon-ec2-ami-when-boot-disk-is-on-ebs/

thx.

-karl

Peter Weldon

unread,
Jul 4, 2011, 11:50:52 PM7/4/11
to ruby...@googlegroups.com
Below is a partial code snippet that does something similar, but there may be a easier way. Maybe someone else has something better. Depending on your distribution and version you may need to run resize2fs /dev/<root device> on the instance once it is up.

def create_instance(connection, options)
    image = connection.images.get(options[:ami])

    block_device_mapping = create_block_device_mapping(image.block_device_mapping)
    set_root_device_size(block_device_mapping, image.root_device_name, options[:root_device_size])

    server = connection.servers.create(image_id: options[:ami],
                                       flavor_id: options[:flavor],
                                       block_device_mapping: block_device_mapping,
                                       key_name: options[:name],
                                       groups: options[:name],
                                       'ClientToken' => SimpleUUID::UUID.new.to_guid)

    connection.tags.create(key: 'Name', value: options[:name], resource_id: server.id)
    
    server.wait_for { printf '.'; ready? }
    server.wait_for { printf '*'; console_output.body['output'] =~ /^cloud-init boot finished/ }
    puts
    server
  end

  def create_block_device_mapping(image_mappings)
    block_device_mapping = []
    name_mapping = {
      'deviceName' => 'DeviceName',
      'snapshotId' => 'Ebs.SnapshotId',
      'volumeSize' => 'Ebs.VolumeSize',
      'deleteOnTermination' => 'Ebs.DeleteOnTermination',
    }
    image_mappings.each do |image_mapping|
      mapping = {}
      name_mapping.each do |key, value|
        mapping[value] = image_mapping[key]
      end
      block_device_mapping << mapping
    end
    block_device_mapping
  end

  def set_root_device_size(block_device_mapping, root_device_name, root_device_size)
    root_device = block_device_mapping.find { |mapping| mapping['DeviceName'] == root_device_name }
    root_device['Ebs.VolumeSize'] = root_device_size
    block_device_mapping
  end

geemus (Wesley Beary)

unread,
Jul 5, 2011, 2:45:15 PM7/5/11
to ruby...@googlegroups.com
That looks about right to me. I would probably take a running version
of it and do a lookup (so that I could see the format of the block
device mapping). Then you can shut it down and restart with the same
block device mapping setting (with a modified size attribute). I
think that is all it should take on the api side, though as peter
mentioned you may need to run stuff on the instance itself so that it
will recognize the change properly.
Thanks!
wes

Karl Baum

unread,
Jul 5, 2011, 2:49:03 PM7/5/11
to ruby...@googlegroups.com
I wasn't able to get it to start up with the larger volume, but i was able to get something similar done by using aws command line tools.

Thanks for your help!

-karl

Message has been deleted

geemus (Wesley Beary)

unread,
Apr 19, 2013, 11:27:42 AM4/19/13
to ruby...@googlegroups.com
A pull request or issue around that would be great, thanks!


On Tue, Apr 16, 2013 at 6:24 PM, Brian Racer <bra...@gmail.com> wrote:
Just a heads up you need to add "'virtualName' => 'VirtualName'," to the name_mapping hash.

--
You received this message because you are subscribed to the Google Groups "ruby-fog" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ruby-fog+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Kubes

unread,
Nov 25, 2013, 6:38:02 PM11/25/13
to ruby...@googlegroups.com
to expand on Peter's solution its seems you can just pass the over-riding values in the block_device_mapping

server = @compute.servers.create( :flavor_id                  => 'm1.small',
                                  :image_id                   => 'ami-83e4bcea',
                                  :block_device_mapping       => [  {:DeviceName => '/dev/sda1', 'Ebs.VolumeSize' => 20 } ]
                                )


This will use the correct snapshot from the ami, and just over-rite the size. 

NOTE, like Peter stated you need to run resize2fs if you are using a filesystem like Amazon's default of ext4
Reply all
Reply to author
Forward
0 new messages