Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Draft] feat: support apt install package #269

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 26 additions & 2 deletions apps/ll-builder/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "linglong/service/app_manager.h"
#include "linglong/util/qserializer/yaml.h"
#include "linglong/util/xdg.h"
#include "linglong/utils/command/env.h"
#include "linglong/utils/error/error.h"
#include "linglong/utils/global/initialize.h"
#include "spdlog/logger.h"
Expand All @@ -26,6 +27,8 @@

#include <fstream>

#include <sys/wait.h>

int main(int argc, char **argv)
{
qputenv("QT_LOGGING_RULES", "*=false");
Expand Down Expand Up @@ -315,12 +318,33 @@ int main(int argc, char **argv)
std::ofstream fout(projectConfigPath.toStdString());
fout << node;
}
auto ret = builder.build();

auto child_pid = fork();
if (child_pid == 0) {
auto ret = builder.build();
if (!ret.has_value()) {
printer.printErr(ret.error());
return ret.error().code();
}
return 0;
}
sleep(1);
QString pidStr = QString("%1").arg(child_pid);
auto ret = linglong::utils::command::Exec(
"newuidmap",
{ pidStr, "0", "1000", "1", "1", "100000", "65536" });
if (!ret.has_value()) {
printer.printErr(ret.error());
return ret.error().code();
}
return 0;
ret = linglong::utils::command::Exec(
"newgidmap",
{ pidStr, "0", "1000", "1", "1", "100000", "65536" });
if (!ret.has_value()) {
printer.printErr(ret.error());
return ret.error().code();
}
return waitpid(child_pid, NULL, 0);
} },
{ "run",
[&](QCommandLineParser &parser) -> int {
Expand Down
36 changes: 33 additions & 3 deletions src/linglong/builder/linglong_builder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include "ocppi/cli/CLI.hpp"
#include "ocppi/runtime/ContainerID.hpp"
#include "ocppi/runtime/config/ConfigLoader.hpp"
#include "ocppi/runtime/config/types/Capabilities.hpp"
#include "ocppi/runtime/config/types/Hook.hpp"
#include "ocppi/runtime/config/types/Hooks.hpp"
#include "project.h"
Expand All @@ -47,9 +48,13 @@

#include <fstream>
#include <optional>
#include <string>
#include <vector>

#include <sched.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <unistd.h>

namespace linglong::builder {

Expand Down Expand Up @@ -309,6 +314,21 @@ linglong::util::Error LinglongBuilder::config(const QString &userName, const QSt
return Success();
}

linglong::utils::error::Result<void> LinglongBuilder::writeFile(QString filename, QByteArray data)
{
LINGLONG_TRACE("write file");
QFile f(filename);
qDebug() << "write file" << f.fileName();
if (!f.open(QIODevice::WriteOnly)) {
return LINGLONG_ERR(f);
}
if (!f.write(data)) {
return LINGLONG_ERR(f);
}
f.close();
return LINGLONG_OK;
}

// FIXME: should merge with runtime
linglong::utils::error::Result<void> LinglongBuilder::buildStageRunContainer(
QDir workdir, ocppi::cli::CLI &cli, ocppi::runtime::config::types::Config &r)
Expand Down Expand Up @@ -738,6 +758,12 @@ LinglongBuilder::buildStageSource(ocppi::runtime::config::types::Config &r, Proj
r.process->user = ocppi::runtime::config::types::User();
r.process->user->uid = getuid();
r.process->user->gid = getgid();
std::vector<std::string> v = { "CAP_CHOWN", "CAP_DAC_OVERRIDE", "CAP_FOWNER",
"CAP_FSETID", "CAP_KILL", "CAP_NET_BIND_SERVICE",
"CAP_SETFCAP", "CAP_SETGID", "CAP_SETPCAP",
"CAP_SETUID", "CAP_SYS_CHROOT" };
r.process->capabilities =
ocppi::runtime::config::types::Capabilities{ .bounding = v, .effective = v, .permitted = v };
return LINGLONG_OK;
}

Expand Down Expand Up @@ -800,7 +826,7 @@ LinglongBuilder::buildStageIDMapping(ocppi::runtime::config::types::Config &r)
}
// 映射gid
QList<QList<quint64>> gidMaps = {
{ getgid(), getuid(), 1 },
{ getgid(), getgid(), 1 },
};
for (auto const &gidMap : gidMaps) {
ocppi::runtime::config::types::IdMapping idMap;
Expand Down Expand Up @@ -859,7 +885,7 @@ linglong::utils::error::Result<void> LinglongBuilder::buildStageRootfs(
}

// 使用rootfs做容器根目录
r.root->readonly = true;
r.root->readonly = false;
r.root->path = workdir.filePath("rootfs").toStdString();
return LINGLONG_OK;
}
Expand Down Expand Up @@ -895,7 +921,11 @@ linglong::utils::error::Result<QSharedPointer<Project>> LinglongBuilder::buildSt
linglong::utils::error::Result<void> LinglongBuilder::build()
{
LINGLONG_TRACE("build");

if (unshare(CLONE_NEWNS | CLONE_NEWUSER) == -1) {
perror("unshare");
exit(EXIT_FAILURE);
}
sleep(3);
auto projectRet = buildStageProjectInit();
if (!projectRet) {
return LINGLONG_ERR(projectRet);
Expand Down
11 changes: 6 additions & 5 deletions src/linglong/builder/linglong_builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,14 @@ class LinglongBuilder : public QObject, public Builder

util::Error exportLayer(const QString &destination) override;

util::Error extractLayer(const QString &layerPath,
const QString &destination) override;
util::Error extractLayer(const QString &layerPath, const QString &destination) override;

util::Error exportBundle(const QString &outputFilepath, bool useLocalDir) override;

util::Error push(const QString &repoUrl,
const QString &repoName,
const QString &channel,
bool pushWithDevel) override;
const QString &repoName,
const QString &channel,
bool pushWithDevel) override;

util::Error import() override;

Expand Down Expand Up @@ -94,6 +93,8 @@ class LinglongBuilder : public QObject, public Builder
linglong::utils::error::Result<void> buildStageCommitBuildOutput(Project *project,
const QString &upperdir,
const QString &workdir);

linglong::utils::error::Result<void> writeFile(QString filename, QByteArray data);
};

// TODO: remove later
Expand Down
3 changes: 0 additions & 3 deletions src/linglong/runtime/resource/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@
},
{
"type": "uts"
},
{
"type": "user"
}
]
},
Expand Down
Loading