Skip to content

Commit

Permalink
SELinux tomfoolery
Browse files Browse the repository at this point in the history
  • Loading branch information
korewaChino committed Sep 24, 2023
1 parent 514ccd5 commit c905cf6
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 11 deletions.
55 changes: 48 additions & 7 deletions src/creator.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use color_eyre::{eyre::eyre, Help, Result};
use tracing_subscriber::field::debug;
use std::{
fs,
io::Write,
Expand All @@ -14,7 +15,7 @@ use crate::{

const DEFAULT_DNF: &str = "dnf5";
const DEFAULT_BOOTLOADER: &str = "limine";
const UBOOT_DATA: &str = "/usr/share/uboot";
// const UBOOT_DATA: &str = "/usr/share/uboot";

#[derive(Debug, Clone)]
pub struct ImageLayout {
Expand Down Expand Up @@ -42,6 +43,42 @@ pub trait ImageCreator {
Ok(())
}

fn genfstab(&self) -> Result<()> {
let cfg = self.get_cfg();
let root = cfg.instroot.canonicalize().expect("Cannot canonicalize instroot.");
let root = root.to_str().unwrap();
let out = format!("{}/etc/fstab", root);
// list mounts in $root
let mounts = cmd_lib::run_fun!(findmnt -n -o UUID,TARGET,FSTYPE,OPTIONS --real --raw --noheadings --notruncate --output-all --target $root)?;

// convert to fstab format
let mut mounts = mounts
.lines()
.map(|x| {
let mut x = x.split_whitespace();
let uuid = x.next().unwrap();
let target = x.next().unwrap();
let fstype = x.next().unwrap();
let options = x.next().unwrap();
format!(
"UUID={uuid}\t{target}\t{fstype}\t{options}\t0\t0",
uuid = uuid,
target = target,
fstype = fstype,
options = options
)
})
.collect::<Vec<String>>()
.join("\n");
mounts.push('\n');


debug!(?mounts, "Mounts");
let mut f = std::fs::File::create(out)?;
f.write_all(mounts.as_bytes())?;
Ok(())
}

fn dracut(&self) -> Result<()> {
// self.fstab()?;
let cfg = self.get_cfg();
Expand Down Expand Up @@ -176,6 +213,7 @@ pub trait ImageCreator {
// self.dracut()?;
self.rootpw()?;
self.postinst_script()?;
self.genfstab()?;

// self.squashfs()?;
// self.liveos()?;
Expand Down Expand Up @@ -322,15 +360,17 @@ pub trait ImageCreator {
debug!(?script, ?dest, "Copying postinst script");
std::fs::copy(script, &dest)?;
// debug!("Mounting /dev, /proc, /sys");
// prepare_chroot(rootname)?;
prepare_chroot(rootname)?;
info!(?script, "Running postinst script");
// TODO: use unshare
run!(~"unshare","-R", &rootname, &*format!("/{name}"))
.map_err(|e| e.wrap_err("postinst script failed"))?;
run!(~"unshare","-R", &rootname, &*format!("/{name}")).map_err(|e| {
unmount_chroot(rootname).unwrap();
e.wrap_err("postinst script failed")
})?;
debug!(?dest, "Removing postinst script");
std::fs::remove_file(dest)?;
// debug!("Unmounting /dev, /proc, /sys");
// unmount_chroot(rootname)?;
unmount_chroot(rootname)?;
Ok(())
}
Expand Down Expand Up @@ -396,7 +436,7 @@ pub trait ImageCreator {
info!(out_file, "Creating disk file");
cmd_lib::run_cmd!(
fallocate -l $disk_size $out_file;
truncate -s $disk_size $out_file;
)?;
// Mount disk image to loop device, and return the loop device name
Expand Down Expand Up @@ -577,7 +617,8 @@ pub trait ImageCreator {
cmd_lib::run_cmd!(
$dnf in -y --releasever=$rel $[extra_args] --installroot $root $[pkgs];
$dnf clean all;
).unwrap_or_else(|e| {
)
.unwrap_or_else(|e| {
error!(?e, "Failed to install packages");
unmount_chroot(root).unwrap_or_else(|e| {
error!(?e, "Failed to unmount chroot");
Expand Down
3 changes: 3 additions & 0 deletions tests/init.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#!/bin/bash -x
set -x

sudo semanage permissive -a setfiles_mac_t || true

echo "Initializing chroot and repos"

mkdir -p ./etc/yum.repos.d ./etc/dnf
Expand Down
6 changes: 3 additions & 3 deletions tests/katsudon-arm.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ dnf: dnf
distro: Ultramarine-ARM
instroot: instroot-arm/
# this is the name of the iso
out: out
out: out2
volid: ULTRAMARINE
arch: aarch64
format: disk
Expand Down Expand Up @@ -50,8 +50,8 @@ packages:

disk:
bootloader: true
root_format: xfs
disk_size: 6G
root_format: btrfs
disk_size: 8G

sys:
releasever: 38
Expand Down
3 changes: 2 additions & 1 deletion tests/postinst.sh
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ rm -f /var/lib/rpm/__db*

echo "Fixing SELinux labels"

# fixfiles -vRa restore
setfiles -v -F -e /proc -e /sys -e /dev -e /bin /etc/selinux/targeted/contexts/files/file_contexts /
setfiles -v -F -e /proc -e /sys -e /dev -e /etc/selinux/targeted/contexts/files/file_contexts.bin /bin

# todo: move this out of postinst
grub2-mkconfig > /boot/grub2/grub.cfg
Expand Down

0 comments on commit c905cf6

Please sign in to comment.