A Go client library for SWUpdate, the software update framework for embedded Linux.
It speaks the SWUpdate IPC protocol over Unix domain sockets, letting you trigger updates and monitor progress from Go code running on the same device.
go get github.com/borgeuz/goswu
sock := goswu.NewSocket(goswu.WithImagePath("/tmp/update.swu"))
client := goswu.NewClient(sock, goswu.ParseSelection("stable,main"))
if err := client.Install(); err != nil {
log.Fatal(err)
}f, err := os.Open("/tmp/update.swu")
if err != nil {
log.Fatal(err)
}
defer f.Close()
sock := goswu.NewSocket(goswu.WithImageReader(f))
client := goswu.NewClient(sock, nil)
if err := client.Install(); err != nil {
log.Fatal(err)
}client.SetDryRun(true)
err := client.Install() // simulates the update, no changes writtenprogress, err := client.Progress()
if err != nil {
log.Fatal(err)
}
fmt.Printf("status=%d step=%d/%d percent=%d%%\n",
progress.Status, progress.CurStep, progress.NSteps, progress.CurPercent)sock := goswu.NewSocket()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
ch, err := client.StreamProgress(ctx)
if err != nil {
log.Fatal(err)
}
for p := range ch {
fmt.Printf("status=%d step=%d/%d percent=%d%%\n",
p.Status, p.CurStep, p.NSteps, p.CurPercent)
}The channel closes when the update reaches StatusSuccess/StatusFailure, the connection drops, or the context is cancelled.
sock := goswu.NewSocket(
goswu.WithControlPath("/run/swupdate/ctrl"),
goswu.WithProgressPath("/run/swupdate/progress"),
goswu.WithImagePath("/tmp/update.swu"),
)SWUpdate exposes two Unix sockets:
- Control socket (
/tmp/sockinstctrl) -- send install requests, receive ACK/NACK - Progress socket (
/tmp/swupdateprog) -- readprogress_msgstructs with status, percentage, current step, etc.
The client serializes requests and messages matching the C structs from SWUpdate's network_ipc.h, so it can talk directly to the daemon without CGo.
types.go -- public enums (Status, RunType, SourceType)
errors.go -- sentinel errors
ipc.go -- internal IPC protocol (magic, msgType, ipcMsg)
progress.go -- ProgressMsg and its deserialization
request.go -- Request struct, binary serialization, Selection
transport.go -- Transport interface
socket.go -- Unix socket Transport implementation
client.go -- high-level Client API
MIT