...

Source file src/github.com/Microsoft/hcsshim/ext4/tar2ext4/vhdfooter.go

Documentation: github.com/Microsoft/hcsshim/ext4/tar2ext4

     1  package tar2ext4
     2  
     3  import (
     4  	"bytes"
     5  	"crypto/rand"
     6  	"encoding/binary"
     7  )
     8  
     9  // Constants for the VHD footer
    10  const (
    11  	cookieMagic            = "conectix"
    12  	featureMask            = 0x2
    13  	fileFormatVersionMagic = 0x00010000
    14  	fixedDataOffset        = -1
    15  	creatorVersionMagic    = 0x000a0000
    16  	diskTypeFixed          = 2
    17  )
    18  
    19  type vhdFooter struct {
    20  	Cookie             [8]byte
    21  	Features           uint32
    22  	FileFormatVersion  uint32
    23  	DataOffset         int64
    24  	TimeStamp          uint32
    25  	CreatorApplication [4]byte
    26  	CreatorVersion     uint32
    27  	CreatorHostOS      [4]byte
    28  	OriginalSize       int64
    29  	CurrentSize        int64
    30  	DiskGeometry       uint32
    31  	DiskType           uint32
    32  	Checksum           uint32
    33  	UniqueID           [16]uint8
    34  	SavedState         uint8
    35  	Reserved           [427]uint8
    36  }
    37  
    38  func makeFixedVHDFooter(size int64) *vhdFooter {
    39  	footer := &vhdFooter{
    40  		Features:          featureMask,
    41  		FileFormatVersion: fileFormatVersionMagic,
    42  		DataOffset:        fixedDataOffset,
    43  		CreatorVersion:    creatorVersionMagic,
    44  		OriginalSize:      size,
    45  		CurrentSize:       size,
    46  		DiskType:          diskTypeFixed,
    47  		UniqueID:          generateUUID(),
    48  	}
    49  	copy(footer.Cookie[:], cookieMagic)
    50  	footer.Checksum = calculateCheckSum(footer)
    51  	return footer
    52  }
    53  
    54  func calculateCheckSum(footer *vhdFooter) uint32 {
    55  	oldchk := footer.Checksum
    56  	footer.Checksum = 0
    57  
    58  	buf := &bytes.Buffer{}
    59  	_ = binary.Write(buf, binary.BigEndian, footer)
    60  
    61  	var chk uint32
    62  	bufBytes := buf.Bytes()
    63  	for i := 0; i < len(bufBytes); i++ {
    64  		chk += uint32(bufBytes[i])
    65  	}
    66  	footer.Checksum = oldchk
    67  	return uint32(^chk)
    68  }
    69  
    70  func generateUUID() [16]byte {
    71  	res := [16]byte{}
    72  	if _, err := rand.Read(res[:]); err != nil {
    73  		panic(err)
    74  	}
    75  	return res
    76  }
    77  

View as plain text