move to zerolog

This commit is contained in:
Mikaël Cluseau
2024-01-20 16:41:54 +01:00
parent 5ab8b74041
commit 6bf1d1ccf2
18 changed files with 205 additions and 170 deletions

48
lvm.go
View File

@ -3,15 +3,17 @@ package main
import (
"bytes"
"io/fs"
"log"
"os"
"os/exec"
"path/filepath"
"sort"
"strconv"
"novit.nc/direktil/initrd/lvm"
"github.com/rs/zerolog/log"
config "novit.tech/direktil/pkg/bootstrapconfig"
"novit.tech/direktil/initrd/lvm"
)
func sortedKeys[T any](m map[string]T) (keys []string) {
@ -25,7 +27,7 @@ func sortedKeys[T any](m map[string]T) (keys []string) {
func setupLVM(cfg *config.Config) {
if len(cfg.LVM) == 0 {
log.Print("no LVM VG configured.")
log.Info().Msg("no LVM VG configured.")
return
}
@ -75,15 +77,17 @@ func setupVG(vg config.LvmVG) {
}
}
log := log.With().Str("vg", vg.VG).Logger()
if devNeeded <= 0 {
log.Print("LVM VG ", vg.VG, " has all its devices")
log.Info().Msg("LVM VG has all its devices")
return
}
if vgExists {
log.Printf("LVM VG %s misses %d devices", vg.VG, devNeeded)
log.Info().Msgf("LVM VG misses %d devices", devNeeded)
} else {
log.Printf("LVM VG %s does not exists, creating", vg.VG)
log.Info().Msg("LVM VG does not exists, creating")
}
devNames := make([]string, 0)
@ -108,20 +112,16 @@ func setupVG(vg config.LvmVG) {
m := regexpSelectN(vg.PVs.N, vg.PVs.Regexps, devNames)
if len(m) == 0 {
log.Printf("no devices match the regexps %v", vg.PVs.Regexps)
for _, d := range devNames {
log.Print("- ", d)
}
log.Error().Strs("regexps", vg.PVs.Regexps).Msg("no device match the regexps")
fatalf("failed to setup VG %s", vg.VG)
}
if vgExists {
log.Print("- extending vg to ", m)
log.Info().Strs("devices", m).Msg("LVM VG: extending")
run("vgextend", append([]string{vg.VG}, m...)...)
devNeeded -= len(m)
} else {
log.Print("- creating vg with devices ", m)
log.Info().Strs("devices", m).Msg("LVM VG: creating")
run("vgcreate", append([]string{vg.VG}, m...)...)
devNeeded -= len(m)
}
@ -142,17 +142,17 @@ func setupLVs(vg config.LvmVG, createdDevs map[string]string) {
defaults := vg.Defaults
for _, lv := range vg.LVs {
lvKey := vg.VG + "/" + lv.Name
for idx, lv := range vg.LVs {
log := log.With().Str("vg", vg.VG).Str("lv", lv.Name).Logger()
if contains(lvs, func(v lvm.LV) bool {
return v.VGName == vg.VG && v.Name == lv.Name
}) {
log.Printf("LV %s exists", lvKey)
log.Info().Msg("LV exists")
continue
}
log.Printf("creating LV %s", lvKey)
log.Info().Msg("LV does not exist")
if lv.Raid == nil {
lv.Raid = defaults.Raid
@ -161,7 +161,7 @@ func setupLVs(vg config.LvmVG, createdDevs map[string]string) {
args := make([]string, 0)
if lv.Name == "" {
fatalf("LV has no name")
fatalf("LV[%d] has no name", idx)
}
args = append(args, vg.VG, "--name", lv.Name)
@ -184,7 +184,7 @@ func setupLVs(vg config.LvmVG, createdDevs map[string]string) {
}
}
log.Print("lvcreate args: ", args)
log.Info().Strs("args", args).Msg("LV: creating")
run("lvcreate", args...)
dev := "/dev/" + vg.VG + "/" + lv.Name
@ -286,12 +286,12 @@ func setupCrypt(devSpecs []config.CryptDev, createdDevs map[string]string) {
}
if !eq {
log.Print("passwords don't match")
log.Error().Msg("passwords don't match")
goto retry
}
}
log.Print("formatting encrypted device ", dev)
log.Info().Str("dev", dev).Msg("formatting encrypted device")
cmd := exec.Command("cryptsetup", "luksFormat", dev, "--key-file=-")
cmd.Stdin = bytes.NewBuffer(password)
cmd.Stdout = stdout
@ -312,7 +312,7 @@ func setupCrypt(devSpecs []config.CryptDev, createdDevs map[string]string) {
}
}
log.Print("openning encrypted device ", name, " from ", dev)
log.Info().Str("name", name).Str("dev", dev).Msg("openning encrypted device")
cmd := exec.Command("cryptsetup", "open", dev, name, "--key-file=-")
cmd.Stdin = bytes.NewBuffer(password)
cmd.Stdout = stdout
@ -364,7 +364,7 @@ func devInitialized(dev string) bool {
func setupFS(dev, fs string) {
if devInitialized(dev) {
log.Print("device ", dev, " already formatted")
log.Info().Str("dev", dev).Msg("device already formatted")
return
}
@ -372,7 +372,7 @@ func setupFS(dev, fs string) {
fs = "ext4"
}
log.Print("formatting ", dev, " (", fs, ")")
log.Info().Str("dev", dev).Str("fs", fs).Msg("formatting device")
args := make([]string, 0)
switch fs {