Skip to content

Commit

Permalink
feat(init): mount partitions dynamically (#169)
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewrynhard authored Oct 17, 2018
1 parent 04bb2da commit 453bc48
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 55 deletions.
9 changes: 2 additions & 7 deletions src/image/src/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ DEFAULT Dianemo
LABEL Dianemo
KERNEL /boot/vmlinuz
INITRD /boot/initramfs.xz
APPEND ${KERNEL_SELF_PROTECTION_PROJECT_KERNEL_PARAMS} ip=dhcp consoleblank=0 console=tty0 console=ttyS0,9600 dianemo.autonomy.io/root=${DIANEMO_ROOT} dianemo.autonomy.io/userdata=${DIANEMO_USERDATA} dianemo.autonomy.io/platform=${DIANEMO_PLATFORM}
APPEND ${KERNEL_SELF_PROTECTION_PROJECT_KERNEL_PARAMS} ip=dhcp consoleblank=0 console=tty0 console=ttyS0,9600 dianemo.autonomy.io/userdata=${DIANEMO_USERDATA} dianemo.autonomy.io/platform=${DIANEMO_PLATFORM}
EOF
}

Expand All @@ -120,7 +120,6 @@ function cleanup {

# Defaults

DIANEMO_ROOT="sda"
DIANEMO_USERDATA=""
DIANEMO_PLATFORM="bare-metal"
RAW_IMAGE="/out/image.raw"
Expand All @@ -136,7 +135,7 @@ KERNEL_SELF_PROTECTION_PROJECT_KERNEL_PARAMS="page_poison=1 slab_nomerge pti=on"
case "$1" in
image)
shift
while getopts "b:flt:p:u:" opt; do
while getopts "b:flp:u:" opt; do
case ${opt} in
b )
DEVICE=${OPTARG}
Expand All @@ -157,10 +156,6 @@ case "$1" in
DIANEMO_PLATFORM=${OPTARG}
echo "Using kernel parameter dianemo.autonomy.io/platform=${DIANEMO_PLATFORM}"
;;
t )
DIANEMO_ROOT=${OPTARG}
echo "Using kernel parameter dianemo.autonomy.io/root=${DIANEMO_ROOT}"
;;
u )
DIANEMO_USERDATA=${OPTARG}
echo "Using kernel parameter dianemo.autonomy.io/userdata=${DIANEMO_USERDATA}"
Expand Down
2 changes: 1 addition & 1 deletion src/image/src/packer.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
"sudo add-apt-repository \"deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable\"",
"sudo apt-get -y update",
"sudo apt-get -y install docker-ce",
"sudo docker run --privileged --volume /dev:/dev autonomy/dianemo:{{ user `version` }} image -b /dev/xvdf -t /dev/xvda -f -p aws -u none"
"sudo docker run --privileged --volume /dev:/dev autonomy/dianemo:{{ user `version` }} image -b /dev/xvdf -f -p aws -u none"
]
}
]
Expand Down
4 changes: 0 additions & 4 deletions src/initramfs/cmd/init/pkg/constants/constants.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
package constants

const (
// KernelParamRoot is the kernel parameter name for specifying the root
// disk.
KernelParamRoot = "dianemo.autonomy.io/root"

// KernelParamUserData is the kernel parameter name for specifying the URL
// to the user data.
KernelParamUserData = "dianemo.autonomy.io/userdata"
Expand Down
32 changes: 32 additions & 0 deletions src/initramfs/cmd/init/pkg/mount/blkid/blkid.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,38 @@ import (
"unsafe"
)

// GetDevWithAttribute returns the dev name of a block device matching the ATTRIBUTE=VALUE
// pair. Supported attributes are:
// TYPE: filesystem type
// UUID: filesystem uuid
// LABEL: filesystem label
func GetDevWithAttribute(attribute, value string) (string, error) {
var cache C.blkid_cache

ret := C.blkid_get_cache(&cache, nil)
if ret != 0 {
return "", fmt.Errorf("failed to get blkid cache: %d", ret)
}

C.blkid_probe_all(cache)

cs_attribute := C.CString(attribute)
cs_value := C.CString(value)
defer C.free(unsafe.Pointer(cs_attribute))
defer C.free(unsafe.Pointer(cs_value))

devname := C.blkid_get_devname(cache, cs_attribute, cs_value)
defer C.free(unsafe.Pointer(devname))

// If you have called blkid_get_cache(), you should call blkid_put_cache()
// when you are done using the blkid library functions. This will save the
// cache to the blkid.tab file, if you have write access to the file. It
// will also free all associated devices and tags:
C.blkid_put_cache(cache)

return C.GoString(devname), nil
}

// NewProbeFromFilename executes lblkid blkid_new_probe_from_filename.
func NewProbeFromFilename(s string) (C.blkid_probe, error) {
cs := C.CString(s)
Expand Down
87 changes: 44 additions & 43 deletions src/initramfs/cmd/init/pkg/mount/mount.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"sync"

"github.com/autonomy/dianemo/src/initramfs/cmd/init/pkg/constants"
"github.com/autonomy/dianemo/src/initramfs/cmd/init/pkg/kernel"
"github.com/autonomy/dianemo/src/initramfs/cmd/init/pkg/mount/blkid"
"golang.org/x/sys/unix"
)
Expand Down Expand Up @@ -206,54 +205,56 @@ func mountBlockDevices(s string) (err error) {
func probe() (b []*BlockDevice, err error) {
b = []*BlockDevice{}

arguments, err := kernel.ParseProcCmdline()
if err != nil {
return
if err := appendBlockDeviceWithLabel(&b, constants.RootPartitionLabel); err != nil {
return nil, err
}
if err := appendBlockDeviceWithLabel(&b, constants.DataPartitionLabel); err != nil {
return nil, err
}

if root, ok := arguments[constants.KernelParamRoot]; ok {
if _, err := os.Stat(root); os.IsNotExist(err) {
return nil, fmt.Errorf("device does not exist: %s", root)
}
pr, err := blkid.NewProbeFromFilename(root)
defer blkid.FreeProbe(pr)
if err != nil {
return nil, fmt.Errorf("failed to probe %s: %s", root, err)
}
return b, nil
}

ls := blkid.ProbeGetPartitions(pr)
nparts := blkid.ProbeGetPartitionsPartlistNumOfPartitions(ls)
func appendBlockDeviceWithLabel(b *[]*BlockDevice, value string) error {
devname, err := blkid.GetDevWithAttribute("LABEL", value)
if err != nil {
return err
}

for i := 0; i < nparts; i++ {
dev := fmt.Sprintf("%s%d", root, i+1)
pr, err = blkid.NewProbeFromFilename(dev)
defer blkid.FreeProbe(pr)
if err != nil {
return nil, fmt.Errorf("failed to probe %s: %s", dev, err)
}
blockDevice, err := probeDevice(devname)
if err != nil {
return err
}

blkid.DoProbe(pr)
UUID, err := blkid.ProbeLookupValue(pr, "UUID", nil)
if err != nil {
return nil, err
}
TYPE, err := blkid.ProbeLookupValue(pr, "TYPE", nil)
if err != nil {
return nil, err
}
LABEL, err := blkid.ProbeLookupValue(pr, "LABEL", nil)
if err != nil {
return nil, err
}
*b = append(*b, blockDevice)

b = append(b, &BlockDevice{
dev: dev,
UUID: UUID,
TYPE: TYPE,
LABEL: LABEL,
})
}
return nil
}

func probeDevice(devname string) (*BlockDevice, error) {
pr, err := blkid.NewProbeFromFilename(devname)
defer blkid.FreeProbe(pr)
if err != nil {
return nil, fmt.Errorf("failed to probe %s: %s", devname, err)
}
blkid.DoProbe(pr)
UUID, err := blkid.ProbeLookupValue(pr, "UUID", nil)
if err != nil {
return nil, err
}
TYPE, err := blkid.ProbeLookupValue(pr, "TYPE", nil)
if err != nil {
return nil, err
}
LABEL, err := blkid.ProbeLookupValue(pr, "LABEL", nil)
if err != nil {
return nil, err
}

return b, nil
return &BlockDevice{
dev: devname,
UUID: UUID,
TYPE: TYPE,
LABEL: LABEL,
}, nil
}

0 comments on commit 453bc48

Please sign in to comment.