Skip to content

Commit

Permalink
extract: Generate mkfs.erofs options
Browse files Browse the repository at this point in the history
The options are for reference only, please modify according to the actual situation.

Signed-off-by: sekaiacg <[email protected]>
  • Loading branch information
sekaiacg committed Jan 26, 2025
1 parent 269f851 commit ceacac7
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 9 deletions.
33 changes: 28 additions & 5 deletions extract/ExtractOperation.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <algorithm>

#include "../lib/liberofs_uuid.h"
#include "ExtractOperation.h"
#include "ExtractState.h"
#include "ExtractHelper.h"
Expand Down Expand Up @@ -181,25 +182,47 @@ namespace skkk {
for_each(erofsNodes.begin(), erofsNodes.end(), printFsConf);
}

void ExtractOperation::extractFsConfigAndSelinuxLabel() const {
void ExtractOperation::extractFsConfigAndSelinuxLabelAndFsOptions() const {
string fsConfigPath = configDir + "/" + imgBaseName + "_fs_config";
string fsSelinuxLabelsPath = configDir + "/" + imgBaseName + "_file_contexts";
string fsOptionPath = configDir + "/" + imgBaseName + "_fs_options";
FILE *fsConfigFile = fopen(fsConfigPath.c_str(), "wb");
FILE *selinuxLabelsFile = fopen(fsSelinuxLabelsPath.c_str(), "wb");
FILE *fsOptionFile = fopen(fsOptionPath.c_str(), "wb");
char uuid[37] = {0};
const char *mountPoint = imgBaseName.c_str();
LOGCI(BROWN "fs_config|file_contexts" LOG_RESET_COLOR " " GREEN2_BOLD "saving..." LOG_RESET_COLOR);
if (fsConfigFile && selinuxLabelsFile) {
LOGCI(BROWN "fs_config|file_contexts|fs_options" LOG_RESET_COLOR " " GREEN2_BOLD "saving..." LOG_RESET_COLOR);
if (fsConfigFile && selinuxLabelsFile && fsOptionFile) {
for (auto &eNode: erofsNodes) {
if (otherPathsInRootDir.count(eNode->getPath()) > 0) continue;
eNode->writeFsConfig2File(fsConfigFile, mountPoint);
if (!eNode->getSelinuxLabel().empty())
eNode->writeSelinuxLabel2File(selinuxLabelsFile, mountPoint);
}
LOGCI(BROWN "fs_config|file_contexts" LOG_RESET_COLOR " " GREEN2_BOLD "done." LOG_RESET_COLOR);
auto time = (time_t) sbi.build_time;
erofs_uuid_unparse_lower(sbi.uuid, uuid);
fprintf(fsOptionFile, "Filesystem created: %s", ctime(&time));
fprintf(fsOptionFile, "Filesystem UUID: %s\n", uuid);
// The options are for reference only, please modify according to the actual situation.
fprintf(fsOptionFile, "mkfs.erofs options: "
"-zlz4hc,1 " // default: lz4hc,1
"-T %" PRIu64 " -U %s "
"--mount-point=/%s "
"--fs-config-file=%s "
"--file-contexts=%s "
"%s " //output image file
"%s", //input dir
sbi.build_time, uuid,
imgBaseName.c_str(),
fsConfigPath.c_str(), fsSelinuxLabelsPath.c_str(),
(imgBaseName + "_repack.img").c_str(),
outDir.c_str());
LOGCI(BROWN "fs_config|file_contexts|fs_options" LOG_RESET_COLOR " " GREEN2_BOLD "done." LOG_RESET_COLOR);
} else
LOGCE(BROWN "fs_config|file_contexts" LOG_RESET_COLOR " " RED2_BOLD "fail!" LOG_RESET_COLOR);
LOGCE(BROWN "fs_config|file_contexts|fs_options" LOG_RESET_COLOR " " RED2_BOLD "fail!" LOG_RESET_COLOR);
if (fsConfigFile) fclose(fsConfigFile);
if (selinuxLabelsFile) fclose(selinuxLabelsFile);
if (fsOptionFile) fclose(fsOptionFile);
}

void ExtractOperation::writeExceptionInfo2File() const {
Expand Down
2 changes: 1 addition & 1 deletion extract/include/ExtractOperation.h
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ namespace skkk {
*/
static void printInitializedNode();

void extractFsConfigAndSelinuxLabel() const;
void extractFsConfigAndSelinuxLabelAndFsOptions() const;

void writeExceptionInfo2File() const;

Expand Down
6 changes: 3 additions & 3 deletions extract/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ static inline void usage() {
" " GREEN2_BOLD "--extract=X" COLOR_NONE " " BROWN "Extract the target of path X" COLOR_NONE "\n"
" " GREEN2_BOLD "-f, --overwrite" COLOR_NONE " " BROWN "[" GREEN2_BOLD "default: skip" COLOR_NONE BROWN "] overwrite files that already exist" COLOR_NONE "\n"
" " GREEN2_BOLD "-T#" COLOR_NONE " " BROWN "[" GREEN2_BOLD "1-%u" COLOR_NONE BROWN "] Use # threads, -T0: " GREEN2_BOLD "%u" COLOR_NONE COLOR_NONE "\n"
" " GREEN2_BOLD "--only-cfg" COLOR_NONE " " BROWN "Only extract fs_config and file_contexts" COLOR_NONE "\n"
" " GREEN2_BOLD "--only-cfg" COLOR_NONE " " BROWN "Only extract fs_config|file_contexts|fs_options" COLOR_NONE "\n"
" " GREEN2_BOLD "-o, --outdir=X" COLOR_NONE " " BROWN "Output dir" COLOR_NONE "\n"
" " GREEN2_BOLD "-V, --version" COLOR_NONE " " BROWN "Print the version info" COLOR_NONE "\n",
eo->limitHardwareConcurrency,
Expand Down Expand Up @@ -232,7 +232,7 @@ int main(int argc, char **argv) {
ret = RET_EXTRACT_CREATE_DIR_FAIL;
goto exit_dev_close;
}
eo->extractFsConfigAndSelinuxLabel();
eo->extractFsConfigAndSelinuxLabelAndFsOptions();
goto end;
}

Expand All @@ -242,7 +242,7 @@ int main(int argc, char **argv) {
ret = RET_EXTRACT_CREATE_DIR_FAIL;
goto exit_dev_close;
}
eo->extractFsConfigAndSelinuxLabel();
eo->extractFsConfigAndSelinuxLabelAndFsOptions();
eo->useMultiThread ? eo->extractErofsNodeMultiThread() : eo->extractErofsNode();
goto end;
}
Expand Down
9 changes: 9 additions & 0 deletions lib/liberofs_uuid.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,17 @@
#ifndef __EROFS_LIB_UUID_H
#define __EROFS_LIB_UUID_H

#ifdef __cplusplus
extern "C"
{
#endif

void erofs_uuid_generate(unsigned char *out);
void erofs_uuid_unparse_lower(const unsigned char *buf, char *out);
int erofs_uuid_parse(const char *in, unsigned char *uu);

#ifdef __cplusplus
}
#endif

#endif

0 comments on commit ceacac7

Please sign in to comment.