2022-01-08 22:33:14 +00:00
|
|
|
package main
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"io"
|
|
|
|
"time"
|
|
|
|
"log"
|
|
|
|
"strings"
|
2022-01-26 22:40:45 +00:00
|
|
|
"runtime"
|
|
|
|
"strconv"
|
2022-02-08 11:28:49 +00:00
|
|
|
"crypto/sha256"
|
2022-01-08 22:33:14 +00:00
|
|
|
"github.com/schollz/progressbar/v3"
|
|
|
|
"github.com/fatih/color"
|
|
|
|
"github.com/briandowns/spinner"
|
2022-01-11 13:43:16 +00:00
|
|
|
flag "github.com/spf13/pflag"
|
2022-01-13 22:25:04 +00:00
|
|
|
ac "github.com/JoaoDanielRufino/go-input-autocomplete"
|
2022-01-08 22:33:14 +00:00
|
|
|
)
|
|
|
|
|
2022-01-11 13:43:16 +00:00
|
|
|
var device string
|
|
|
|
var input string
|
|
|
|
var force bool
|
2022-02-08 11:28:49 +00:00
|
|
|
var disable_hash bool
|
2022-01-11 13:43:16 +00:00
|
|
|
|
|
|
|
func init() {
|
|
|
|
flag.StringVarP(&device, "device", "d", "", "target device")
|
|
|
|
flag.StringVarP(&input, "input", "i", "", "input file")
|
|
|
|
flag.BoolVarP(&force, "force", "f", false, "override safety features")
|
2022-02-08 11:28:49 +00:00
|
|
|
flag.BoolVarP(&disable_hash, "no-hash", "n", false, "disable hash verification")
|
2022-01-11 13:43:16 +00:00
|
|
|
flag.Parse()
|
|
|
|
}
|
|
|
|
|
2022-01-08 22:33:14 +00:00
|
|
|
func GetPath() string {
|
2022-01-13 22:25:04 +00:00
|
|
|
path, err := ac.Read("[ " + color.YellowString("i") + " ] Please input your image file: ")
|
2022-01-08 22:33:14 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
path = strings.TrimSpace(path)
|
|
|
|
if strings.HasPrefix(path, "~/") {
|
|
|
|
homedir, err := os.UserHomeDir()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
return homedir + path[1:]
|
|
|
|
}
|
2022-01-13 22:32:55 +00:00
|
|
|
if path == "" {
|
|
|
|
fmt.Println("[", color.RedString("!"), "] No image given, retrying.")
|
|
|
|
path = GetPath()
|
|
|
|
}
|
2022-01-08 22:33:14 +00:00
|
|
|
return path
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetDest() string {
|
2022-01-26 22:40:45 +00:00
|
|
|
PrintAvail()
|
2022-01-13 22:25:04 +00:00
|
|
|
dest, err := ac.Read("[ " + color.YellowString("i") + " ] Please input destination: ")
|
2022-01-08 22:33:14 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
dest = strings.TrimSpace(dest)
|
|
|
|
if strings.HasPrefix(dest, "~/") {
|
|
|
|
homedir, err := os.UserHomeDir()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
return homedir + dest[1:]
|
|
|
|
}
|
2022-01-13 22:32:55 +00:00
|
|
|
if dest == "" {
|
|
|
|
fmt.Println("[", color.RedString("!"), "] No destination given, retrying.")
|
|
|
|
dest = GetDest()
|
|
|
|
}
|
2022-01-08 22:33:14 +00:00
|
|
|
return dest
|
|
|
|
}
|
|
|
|
|
2022-01-09 15:49:43 +00:00
|
|
|
func WriteImage(image *os.File, target *os.File, size int64) (int64, error) {
|
2022-01-12 23:09:48 +00:00
|
|
|
bar := progressbar.NewOptions(int(size),
|
|
|
|
progressbar.OptionSetWriter(os.Stderr),
|
|
|
|
progressbar.OptionShowBytes(true),
|
2022-01-13 10:43:32 +00:00
|
|
|
progressbar.OptionSetWidth(25),
|
2022-01-12 23:09:48 +00:00
|
|
|
progressbar.OptionSetDescription("Writing image file..."),
|
|
|
|
progressbar.OptionSetTheme(progressbar.Theme{
|
|
|
|
Saucer: "=",
|
|
|
|
SaucerHead: ">",
|
|
|
|
SaucerPadding: " ",
|
|
|
|
BarStart: "[",
|
|
|
|
BarEnd: "]",
|
|
|
|
}))
|
2022-01-09 15:49:43 +00:00
|
|
|
writer := io.MultiWriter(target, bar)
|
|
|
|
written, err := io.Copy(writer, image)
|
2022-01-08 22:33:14 +00:00
|
|
|
if err != nil {
|
2022-01-09 15:49:43 +00:00
|
|
|
return 0, err
|
2022-01-08 22:33:14 +00:00
|
|
|
}
|
2022-01-09 15:49:43 +00:00
|
|
|
return written, err
|
2022-01-08 22:33:14 +00:00
|
|
|
}
|
|
|
|
|
2022-01-26 22:40:45 +00:00
|
|
|
func PrintAvail() {
|
|
|
|
if runtime.GOOS == "linux" {
|
|
|
|
block, _ := os.ReadDir("/sys/block")
|
|
|
|
if len(block) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
var targets []string
|
|
|
|
for _, device := range block {
|
|
|
|
if strings.HasPrefix(device.Name(), "sd") {
|
|
|
|
targets = append(targets, device.Name())
|
|
|
|
}
|
|
|
|
if strings.HasPrefix(device.Name(), "nvme") {
|
|
|
|
targets = append(targets, device.Name())
|
|
|
|
}
|
|
|
|
if strings.HasPrefix(device.Name(), "vd") {
|
|
|
|
targets = append(targets, device.Name())
|
|
|
|
}
|
|
|
|
}
|
2022-02-07 12:03:38 +00:00
|
|
|
fmt.Println("Available devices:")
|
2022-01-26 22:40:45 +00:00
|
|
|
for _, target := range targets {
|
|
|
|
sizefile, _ := os.Open("/sys/block/" + target + "/size")
|
|
|
|
sizeread, _ := io.ReadAll(sizefile)
|
|
|
|
_ = sizefile.Close()
|
|
|
|
sizestring := strings.ReplaceAll(string(sizeread), "\n", "")
|
|
|
|
size, _ := strconv.Atoi(sizestring)
|
|
|
|
size = size * 512
|
|
|
|
size = size / 1024 / 1024 / 1024
|
|
|
|
|
|
|
|
fmt.Print(" * ", "/dev/" + target)
|
|
|
|
if size > 0 {
|
|
|
|
fmt.Print(" [", size, "GB]\n")
|
|
|
|
} else {
|
|
|
|
fmt.Println("")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-08 22:33:14 +00:00
|
|
|
func main() {
|
|
|
|
s := spinner.New(spinner.CharSets[14], 100*time.Millisecond)
|
|
|
|
|
2022-01-11 13:43:16 +00:00
|
|
|
if input == "" {
|
2022-01-12 22:35:36 +00:00
|
|
|
if len(flag.Args()) == 0 {
|
|
|
|
input = GetPath()
|
|
|
|
} else if len(flag.Args()) > 0 {
|
|
|
|
input = flag.Args()[0]
|
|
|
|
}
|
2022-01-11 13:43:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if device == "" {
|
2022-01-12 22:35:36 +00:00
|
|
|
if len(flag.Args()) == 0 {
|
|
|
|
device = GetDest()
|
|
|
|
} else if len(flag.Args()) > 0 {
|
|
|
|
if input == flag.Args()[0] && len(flag.Args()) > 1 {
|
|
|
|
device = flag.Args()[1]
|
|
|
|
} else if input != flag.Args()[0] && len(flag.Args()) > 0 {
|
|
|
|
device = flag.Args()[0]
|
|
|
|
}
|
|
|
|
}
|
2022-01-11 13:43:16 +00:00
|
|
|
}
|
2022-01-09 15:56:13 +00:00
|
|
|
|
|
|
|
s.Prefix = "[ "
|
|
|
|
s.Suffix = " ] Getting file details"
|
2022-01-08 22:33:14 +00:00
|
|
|
s.Start()
|
2022-01-13 22:53:39 +00:00
|
|
|
statinput, err := os.Stat(input)
|
2022-01-16 01:56:10 +00:00
|
|
|
if err != nil {
|
|
|
|
s.Stop()
|
|
|
|
fmt.Println("\r[", color.RedString("✘"), "] Getting file details ")
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2022-01-13 22:53:39 +00:00
|
|
|
statdevice, err := os.Stat(device)
|
2022-01-08 22:33:14 +00:00
|
|
|
if err != nil {
|
|
|
|
s.Stop()
|
|
|
|
fmt.Println("\r[", color.RedString("✘"), "] Getting file details ")
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2022-01-11 13:43:16 +00:00
|
|
|
image, err := os.Open(input)
|
2022-01-16 01:56:10 +00:00
|
|
|
if err != nil {
|
|
|
|
s.Stop()
|
2022-02-07 11:36:08 +00:00
|
|
|
fmt.Println("\r[", color.RedString("✘"), "] Getting file details ")
|
2022-01-16 01:56:10 +00:00
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2022-01-14 22:48:30 +00:00
|
|
|
var inputsize int64
|
2022-01-16 01:56:10 +00:00
|
|
|
var inputisblock bool
|
2022-01-14 22:48:30 +00:00
|
|
|
if statinput.Size() != 0 {
|
|
|
|
inputsize = statinput.Size()
|
2022-01-16 01:56:10 +00:00
|
|
|
inputisblock = false
|
2022-01-14 22:48:30 +00:00
|
|
|
} else {
|
|
|
|
inputsize, err = image.Seek(0, io.SeekEnd)
|
2022-01-16 01:56:10 +00:00
|
|
|
inputisblock = true
|
2022-02-07 11:21:51 +00:00
|
|
|
_, _ = image.Seek(0, 0)
|
2022-01-09 15:49:43 +00:00
|
|
|
}
|
2022-01-11 13:43:16 +00:00
|
|
|
target, err := os.OpenFile(device, os.O_RDWR, 0660)
|
2022-01-16 01:56:10 +00:00
|
|
|
if err != nil {
|
|
|
|
s.Stop()
|
2022-02-07 11:36:08 +00:00
|
|
|
fmt.Println("\r[", color.RedString("✘"), "] Getting file details ")
|
2022-01-16 01:56:10 +00:00
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2022-01-14 22:48:30 +00:00
|
|
|
var targetsize int64
|
2022-01-16 01:56:10 +00:00
|
|
|
var targetisblock bool
|
2022-01-14 22:48:30 +00:00
|
|
|
if statdevice.Size() != 0 {
|
|
|
|
targetsize = statdevice.Size()
|
2022-01-16 01:56:10 +00:00
|
|
|
targetisblock = false
|
2022-01-14 22:48:30 +00:00
|
|
|
} else {
|
|
|
|
targetsize, err = target.Seek(0, io.SeekEnd)
|
2022-01-16 01:56:10 +00:00
|
|
|
targetisblock = true
|
2022-02-07 11:21:51 +00:00
|
|
|
_, _ = target.Seek(0, 0)
|
2022-01-14 22:48:30 +00:00
|
|
|
}
|
2022-02-08 11:28:49 +00:00
|
|
|
prehash := sha256.New()
|
|
|
|
if ! (force || disable_hash) {
|
|
|
|
if err != nil {
|
|
|
|
s.Stop()
|
|
|
|
fmt.Println("\r[", color.RedString("✘"), "] Getting file details ")
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = io.Copy(prehash, image)
|
|
|
|
_, _ = image.Seek(0, 0)
|
|
|
|
}
|
2022-01-09 15:49:43 +00:00
|
|
|
if err != nil {
|
|
|
|
s.Stop()
|
2022-02-07 11:36:08 +00:00
|
|
|
fmt.Println("\r[", color.RedString("✘"), "] Getting file details ")
|
2022-01-09 15:49:43 +00:00
|
|
|
log.Fatal(err)
|
|
|
|
} else {
|
|
|
|
s.Stop()
|
2022-02-07 11:36:08 +00:00
|
|
|
fmt.Println("\r[", color.GreenString("✓"), "] Getting file details ")
|
2022-01-09 15:49:43 +00:00
|
|
|
}
|
2022-01-14 22:48:30 +00:00
|
|
|
inputmb := fmt.Sprint("[", inputsize / 1024 / 1024, "MB]")
|
|
|
|
devicemb := fmt.Sprint("[", targetsize / 1024 / 1024, "MB]")
|
2022-01-16 01:56:10 +00:00
|
|
|
var inputblock string
|
|
|
|
var targetblock string
|
|
|
|
if inputisblock == true {
|
|
|
|
inputblock = "[Blockdevice]"
|
|
|
|
} else {
|
|
|
|
inputblock = "[File]"
|
|
|
|
}
|
|
|
|
if targetisblock == true {
|
|
|
|
targetblock = "[Blockdevice]"
|
|
|
|
} else {
|
|
|
|
targetblock = "[File]"
|
|
|
|
}
|
|
|
|
fmt.Println("[", color.BlueString("i"), "] Input device/file: " + input, inputmb, inputblock)
|
|
|
|
fmt.Println("[", color.BlueString("i"), "] Output device/file: " + device, devicemb, targetblock)
|
2022-02-07 11:44:07 +00:00
|
|
|
if force == false {
|
|
|
|
if inputsize > targetsize {
|
|
|
|
fmt.Println("[", color.RedString("w"), "]", color.RedString(" Warning:"), "Input file seems to be bigger than the destination!")
|
|
|
|
}
|
|
|
|
fmt.Print(color.HiWhiteString("Do you want to continue? [y/N]: "))
|
|
|
|
var yesno string
|
|
|
|
_, _ = fmt.Scanln(&yesno)
|
|
|
|
yesno = strings.TrimSpace(yesno)
|
|
|
|
if ! (yesno == "y" || yesno == "Y") {
|
|
|
|
log.Fatal("aborted")
|
|
|
|
}
|
2022-01-12 23:09:48 +00:00
|
|
|
}
|
2022-01-14 22:48:30 +00:00
|
|
|
written, err := WriteImage(image, target, inputsize)
|
2022-02-08 11:28:49 +00:00
|
|
|
_, _ = target.Seek(0, 0)
|
2022-01-08 22:33:14 +00:00
|
|
|
if err != nil {
|
2022-01-12 23:09:48 +00:00
|
|
|
fmt.Println("\r[", color.RedString("✘"), "] Writing image,", written, "bytes written ")
|
2022-01-08 22:33:14 +00:00
|
|
|
log.Fatal(err)
|
|
|
|
} else {
|
2022-01-12 23:09:48 +00:00
|
|
|
fmt.Println("\r[", color.GreenString("✓"), "] Writing image,", written, "bytes written ")
|
2022-01-08 22:33:14 +00:00
|
|
|
}
|
2022-01-09 15:49:43 +00:00
|
|
|
|
|
|
|
s.Prefix = "[ "
|
|
|
|
s.Suffix = " ] Syncing"
|
|
|
|
s.Start()
|
2022-02-08 11:28:49 +00:00
|
|
|
err = image.Sync()
|
|
|
|
if err != nil {
|
|
|
|
s.Stop()
|
|
|
|
fmt.Println("\r[", color.RedString("✘"), "] Syncing ")
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
err = target.Sync()
|
2022-01-09 15:49:43 +00:00
|
|
|
if err != nil {
|
|
|
|
s.Stop()
|
|
|
|
fmt.Println("\r[", color.RedString("✘"), "] Syncing ")
|
|
|
|
log.Fatal(err)
|
|
|
|
} else {
|
|
|
|
s.Stop()
|
|
|
|
fmt.Println("\r[", color.GreenString("✓"), "] Syncing ")
|
|
|
|
}
|
2022-02-08 11:28:49 +00:00
|
|
|
if ! (force || disable_hash) {
|
|
|
|
s.Prefix = "[ "
|
|
|
|
s.Suffix = " ] Verifying"
|
|
|
|
s.Start()
|
|
|
|
posthash := sha256.New()
|
|
|
|
_, err = io.CopyN(posthash, target, inputsize)
|
|
|
|
presum := fmt.Sprintf("%x", prehash.Sum(nil))
|
|
|
|
postsum := fmt.Sprintf("%x", posthash.Sum(nil))
|
|
|
|
if err != nil || presum != postsum {
|
|
|
|
s.Stop()
|
|
|
|
fmt.Println("\r[", color.RedString("✘"), "] Verifying ")
|
|
|
|
log.Fatal(err)
|
|
|
|
} else {
|
|
|
|
s.Stop()
|
|
|
|
fmt.Println("\r[", color.GreenString("✓"), "] Verifying ")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
err = image.Close()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
err = target.Close()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2022-01-08 22:33:14 +00:00
|
|
|
}
|