Skip to content

Commit

Permalink
examples: loop & null: add command line of --unprivileged
Browse files Browse the repository at this point in the history
For adding ublk device in unprivlileged user session.

Signed-off-by: Ming Lei <[email protected]>
  • Loading branch information
ming1 committed Oct 21, 2023
1 parent 3bfebdf commit 66b52be
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
24 changes: 22 additions & 2 deletions examples/loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,14 @@ fn _lo_handle_io(q: &UblkQueue, tag: u16, i: &UblkIOCtx) {
loop_queue_tgt_io(q, tag, i);
}

fn test_add(id: i32, nr_queues: u32, depth: u32, buf_sz: u32, backing_file: &String) {
fn test_add(
id: i32,
nr_queues: u32,
depth: u32,
buf_sz: u32,
backing_file: &String,
ctrl_flags: u64,
) {
let _pid = unsafe { libc::fork() };

if _pid == 0 {
Expand All @@ -156,6 +163,7 @@ fn test_add(id: i32, nr_queues: u32, depth: u32, buf_sz: u32, backing_file: &Str
let sess = libublk::UblkSessionBuilder::default()
.name("example_loop")
.id(id)
.ctrl_flags(ctrl_flags)
.nr_queues(nr_queues)
.depth(depth)
.io_buf_bytes(buf_sz)
Expand Down Expand Up @@ -224,6 +232,13 @@ fn main() {
.help("io buffer size")
.action(ArgAction::Set),
)
.arg(
Arg::new("unprivileged")
.long("unprivileged")
.short('p')
.action(ArgAction::SetTrue)
.help("enable UBLK_F_UN_PRIVILEGED_DEV"),
)
.arg(
Arg::new("backing_file")
.long("backing_file")
Expand Down Expand Up @@ -270,7 +285,12 @@ fn main() {
.unwrap_or(52288);
let backing_file = add_matches.get_one::<String>("backing_file").unwrap();

test_add(id, nr_queues, depth, buf_size, backing_file);
let ctrl_flags: u64 = if add_matches.get_flag("unprivileged") {
libublk::sys::UBLK_F_UNPRIVILEGED_DEV as u64
} else {
0
};
test_add(id, nr_queues, depth, buf_size, backing_file, ctrl_flags);
}
Some(("del", add_matches)) => {
let id = add_matches
Expand Down
11 changes: 11 additions & 0 deletions examples/null.rs
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,13 @@ fn main() {
.short('u')
.action(ArgAction::SetTrue)
.help("enable UBLK_F_USER_COPY"),
)
.arg(
Arg::new("unprivileged")
.long("unprivileged")
.short('p')
.action(ArgAction::SetTrue)
.help("enable UBLK_F_UN_PRIVILEGED_DEV"),
),
)
.subcommand(
Expand Down Expand Up @@ -137,6 +144,10 @@ fn main() {
libublk::sys::UBLK_F_USER_COPY as u64
} else {
0
} | if add_matches.get_flag("unprivileged") {
libublk::sys::UBLK_F_UNPRIVILEGED_DEV as u64
} else {
0
};

test_add(id, nr_queues, depth, ctrl_flags, buf_size);
Expand Down

0 comments on commit 66b52be

Please sign in to comment.