-
Notifications
You must be signed in to change notification settings - Fork 7
9pfs: root device #7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,35 @@ | ||
// SPDX-License-Identifier: GPL-2.0-only | ||
use crate::cmdline::CmdlineOptions; | ||
use crate::mount::mount_apivfs; | ||
use crate::{mkdir, Result}; | ||
use log::debug; | ||
use std::fs::{read_dir, write}; | ||
use std::os::unix::ffi::OsStrExt; | ||
|
||
use std::fs::read_dir; | ||
use std::os::unix::fs::symlink; | ||
use std::{thread, time}; | ||
use std::thread; | ||
use std::time::Duration; | ||
|
||
fn write_file<C: AsRef<[u8]>>(path: &str, content: C) -> Result<()> { | ||
write(path, content).map_err(|e| format!("Failed to write to {path}: {e}").into()) | ||
} | ||
use log::debug; | ||
|
||
fn setup_9pfs_gadget(device: &String) -> Result<()> { | ||
use crate::cmdline::CmdlineOptions; | ||
use crate::mount::mount_apivfs; | ||
use crate::util::{mkdir, write_file}; | ||
use crate::Result; | ||
|
||
fn setup_9pfs_gadget(options: &mut CmdlineOptions) -> Result<()> { | ||
debug!("Initializing USB 9pfs gadget ..."); | ||
|
||
let udc = read_dir("/sys/class/udc") | ||
.map_err(|e| format!("Failed to list /sys/class/udc: {e}"))? | ||
.next() | ||
.ok_or("No UDC found to attach the 9pfs gadget".to_string())? | ||
.map_err(|e| format!("Failed to inspect the first entry in /sys/class/udc: {e}"))? | ||
.file_name(); | ||
let udc = if let Some(device) = &options.root { | ||
device.to_owned() | ||
} else { | ||
read_dir("/sys/class/udc") | ||
.map_err(|e| format!("Failed to list /sys/class/udc: {e}"))? | ||
.next() | ||
.ok_or("No UDC found to attach the 9pfs gadget".to_string())? | ||
.map_err(|e| format!("Failed to inspect the first entry in /sys/class/udc: {e}"))? | ||
.file_name() | ||
.into_string() | ||
.map_err(|e| format!("invalid utf-8 in file name: {e:?}"))? | ||
} | ||
.as_bytes() | ||
.escape_ascii() | ||
.to_string(); | ||
|
||
mount_apivfs("/sys/kernel/config", "configfs")?; | ||
|
||
|
@@ -46,38 +55,30 @@ fn setup_9pfs_gadget(device: &String) -> Result<()> { | |
mkdir("/sys/kernel/config/usb_gadget/9pfs/configs/c.1")?; | ||
mkdir("/sys/kernel/config/usb_gadget/9pfs/configs/c.1/strings/0x409")?; | ||
|
||
let function = format!("/sys/kernel/config/usb_gadget/9pfs/functions/usb9pfs.{device}"); | ||
let link = format!("/sys/kernel/config/usb_gadget/9pfs/configs/c.1/usb9pfs.{device}"); | ||
let function = format!("/sys/kernel/config/usb_gadget/9pfs/functions/usb9pfs.{udc}"); | ||
let link = format!("/sys/kernel/config/usb_gadget/9pfs/configs/c.1/usb9pfs.{udc}"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was mistaken. If I change this to something different from the first argument of the mount call, mounting fails. |
||
mkdir(&function)?; | ||
symlink(&function, &link)?; | ||
|
||
debug!( | ||
"Attaching 9pfs gatget to UDC {}", | ||
udc.as_bytes().escape_ascii() | ||
); | ||
write_file( | ||
"/sys/kernel/config/usb_gadget/9pfs/UDC", | ||
udc.as_encoded_bytes(), | ||
)?; | ||
debug!("Attaching 9pfs gatget to UDC {udc}",); | ||
write_file("/sys/kernel/config/usb_gadget/9pfs/UDC", &udc)?; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm, the file name read above is not necessarily UTF-8, so converting it into a String is technically not correct. |
||
|
||
thread::sleep(Duration::from_secs(1)); | ||
|
||
options.root = Some(udc); | ||
|
||
let d = time::Duration::new(1, 0); | ||
thread::sleep(d); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unrelated, this should be a separate commit. |
||
Ok(()) | ||
} | ||
|
||
pub fn prepare_9pfs_gadget(options: &CmdlineOptions) -> Result<bool> { | ||
pub fn prepare_9pfs_gadget(options: &mut CmdlineOptions) -> Result<bool> { | ||
if options.rootfstype.as_deref() == Some("9p") | ||
&& options | ||
.rootflags | ||
.as_deref() | ||
.is_some_and(|flags| flags.contains("trans=usbg")) | ||
{ | ||
if let Some(root) = &options.root { | ||
setup_9pfs_gadget(root)?; | ||
Ok(true) | ||
} else { | ||
Err("Missing root= for 9p!".into()) | ||
} | ||
setup_9pfs_gadget(options)?; | ||
Ok(true) | ||
} else { | ||
Ok(false) | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
use std::{ | ||
fs::{create_dir_all, read_to_string, write}, | ||
path::Path, | ||
}; | ||
|
||
use crate::Result; | ||
|
||
pub(crate) fn mkdir(dir: impl AsRef<Path>) -> Result<()> { | ||
create_dir_all(dir.as_ref()).map_err(|e| { | ||
format!( | ||
"Failed to create directory {}: {e}", | ||
dir.as_ref().to_string_lossy() | ||
) | ||
.into() | ||
}) | ||
} | ||
|
||
pub(crate) fn read_file(filename: impl AsRef<Path>) -> Result<String> { | ||
read_to_string(filename.as_ref()).map_err(|e| { | ||
format!( | ||
"Failed to read {}: {e}", | ||
filename.as_ref().to_string_lossy() | ||
) | ||
.into() | ||
}) | ||
} | ||
|
||
pub(crate) fn write_file<C: AsRef<[u8]>>(path: impl AsRef<Path>, content: C) -> Result<()> { | ||
write(&path, content).map_err(|e| { | ||
format!( | ||
"Failed to write to {}: {e}", | ||
path.as_ref().to_string_lossy() | ||
) | ||
.into() | ||
}) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks wrong. In the else case, the string is converted multiple times.