github.com/kaydxh/golang@v0.0.131/pkg/storage/mount/mount.go (about) 1 /* 2 *Copyright (c) 2022, kaydxh 3 * 4 *Permission is hereby granted, free of charge, to any person obtaining a copy 5 *of this software and associated documentation files (the "Software"), to deal 6 *in the Software without restriction, including without limitation the rights 7 *to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 *copies of the Software, and to permit persons to whom the Software is 9 *furnished to do so, subject to the following conditions: 10 * 11 *The above copyright notice and this permission notice shall be included in all 12 *copies or substantial portions of the Software. 13 * 14 *THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 *IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 *FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 *AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 *LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 *OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 *SOFTWARE. 21 */ 22 package mount 23 24 import ( 25 "fmt" 26 "os" 27 "path/filepath" 28 "time" 29 30 io_ "github.com/kaydxh/golang/go/io" 31 32 os_ "github.com/kaydxh/golang/go/os" 33 exec_ "github.com/kaydxh/golang/go/os/exec" 34 ) 35 36 const ( 37 mountLabelFileName = "mount-label-file-75248" 38 ) 39 40 func MountCeph( 41 prefixMountPath, userName, address, keyring, workDir string, forceMount bool, timeout int, 42 ) (mountPoint string, err error) { 43 keyringFilePath, err := genCephKeyringConfFile(prefixMountPath, userName, keyring) 44 if err != nil { 45 return "", err 46 } 47 48 workDir = filepath.Join("/", workDir) 49 50 cmd := fmt.Sprintf( 51 `ceph-fuse -m %s -k %s -n client.%s -o rw.nonempty -r %s %s`, 52 address, 53 keyringFilePath, 54 userName, 55 workDir, 56 mountPoint, 57 ) 58 return doMountCmd(prefixMountPath, workDir, cmd, forceMount, timeout) 59 } 60 61 func MountCfs( 62 prefixMountPath, address, workDir string, 63 forceMount bool, 64 timeout int, 65 ) (mountPoint string, err error) { 66 67 mountPoint = filepath.Join(prefixMountPath, workDir) 68 workDir = filepath.Join("/", workDir) 69 70 cmd := fmt.Sprintf( 71 `mount -t nfs -o vers=4 %s:%s %s`, 72 address, 73 workDir, 74 mountPoint, 75 ) 76 return doMountCmd(prefixMountPath, workDir, cmd, forceMount, timeout) 77 } 78 79 func doMountCmd( 80 prefixMountPath, workDir, cmd string, 81 forceMount bool, 82 timeout int, 83 ) (mountPoint string, err error) { 84 85 mountPoint = filepath.Join("/", prefixMountPath, workDir) 86 if !forceMount { 87 labelFilePath := filepath.Join(mountPoint, mountLabelFileName) 88 exist, err := os_.PathExist(labelFilePath) 89 if err == nil && exist { 90 return mountPoint, nil 91 } 92 } 93 94 err = os.MkdirAll(mountPoint, 0755) 95 if err != nil { 96 return "", err 97 } 98 99 _, _, err = exec_.Exec(time.Duration(timeout), cmd) 100 if err != nil { 101 return "", err 102 } 103 104 return mountPoint, nil 105 } 106 107 func genCephKeyringConfFile(prefixMountPath, userName, keyring string) (string, error) { 108 keyringFileName := userName + ".keyring" 109 keyringFilePath := filepath.Join(prefixMountPath, keyringFileName) 110 111 line1 := "[client." + userName + "]" 112 line2 := fmt.Sprintf(" key = %s\n", keyring) 113 lines := []string{line1, line2} 114 115 err := io_.WriteFileLines(keyringFilePath, lines, false) 116 if err != nil { 117 return "", err 118 } 119 120 return keyringFilePath, nil 121 }