~danyspin97/initrz

ref: 78367ad283c51c724dfadab12411c857a6ce584d initrz/src/mkinitrz/src/initramfs.rs -rw-r--r-- 5.8 KiB
78367ad2Danilo Spinella Refactor common code into a new module and improve module packing 5 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
use std::{
    collections::HashSet,
    convert::TryInto,
    env, fs,
    os::unix::fs::MetadataExt,
    path::{Path, PathBuf},
};

use anyhow::{bail, Context, Result};
use log::debug;

use crate::config::Config;
use crate::depend;
use crate::initramfs_modules;
use crate::initramfs_type::InitramfsType;
use crate::newc::{self, Archive, Entry, EntryBuilder};

const ROOT_DIRECTORIES: [&str; 9] = [
    "/dev",
    "/etc",
    "/new_root",
    "/proc",
    "/run",
    "/sys",
    "/usr",
    "/usr/bin",
    "/usr/lib",
];

const ROOT_SYMLINKS: [(&str, &str); 6] = [
    ("/bin", "usr/bin"),
    ("/lib", "usr/lib"),
    ("/lib64", "lib"),
    ("/sbin", "usr/sbin"),
    ("/usr/lib64", "lib"),
    ("/usr/sbin", "bin"),
];

const DEFAULT_DIR_MODE: u32 = 0o040_000 + 0o755;
const DEFAULT_SYMLINK_MODE: u32 = 0o120_000;
const DEFAULT_FILE_MODE: u32 = 0o644;

pub struct Initramfs {
    entries: Vec<Entry>,
    files: HashSet<PathBuf>,
}

impl Initramfs {
    pub fn new(initramfs_type: InitramfsType, kroot: PathBuf, config: Config) -> Result<Initramfs> {
        let mut initramfs = Initramfs::new_basic_structure()?;
        initramfs.init(initramfs_type, kroot, config)?;
        Ok(initramfs)
    }

    fn init(
        &mut self,
        initramfs_type: InitramfsType,
        kroot: PathBuf,
        config: Config,
    ) -> Result<()> {
        let mut initrz: PathBuf =
            Path::new(&env::var("INITRZ").unwrap_or("target/release/initrz".to_string())).into();
        if !initrz.exists() {
            initrz = Path::new("/sbin/initrz").into();
            if !initrz.exists() {
                bail!("unable to find initrz executable. Please set INITRZ environment variable");
            }
        }
        self.add_elf_with_path(&initrz, Path::new("/init"))?;

        self.add_elf(Path::new("/sbin/vgchange"))?;
        self.add_elf(Path::new("/sbin/vgmknodes"))?;

        self.add_elf(Path::new("/bin/busybox"))?;

        let ld_conf = Path::new("/etc/ld.so.conf");
        self.add_entry(
            ld_conf,
            EntryBuilder::file(ld_conf, Vec::new())
                .with_metadata(&fs::metadata(&ld_conf)?)
                .build(),
        );

        self.add_file(&kroot.join("modules.dep"))?;
        self.add_file(&kroot.join("modules.alias"))?;

        self.apply_config(&config);

        initramfs_modules::get_modules(&initramfs_type, &kroot, config.modules)?
            .iter()
            .try_for_each(|module| -> Result<()> {
                self.add_file(&module)?;
                Ok(())
            })?;

        match initramfs_type {
            InitramfsType::Host => {
                let crypttab = Path::new("/etc/crypttab.initramfs");
                if crypttab.exists() {
                    self.add_file(crypttab)?;
                }
            }
            InitramfsType::General => {}
        }

        Ok(())
    }

    fn new_basic_structure() -> Result<Initramfs> {
        let mut entries = Vec::new();
        let mut files: HashSet<PathBuf> = HashSet::new();

        ROOT_DIRECTORIES.iter().for_each(|dir| {
            files.insert((*dir).into());
            entries.push(EntryBuilder::directory(dir).mode(DEFAULT_DIR_MODE).build())
        });

        ROOT_SYMLINKS.iter().for_each(|(src, dest)| {
            files.insert((*src).into());
            entries.push(
                EntryBuilder::symlink(src, Path::new(dest))
                    .mode(DEFAULT_SYMLINK_MODE)
                    .build(),
            )
        });

        Ok(Initramfs { entries, files })
    }

    fn apply_config(&mut self, config: &Config) {}

    fn add_elf(&mut self, exe: &Path) -> Result<()> {
        self.add_elf_with_path(exe, exe)
    }

    fn add_elf_with_path(&mut self, exe: &Path, path: &Path) -> Result<()> {
        if !self.add_file_with_path(exe, path)? {
            return Ok(());
        }
        depend::resolve(Path::new(exe))?
            .iter()
            .try_for_each(|lib| -> Result<()> { self.add_library(lib) })?;

        Ok(())
    }

    fn add_library(&mut self, lib: &Path) -> Result<()> {
        let libname = lib.file_name().unwrap();
        if !self.add_file_with_path(lib, &Path::new("/usr/lib").join(libname))? {
            return Ok(());
        }

        depend::resolve(lib)?
            .iter()
            .try_for_each(|lib| -> Result<()> { self.add_library(lib) })?;

        Ok(())
    }

    fn add_file(&mut self, path: &Path) -> Result<bool> {
        self.add_file_with_path(path, path)
    }

    fn add_file_with_path(&mut self, file: &Path, path: &Path) -> Result<bool> {
        let file = fs::canonicalize(file)?;
        let path = path.to_path_buf();
        if self.files.contains(&path) {
            return Ok(false);
        }
        self.add_directory(
            path.parent()
                .expect("Files path shall contain a parent directory"),
        );
        self.add_entry(
            &path,
            EntryBuilder::file(
                &path,
                fs::read(&file).with_context(|| format!("unable to read from file {:?}", file))?,
            )
            .with_metadata(
                &fs::metadata(&file)
                    .with_context(|| format!("unable to read metadata of file {:?}", file))?,
            )
            .build(),
        );
        Ok(true)
    }

    fn add_directory(&mut self, dir: &Path) {
        if self.files.contains(dir) {
            return;
        }
        if let Some(parent) = dir.parent() {
            self.add_directory(parent);
        }

        self.add_entry(
            &dir,
            EntryBuilder::directory(&dir).mode(DEFAULT_DIR_MODE).build(),
        );
    }

    fn add_entry(&mut self, path: &Path, entry: Entry) {
        debug!("Added entry {:?}", path);
        self.files.insert(path.into());
        self.entries.push(entry);
    }

    pub fn into_bytes(self) -> Result<Vec<u8>> {
        Archive::new(self.entries).into_bytes()
    }
}