Skip to content

Commit 987223a

Browse files
authored
Add support for custom mount options (#25)
2 parents 5e86be6 + ed58075 commit 987223a

File tree

3 files changed

+40
-17
lines changed

3 files changed

+40
-17
lines changed

cmd/run.go

+13-6
Original file line numberDiff line numberDiff line change
@@ -80,13 +80,18 @@ var runCmd = &cobra.Command{
8080
fsToLog = fsTypeOverride
8181
}
8282

83-
slog.Info("Mounting the device", "dev", vmMountDevName, "fs", fsToLog, "luks", luksFlag)
83+
mountOptionsToLog := "<default>"
84+
if mountOptionsFlag != "" {
85+
mountOptionsToLog = mountOptionsFlag
86+
}
8487

85-
err := fm.Mount(vmMountDevName, vm.MountOptions{
86-
LUKSContainerPreopen: vmRuntimeLUKSContainerDevice,
88+
slog.Info("Mounting the device", "dev", vmMountDevName, "fs", fsToLog, "luks", luksFlag, "mountoptions", mountOptionsToLog)
8789

88-
FSTypeOverride: fsTypeOverride,
89-
LUKS: luksFlag,
90+
err := fm.Mount(vmMountDevName, vm.MountConfig{
91+
LUKSContainerPreopen: vmRuntimeLUKSContainerDevice,
92+
FSTypeOverride: fsTypeOverride,
93+
LUKS: luksFlag,
94+
MountOptions: mountOptionsFlag,
9095
})
9196
if err != nil {
9297
slog.Error("Failed to mount the disk inside the VM", "error", err.Error())
@@ -143,6 +148,7 @@ var (
143148
shareBackendFlag string
144149
smbUseExternAddrFlag bool
145150
debugShellFlag bool
151+
mountOptionsFlag string
146152
)
147153

148154
func init() {
@@ -163,5 +169,6 @@ func init() {
163169
runCmd.Flags().StringVar(&shareListenIPFlag, "share-listen", share.GetDefaultListenIPStr(), "Specifies the IP to bind the network share port to. NOTE: For FTP, changing the bind address is not enough to connect remotely. You should also specify --ftp-extip.")
164170

165171
runCmd.Flags().StringVar(&ftpExtIPFlag, "ftp-extip", share.GetDefaultListenIPStr(), "Specifies the external IP the FTP server should advertise.")
166-
runCmd.Flags().BoolVar(&smbUseExternAddrFlag, "smb-extern", share.IsSMBExtModeDefault(), "Specifies whether Linsk emulate external networking for the VM's SMB server. This is the default for Windows as there is no way to specify ports in Windows SMB client.")
172+
runCmd.Flags().BoolVar(&smbUseExternAddrFlag, "smb-extern", share.IsSMBExtModeDefault(), "Specifies whether Linsk should emulate external networking for the VM's SMB server. This is the default for Windows as there is no way to specify ports in Windows SMB client.")
173+
runCmd.Flags().StringVar(&mountOptionsFlag, "mount-options", "", "Specifies the mount options to be passed to the -o flag of the mount.")
167174
}

utils/validations.go

+8-2
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,16 @@ func ClearUnprintableChars(s string, allowNewlines bool) string {
3737
}, s)
3838
}
3939

40-
var fsTypeRegex = regexp.MustCompile(`^[a-z0-9]+$`)
40+
var fsTypeRegexp = regexp.MustCompile(`^[a-z0-9]+$`)
4141

4242
func ValidateFsType(s string) bool {
43-
return fsTypeRegex.MatchString(s)
43+
return fsTypeRegexp.MatchString(s)
44+
}
45+
46+
var mountOptionsRegexp = regexp.MustCompile(`^([a-zA-Z0-9_]+(=[a-zA-Z0-9]+)?)(,[a-zA-Z0-9_]+(=[a-zA-Z0-9]+)?)*$`)
47+
48+
func ValidateMountOptions(s string) bool {
49+
return mountOptionsRegexp.MatchString(s)
4450
}
4551

4652
var devNameRegexp = regexp.MustCompile(`^[0-9A-Za-z_-]+$`)

vm/filemanager.go

+19-9
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,12 @@ func (fm *FileManager) Lsblk() ([]byte, error) {
8282
return ret, nil
8383
}
8484

85-
type MountOptions struct {
85+
type MountConfig struct {
8686
LUKSContainerPreopen string
8787

8888
FSTypeOverride string
8989
LUKS bool
90+
MountOptions string
9091
}
9192

9293
func (fm *FileManager) luksOpen(sc *ssh.Client, fullDevPath string, luksDMName string) error {
@@ -202,7 +203,7 @@ func (fm *FileManager) preopenLUKSContainerWithSSH(sc *ssh.Client, containerDevP
202203
return nil
203204
}
204205

205-
func (fm *FileManager) Mount(devName string, mo MountOptions) error {
206+
func (fm *FileManager) Mount(devName string, mc MountConfig) error {
206207
if devName == "" {
207208
return fmt.Errorf("device name is empty")
208209
}
@@ -220,13 +221,19 @@ func (fm *FileManager) Mount(devName string, mo MountOptions) error {
220221
fullDevPath := "/dev/" + devName
221222

222223
var fsOverride string
223-
224-
if mo.FSTypeOverride != "" {
225-
if !utils.ValidateFsType(mo.FSTypeOverride) {
224+
if mc.FSTypeOverride != "" {
225+
if !utils.ValidateFsType(mc.FSTypeOverride) {
226226
return fmt.Errorf("bad fs type override (contains illegal characters)")
227227
}
228+
fsOverride = mc.FSTypeOverride
229+
}
228230

229-
fsOverride = mo.FSTypeOverride
231+
var mountOptions string
232+
if mc.MountOptions != "" {
233+
if !utils.ValidateMountOptions(mc.MountOptions) {
234+
return fmt.Errorf("invalid mount options (contains illegal characters)")
235+
}
236+
mountOptions = mc.MountOptions
230237
}
231238

232239
sc, err := fm.vm.DialSSH()
@@ -236,14 +243,14 @@ func (fm *FileManager) Mount(devName string, mo MountOptions) error {
236243

237244
defer func() { _ = sc.Close() }()
238245

239-
if mo.LUKSContainerPreopen != "" {
240-
err := fm.preopenLUKSContainerWithSSH(sc, mo.LUKSContainerPreopen)
246+
if mc.LUKSContainerPreopen != "" {
247+
err := fm.preopenLUKSContainerWithSSH(sc, mc.LUKSContainerPreopen)
241248
if err != nil {
242249
return errors.Wrap(err, "preopen luks container")
243250
}
244251
}
245252

246-
if mo.LUKS {
253+
if mc.LUKS {
247254
luksDMName := "cryptmnt"
248255

249256
err = fm.luksOpen(sc, fullDevPath, luksDMName)
@@ -258,6 +265,9 @@ func (fm *FileManager) Mount(devName string, mo MountOptions) error {
258265
if fsOverride != "" {
259266
cmd += "-t " + shellescape.Quote(fsOverride) + " "
260267
}
268+
if mountOptions != "" {
269+
cmd += "-o " + shellescape.Quote(mountOptions) + " "
270+
}
261271
cmd += shellescape.Quote(fullDevPath) + " /mnt"
262272

263273
_, err = sshutil.RunSSHCmd(fm.vm.ctx, sc, cmd)

0 commit comments

Comments
 (0)