Skip to content

Commit 394b17e

Browse files
adrianriobocodex
andcommitted
fix(rhel): grow disk size to external volume size
Previously the instance kept the size for the image, now it is expanded within cloud-init to the actual space for the external disk. Co-authored-by: ChatGPT <noreply@openai.com> Signed-off-by: Adrian Riobo <ariobolo@redhat.com>
1 parent d2f0535 commit 394b17e

File tree

6 files changed

+123
-35
lines changed

6 files changed

+123
-35
lines changed

pkg/integrations/integrations.go

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
package integrations
22

33
import (
4-
"fmt"
5-
"strings"
6-
4+
cloudinit "github.com/redhat-developer/mapt/pkg/util/cloud-init"
75
"github.com/redhat-developer/mapt/pkg/util/file"
86
)
97

@@ -49,11 +47,5 @@ func GetIntegrationSnippetAsCloudInitWritableFile(intCfg IntegrationConfig, user
4947
if err != nil || len(*snippet) == 0 {
5048
return snippet, err
5149
}
52-
lines := strings.Split(strings.TrimSpace(*snippet), "\n")
53-
for i, line := range lines {
54-
// Added 6 spaces before each line
55-
lines[i] = fmt.Sprintf(" %s", line)
56-
}
57-
identedSnippet := strings.Join(lines, "\n")
58-
return &identedSnippet, nil
50+
return cloudinit.IndentWriteFile(snippet)
5951
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#!/bin/sh
2+
set -eu
3+
4+
# Detect root LV (e.g. /dev/mapper/rootvg-rootlv)
5+
ROOT_LV=$(findmnt -n -o SOURCE /)
6+
7+
# Get VG name
8+
VG=$(lvs --noheadings -o vg_name "$ROOT_LV" | xargs)
9+
10+
# Get PV device (e.g. /dev/sda4 or /dev/nvme0n1p4)
11+
PV=$(pvs --noheadings -o pv_name --select vg_name="$VG" | xargs)
12+
13+
# Extract base disk and partition number safely
14+
DISK_PATH="$PV"
15+
16+
# NVMe devices end with pN (nvme0n1p4)
17+
case "$DISK_PATH" in
18+
*nvme*)
19+
DISK=$(echo "$DISK_PATH" | sed -E 's/p[0-9]+$//')
20+
PART=$(echo "$DISK_PATH" | sed -E 's/.*p([0-9]+)$/\1/')
21+
;;
22+
*)
23+
# Standard SCSI (sda4 → sda + 4)
24+
DISK=$(echo "$DISK_PATH" | sed -E 's/[0-9]+$//')
25+
PART=$(echo "$DISK_PATH" | sed -E 's/.*([0-9]+)$/\1/')
26+
;;
27+
esac
28+
29+
# Expand partition
30+
growpart "$DISK" "$PART"
31+
32+
# Resize PV
33+
pvresize "$PV"
34+
35+
# Extend LV to full free space and resize filesystem
36+
lvextend -r -l +100%FREE "$ROOT_LV"

pkg/provider/azure/action/rhel/rhel.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package rhel
22

33
import (
4+
_ "embed"
45
"fmt"
56
"strings"
67

@@ -44,13 +45,17 @@ func imageRef(version, arch string) *data.ImageReference {
4445
strings.ReplaceAll(version, ".", ""))}
4546
}
4647

48+
//go:embed expand-root-disk.sh
49+
var ExpandRootDisk []byte
50+
4751
func Create(mCtxArgs *maptContext.ContextArgs, r *RhelArgs) (err error) {
4852
logging.Debug("Creating RHEL Server")
4953
rhelCloudConfig := &rhelApi.CloudConfigArgs{
50-
SNCProfile: r.ProfileSNC,
51-
SubsUsername: r.SubsUsername,
52-
SubsPassword: r.SubsUserpass,
53-
Username: r.Username}
54+
SNCProfile: r.ProfileSNC,
55+
SubsUsername: r.SubsUsername,
56+
SubsPassword: r.SubsUserpass,
57+
Username: r.Username,
58+
ExpandRootDisk: ExpandRootDisk}
5459
azureLinuxRequest :=
5560
&azureLinux.LinuxArgs{
5661
Prefix: r.Prefix,

pkg/target/host/rhel/cloud-config-base

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,38 @@ rh_subscription:
33
username: {{.SubscriptionUsername}}
44
password: {{.SubscriptionPassword}}
55
auto-attach: true
6+
growpart:
7+
mode: auto
8+
devices: ["/"]
9+
ignore_growroot_disabled: false
610
runcmd:
11+
- /usr/local/bin/expand-root-disk.sh
712
- while fuser /var/lib/rpm/.rpm.lock > /dev/null 2>&1 ; do sleep 1 ; done
813
- dnf install -y podman
914
{{ if .ActionsRunnerSnippet }} - sudo -u {{ .Username }} bash -c /opt/install-ghrunner.sh{{ end }}
1015
{{ if .CirrusSnippet }} - /opt/setup-cirrus-service.sh{{ end }}
1116
{{ if .GitLabSnippet }} - sudo -u {{ .Username }} bash -c /opt/install-glrunner.sh{{ end }}
12-
{{ if .ActionsRunnerSnippet }}write_files:
17+
write_files:
18+
{{- if .ExpandRootDisk }}
19+
- path: /usr/local/bin/expand-root-disk.sh
20+
permissions: '0755'
21+
content: |
22+
{{ .ExpandRootDisk }}
23+
{{- end }}
24+
{{ if .ActionsRunnerSnippet }}
1325
# Github actions runner installation
1426
- content: |
1527
{{ .ActionsRunnerSnippet }}
1628
path: /opt/install-ghrunner.sh
1729
permissions: '0755'
1830
{{ end }}
19-
{{ if .CirrusSnippet }}write_files:
31+
{{ if .CirrusSnippet }}
2032
- content: |
2133
{{.CirrusSnippet}}
2234
path: /opt/setup-cirrus-service.sh
2335
permissions: '0755'
2436
{{ end }}
25-
{{ if .GitLabSnippet }}write_files:
37+
{{ if .GitLabSnippet }}
2638
- content: |
2739
{{.GitLabSnippet}}
2840
path: /opt/install-glrunner.sh

pkg/target/host/rhel/cloud-config.go

Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,15 @@ import (
99
"github.com/redhat-developer/mapt/pkg/integrations/cirrus"
1010
"github.com/redhat-developer/mapt/pkg/integrations/github"
1111
"github.com/redhat-developer/mapt/pkg/integrations/gitlab"
12+
cloudinit "github.com/redhat-developer/mapt/pkg/util/cloud-init"
1213
"github.com/redhat-developer/mapt/pkg/util/file"
1314
)
1415

1516
type CloudConfigArgs struct {
1617
SNCProfile bool
1718
SubsUsername, SubsPassword string
1819
Username string
20+
ExpandRootDisk []byte
1921
}
2022

2123
type userDataValues struct {
@@ -25,6 +27,7 @@ type userDataValues struct {
2527
ActionsRunnerSnippet string
2628
CirrusSnippet string
2729
GitLabSnippet string
30+
ExpandRootDisk string
2831
}
2932

3033
//go:embed cloud-config-base
@@ -50,15 +53,22 @@ func (r *CloudConfigArgs) CloudConfig() (*string, error) {
5053
if err != nil {
5154
return nil, err
5255
}
53-
userdata, err := file.Template(
54-
userDataValues{
55-
r.SubsUsername,
56-
r.SubsPassword,
57-
r.Username,
58-
*ghActionsRunnerSnippet,
59-
*cirrusSnippet,
60-
*gitlabSnippet},
61-
templateConfig)
56+
udv := userDataValues{
57+
SubscriptionUsername: r.SubsUsername,
58+
SubscriptionPassword: r.SubsPassword,
59+
Username: r.Username,
60+
ActionsRunnerSnippet: *ghActionsRunnerSnippet,
61+
CirrusSnippet: *cirrusSnippet,
62+
GitLabSnippet: *gitlabSnippet}
63+
if r.ExpandRootDisk != nil {
64+
snippet := string(r.ExpandRootDisk[:])
65+
iSnippet, err := cloudinit.IndentWriteFile(&snippet)
66+
if err != nil {
67+
return nil, err
68+
}
69+
udv.ExpandRootDisk = *iSnippet
70+
}
71+
userdata, err := file.Template(udv, templateConfig)
6272
if err != nil {
6373
return nil, err
6474
}
@@ -88,15 +98,22 @@ func (r *CloudConfigArgs) CloudConfigWithGitLabToken(gitlabAuthToken string) (st
8898
if err != nil {
8999
return "", err
90100
}
91-
userdata, err := file.Template(
92-
userDataValues{
93-
r.SubsUsername,
94-
r.SubsPassword,
95-
r.Username,
96-
*ghActionsRunnerSnippet,
97-
*cirrusSnippet,
98-
*gitlabSnippet},
99-
templateConfig)
101+
udv := userDataValues{
102+
SubscriptionUsername: r.SubsUsername,
103+
SubscriptionPassword: r.SubsPassword,
104+
Username: r.Username,
105+
ActionsRunnerSnippet: *ghActionsRunnerSnippet,
106+
CirrusSnippet: *cirrusSnippet,
107+
GitLabSnippet: *gitlabSnippet}
108+
if r.ExpandRootDisk != nil {
109+
snippet := string(r.ExpandRootDisk[:])
110+
iSnippet, err := cloudinit.IndentWriteFile(&snippet)
111+
if err != nil {
112+
return "", err
113+
}
114+
udv.ExpandRootDisk = *iSnippet
115+
}
116+
userdata, err := file.Template(udv, templateConfig)
100117
if err != nil {
101118
return "", err
102119
}

pkg/util/cloud-init/util.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package cloudinit
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
)
7+
8+
// If we add the snippet as part of a cloud init file the strategy
9+
// would be create the file with write_files:
10+
// i.e.
11+
// write_files:
12+
//
13+
// # Cirrus service setup
14+
// - content: |
15+
// {{ .CirrusSnippet }} <----- 6 spaces
16+
//
17+
// to do so we need to indent 6 spaces each line of the snippet
18+
func IndentWriteFile(snippet *string) (*string, error) {
19+
lines := strings.Split(strings.TrimSpace(*snippet), "\n")
20+
for i, line := range lines {
21+
// Added 6 spaces before each line
22+
lines[i] = fmt.Sprintf(" %s", line)
23+
}
24+
identedSnippet := strings.Join(lines, "\n")
25+
return &identedSnippet, nil
26+
}

0 commit comments

Comments
 (0)