-
Notifications
You must be signed in to change notification settings - Fork 198
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
Add hidden coreos-rootfs seal
command
#1911
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
/* | ||
* Copyright (C) 2018 Red Hat, Inc. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 OR MIT | ||
*/ | ||
|
||
//! # Code for CoreOS rootfs | ||
//! | ||
//! This backs the `rpm-ostree coreos-rootfs` CLI, which is intended as | ||
//! a hidden implmentation detail of CoreOS style systems. The code | ||
//! is here in rpm-ostree as a convenience. | ||
|
||
use std::os::unix::prelude::*; | ||
use failure::{Fallible, ResultExt}; | ||
use structopt::StructOpt; | ||
use openat; | ||
use nix; | ||
use libc; | ||
|
||
/// A reference to a *directory* file descriptor. Unfortunately, | ||
/// the openat crate always uses O_PATH which doesn't support ioctl(). | ||
pub struct RawDirFd(RawFd); | ||
|
||
impl AsRawFd for RawDirFd { | ||
#[inline] | ||
fn as_raw_fd(&self) -> RawFd { | ||
self.0 | ||
} | ||
} | ||
|
||
impl FromRawFd for RawDirFd { | ||
#[inline] | ||
unsafe fn from_raw_fd(fd: RawFd) -> RawDirFd { | ||
RawDirFd(fd) | ||
} | ||
} | ||
|
||
impl Drop for RawDirFd { | ||
fn drop(&mut self) { | ||
let fd = self.0; | ||
unsafe { | ||
libc::close(fd); | ||
} | ||
} | ||
} | ||
|
||
/* From /usr/include/ext2fs/ext2_fs.h */ | ||
const EXT2_IMMUTABLE_FL : libc::c_long = 0x00000010; /* Immutable file */ | ||
|
||
nix::ioctl_read!(ext2_get_flags, b'f', 1, libc::c_long); | ||
nix::ioctl_write_ptr!(ext2_set_flags, b'f', 2, libc::c_long); | ||
|
||
#[derive(Debug, StructOpt)] | ||
struct SealOpts { | ||
/// Path to rootfs | ||
sysroot: String, | ||
} | ||
|
||
#[derive(Debug, StructOpt)] | ||
#[structopt(name = "coreos-rootfs")] | ||
#[structopt(rename_all = "kebab-case")] | ||
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. kebab-case is best case |
||
enum Opt { | ||
/// Final step after changing a sysroot | ||
Seal(SealOpts), | ||
} | ||
|
||
// taken from openat code | ||
fn to_cstr<P: openat::AsPath>(path: P) -> std::io::Result<P::Buffer> { | ||
path.to_path() | ||
.ok_or_else(|| { | ||
std::io::Error::new(std::io::ErrorKind::InvalidInput, | ||
"nul byte in file name") | ||
}) | ||
} | ||
|
||
/// Set the immutable bit | ||
fn seal(opts: &SealOpts) -> Fallible<()> { | ||
let fd = unsafe { | ||
let fd = libc::open(to_cstr(opts.sysroot.as_str())?.as_ref().as_ptr(), libc::O_CLOEXEC | libc::O_DIRECTORY); | ||
if fd < 0 { | ||
Err(std::io::Error::last_os_error())? | ||
} else { | ||
RawDirFd::from_raw_fd(fd) | ||
} | ||
}; | ||
|
||
let mut flags : libc::c_long = 0; | ||
unsafe { ext2_get_flags(fd.as_raw_fd(), &mut flags as *mut libc::c_long)? }; | ||
if flags & EXT2_IMMUTABLE_FL == 0 { | ||
flags |= EXT2_IMMUTABLE_FL; | ||
unsafe { ext2_set_flags(fd.as_raw_fd(), &flags as *const libc::c_long)? }; | ||
} | ||
Ok(()) | ||
} | ||
|
||
/// Main entrypoint | ||
fn coreos_rootfs_main(args: &Vec<String>) -> Fallible<()> { | ||
let opt = Opt::from_iter(args.iter()); | ||
match opt { | ||
Opt::Seal(ref opts) => seal(opts).with_context(|e| format!("Sealing: {}", e.to_string()))?, | ||
}; | ||
Ok(()) | ||
} | ||
|
||
mod ffi { | ||
use super::*; | ||
use glib_sys; | ||
use libc; | ||
|
||
use crate::ffiutil::*; | ||
|
||
#[no_mangle] | ||
pub extern "C" fn ror_coreos_rootfs_entrypoint(argv: *mut *mut libc::c_char, | ||
gerror: *mut *mut glib_sys::GError) -> libc::c_int { | ||
let v: Vec<String> = unsafe { glib::translate::FromGlibPtrContainer::from_glib_none(argv) }; | ||
int_glib_error(coreos_rootfs_main(&v), gerror) | ||
} | ||
} | ||
pub use self::ffi::*; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- | ||
* | ||
* Copyright (C) 2019 Red Hat, Inc. | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Lesser General Public License as published | ||
* by the Free Software Foundation; either version 2 of the licence or (at | ||
* your option) any later version. | ||
* | ||
* This library is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General | ||
* Public License along with this library; if not, write to the | ||
* Free Software Foundation, Inc., 59 Temple Place, Suite 330, | ||
* Boston, MA 02111-1307, USA. | ||
*/ | ||
|
||
#include "config.h" | ||
|
||
#include <string.h> | ||
#include <glib-unix.h> | ||
#include <gio/gio.h> | ||
|
||
#include "rpmostree-builtins.h" | ||
#include "rpmostree-libbuiltin.h" | ||
#include "rpmostree-rust.h" | ||
|
||
#include <libglnx.h> | ||
|
||
gboolean | ||
rpmostree_builtin_coreos_rootfs (int argc, | ||
char **argv, | ||
RpmOstreeCommandInvocation *invocation, | ||
GCancellable *cancellable, | ||
GError **error) | ||
{ | ||
g_autoptr(GPtrArray) args = g_ptr_array_new (); | ||
for (int i = 0; i < argc; i++) | ||
g_ptr_array_add (args, argv[i]); | ||
g_ptr_array_add (args, NULL); | ||
return ror_coreos_rootfs_entrypoint ((char**)args->pdata, error); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
And yeah, this turned out to be a lot more code in Rust than it would have been in C particularly if we just copy-pasted the libostree code but I doggedly kept at it 😉