Permalink
Browse files

format vhd blob

Signed-off-by: Huamin Chen <hchen@redhat.com>
  • Loading branch information...
rootfs committed Aug 7, 2016
1 parent 54152fe commit f22537ba891348076c50ed9647736d18898ed75a
Showing with 48 additions and 1 deletion.
  1. +14 −1 pkg/cloudprovider/providers/azure/azure_blob.go
  2. +34 −0 pkg/cloudprovider/providers/azure/vhd.go
@@ -27,6 +27,7 @@ const (
vhdContainerName = "vhds"
useHttps = true
blobServiceName = "blob"
vhdHeaderSize = 512
)
// switch to asm mode for blob service
@@ -36,12 +37,24 @@ func (az *Cloud) createVhdBlob(accountName, accountKey, name string, sizeGB int6
blobClient, err := az.getBlobClient(accountName, accountKey)
if err == nil {
size := 1024 * 1024 * 1024 * sizeGB
vhdSize := size + vhdHeaderSize /* header size */
// Blob name in URL must end with '.vhd' extension.
name = name + ".vhd"
err = blobClient.PutPageBlob(vhdContainerName, name, size, tags)
err = blobClient.PutPageBlob(vhdContainerName, name, vhdSize, tags)
if err != nil {
return "", "", fmt.Errorf("failed to put page blob: %v", err)
}
// add VHD signature to the blob
h, err := createVHDHeader(uint64(size))
if err != nil {
az.deleteVhdBlob(accountName, accountKey, name)
return "", "", fmt.Errorf("failed to create vhd header, err: %v", err)
}
if err = blobClient.PutPage(vhdContainerName, name, size, vhdSize-1, azs.PageWriteTypeUpdate, h[:vhdHeaderSize], nil); err != nil {
az.deleteVhdBlob(accountName, accountKey, name)
return "", "", fmt.Errorf("failed to update vhd header, err: %v", err)
}
scheme := "http"
if useHttps {
scheme = "https"
@@ -0,0 +1,34 @@
/*
Copyright 2016 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package azure
import (
"bytes"
"encoding/binary"
"github.com/rootfs/go-vhd/vhd"
)
func createVHDHeader(size uint64) ([]byte, error) {
h := vhd.CreateFixedHeader(size, &vhd.VHDOptions{})
b := new(bytes.Buffer)
err := binary.Write(b, binary.BigEndian, h)
if err != nil {
return nil, err
}
return b.Bytes(), nil
}

0 comments on commit f22537b

Please sign in to comment.