|
| 1 | +// Copyright (c) 2023-2025, Nubificus LTD |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package initrd |
| 16 | + |
| 17 | +import ( |
| 18 | + "fmt" |
| 19 | + "os" |
| 20 | + "syscall" |
| 21 | + "time" |
| 22 | + |
| 23 | + "github.com/cavaliergopher/cpio" |
| 24 | + "github.com/opencontainers/runtime-spec/specs-go" |
| 25 | +) |
| 26 | + |
| 27 | +func AddInitrdRecord(w *cpio.Writer, content []byte, fileInfo *syscall.Stat_t, name string) error { |
| 28 | + hdr := &cpio.Header{ |
| 29 | + Name: name, |
| 30 | + Mode: cpio.FileMode(fileInfo.Mode), |
| 31 | + Uid: int(fileInfo.Uid), |
| 32 | + Guid: int(fileInfo.Gid), |
| 33 | + ModTime: time.Unix(fileInfo.Mtim.Sec, fileInfo.Mtim.Nsec), |
| 34 | + Size: fileInfo.Size, |
| 35 | + } |
| 36 | + err := w.WriteHeader(hdr) |
| 37 | + if err != nil { |
| 38 | + return fmt.Errorf("could not write header in initrd: %v", err) |
| 39 | + } |
| 40 | + _, err = w.Write(content) |
| 41 | + if err != nil { |
| 42 | + return fmt.Errorf("could not write contents in initrd: %v", err) |
| 43 | + } |
| 44 | + |
| 45 | + return nil |
| 46 | +} |
| 47 | + |
| 48 | +func AddFileToInitrd(w *cpio.Writer, srcFile string, destFile string) error { |
| 49 | + // Get the info of the original file |
| 50 | + fi, err := os.Stat(srcFile) |
| 51 | + if err != nil { |
| 52 | + return fmt.Errorf("Could not Stat file %s: %w", srcFile, err) |
| 53 | + } |
| 54 | + fileInfo := fi.Sys().(*syscall.Stat_t) |
| 55 | + if fi.Mode().IsRegular() { |
| 56 | + content, err := os.ReadFile(srcFile) |
| 57 | + if err != nil { |
| 58 | + return fmt.Errorf("could not read file %s: %w", srcFile, err) |
| 59 | + } |
| 60 | + err = AddInitrdRecord(w, content, fileInfo, destFile) |
| 61 | + if err != nil { |
| 62 | + return fmt.Errorf("could not add record for %s: %w", srcFile, err) |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + return nil |
| 67 | +} |
| 68 | + |
| 69 | +func CopyFileMountsToInitrd(oldInitrd string, mounts []specs.Mount) error { |
| 70 | + f, err := os.OpenFile(oldInitrd, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) |
| 71 | + if err != nil { |
| 72 | + return fmt.Errorf("Could not open %s: %v", oldInitrd, err) |
| 73 | + } |
| 74 | + defer f.Close() |
| 75 | + |
| 76 | + w := cpio.NewWriter(f) |
| 77 | + for _, m := range mounts { |
| 78 | + if m.Type != "bind" { |
| 79 | + continue |
| 80 | + } |
| 81 | + err = AddFileToInitrd(w, m.Source, m.Destination) |
| 82 | + if err != nil { |
| 83 | + return fmt.Errorf("Could not add file %s to initrd: %v", m.Source, err) |
| 84 | + } |
| 85 | + } |
| 86 | + err = w.Close() |
| 87 | + if err != nil { |
| 88 | + return fmt.Errorf("Could not close initrd %v", err) |
| 89 | + } |
| 90 | + |
| 91 | + return nil |
| 92 | +} |
0 commit comments