From 985282cbb1799be86039b21768f2041e62b473a1 Mon Sep 17 00:00:00 2001 From: Wine93 Date: Thu, 15 Jun 2023 19:10:56 +0800 Subject: [PATCH] Fix(format): use blkid instead lsblk to get block device uuid. Signed-off-by: Wine93 --- internal/errno/errno.go | 1 + internal/task/step/shell.go | 22 ++++++++++++++++++++++ internal/task/task/bs/format.go | 17 ++++++++--------- internal/task/task/bs/format_stop.go | 8 ++++---- pkg/module/shell.go | 7 +++++++ 5 files changed, 42 insertions(+), 13 deletions(-) diff --git a/internal/errno/errno.go b/internal/errno/errno.go index b6a281f71..7aab4b696 100644 --- a/internal/errno/errno.go +++ b/internal/errno/errno.go @@ -493,6 +493,7 @@ var ( ERR_INSTALL_OR_REMOVE_DEBIAN_PACKAGE_FAILED = EC(620024, "install or remove debian package failed (dpkg)") ERR_INSTALL_OR_REMOVE_RPM_PACKAGE_FAILED = EC(620025, "install or remove rpm package failed (rpm)") ERR_SECURE_COPY_FILE_TO_REMOTE_FAILED = EC(620026, "secure copy file to remote failed (scp)") + ERR_GET_BLOCK_DEVICE_UUID_FAILED = EC(620027, "get block device uuid failed (blkid)") ERR_RUN_SCRIPT_FAILED = EC(620998, "run script failed (bash script.sh)") ERR_RUN_A_BASH_COMMAND_FAILED = EC(620999, "run a bash command failed (bash -c)") diff --git a/internal/task/step/shell.go b/internal/task/step/shell.go index efc70d7e3..b8af2ba26 100644 --- a/internal/task/step/shell.go +++ b/internal/task/step/shell.go @@ -137,6 +137,15 @@ type ( module.ExecOptions } + BlockId struct { + Device string + Format string + MatchTag string + Success *bool + Out *string + module.ExecOptions + } + // network SocketStatistics struct { Filter string @@ -406,6 +415,19 @@ func (s *ListBlockDevice) Execute(ctx *context.Context) error { return PostHandle(s.Success, s.Out, out, err, errno.ERR_LIST_BLOCK_DEVICES_FAILED) } +func (s *BlockId) Execute(ctx *context.Context) error { + cmd := ctx.Module().Shell().BlkId(s.Device) + if len(s.Format) > 0 { + cmd.AddOption("--output=%s", s.Format) + } + if len(s.MatchTag) > 0 { + cmd.AddOption("--match-tag=%s", s.MatchTag) + } + + out, err := cmd.Execute(s.ExecOptions) + return PostHandle(s.Success, s.Out, out, err, errno.ERR_GET_BLOCK_DEVICE_UUID_FAILED) +} + // network func (s *SocketStatistics) Execute(ctx *context.Context) error { cmd := ctx.Module().Shell().SocketStatistics(s.Filter) diff --git a/internal/task/task/bs/format.go b/internal/task/task/bs/format.go index c630bd5bd..cc2291407 100644 --- a/internal/task/task/bs/format.go +++ b/internal/task/task/bs/format.go @@ -116,11 +116,10 @@ func (s *step2EditFSTab) execute(ctx *context.Context) error { NoClobber: true, ExecOptions: curveadm.ExecOptions(), }) - steps = append(steps, &step.ListBlockDevice{ // uuid for device - Device: []string{s.device}, - Format: "UUID", - NoHeadings: true, - Success: &success, + steps = append(steps, &step.BlockId{ // uuid for device + Device: s.device, + Format: "value", + MatchTag: "UUID", Out: &s.uuid, ExecOptions: curveadm.ExecOptions(), }) @@ -205,10 +204,10 @@ func NewFormatChunkfilePoolTask(curveadm *cli.CurveAdm, fc *configure.FormatConf Lambda: skipFormat(&oldContainerId), }) // 2: mkfs, mount device, edit fstab - t.AddStep(&step.ListBlockDevice{ - Device: []string{device}, - Format: "UUID", - NoHeadings: true, + t.AddStep(&step.BlockId{ + Device: device, + Format: "value", + MatchTag: "UUID", Out: &oldUUID, ExecOptions: curveadm.ExecOptions(), }) diff --git a/internal/task/task/bs/format_stop.go b/internal/task/task/bs/format_stop.go index 424b1ec2a..cb0592e8d 100644 --- a/internal/task/task/bs/format_stop.go +++ b/internal/task/task/bs/format_stop.go @@ -85,10 +85,10 @@ func NewStopFormatTask(curveadm *cli.CurveAdm, fc *configure.FormatConfig) (*tas var oldUUID string // 1: list block device and edit fstab delete record - t.AddStep(&step.ListBlockDevice{ - Device: []string{device}, - Format: "UUID", - NoHeadings: true, + t.AddStep(&step.BlockId{ + Device: device, + Format: "value", + MatchTag: "UUID", Out: &oldUUID, ExecOptions: curveadm.ExecOptions(), }) diff --git a/pkg/module/shell.go b/pkg/module/shell.go index b9f246a6f..e3c83149a 100644 --- a/pkg/module/shell.go +++ b/pkg/module/shell.go @@ -51,6 +51,7 @@ const ( TEMPLATE_FUSER = "fuser {{.options}} {{.names}}" TEMPLATE_DISKFREE = "df {{.options}} {{.files}}" TEMPLATE_LSBLK = "lsblk {{.options}} {{.devices}}" + TEMPLATE_BLKID = "blkid {{.options}} {{.device}}" // network TEMPLATE_SS = "ss {{.options}} '{{.filter}}'" @@ -214,6 +215,12 @@ func (s *Shell) LsBlk(device ...string) *Shell { return s } +func (s *Shell) BlkId(device string) *Shell { + s.tmpl = template.Must(template.New("blkid").Parse(TEMPLATE_BLKID)) + s.data["device"] = device + return s +} + // network func (s *Shell) SocketStatistics(filter string) *Shell { s.tmpl = template.Must(template.New("ss").Parse(TEMPLATE_SS))