Skip to content

Commit 655822f

Browse files
praveenkumaropenshift-merge-robot
authored andcommitted
machine: Refactor disk resize code
Add `getrootPartition` and `runGrowpart` helper which is used in follow-up commit.
1 parent ef416c4 commit 655822f

File tree

1 file changed

+28
-14
lines changed

1 file changed

+28
-14
lines changed

pkg/crc/machine/start.go

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515
"github.com/crc-org/crc/pkg/crc/cluster"
1616
"github.com/crc-org/crc/pkg/crc/constants"
1717
crcerrors "github.com/crc-org/crc/pkg/crc/errors"
18-
"github.com/crc-org/crc/pkg/crc/logging"
18+
logging "github.com/crc-org/crc/pkg/crc/logging"
1919
"github.com/crc-org/crc/pkg/crc/machine/bundle"
2020
"github.com/crc-org/crc/pkg/crc/machine/config"
2121
"github.com/crc-org/crc/pkg/crc/machine/state"
@@ -118,14 +118,39 @@ func growRootFileSystem(sshRunner *crcssh.Runner, preset crcPreset.Preset) error
118118
}
119119
// With 4.7, this is quite a manual process until https://github.com/openshift/installer/pull/4746 gets fixed
120120
// See https://github.com/crc-org/crc/issues/2104 for details
121-
rootPart, _, err := sshRunner.Run("realpath", "/dev/disk/by-label/root")
121+
rootPart, err := getrootPartition(sshRunner, "/dev/disk/by-label/root")
122122
if err != nil {
123123
return err
124124
}
125+
126+
if err := runGrowpart(sshRunner, rootPart); err != nil {
127+
return err
128+
}
129+
130+
logging.Infof("Resizing %s filesystem", rootPart)
131+
if _, _, err := sshRunner.RunPrivileged("Remounting /sysroot read/write", "mount -o remount,rw /sysroot"); err != nil {
132+
return err
133+
}
134+
if _, _, err = sshRunner.RunPrivileged(fmt.Sprintf("Growing %s filesystem", rootPart), "xfs_growfs", rootPart); err != nil {
135+
return err
136+
}
137+
138+
return nil
139+
}
140+
141+
func getrootPartition(sshRunner *crcssh.Runner, label string) (string, error) {
142+
rootPart, _, err := sshRunner.Run("realpath", label)
143+
if err != nil {
144+
return "", err
145+
}
125146
rootPart = strings.TrimSpace(rootPart)
126147
if !strings.HasPrefix(rootPart, "/dev/vda") && !strings.HasPrefix(rootPart, "/dev/sda") {
127-
return fmt.Errorf("Unexpected root device: %s", rootPart)
148+
return "", fmt.Errorf("Unexpected root device: %s", rootPart)
128149
}
150+
return rootPart, nil
151+
}
152+
153+
func runGrowpart(sshRunner *crcssh.Runner, rootPart string) error {
129154
// with '/dev/[sv]da4' as input, run 'growpart /dev/[sv]da 4'
130155
if _, _, err := sshRunner.RunPrivileged(fmt.Sprintf("Growing %s partition", rootPart), "/usr/bin/growpart", rootPart[:len("/dev/.da")], rootPart[len(rootPart)-1:]); err != nil {
131156
var exitErr *ssh.ExitError
@@ -135,19 +160,8 @@ func growRootFileSystem(sshRunner *crcssh.Runner, preset crcPreset.Preset) error
135160
if exitErr.ExitStatus() != 1 {
136161
return err
137162
}
138-
139163
logging.Debugf("No free space after %s, nothing to do", rootPart)
140-
return nil
141164
}
142-
143-
logging.Infof("Resizing %s filesystem", rootPart)
144-
if _, _, err := sshRunner.RunPrivileged("Remounting /sysroot read/write", "mount -o remount,rw /sysroot"); err != nil {
145-
return err
146-
}
147-
if _, _, err = sshRunner.RunPrivileged(fmt.Sprintf("Growing %s filesystem", rootPart), "xfs_growfs", rootPart); err != nil {
148-
return err
149-
}
150-
151165
return nil
152166
}
153167

0 commit comments

Comments
 (0)