Merge branch 'installer_groups' into 'master'

Move /etc/group generation to the installer

See merge request redox-os/installer!30
This commit is contained in:
Jeremy Soller
2024-01-06 16:28:32 +00:00
5 changed files with 69 additions and 17 deletions
+4 -8
View File
@@ -69,6 +69,10 @@ home = "/root"
# Password is unset
password = ""
[groups.sudo]
gid = 1
members = ["user"]
[[files]]
path = "/etc/init.d/00_base"
data = """
@@ -135,14 +139,6 @@ data = """
path = "/etc/pkg.d/50_redox"
data = "https://static.redox-os.org/pkg"
[[files]]
path = "/etc/group"
data = """
root;0;root
user;1000;user
sudo;1;user
"""
[[files]]
path = "/etc/hostname"
data = """
+7
View File
@@ -20,6 +20,8 @@ pub struct Config {
pub files: Vec<file::FileConfig>,
#[serde(default)]
pub users: BTreeMap<String, user::UserConfig>,
#[serde(default)]
pub groups: BTreeMap<String, user::GroupConfig>,
}
impl Config {
@@ -63,6 +65,7 @@ impl Config {
packages: other_packages,
files: other_files,
users: other_users,
groups: other_groups,
} = other;
self.general.merge(other_general);
@@ -76,5 +79,9 @@ impl Config {
for (user, user_config) in other_users {
self.users.insert(user, user_config);
}
for (group, group_config) in other_groups {
self.groups.insert(group, group_config);
}
}
}
+7
View File
@@ -7,3 +7,10 @@ pub struct UserConfig {
pub home: Option<String>,
pub shell: Option<String>,
}
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
pub struct GroupConfig {
pub gid: Option<u32>,
// FIXME move this to the UserConfig struct as extra_groups
pub members: Vec<String>,
}
+46 -1
View File
@@ -181,6 +181,9 @@ pub fn install_dir<P: AsRef<Path>, S: AsRef<str>>(config: Config, output_dir: P,
let mut passwd = String::new();
let mut shadow = String::new();
let mut next_uid = 1000;
let mut next_gid = 1000;
let mut groups = vec![];
for (username, user) in config.users {
// plaintext
@@ -200,7 +203,11 @@ pub fn install_dir<P: AsRef<Path>, S: AsRef<str>>(config: Config, output_dir: P,
next_uid = uid + 1;
}
let gid = user.gid.unwrap_or(uid);
let gid = user.gid.unwrap_or(next_gid);
if gid >= next_gid {
next_gid = gid + 1;
}
let name = prompt!(user.name, username.clone(), "{}: name (GECOS) [{}]: ", username, username)?;
let home = prompt!(user.home, format!("/home/{}", username), "{}: home [/home/{}]: ", username, username)?;
@@ -229,6 +236,19 @@ pub fn install_dir<P: AsRef<Path>, S: AsRef<str>>(config: Config, output_dir: P,
passwd.push_str(&format!("{};{};{};{};file:{};file:{}\n", username, uid, gid, name, home, shell));
shadow.push_str(&format!("{};{}\n", username, password));
groups.push((username.clone(), gid, vec![username]));
}
for (group, group_config) in config.groups {
// FIXME this assumes there is no overlap between auto-created groups for users
// and explicitly specified groups.
let gid = group_config.gid.unwrap_or(next_gid);
if gid >= next_gid {
next_gid = gid + 1;
}
groups.push((group, gid, group_config.members));
}
if !passwd.is_empty() {
@@ -258,6 +278,31 @@ pub fn install_dir<P: AsRef<Path>, S: AsRef<str>>(config: Config, output_dir: P,
}.create(&output_dir)?;
}
if !groups.is_empty() {
let mut groups_data = String::new();
for (name, gid, members) in groups {
use std::fmt::Write;
writeln!(groups_data, "{name};{gid};{}", members.join(",")).unwrap();
println!("Adding group {}:", name);
println!("\tGID: {}", gid);
println!("\tMembers: {}", members.join(", "));
}
FileConfig {
path: "/etc/group".to_string(),
data: groups_data,
symlink: false,
directory: false,
// Take defaults
mode: None,
uid: None,
gid: None,
recursive_chown: false,
}.create(&output_dir)?;
}
Ok(())
}
+5 -8
View File
@@ -54,6 +54,11 @@ home = "/root"
# Password is unset
password = ""
# Group settings
[groups.sudo]
gid = 1
members = ["user"]
[[files]]
path = "/etc/init.d/00_base"
data = """
@@ -118,14 +123,6 @@ data = """
path = "/etc/pkg.d/50_redox"
data = "https://static.redox-os.org/pkg"
[[files]]
path = "/etc/group"
data = """
root;0;root
user;1000;user
sudo;1;user
"""
[[files]]
path = "/etc/hostname"
data = """