masahiro331 / go-vmdk-parser

Apache License 2.0
5 stars 1 forks source link

Is there a way to use this API to find the MBR and filesystem? #6

Closed schmidtw closed 1 year ago

schmidtw commented 1 year ago

I'm trying to figure out how to use this API to find the MBR/filesystem blob in a vmdk image. Can I do that with this library? If so, any suggestions on how to do that?

Thanks

masahiro331 commented 1 year ago

This Library parse only vmdk, not MBR{Filesystem}

You can use go-disk library to parse disk layer(MBR or GUID Partition Table) https://github.com/masahiro331/go-disk

And, use go-xxx-fileystem libraries to parse filesystem. https://github.com/masahiro331/go-xfs-filesystem https://github.com/masahiro331/go-ext4-filesystem

sample code

package main

import (
    "fmt"
    "io/fs"
    "log"
    "os"

    "github.com/masahiro331/go-disk"
    "github.com/masahiro331/go-xfs-filesystem/xfs"
    "github.com/masahiro331/go-vmdk-parser/pkg/virtualization/vmdk"
)

func main() {
    f, err := os.Open(os.Args[1]) // some vmdk file
    if err != nil {
        log.Fatal(err)
    }

    vreader, err := vmdk.Open(f, nil)
    if err != nil {
        log.Fatal(err)
    }

    d, err := disk.NewDriver(vreader)
    if err != nil {
        log.Fatal(err)
    }

    for {
        partition, err := d.Next()
        if err != nil {
            log.Fatal(err)
        }

        reader := partition.GetSectionReader()
        if !partition.Bootable() {
                         // for xfs filesystem
            fsys, err := xfs.NewFS(reader, nil)
            if err != nil {
                log.Fatal(err)
            }
            fs.WalkDir(fsys, "/etc/", func(path string, d fs.DirEntry, err error) error {
                if err != nil {
                    return err
                }
                fmt.Println(path)
                return nil
            })
        }
    }
}
masahiro331 commented 1 year ago

If you want to know at which offset in the vmdk the MBR or filesystem starts, this library currently only works with compressed VMDKs, so you won't know until you decompress it.

masahiro331 commented 1 year ago

You can read specification of vmdk. https://www.vmware.com/app/vmdk/?src=vmdk

schmidtw commented 1 year ago

Ok, great - that's exactly what I was hoping to get out of this. While I have a different filesystem impl, I'm also going to check out yours.

We're putting metadata about an image in the image (in a fat32 fs right now), but it would be easier if it was available in the ext4 fs. :smile:

masahiro331 commented 1 year ago

Can I read fat32 fs implementation?? I want to parse fat32 for windows image.

masahiro331 commented 1 year ago

I have libraries to parse VHD and VHDX for windows virtual machine image format.