Skip to content

Instantly share code, notes, and snippets.

@hallyn
Created October 14, 2021 03:57
Show Gist options
  • Select an option

  • Save hallyn/24acd9c7cf407c0561629b5c032f2bb8 to your computer and use it in GitHub Desktop.

Select an option

Save hallyn/24acd9c7cf407c0561629b5c032f2bb8 to your computer and use it in GitHub Desktop.
broken go-disks fat32 example
package main
import (
"fmt"
"io"
"os"
"github.com/diskfs/go-diskfs/filesystem/fat32"
)
func main() {
var size int64 = 128 * 1024 * 1024
fp, err := os.OpenFile("/tmp/fat", os.O_TRUNC|os.O_RDWR|os.O_CREATE|os.O_EXCL, 0644)
if err != nil {
fmt.Printf("open failed: %v", err)
os.Exit(1)
}
_, err = fat32.Create(fp, size, 0, 512, "EFIBOOT")
if err != nil {
fmt.Printf("Create failed: %v", err)
os.Exit(1)
}
defer fp.Close()
fs, err := fat32.Read(fp, size, 0, 512)
if err != nil {
fmt.Printf("fat32.Read failed: %v", err)
os.Exit(1)
}
src, err := os.Open("/tmp/in")
if err != nil {
fmt.Printf("os.Open failed: %v", err)
os.Exit(1)
}
defer src.Close()
err = fs.Mkdir("/EFI/BOOT")
if err != nil {
fmt.Printf("mkdir failed: %v", err)
os.Exit(1)
}
dest, err := fs.OpenFile("/EFI/BOOT/kernel.efi", os.O_CREATE|os.O_RDWR|os.O_TRUNC)
if err != nil {
fmt.Printf("fs.OpenFile failed: %v", err)
os.Exit(1)
}
defer dest.Close()
b, err := io.Copy(dest, src)
if err != nil {
fmt.Printf("io.Copy failed: %v", err)
os.Exit(1)
}
fmt.Printf("copied %d bytes\n", b)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment