Skip to content

Commit 592fac3

Browse files
committed
9pfs: wip
1 parent ed59bf5 commit 592fac3

File tree

1 file changed

+29
-28
lines changed

1 file changed

+29
-28
lines changed

src/usbg_9pfs.rs

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
// SPDX-License-Identifier: GPL-2.0-only
22

33
use std::fs::read_dir;
4-
use std::os::unix::ffi::OsStrExt;
54
use std::os::unix::fs::symlink;
6-
use std::{thread, time};
5+
use std::thread;
6+
use std::time::Duration;
77

88
use log::debug;
99

@@ -12,15 +12,24 @@ use crate::mount::mount_apivfs;
1212
use crate::util::{mkdir, write_file};
1313
use crate::Result;
1414

15-
fn setup_9pfs_gadget(device: &String) -> Result<()> {
15+
fn setup_9pfs_gadget(options: &mut CmdlineOptions) -> Result<()> {
1616
debug!("Initializing USB 9pfs gadget ...");
1717

18-
let udc = read_dir("/sys/class/udc")
19-
.map_err(|e| format!("Failed to list /sys/class/udc: {e}"))?
20-
.next()
21-
.ok_or("No UDC found to attach the 9pfs gadget".to_string())?
22-
.map_err(|e| format!("Failed to inspect the first entry in /sys/class/udc: {e}"))?
23-
.file_name();
18+
let udc = if let Some(device) = &options.root {
19+
device.to_owned()
20+
} else {
21+
read_dir("/sys/class/udc")
22+
.map_err(|e| format!("Failed to list /sys/class/udc: {e}"))?
23+
.next()
24+
.ok_or("No UDC found to attach the 9pfs gadget".to_string())?
25+
.map_err(|e| format!("Failed to inspect the first entry in /sys/class/udc: {e}"))?
26+
.file_name()
27+
.into_string()
28+
.map_err(|e| format!("invalid utf-8 in file name: {e:?}"))?
29+
}
30+
.as_bytes()
31+
.escape_ascii()
32+
.to_string();
2433

2534
mount_apivfs("/sys/kernel/config", "configfs")?;
2635

@@ -46,38 +55,30 @@ fn setup_9pfs_gadget(device: &String) -> Result<()> {
4655
mkdir("/sys/kernel/config/usb_gadget/9pfs/configs/c.1")?;
4756
mkdir("/sys/kernel/config/usb_gadget/9pfs/configs/c.1/strings/0x409")?;
4857

49-
let function = format!("/sys/kernel/config/usb_gadget/9pfs/functions/usb9pfs.{device}");
50-
let link = format!("/sys/kernel/config/usb_gadget/9pfs/configs/c.1/usb9pfs.{device}");
58+
let function = format!("/sys/kernel/config/usb_gadget/9pfs/functions/usb9pfs.{udc}");
59+
let link = format!("/sys/kernel/config/usb_gadget/9pfs/configs/c.1/usb9pfs.{udc}");
5160
mkdir(&function)?;
5261
symlink(&function, &link)?;
5362

54-
debug!(
55-
"Attaching 9pfs gatget to UDC {}",
56-
udc.as_bytes().escape_ascii()
57-
);
58-
write_file(
59-
"/sys/kernel/config/usb_gadget/9pfs/UDC",
60-
udc.as_encoded_bytes(),
61-
)?;
63+
debug!("Attaching 9pfs gatget to UDC {udc}",);
64+
write_file("/sys/kernel/config/usb_gadget/9pfs/UDC", &udc)?;
65+
66+
thread::sleep(Duration::from_secs(1));
67+
68+
options.root = Some(udc);
6269

63-
let d = time::Duration::new(1, 0);
64-
thread::sleep(d);
6570
Ok(())
6671
}
6772

68-
pub fn prepare_9pfs_gadget(options: &CmdlineOptions) -> Result<bool> {
73+
pub fn prepare_9pfs_gadget(options: &mut CmdlineOptions) -> Result<bool> {
6974
if options.rootfstype.as_deref() == Some("9p")
7075
&& options
7176
.rootflags
7277
.as_deref()
7378
.is_some_and(|flags| flags.contains("trans=usbg"))
7479
{
75-
if let Some(root) = &options.root {
76-
setup_9pfs_gadget(root)?;
77-
Ok(true)
78-
} else {
79-
Err("Missing root= for 9p!".into())
80-
}
80+
setup_9pfs_gadget(options)?;
81+
Ok(true)
8182
} else {
8283
Ok(false)
8384
}

0 commit comments

Comments
 (0)