mirror of https://github.com/cutefishos/calamares
commit
faa88afc88
@ -0,0 +1,165 @@
|
|||||||
|
/* === This file is part of Calamares - <https://github.com/calamares> ===
|
||||||
|
*
|
||||||
|
* Copyright 2018, Adriaan de Groot <groot@kde.org>
|
||||||
|
*
|
||||||
|
* Calamares is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* Calamares is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with Calamares. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "CalamaresUtilsSystem.h"
|
||||||
|
#include "Entropy.h"
|
||||||
|
#include "Logger.h"
|
||||||
|
#include "UMask.h"
|
||||||
|
#include "Yaml.h"
|
||||||
|
|
||||||
|
#include "GlobalStorage.h"
|
||||||
|
#include "JobQueue.h"
|
||||||
|
|
||||||
|
#include <QDir>
|
||||||
|
// #include <QTemporaryFile>
|
||||||
|
|
||||||
|
#include <QtTest/QtTest>
|
||||||
|
|
||||||
|
// #include <fcntl.h>
|
||||||
|
// #include <sys/stat.h>
|
||||||
|
// #include <unistd.h>
|
||||||
|
|
||||||
|
class TestPaths : public QObject
|
||||||
|
{
|
||||||
|
Q_OBJECT
|
||||||
|
public:
|
||||||
|
TestPaths() {};
|
||||||
|
virtual ~TestPaths() {};
|
||||||
|
|
||||||
|
private Q_SLOTS:
|
||||||
|
void initTestCase();
|
||||||
|
void init();
|
||||||
|
void cleanupTestCase();
|
||||||
|
|
||||||
|
void testTargetPath();
|
||||||
|
void testCreateTarget();
|
||||||
|
void testCreateTargetBasedirs();
|
||||||
|
|
||||||
|
private:
|
||||||
|
CalamaresUtils::System* m_system = nullptr; // Points to singleton instance, not owned
|
||||||
|
Calamares::GlobalStorage* m_gs = nullptr;
|
||||||
|
};
|
||||||
|
|
||||||
|
static const char testFile[] = "/calamares-testcreate";
|
||||||
|
static const char absFile[] = "/tmp/calamares-testcreate"; // With rootMountPoint prepended
|
||||||
|
|
||||||
|
void
|
||||||
|
TestPaths::initTestCase()
|
||||||
|
{
|
||||||
|
Logger::setupLogLevel( Logger::LOGDEBUG );
|
||||||
|
|
||||||
|
// Ensure we have a system object, expect it to be a "bogus" one
|
||||||
|
CalamaresUtils::System* system = CalamaresUtils::System::instance();
|
||||||
|
QVERIFY( system );
|
||||||
|
QVERIFY( system->doChroot() );
|
||||||
|
|
||||||
|
// Ensure we have a system-wide GlobalStorage with /tmp as root
|
||||||
|
if ( !Calamares::JobQueue::instance() )
|
||||||
|
{
|
||||||
|
cDebug() << "Creating new JobQueue";
|
||||||
|
(void)new Calamares::JobQueue();
|
||||||
|
}
|
||||||
|
Calamares::GlobalStorage* gs
|
||||||
|
= Calamares::JobQueue::instance() ? Calamares::JobQueue::instance()->globalStorage() : nullptr;
|
||||||
|
QVERIFY( gs );
|
||||||
|
|
||||||
|
m_system = system;
|
||||||
|
m_gs = gs;
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
TestPaths::cleanupTestCase()
|
||||||
|
{
|
||||||
|
QFile::remove( absFile );
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
TestPaths::init()
|
||||||
|
{
|
||||||
|
cDebug() << "Setting rootMountPoint";
|
||||||
|
m_gs->insert( "rootMountPoint", "/tmp" );
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void
|
||||||
|
TestPaths::testTargetPath()
|
||||||
|
{
|
||||||
|
// Paths mapped normally
|
||||||
|
QCOMPARE( m_system->targetPath( "/etc/calamares" ), QStringLiteral( "/tmp/etc/calamares" ) );
|
||||||
|
QCOMPARE( m_system->targetPath( "//etc//calamares" ),
|
||||||
|
QStringLiteral( "/tmp//etc//calamares" ) ); // extra / are not cleaned up
|
||||||
|
QCOMPARE( m_system->targetPath( "etc/calamares" ), QStringLiteral( "/tmp/etc/calamares" ) ); // relative to root
|
||||||
|
|
||||||
|
// Weird Paths
|
||||||
|
QCOMPARE( m_system->targetPath( QString() ), QStringLiteral( "/tmp/" ) );
|
||||||
|
|
||||||
|
// Now break GS
|
||||||
|
m_gs->remove( "rootMountPoint" );
|
||||||
|
QCOMPARE( m_system->targetPath( QString() ), QString() ); // Without root, no path
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void
|
||||||
|
TestPaths::testCreateTarget()
|
||||||
|
{
|
||||||
|
QCOMPARE( m_system->createTargetFile( testFile, "Hello" ), QString( absFile ) ); // Success
|
||||||
|
|
||||||
|
QFileInfo fi( absFile );
|
||||||
|
QVERIFY( fi.exists() );
|
||||||
|
QCOMPARE( fi.size(), 5 );
|
||||||
|
|
||||||
|
m_system->removeTargetFile( testFile );
|
||||||
|
QFileInfo fi2( absFile ); // fi caches information
|
||||||
|
QVERIFY( !fi2.exists() );
|
||||||
|
}
|
||||||
|
|
||||||
|
struct DirRemover
|
||||||
|
{
|
||||||
|
DirRemover( const QString& base, const QString& dir )
|
||||||
|
: m_base( base )
|
||||||
|
, m_dir( dir )
|
||||||
|
{
|
||||||
|
}
|
||||||
|
~DirRemover() { QDir( m_base ).rmpath( m_dir ); }
|
||||||
|
|
||||||
|
bool exists() const { return QDir( m_base ).exists( m_dir ); }
|
||||||
|
|
||||||
|
QString m_base, m_dir;
|
||||||
|
};
|
||||||
|
|
||||||
|
void
|
||||||
|
TestPaths::testCreateTargetBasedirs()
|
||||||
|
{
|
||||||
|
{
|
||||||
|
DirRemover dirrm( "/tmp", "var/lib/dbus" );
|
||||||
|
QVERIFY( m_system->createTargetDirs( "/" ) );
|
||||||
|
QVERIFY( m_system->createTargetDirs( "/var/lib/dbus" ) );
|
||||||
|
QVERIFY( QFile( "/tmp/var/lib/dbus" ).exists() );
|
||||||
|
QVERIFY( dirrm.exists() );
|
||||||
|
}
|
||||||
|
QVERIFY( !QFile( "/tmp/var/lib/dbus" ).exists() );
|
||||||
|
|
||||||
|
// QFileInfo.dir() behaves even when things don't exist
|
||||||
|
QCOMPARE( QFileInfo( "/tmp/var/lib/dbus/bogus" ).dir().path(), QStringLiteral( "/tmp/var/lib/dbus" ) );
|
||||||
|
}
|
||||||
|
|
||||||
|
QTEST_GUILESS_MAIN( TestPaths )
|
||||||
|
|
||||||
|
#include "utils/moc-warnings.h"
|
||||||
|
|
||||||
|
#include "TestPaths.moc"
|
@ -1,77 +0,0 @@
|
|||||||
#!/usr/bin/env python3
|
|
||||||
# -*- coding: utf-8 -*-
|
|
||||||
#
|
|
||||||
# === This file is part of Calamares - <https://github.com/calamares> ===
|
|
||||||
#
|
|
||||||
# Copyright 2014, Kevin Kofler <kevin.kofler@chello.at>
|
|
||||||
# Copyright 2016,2020 Philip Müller <philm@manjaro.org>
|
|
||||||
# Copyright 2017, Alf Gaida <agaida@siduction.org>
|
|
||||||
# Copyright 2019, Adriaan de Groot <groot@kde.org>
|
|
||||||
#
|
|
||||||
# Calamares is free software: you can redistribute it and/or modify
|
|
||||||
# it under the terms of the GNU General Public License as published by
|
|
||||||
# the Free Software Foundation, either version 3 of the License, or
|
|
||||||
# (at your option) any later version.
|
|
||||||
#
|
|
||||||
# Calamares is distributed in the hope that it will be useful,
|
|
||||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
# GNU General Public License for more details.
|
|
||||||
#
|
|
||||||
# You should have received a copy of the GNU General Public License
|
|
||||||
# along with Calamares. If not, see <http://www.gnu.org/licenses/>.
|
|
||||||
|
|
||||||
import libcalamares
|
|
||||||
import os
|
|
||||||
from libcalamares.utils import check_target_env_call, debug
|
|
||||||
|
|
||||||
import gettext
|
|
||||||
_ = gettext.translation("calamares-python",
|
|
||||||
localedir=libcalamares.utils.gettext_path(),
|
|
||||||
languages=libcalamares.utils.gettext_languages(),
|
|
||||||
fallback=True).gettext
|
|
||||||
|
|
||||||
|
|
||||||
def pretty_name():
|
|
||||||
return _("Generate machine-id.")
|
|
||||||
|
|
||||||
|
|
||||||
def run():
|
|
||||||
"""
|
|
||||||
Generate machine-id using dbus and systemd.
|
|
||||||
|
|
||||||
:return:
|
|
||||||
"""
|
|
||||||
root_mount_point = libcalamares.globalstorage.value("rootMountPoint")
|
|
||||||
|
|
||||||
if root_mount_point is None:
|
|
||||||
libcalamares.utils.warning("rootMountPoint is empty, {!s}".format(root_mount_point))
|
|
||||||
return (_("Configuration Error"),
|
|
||||||
_("No root mount point is given for <pre>{!s}</pre> to use." ).format("machineid"))
|
|
||||||
|
|
||||||
enable_systemd = libcalamares.job.configuration["systemd"]
|
|
||||||
enable_dbus = libcalamares.job.configuration["dbus"]
|
|
||||||
enable_symlink = libcalamares.job.configuration["symlink"]
|
|
||||||
target_systemd_machineid_file = root_mount_point + "/etc/machine-id"
|
|
||||||
target_dbus_machineid_file = root_mount_point + "/var/lib/dbus/machine-id"
|
|
||||||
target_dbus_folder = root_mount_point + "/var/lib/dbus"
|
|
||||||
|
|
||||||
if not os.path.exists(target_dbus_folder):
|
|
||||||
os.mkdir(target_dbus_folder, 0644)
|
|
||||||
|
|
||||||
if os.path.exists(target_dbus_machineid_file):
|
|
||||||
os.remove(target_dbus_machineid_file)
|
|
||||||
|
|
||||||
if enable_systemd:
|
|
||||||
if os.path.exists(target_systemd_machineid_file):
|
|
||||||
os.remove(target_systemd_machineid_file)
|
|
||||||
check_target_env_call("systemd-machine-id-setup")
|
|
||||||
|
|
||||||
if enable_dbus:
|
|
||||||
if enable_symlink and os.path.exists(target_systemd_machineid_file):
|
|
||||||
check_target_env_call(["ln", "-sf", "/etc/machine-id",
|
|
||||||
"/var/lib/dbus/machine-id"])
|
|
||||||
else:
|
|
||||||
check_target_env_call(["dbus-uuidgen", "--ensure"])
|
|
||||||
|
|
||||||
return None
|
|
Loading…
Reference in New Issue