Skip to content

Commit

Permalink
Improve(format): write disk uuid information into file.
Browse files Browse the repository at this point in the history
Signed-off-by: Wine93 <[email protected]>
  • Loading branch information
Wine93 committed Feb 20, 2024
1 parent bac2b07 commit 1425127
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 14 deletions.
39 changes: 28 additions & 11 deletions internal/task/task/bs/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ package bs

import (
"fmt"
"path"
"regexp"
"strings"
"time"
Expand Down Expand Up @@ -57,8 +58,8 @@ type (
host string
device string
mountPoint string
oldUUID *string
uuid string
oldUuid *string
uuid *string
skipAdd bool
curveadm *cli.CurveAdm
}
Expand Down Expand Up @@ -96,9 +97,9 @@ func checkDeviceUUID(host, device string, success *bool, uuid *string) step.Lamb

func (s *step2EditFSTab) expression(express2del, express2add *string) step.LambdaType {
return func(ctx *context.Context) error {
*express2del = fmt.Sprintf("/UUID=%s/d", *s.oldUUID)
*express2del = fmt.Sprintf("/UUID=%s/d", *s.oldUuid)
*express2add = fmt.Sprintf("$ a UUID=%s %s ext4 rw,errors=remount-ro 0 0 # %s",
s.uuid, s.mountPoint, WARNING_EDIT)
*s.uuid, s.mountPoint, WARNING_EDIT)
return nil
}
}
Expand All @@ -121,16 +122,16 @@ func (s *step2EditFSTab) execute(ctx *context.Context) error {
Format: "value",
MatchTag: "UUID",
Success: &success,
Out: &s.uuid,
Out: s.uuid,
ExecOptions: curveadm.ExecOptions(),
})
steps = append(steps, &step.Lambda{
Lambda: checkDeviceUUID(s.host, s.device, &success, &s.uuid),
Lambda: checkDeviceUUID(s.host, s.device, &success, s.uuid),
})
steps = append(steps, &step.Lambda{ // generate record
Lambda: s.expression(&express2del, &express2add),
})
if len(*s.oldUUID) > 0 {
if len(*s.oldUuid) > 0 {
steps = append(steps, &step.Sed{ // remove old record
Files: []string{os.GetFSTabPath()},
Expression: &express2del,
Expand Down Expand Up @@ -163,6 +164,13 @@ func (s *step2EditFSTab) Execute(ctx *context.Context) error {
})
}

func prepare(uuid, content *string) step.LambdaType {
return func(ctx *context.Context) error {
*content = fmt.Sprintf("uuid=%s\nuuidmd5=%s", *uuid, utils.MD5Sum(*uuid))
return nil
}
}

func device2ContainerName(device string) string {
return fmt.Sprintf("curvebs-format-%s", utils.MD5Sum(device))
}
Expand All @@ -185,7 +193,7 @@ func NewFormatChunkfilePoolTask(curveadm *cli.CurveAdm, fc *configure.FormatConf
t := task.NewTask("Start Format Chunkfile Pool", subname, hc.GetSSHConfig())

// add step to task
var output, containerId, oldUUID string
var output, containerId, oldUuid, uuid, content string
var success bool
containerName := device2ContainerName(device)
layout := topology.GetCurveBSProjectLayout()
Expand All @@ -206,13 +214,13 @@ func NewFormatChunkfilePoolTask(curveadm *cli.CurveAdm, fc *configure.FormatConf
t.AddStep(&step.Lambda{
Lambda: skipFormat(&output, containerName),
})
// 2: mkfs, mount device, edit fstab, tune2fs
// 2: mkfs, mount device, edit fstab, tune2fs, write disk.meta
t.AddStep(&step.BlockId{
Device: device,
Format: "value",
MatchTag: "UUID",
Success: &success,
Out: &oldUUID,
Out: &oldUuid,
ExecOptions: curveadm.ExecOptions(),
})
t.AddStep(&step.UmountFilesystem{
Expand All @@ -237,7 +245,8 @@ func NewFormatChunkfilePoolTask(curveadm *cli.CurveAdm, fc *configure.FormatConf
t.AddStep(&step2EditFSTab{
host: host,
device: device,
oldUUID: &oldUUID,
oldUuid: &oldUuid,
uuid: &uuid,
mountPoint: mountPoint,
curveadm: curveadm,
})
Expand All @@ -246,6 +255,14 @@ func NewFormatChunkfilePoolTask(curveadm *cli.CurveAdm, fc *configure.FormatConf
ReservedBlocksPercentage: "0",
ExecOptions: curveadm.ExecOptions(),
})
t.AddStep(&step.Lambda{ // write disk.meta
Lambda: prepare(&uuid, &content),
})
t.AddStep(&step.InstallFile{
Content: &content,
HostDestPath: path.Join(mountPoint, "disk.meta"),
ExecOptions: curveadm.ExecOptions(),
})
// 3: run container to format chunkfile pool
t.AddStep(&step.PullImage{
Image: fc.GetContainerImage(),
Expand Down
6 changes: 3 additions & 3 deletions internal/task/task/bs/format_stop.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,20 +82,20 @@ func NewStopFormatTask(curveadm *cli.CurveAdm, fc *configure.FormatConfig) (*tas
t := task.NewTask("Stop Format Chunkfile Pool", subname, hc.GetSSHConfig())

var oldContainerId string
var oldUUID string
var oldUuid string

// 1: list block device and edit fstab delete record
t.AddStep(&step.BlockId{
Device: device,
Format: "value",
MatchTag: "UUID",
Out: &oldUUID,
Out: &oldUuid,
ExecOptions: curveadm.ExecOptions(),
})
t.AddStep(&step2EditFSTab{
host: host,
device: device,
oldUUID: &oldUUID,
oldUuid: &oldUuid,
mountPoint: mountPoint,
curveadm: curveadm,
skipAdd: true,
Expand Down

0 comments on commit 1425127

Please sign in to comment.