Skip to content
This repository has been archived by the owner on May 7, 2021. It is now read-only.

Commit

Permalink
ore/upload: Add --image-size and --image-size-inspect
Browse files Browse the repository at this point in the history
We want to expand the disk size of Red Hat CoreOS; previously
there was a hardcoded 8GiB size for Container Linux which
(I believe entirely coincidentally) matched RHCOS.

See #948
and #944
  • Loading branch information
cgwalters committed Nov 29, 2018
1 parent f74ea5a commit f073d0f
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 11 deletions.
25 changes: 23 additions & 2 deletions cmd/ore/aws/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (

"github.com/coreos/mantle/platform/api/aws"
"github.com/coreos/mantle/sdk"
"github.com/coreos/mantle/util"
"github.com/spf13/cobra"
)

Expand All @@ -50,6 +51,8 @@ After a successful run, the final line of output will be a line of JSON describi
uploadImageName string
uploadBoard string
uploadFile string
uploadSizeGiB uint
uploadSizeInspect bool
uploadDeleteObject bool
uploadForce bool
uploadSourceSnapshot string
Expand All @@ -70,6 +73,8 @@ func init() {
cmdUpload.Flags().StringVar(&uploadFile, "file",
defaultUploadFile(),
"path to CoreOS image (build with: ./image_to_vm.sh --format=ami_vmdk ...)")
cmdUpload.Flags().UintVarP(&uploadSizeGiB, "size-gib", "", 0, "Use this size in GiB")
cmdUpload.Flags().BoolVar(&uploadSizeInspect, "size-inspect", false, "With --file, determine size from the image")
cmdUpload.Flags().BoolVar(&uploadDeleteObject, "delete-object", true, "delete uploaded S3 object after snapshot is created")
cmdUpload.Flags().BoolVar(&uploadForce, "force", false, "overwrite existing S3 object rather than reusing")
cmdUpload.Flags().StringVar(&uploadSourceSnapshot, "source-snapshot", "", "the snapshot ID to base this AMI on (default: create new snapshot)")
Expand Down Expand Up @@ -143,6 +148,22 @@ func runUpload(cmd *cobra.Command, args []string) error {
imageName = ver.Version
}

if uploadSizeInspect {
imageInfo, err := util.GetImageInfo(uploadFile)
if err != nil {
fmt.Fprintf(os.Stderr, "Unable to query size of disk: %v\n", err)
os.Exit(1)
}
plog.Debugf("Image size: %v\n", imageInfo.VirtualSize)
const GiB = 1024 * 1024 * 1024
quotient, remainder := imageInfo.VirtualSize/GiB, imageInfo.VirtualSize%GiB
// Round up if there's leftover
if remainder > 0 {
quotient = quotient + 1
}
uploadSizeGiB = uint(quotient)
}

amiName := uploadAMIName
if amiName == "" {
ver, err := sdk.VersionsFromDir(filepath.Dir(uploadFile))
Expand Down Expand Up @@ -225,7 +246,7 @@ func runUpload(cmd *cobra.Command, args []string) error {
}

// create AMIs and grant permissions
hvmID, err := API.CreateHVMImage(sourceSnapshot, amiName+"-hvm", uploadAMIDescription)
hvmID, err := API.CreateHVMImage(sourceSnapshot, amiName+"-hvm", uint32(uploadSizeGiB), uploadAMIDescription)
if err != nil {
fmt.Fprintf(os.Stderr, "unable to create HVM image: %v\n", err)
os.Exit(1)
Expand Down Expand Up @@ -257,7 +278,7 @@ func runUpload(cmd *cobra.Command, args []string) error {

var pvID string
if uploadCreatePV {
pvImageID, err := API.CreatePVImage(sourceSnapshot, amiName, uploadAMIDescription)
pvImageID, err := API.CreatePVImage(sourceSnapshot, amiName, uint32(uploadSizeGiB), uploadAMIDescription)
if err != nil {
fmt.Fprintf(os.Stderr, "unable to create PV image: %v\n", err)
os.Exit(1)
Expand Down
4 changes: 2 additions & 2 deletions cmd/plume/prerelease.go
Original file line number Diff line number Diff line change
Expand Up @@ -407,12 +407,12 @@ func awsUploadToPartition(spec *channelSpec, part *awsPartitionSpec, imageName,

plog.Printf("Creating AMIs from %v...", snapshot.SnapshotID)

hvmImageID, err := api.CreateHVMImage(snapshot.SnapshotID, imageName+"-hvm", imageDescription+" (HVM)")
hvmImageID, err := api.CreateHVMImage(snapshot.SnapshotID, imageName+"-hvm", 0, imageDescription+" (HVM)")
if err != nil {
return nil, nil, fmt.Errorf("unable to create HVM image: %v", err)
}

pvImageID, err := api.CreatePVImage(snapshot.SnapshotID, imageName, imageDescription+" (PV)")
pvImageID, err := api.CreatePVImage(snapshot.SnapshotID, imageName, 0, imageDescription+" (PV)")
if err != nil {
return nil, nil, fmt.Errorf("unable to create PV image: %v", err)
}
Expand Down
26 changes: 19 additions & 7 deletions platform/api/aws/images.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ import (
"github.com/aws/aws-sdk-go/service/iam"
)

// The size of Container Linux on AWS, in GiB. See discussion in
// https://github.com/coreos/mantle/pull/944
// This is used if the provided size is less than 8, to ensure
// compatibility with CL which has 4.5GiB disks right now but
// wants 8GiB in AWS. Otherwise, if a larger disk is provided,
// its size will be honored.
const containerLinuxDiskSize = 8

var (
NoRegionPVSupport = errors.New("Region does not support PV")
)
Expand Down Expand Up @@ -350,18 +358,18 @@ func (a *API) CreateImportRole(bucket string) error {
return nil
}

func (a *API) CreateHVMImage(snapshotID string, name string, description string) (string, error) {
params := registerImageParams(snapshotID, name, description, "xvd", EC2ImageTypeHVM)
func (a *API) CreateHVMImage(snapshotID string, name string, sizeGiB uint32, description string) (string, error) {
params := registerImageParams(snapshotID, name, sizeGiB, description, "xvd", EC2ImageTypeHVM)
params.EnaSupport = aws.Bool(true)
params.SriovNetSupport = aws.String("simple")
return a.createImage(params)
}

func (a *API) CreatePVImage(snapshotID string, name string, description string) (string, error) {
func (a *API) CreatePVImage(snapshotID string, name string, sizeGiB uint32, description string) (string, error) {
if !RegionSupportsPV(a.opts.Region) {
return "", NoRegionPVSupport
}
params := registerImageParams(snapshotID, name, description, "sd", EC2ImageTypePV)
params := registerImageParams(snapshotID, name, sizeGiB, description, "sd", EC2ImageTypePV)
params.KernelId = aws.String(akis[a.opts.Region])
return a.createImage(params)
}
Expand Down Expand Up @@ -403,9 +411,13 @@ func (a *API) createImage(params *ec2.RegisterImageInput) (string, error) {
return imageID, nil
}

const diskSize = 8 // GB
func registerImageParams(snapshotID, name string, sizeGiB uint32, description string, diskBaseName string, imageType EC2ImageType) *ec2.RegisterImageInput {
// See comments around the containerLinuxDiskSize constant above; this
// ensures that e.g. the 0 passed by default turns into the default CL size.
if sizeGiB < containerLinuxDiskSize {
sizeGiB = containerLinuxDiskSize
}

func registerImageParams(snapshotID, name, description string, diskBaseName string, imageType EC2ImageType) *ec2.RegisterImageInput {
return &ec2.RegisterImageInput{
Name: aws.String(name),
Description: aws.String(description),
Expand All @@ -418,7 +430,7 @@ func registerImageParams(snapshotID, name, description string, diskBaseName stri
Ebs: &ec2.EbsBlockDevice{
SnapshotId: aws.String(snapshotID),
DeleteOnTermination: aws.Bool(true),
VolumeSize: aws.Int64(diskSize),
VolumeSize: aws.Int64(int64(sizeGiB)),
VolumeType: aws.String("gp2"),
},
},
Expand Down
1 change: 1 addition & 0 deletions util/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (

type ImageInfo struct {
Format string `json:"format"`
VirtualSize uint64 `json:"virtual-size"`
}

func GetImageInfo(path string) (*ImageInfo, error) {
Expand Down

0 comments on commit f073d0f

Please sign in to comment.