-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
mount PVC as block device #14486
Closed
Closed
mount PVC as block device #14486
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
fbee67f
mount PVC as block device
sagor999 2a5366a
wip
sagor999 c4f4423
wip
sagor999 8d6c1ce
wip
sagor999 77c6787
wip
sagor999 21bd36b
wip
sagor999 e2533bd
wip
sagor999 987523f
wip
sagor999 5a8556c
wip
sagor999 e591856
wip
sagor999 1db627e
wip
sagor999 87ae3c7
wip
sagor999 83b7bd6
wip
sagor999 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
// Copyright (c) 2022 Gitpod GmbH. All rights reserved. | ||
// Licensed under the GNU Affero General Public License (AGPL). | ||
// See License-AGPL.txt in the project root for license information. | ||
|
||
package cmd | ||
|
||
import ( | ||
"bytes" | ||
"os" | ||
"os/exec" | ||
"time" | ||
|
||
"github.com/spf13/cobra" | ||
"golang.org/x/sys/unix" | ||
"golang.org/x/xerrors" | ||
|
||
"github.com/gitpod-io/gitpod/common-go/log" | ||
) | ||
|
||
const ( | ||
workspaceDevice = "/dev/workspace" | ||
) | ||
|
||
var fsPrepCmd = &cobra.Command{ | ||
Use: "fsprep", | ||
Short: "does fs prep and call supervisor", | ||
RunE: func(_ *cobra.Command, args []string) (err error) { | ||
defer func() { | ||
if err != nil { | ||
time.Sleep(5 * time.Minute) | ||
} | ||
}() | ||
|
||
isReady, err := isWorkspaceDeviceReady(workspaceDevice) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if !isReady { | ||
err = prepareWorkspaceDevice(workspaceDevice) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
err = os.RemoveAll("/workspace") | ||
if err != nil { | ||
log.WithError(err).Error("cannot remove /workspace") | ||
} | ||
|
||
err = mountWorkspaceDevice(workspaceDevice, "/pvc/workspace") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// write a test file into /pvc/workspace folder | ||
// this is to make sure that the workspace folder is writable | ||
// and the workspace disk is mounted | ||
err = os.WriteFile("/pvc/workspace/test", []byte("test"), 0644) | ||
if err != nil { | ||
log.WithError(err).Error("cannot write test file to /pvc/workspace") | ||
} | ||
|
||
log.Info("All done") | ||
|
||
return nil | ||
}, | ||
} | ||
|
||
func init() { | ||
rootCmd.AddCommand(fsPrepCmd) | ||
} | ||
|
||
func prepareWorkspaceDevice(device string) error { | ||
var stderr bytes.Buffer | ||
cmd := exec.Command("mkfs.ext4", "-m1", device) | ||
cmd.Stderr = &stderr | ||
if err := cmd.Run(); err != nil { | ||
log.WithError(err).WithField("reason", stderr.String()).Error("cannot mount workspace disk") | ||
return xerrors.Errorf("cannot format workspace disk using ext4: %w", err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func mountWorkspaceDevice(device, target string) error { | ||
if err := os.MkdirAll(target, 0755); err != nil { | ||
return xerrors.Errorf("cannot create directory %v: %w", target, err) | ||
} | ||
|
||
// chown it so that it is owned by gitpod:gitpod when workspace starts | ||
if err := os.Chown(target, 133332, 133332); err != nil { | ||
return xerrors.Errorf("cannot chown directory %v: %w", target, err) | ||
} | ||
|
||
if err := unix.Mount(device, target, "ext4", uintptr(0), "user_xattr"); err != nil { | ||
return xerrors.Errorf("cannot mount workspace disk in %v: %w", target, err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func isWorkspaceDeviceReady(device string) (bool, error) { | ||
var stderr bytes.Buffer | ||
cmd := exec.Command("blkid", "-o", "value", "-s", "TYPE", device) | ||
cmd.Stderr = &stderr | ||
out, err := cmd.Output() | ||
if err != nil { | ||
if exitErr, ok := err.(*exec.ExitError); ok { | ||
if exitErr.ExitCode() == 2 { | ||
// unformatted device | ||
return false, nil | ||
} | ||
} | ||
|
||
log.WithError(err).WithField("stdout", string(out)).WithField("stderr", stderr.String()).Error("cannot obtain details from the workspace disk") | ||
return false, xerrors.Errorf("cannot obtain details from the workspace disk: %w", err) | ||
} | ||
|
||
return string(out) == "ext4", nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,7 +26,8 @@ | |
"requests": { | ||
"storage": "30Gi" | ||
} | ||
} | ||
}, | ||
"volumeMode": "Block" | ||
}, | ||
"status": {} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,7 +26,8 @@ | |
"requests": { | ||
"storage": "0" | ||
} | ||
} | ||
}, | ||
"volumeMode": "Block" | ||
}, | ||
"status": {} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,7 +26,8 @@ | |
"requests": { | ||
"storage": "0" | ||
} | ||
} | ||
}, | ||
"volumeMode": "Block" | ||
}, | ||
"status": {} | ||
} | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This suggests that within a container, we should specify the device path, instead of the mount path, when mounting a PVC in volumeMode
block
.See how in this example for a pod spec, there is no volumeMount?
If we do like ☝️ , I assume we'd then have to mount the device from
workspacekit
. 🤔