Skip to content

Commit

Permalink
Fix(format): use blkid instead lsblk to get block device uuid.
Browse files Browse the repository at this point in the history
Signed-off-by: Wine93 <[email protected]>
  • Loading branch information
Wine93 committed Jun 15, 2023
1 parent 19e2706 commit 985282c
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 13 deletions.
1 change: 1 addition & 0 deletions internal/errno/errno.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)")

Expand Down
22 changes: 22 additions & 0 deletions internal/task/step/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
17 changes: 8 additions & 9 deletions internal/task/task/bs/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
})
Expand Down Expand Up @@ -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(),
})
Expand Down
8 changes: 4 additions & 4 deletions internal/task/task/bs/format_stop.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
})
Expand Down
7 changes: 7 additions & 0 deletions pkg/module/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}}'"
Expand Down Expand Up @@ -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))
Expand Down

0 comments on commit 985282c

Please sign in to comment.