package platform import ( "context" "encoding/json" "fmt" "io" "os" "os/exec" "path/filepath" "strings" ) func (s *System) IsLiveMediaInRAM() bool { fsType := mountFSType("/run/live/medium") if fsType == "" { return toramActive() } return strings.EqualFold(fsType, "tmpfs") } func (s *System) LiveBootSource() LiveBootSource { fsType := mountFSType("/run/live/medium") source := mountSource("/run/live/medium") device := findLiveBootDevice() status := LiveBootSource{ InRAM: strings.EqualFold(fsType, "tmpfs"), Source: source, Device: device, } if fsType == "" && source == "" && device == "" { if toramActive() { status.InRAM = true status.Kind = "ram" status.Source = "tmpfs" return status } status.Kind = "unknown" return status } status.Kind = inferLiveBootKind(fsType, source, blockDeviceType(device), blockDeviceTransport(device)) if status.Kind == "" { status.Kind = "unknown" } if status.InRAM && strings.TrimSpace(status.Source) == "" { status.Source = "tmpfs" } return status } func (s *System) RunInstallToRAM(ctx context.Context, logFunc func(string)) error { log := func(msg string) { if logFunc != nil { logFunc(msg) } } if s.IsLiveMediaInRAM() { log("Already running from RAM — installation media can be safely disconnected.") return nil } squashfsFiles, err := filepath.Glob("/run/live/medium/live/*.squashfs") if err != nil || len(squashfsFiles) == 0 { return fmt.Errorf("no squashfs files found in /run/live/medium/live/") } free := freeMemBytes() var needed int64 for _, sf := range squashfsFiles { fi, err2 := os.Stat(sf) if err2 != nil { return fmt.Errorf("stat %s: %v", sf, err2) } needed += fi.Size() } const headroom = 256 * 1024 * 1024 if free > 0 && needed+headroom > free { return fmt.Errorf("insufficient RAM: need %s, available %s", humanBytes(needed+headroom), humanBytes(free)) } dstDir := "/dev/shm/bee-live" if err := os.MkdirAll(dstDir, 0755); err != nil { return fmt.Errorf("create tmpfs dir: %v", err) } for _, sf := range squashfsFiles { if err := ctx.Err(); err != nil { return err } base := filepath.Base(sf) dst := filepath.Join(dstDir, base) log(fmt.Sprintf("Copying %s to RAM...", base)) if err := copyFileLarge(ctx, sf, dst, log); err != nil { return fmt.Errorf("copy %s: %v", base, err) } log(fmt.Sprintf("Copied %s.", base)) loopDev, err := findLoopForFile(sf) if err != nil { log(fmt.Sprintf("Loop device for %s not found (%v) — skipping re-association.", base, err)) continue } if err := reassociateLoopDevice(loopDev, dst); err != nil { log(fmt.Sprintf("Warning: could not re-associate %s → %s: %v", loopDev, dst, err)) } else { log(fmt.Sprintf("Loop device %s now backed by RAM copy.", loopDev)) } } log("Copying remaining medium files...") if err := cpDir(ctx, "/run/live/medium", dstDir, log); err != nil { log(fmt.Sprintf("Warning: partial copy: %v", err)) } if err := ctx.Err(); err != nil { return err } if err := exec.Command("mount", "--bind", dstDir, "/run/live/medium").Run(); err != nil { log(fmt.Sprintf("Warning: rebind /run/live/medium failed: %v", err)) } log("Verifying live medium now served from RAM...") status := s.LiveBootSource() if err := verifyInstallToRAMStatus(status); err != nil { return err } log(fmt.Sprintf("Verification passed: live medium now served from %s.", describeLiveBootSource(status))) log("Done. Installation media can be safely disconnected.") return nil } func verifyInstallToRAMStatus(status LiveBootSource) error { if status.InRAM { return nil } return fmt.Errorf("install to RAM verification failed: live medium still mounted from %s", describeLiveBootSource(status)) } func describeLiveBootSource(status LiveBootSource) string { source := strings.TrimSpace(status.Device) if source == "" { source = strings.TrimSpace(status.Source) } if source == "" { source = "unknown source" } switch strings.TrimSpace(status.Kind) { case "ram": return "RAM" case "usb": return "USB (" + source + ")" case "cdrom": return "CD-ROM (" + source + ")" case "disk": return "disk (" + source + ")" default: return source } } func copyFileLarge(ctx context.Context, src, dst string, logFunc func(string)) error { in, err := os.Open(src) if err != nil { return err } defer in.Close() fi, err := in.Stat() if err != nil { return err } out, err := os.Create(dst) if err != nil { return err } defer out.Close() total := fi.Size() var copied int64 buf := make([]byte, 4*1024*1024) for { if err := ctx.Err(); err != nil { return err } n, err := in.Read(buf) if n > 0 { if _, werr := out.Write(buf[:n]); werr != nil { return werr } copied += int64(n) if logFunc != nil && total > 0 { pct := int(float64(copied) / float64(total) * 100) logFunc(fmt.Sprintf(" %s / %s (%d%%)", humanBytes(copied), humanBytes(total), pct)) } } if err == io.EOF { break } if err != nil { return err } } return out.Sync() } func cpDir(ctx context.Context, src, dst string, logFunc func(string)) error { return filepath.Walk(src, func(path string, fi os.FileInfo, err error) error { if ctx.Err() != nil { return ctx.Err() } if err != nil { return nil } rel, _ := filepath.Rel(src, path) target := filepath.Join(dst, rel) if fi.IsDir() { return os.MkdirAll(target, fi.Mode()) } if strings.HasSuffix(path, ".squashfs") { return nil } if _, err := os.Stat(target); err == nil { return nil } return copyFileLarge(ctx, path, target, nil) }) } func findLoopForFile(backingFile string) (string, error) { out, err := exec.Command("losetup", "--list", "--json").Output() if err != nil { return "", err } var result struct { Loopdevices []struct { Name string `json:"name"` BackFile string `json:"back-file"` } `json:"loopdevices"` } if err := json.Unmarshal(out, &result); err != nil { return "", err } for _, dev := range result.Loopdevices { if dev.BackFile == backingFile { return dev.Name, nil } } return "", fmt.Errorf("no loop device found for %s", backingFile) } func reassociateLoopDevice(loopDev, newFile string) error { if err := exec.Command("losetup", "--replace", loopDev, newFile).Run(); err == nil { return nil } return loopChangeFD(loopDev, newFile) }