mirror of
https://github.com/sipeed/NanoKVM.git
synced 2026-11-09 17:44:43 -06:00
Only one side may write the mounted image or the /data partition at a time, and uploaded images are validated before they are stored. Image mounts: * Serve every mounted image read-only, in CD-ROM and mass storage mode alike: detach the LUN first and set the read-only flag before the image is attached, so the target machine can neither write to nor format it. The flags were previously only written for CD-ROM mounts, so a mass storage mount inherited ro=0 and the image stayed writable from the host. * Report the virtual disk as on only while the LUN really carries the /data partition. With an image mounted the switch used to stay active but unchangeable, and enabling the disk is now a confirmed action instead of a rejected one: it cancels the mounted image, and the confirm also warns that the image download is interrupted because /data becomes read-only here. * Ask before mounting: the image list warns that the virtual disk is released for the image, or offers to enable it first when it is off, instead of dropping the /data share silently. The new strings are translated for every locale. * Name the mounted medium and the USB device after the image file, cut to the 16 bytes of the SCSI inquiry product field and the 126 bytes of the USB product string, and restore the previous names on unmount. * Reset the LUN flags on every gadget restart. They survived a restart, so the shared partition came back to the host read-only, or as an optical drive, after an image had been mounted. The /data handover: * Mount /data read-only at boot whenever the virtual disk serves the raw partition as a writable disk, so the handover holds from the first boot on. * Add SetDataWritable(), which remounts /data and verifies the new state. Enabling the virtual disk makes /data read-only before the host can write it, and refuses to expose the disk when the remount fails instead of taking the risk. Unmounting an image, which hands the raw partition back to the host, does the same, while mounting an image switches /data back to writable because a served image is read-only for the host. * Cancel a running image transfer before the handover and wait for it to stop, and report the state in the image list, where mounting still works while deleting and uploading do not. The delete action is disabled with a hint, and the downloader offers to turn the virtual disk off. Image uploads: * Validate the volume descriptors while streaming, so an unusable image is rejected in seconds instead of after the whole transfer: accept ISO 9660 and UDF volume descriptors (CD001, BEA01) as well as raw disk images (GPT, APFS, HFS+, MBR), and reject anything else. Require the image to fit in /data with a 128 MiB reserve before any of it is received, log the underlying errors on the rejection paths, and keep the temporary file, the progress ticker and the close handling in defers. Add unit tests for the upload header check, the free space preflight and the image name truncation.
43 lines
1.0 KiB
Go
43 lines
1.0 KiB
Go
package storage
|
|
|
|
import "testing"
|
|
|
|
func TestImageName(t *testing.T) {
|
|
tests := []struct {
|
|
file string
|
|
want string
|
|
}{
|
|
{"/data/ubuntu.iso", "ubuntu"},
|
|
{"/data/win11.img", "win11"},
|
|
{"/data/a.b.c.iso", "a.b.c"},
|
|
{"/data/no-extension", "no-extension"},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
if got := imageName(tt.file); got != tt.want {
|
|
t.Errorf("imageName(%q) = %q, want %q", tt.file, got, tt.want)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestCutName(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
size int
|
|
want string
|
|
}{
|
|
{"ubuntu", 16, "ubuntu"},
|
|
{"Install_macOS_27_Golden_Gate", 16, "Install_macOS_27"},
|
|
{"Install_macOS_27_Golden_Gate", 126, "Install_macOS_27_Golden_Gate"},
|
|
// 16 bytes is 5 three byte runes and 1 spare byte
|
|
{"中文安装镜像文件系统", 16, "中文安装镜"},
|
|
{"中文安装镜像文件系统", 126, "中文安装镜像文件系统"},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
if got := cutName(tt.name, tt.size); got != tt.want {
|
|
t.Errorf("cutName(%q, %d) = %q, want %q", tt.name, tt.size, got, tt.want)
|
|
}
|
|
}
|
|
}
|