Skip to content

Commit

Permalink
Fix typos and duplicate code
Browse files Browse the repository at this point in the history
Signed-off-by: paulober <[email protected]>
  • Loading branch information
paulober committed Sep 20, 2024
1 parent 38ee103 commit bd69720
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 49 deletions.
13 changes: 1 addition & 12 deletions src/OptionsPopup.qml
Original file line number Diff line number Diff line change
Expand Up @@ -885,17 +885,6 @@ Window {
addCloudInit(" layout: \"" + fieldKeyboardLayout.editText + "\"")
}

if (firstrun.length) {
firstrun = "#!/bin/bash\n\n"+"set +e\n\n"+firstrun
addFirstRun("rm -f /boot/firstrun.sh")
addFirstRun("sed -i 's| systemd.run.*||g' /boot/cmdline.txt")
addFirstRun("exit 0")
/* using systemd.run_success_action=none does not seem to have desired effect
systemd then stays at "reached target kernel command line", so use reboot instead */
//addCmdline("systemd.run=/boot/firstrun.sh systemd.run_success_action=reboot systemd.unit=kernel-command-line.target")
// cmdline changing moved to DownloadThread::_customizeImage()
}

if (cloudinitwrite !== "") {
addCloudInit("write_files:\n"+cloudinitwrite+"\n")
}
Expand All @@ -904,7 +893,7 @@ Window {
addCloudInit("runcmd:\n"+cloudinitrun+"\n")
}

imageWriter.setImageCustomization(config, cmdline, firstrun, cloudinit, cloudinitnetwork, enableEtherGadget)
imageWriter.setImageCustomization(config, cmdline, firstrun, cloudinit, cloudinitnetwork, false, enableEtherGadget)
}

function saveSettings()
Expand Down
27 changes: 8 additions & 19 deletions src/cli.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ int Cli::run()
{"first-run-script", "Add firstrun.sh to image", "first-run-script", ""},
{"cloudinit-userdata", "Add cloud-init user-data file to image", "cloudinit-userdata", ""},
{"cloudinit-networkconfig", "Add cloud-init network-config file to image", "cloudinit-networkconfig", ""},
{"usb-ether-gadget", "Enable USB Ethernet Gadget mode"},
{"usb-ether-gadget", "Enable USB Ethernet Gadget mode (does not support --first-run-script)"},
{"disable-eject", "Disable automatic ejection of storage media after verification"},
{"debug", "Output debug messages to console"},
{"quiet", "Only write to console on error"},
Expand Down Expand Up @@ -152,19 +152,6 @@ int Cli::run()
}

bool isEtherGadgetEnabled = parser.isSet("usb-ether-gadget");
QString firstRunScriptAddition = "";
QString configTxt = "";
if (isEtherGadgetEnabled) {
// setup config and first run script addition - keep parity with OptionsPopup.qml
configTxt += "dtoverlay=dwc2,dr_mode=peripheral\n";

firstRunScriptAddition += "\nmv /boot/firmware/10usb.net /etc/systemd/network/10-usb.network\n";
firstRunScriptAddition += "mv /boot/firmware/geth.cnf /etc/modprobe.d/g_ether.conf\n";
firstRunScriptAddition += "mv /boot/firmware/gemod.cnf /etc/modules-load.d/usb-ether-gadget.conf\n\n";
firstRunScriptAddition += "SERIAL=$(grep Serial /proc/cpuinfo | awk '{print $3}')\n";
firstRunScriptAddition += "sed -i \"s/<serial>/$SERIAL/g\" /etc/modprobe.d/g_ether.conf\n";
firstRunScriptAddition += "systemctl enable systemd-networkd\n";
}

if (!parser.value("cloudinit-userdata").isEmpty())
{
Expand Down Expand Up @@ -204,10 +191,15 @@ int Cli::run()
return 1;
}

_imageWriter->setImageCustomization(configTxt.toUtf8(), "", firstRunScriptAddition.toUtf8(), userData, networkConfig, isEtherGadgetEnabled);
_imageWriter->setImageCustomization("", "", "", userData, networkConfig, false, isEtherGadgetEnabled);
}
else if (!parser.value("first-run-script").isEmpty())
{
if (isEtherGadgetEnabled) {
std::cerr << "Error: the --usb-ether-gadget option is not supported when --first-run-script is used.";
return 1;
}

QByteArray firstRunScript;
QFile f(parser.value("first-run-script"));
if (!f.exists())
Expand All @@ -226,10 +218,7 @@ int Cli::run()
return 1;
}

_imageWriter->setImageCustomization(configTxt.toUtf8(), "", firstRunScript.append(firstRunScriptAddition.toUtf8()), "", "", isEtherGadgetEnabled);
}
else if (!firstRunScriptAddition.isEmpty() || !configTxt.isEmpty()) {
_imageWriter->setImageCustomization(configTxt.toUtf8(), "", firstRunScriptAddition.toUtf8(), "", "", isEtherGadgetEnabled);
_imageWriter->setImageCustomization("", "", firstRunScript, "", "", true, isEtherGadgetEnabled);
}

_imageWriter->setDst(args[1]);
Expand Down
43 changes: 32 additions & 11 deletions src/downloadthread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -889,14 +889,15 @@ qint64 DownloadThread::_sectorsWritten()
return -1;
}

void DownloadThread::setImageCustomization(const QByteArray &config, const QByteArray &cmdline, const QByteArray &firstrun, const QByteArray &cloudinit, const QByteArray &cloudInitNetwork, const QByteArray &initFormat, const bool enableEtherGadget)
void DownloadThread::setImageCustomization(const QByteArray &config, const QByteArray &cmdline, const QByteArray &firstrun, const QByteArray &cloudinit, const QByteArray &cloudInitNetwork, const QByteArray &initFormat, const bool userDefinedFirstRun, const bool enableEtherGadget)
{
_config = config;
_cmdline = cmdline;
_firstrun = firstrun;
_cloudinit = cloudinit;
_cloudinitNetwork = cloudInitNetwork;
_initFormat = initFormat;
_userDefinedFirstRun = userDefinedFirstRun;
_enableEtherGadget = enableEtherGadget;
}

Expand Down Expand Up @@ -973,29 +974,49 @@ bool DownloadThread::_customizeImage()
}
}

if (!_firstrun.isEmpty() && _initFormat == "systemd")
{
fat->writeFile("firstrun.sh", _firstrun);
_cmdline += " systemd.run=/boot/firstrun.sh systemd.run_success_action=reboot systemd.unit=kernel-command-line.target";
}

if (_enableEtherGadget) {
// load files from disk and write
QByteArray networkConfig = _fileGetContentsTrimmed("://extraFiles/10-usb.network");
fat->writeFile("10usb.net", networkConfig);
// little optimization for memory constraint systems
// little optimization for memory constrained systems
networkConfig.clear();

// only needed for manual config without g_ether
QByteArray modprobeConf = _fileGetContentsTrimmed("://extraFiles/g_ether.conf");
fat->writeFile("geth.cnf", modprobeConf);
// little optimization for memory constraint systems
// little optimization for memory constrained systems
modprobeConf.clear();

QByteArray modulesConf = _fileGetContentsTrimmed("://extraFiles/usb-ether-gadget.conf");
fat->writeFile("gemod.cnf", modulesConf);
// not needed anymore, because auto cleanup after out of scope
//modulesConf.clear();
// little optimization for memory constrained systems
modulesConf.clear();

// add config.txt change - \n prefix to also work if user defined config doesn't end with a LF
_config.append("\ndtoverlay=dwc2,dr_mode=peripheral\n");
_firstrun.append("\nmv /boot/firmware/10usb.net /etc/systemd/network/10-usb.network\n\n");
_firstrun.append("mv /boot/firmware/geth.cnf /etc/modprobe.d/g_ether.conf\n");
_firstrun.append("mv /boot/firmware/gemod.cnf /etc/modules-load.d/usb-ether-gadget.conf\n\n");
_firstrun.append("SERIAL=$(grep Serial /proc/cpuinfo | awk '{print $3}')\n");
_firstrun.append("sed -i \"s/<serial>/$SERIAL/g\" /etc/modprobe.d/g_ether.conf\n");
_firstrun.append("systemctl enable systemd-networkd\n\n");
}

if (!_firstrun.isEmpty())
{
if (!_userDefinedFirstRun) {
_firstrun = "#!/bin/bash\n\n" + QByteArray("set +e\n\n") + _firstrun;

// Add file cleanup and exit commands
_firstrun.append("\nrm -f /boot/firstrun.sh\n");
_firstrun.append("sed -i 's| systemd.run.*||g' /boot/cmdline.txt\n");
_firstrun.append("exit 0\n");
}

if (_initFormat == "systemd") {
fat->writeFile("firstrun.sh", _firstrun);
_cmdline += " systemd.run=/boot/firstrun.sh systemd.run_success_action=reboot systemd.unit=kernel-command-line.target";
}
}

if (!_cloudinit.isEmpty() && _initFormat == "cloudinit")
Expand Down
4 changes: 2 additions & 2 deletions src/downloadthread.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ class DownloadThread : public QThread
/*
* Enable image customization
*/
void setImageCustomization(const QByteArray &config, const QByteArray &cmdline, const QByteArray &firstrun, const QByteArray &cloudinit, const QByteArray &cloudinitNetwork, const QByteArray &initFormat, const bool enableEtherGadget);
void setImageCustomization(const QByteArray &config, const QByteArray &cmdline, const QByteArray &firstrun, const QByteArray &cloudinit, const QByteArray &cloudinitNetwork, const QByteArray &initFormat, const bool userDefinedFirstRun, const bool enableEtherGadget);

/*
* Thread safe download progress query functions
Expand Down Expand Up @@ -176,7 +176,7 @@ class DownloadThread : public QThread
std::uint64_t _lastFailureOffset;
qint64 _sectorsStart;
QByteArray _url, _useragent, _buf, _filename, _lastError, _expectedHash, _config, _cmdline, _firstrun, _cloudinit, _cloudinitNetwork, _initFormat;
bool _enableEtherGadget;
bool _userDefinedFirstRun, _enableEtherGadget;
char *_firstBlock;
size_t _firstBlockSize;
static QByteArray _proxy;
Expand Down
6 changes: 4 additions & 2 deletions src/imagewriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ void ImageWriter::startWrite()
connect(_thread, SIGNAL(preparationStatusUpdate(QString)), SLOT(onPreparationStatusUpdate(QString)));
_thread->setVerifyEnabled(_verifyEnabled);
_thread->setUserAgent(QString("Mozilla/5.0 rpi-imager/%1").arg(constantVersion()).toUtf8());
_thread->setImageCustomization(_config, _cmdline, _firstrun, _cloudinit, _cloudinitNetwork, _initFormat, _enableEtherGadget);
_thread->setImageCustomization(_config, _cmdline, _firstrun, _cloudinit, _cloudinitNetwork, _initFormat, _userDefinedFirstRun, _enableEtherGadget);

if (!_expectedHash.isEmpty() && _cachedFileHash != _expectedHash && _cachingEnabled)
{
Expand Down Expand Up @@ -1203,19 +1203,21 @@ void ImageWriter::setSetting(const QString &key, const QVariant &value)
_settings.sync();
}

void ImageWriter::setImageCustomization(const QByteArray &config, const QByteArray &cmdline, const QByteArray &firstrun, const QByteArray &cloudinit, const QByteArray &cloudinitNetwork, const bool enableEtherGadget)
void ImageWriter::setImageCustomization(const QByteArray &config, const QByteArray &cmdline, const QByteArray &firstrun, const QByteArray &cloudinit, const QByteArray &cloudinitNetwork, const bool userDefinedFirstRun, const bool enableEtherGadget)
{
_config = config;
_cmdline = cmdline;
_firstrun = firstrun;
_cloudinit = cloudinit;
_cloudinitNetwork = cloudinitNetwork;
_userDefinedFirstRun = userDefinedFirstRun;
_enableEtherGadget = enableEtherGadget;

qDebug() << "Custom config.txt entries:" << config;
qDebug() << "Custom cmdline.txt entries:" << cmdline;
qDebug() << "Custom firstuse.sh:" << firstrun;
qDebug() << "Cloudinit:" << cloudinit;
qDebug() << "Is user-defined firstRun.sh:" << userDefinedFirstRun;
qDebug() << "Enable USB Ethernet Gadget mode:" << enableEtherGadget;
}

Expand Down
6 changes: 3 additions & 3 deletions src/imagewriter.h
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ class ImageWriter : public QObject

Q_INVOKABLE bool getBoolSetting(const QString &key);
Q_INVOKABLE void setSetting(const QString &key, const QVariant &value);
Q_INVOKABLE void setImageCustomization(const QByteArray &config, const QByteArray &cmdline, const QByteArray &firstrun, const QByteArray &cloudinit, const QByteArray &cloudinitNetwork, const bool enableEtherGadget);
Q_INVOKABLE void setImageCustomization(const QByteArray &config, const QByteArray &cmdline, const QByteArray &firstrun, const QByteArray &cloudinit, const QByteArray &cloudinitNetwork, const bool userDefinedFirstRun, const bool enableEtherGadget);
Q_INVOKABLE void setSavedCustomizationSettings(const QVariantMap &map);
Q_INVOKABLE QVariantMap getSavedCustomizationSettings();
Q_INVOKABLE void clearSavedCustomizationSettings();
Expand Down Expand Up @@ -198,14 +198,14 @@ protected slots:
QJsonDocument _completeOsList;
QJsonArray _deviceFilter;
bool _deviceFilterIsInclusive;
/* As there is no distinction between normal pi models and zeros (in the tags), this flag can be used to differenciate */
/* As there is no distinction between normal pi models and zeros (in the tags), this flag can be used to differentiate */
bool _isModelZero;

protected:
QUrl _src, _repo;
QString _dst, _cacheFileName, _parentCategory, _osName, _currentLang, _currentLangcode, _currentKeyboard;
QByteArray _expectedHash, _cachedFileHash, _cmdline, _config, _firstrun, _cloudinit, _cloudinitNetwork, _initFormat;
bool _enableEtherGadget;
bool _userDefinedFirstRun, _enableEtherGadget;
quint64 _downloadLen, _extrLen, _devLen, _dlnow, _verifynow;
DriveListModel _drivelist;
QQmlApplicationEngine *_engine;
Expand Down

0 comments on commit bd69720

Please sign in to comment.