Skip to content

Commit

Permalink
compose: Change /etc/default/useradd to use HOME=/var/home
Browse files Browse the repository at this point in the history
For a long time I've resisted encoding "policy" into rpm-ostree
as much as possible.  Doing so makes it more distribution specific
for example.  That said, for `/var/home` there argument for doing
this in rpm-ostree is that we already make that symlink in our
hardcoded rootfs.  So we might as well do the other fixups for it.

coreos/fedora-coreos-config#18
https://pagure.io/workstation-ostree-config/pull-request/121
https://discussion.fedoraproject.org/t/adapting-user-home-in-etc-passwd/487/6
justjanne/powerline-go#94

Closes: #1726
Approved by: jlebon
  • Loading branch information
cgwalters authored and rh-atomic-bot committed Jan 7, 2019
1 parent bb4a17d commit 56e6ddb
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 2 deletions.
63 changes: 61 additions & 2 deletions rust/src/composepost.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,71 @@
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/

use openat;
use failure::Fallible;
use openat;
use std::io::{BufRead, Write};
use std::path::Path;
use std::{fs, io};

/// Helper functions for openat::Dir
trait OpenatDirExt {
// IMO should propose this at least in a "utils" bit of the openat crate;
// Like 95% of the time I'm looking at errno (with files) it's for ENOENT,
// and Rust has an elegant way to map that with Option<>. Every other
// error I usually just want to propagate back up.
fn open_file_optional<P: openat::AsPath>(&self, p: P) -> io::Result<Option<fs::File>>;
}

impl OpenatDirExt for openat::Dir {
fn open_file_optional<P: openat::AsPath>(&self, p: P) -> io::Result<Option<fs::File>> {
match self.open_file(p) {
Ok(f) => Ok(Some(f)),
Err(e) => {
if e.kind() == io::ErrorKind::NotFound {
Ok(None)
} else {
Err(e)
}
}
}
}
}

// rpm-ostree uses /home → /var/home by default as generated by our
// rootfs; we don't expect people to change this. Let's be nice
// and also fixup the $HOME entries generated by `useradd` so
// that `~` shows up as expected in shells, etc.
//
// https://github.com/coreos/fedora-coreos-config/pull/18
// https://pagure.io/workstation-ostree-config/pull-request/121
// https://discussion.fedoraproject.org/t/adapting-user-home-in-etc-passwd/487/6
// https://github.com/justjanne/powerline-go/issues/94
fn postprocess_useradd(rootfs_dfd: &openat::Dir) -> Fallible<()> {
let path = Path::new("usr/etc/default/useradd");
if let Some(f) = rootfs_dfd.open_file_optional(path)? {
let mut f = io::BufReader::new(f);
let tmp_path = path.parent().unwrap().join("useradd.tmp");
let o = rootfs_dfd.write_file(&tmp_path, 0644)?;
let mut bufw = io::BufWriter::new(&o);
for line in f.lines() {
let line = line?;
if !line.starts_with("HOME=") {
bufw.write(line.as_bytes())?;
} else {
bufw.write("HOME=/var/home".as_bytes())?;
}
bufw.write("\n".as_bytes())?;
}
bufw.flush()?;
rootfs_dfd.local_rename(&tmp_path, path)?;
}
Ok(())
}

// This function is called from rpmostree_postprocess_final(); think of
// it as the bits of that function that we've chosen to implement in Rust.
fn compose_postprocess_final(_rootfs_dfd: &openat::Dir) -> Fallible<()> {
fn compose_postprocess_final(rootfs_dfd: &openat::Dir) -> Fallible<()> {
postprocess_useradd(rootfs_dfd)?;
Ok(())
}

Expand Down
5 changes: 5 additions & 0 deletions tests/compose-tests/libbasic-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ validate_passwd() {
validate_passwd passwd
validate_passwd group


ostree --repo=${repobuild} cat ${treeref} /usr/etc/default/useradd > useradd.txt
assert_file_has_content_literal useradd.txt HOME=/var/home
echo "ok etc/default/useradd"

for path in /usr/share/rpm /usr/lib/sysimage/rpm-ostree-base-db; do
ostree --repo=${repobuild} ls -R ${treeref} ${path} > db.txt
assert_file_has_content_literal db.txt /Packages
Expand Down

0 comments on commit 56e6ddb

Please sign in to comment.