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

Cloud-init support for Raspberry Pi OS #950

Draft
wants to merge 14 commits into
base: qml
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
6 changes: 4 additions & 2 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -282,9 +282,11 @@ set(SOURCES ${SOURCES} ${CMAKE_CURRENT_BINARY_DIR}/translations.qrc ${QM_FILES})

if (WIN32)
# Adding WIN32 prevents a console window being opened on Windows
add_executable(${PROJECT_NAME} WIN32 ${SOURCES} ${HEADERS} ${DEPENDENCIES})
add_executable(${PROJECT_NAME} WIN32 ${SOURCES} ${HEADERS} ${DEPENDENCIES}
extraFiles.qrc)
else()
add_executable(${PROJECT_NAME} ${SOURCES} ${HEADERS} ${DEPENDENCIES})
add_executable(${PROJECT_NAME} ${SOURCES} ${HEADERS} ${DEPENDENCIES}
extraFiles.qrc)
endif()

set_property(TARGET ${PROJECT_NAME} PROPERTY AUTOMOC ON)
Expand Down
107 changes: 77 additions & 30 deletions src/OptionsPopup.qml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ Window {
property string cloudinitrun
property string cloudinitwrite
property string cloudinitnetwork
property bool deviceUsbOtgSupport: false
property bool enableEtherGadget

signal saveSettingsSignal(var settings)

Expand Down Expand Up @@ -358,6 +360,12 @@ Window {
ColumnLayout {
// Remote access tab

ImCheckBox {
id: chkUSBEther
text: qsTr("Enable USB Ethernet Gadget")
enabled: deviceUsbOtgSupport
}

ImCheckBox {
id: chkSSH
text: qsTr("Enable SSH")
Expand Down Expand Up @@ -632,6 +640,14 @@ Window {
}
}

if (imageWriter.checkHWAndSWCapability("usb_otg")) {
deviceUsbOtgSupport = true
} else {
deviceUsbOtgSupport = false
// make sure it isn't disabled and selected
chkUSBEther.checked = false
}

//open()
show()
raise()
Expand Down Expand Up @@ -696,21 +712,35 @@ Window {
addCloudInit("")
}

var isRpiosCloudInit = imageWriter.checkSWCapability("rpios_cloudinit");

if (chkSSH.checked || chkSetUser.checked) {
// First user may not be called 'pi' on all distributions, so look username up
addFirstRun("FIRSTUSER=`getent passwd 1000 | cut -d: -f1`");
addFirstRun("FIRSTUSERHOME=`getent passwd 1000 | cut -d: -f6`")

addCloudInit("users:")
addCloudInit("- name: "+fieldUserName.text)
addCloudInit(" groups: users,adm,dialout,audio,netdev,video,plugdev,cdrom,games,input,gpio,spi,i2c,render,sudo")
addCloudInit(" shell: /bin/bash")

var cryptedPassword;
if (chkSetUser.checked) {
cryptedPassword = fieldUserPassword.alreadyCrypted ? fieldUserPassword.text : imageWriter.crypt(fieldUserPassword.text)
addCloudInit(" lock_passwd: false")
addCloudInit(" passwd: "+cryptedPassword)
}

if (isRpiosCloudInit && cryptedPassword) {
addCloudInit("rpi_userconf:")
addCloudInit(" password: " + cryptedPassword)
addCloudInit(" user: " + fieldUserName.text)
addCloudInit("")
}

addCloudInit("users:")
addCloudInit("- name: " + fieldUserName.text)
if (!isRpiosCloudInit) {
addCloudInit(" groups: users,adm,dialout,audio,netdev,video,plugdev,cdrom,games,input,gpio,spi,i2c,render,sudo")
addCloudInit(" shell: /bin/bash")

if (chkSetUser.checked) {
addCloudInit(" lock_passwd: false")
addCloudInit(" passwd: "+cryptedPassword)
}
}

if (chkSSH.checked && radioPubKeyAuthentication.checked) {
Expand Down Expand Up @@ -743,7 +773,12 @@ Window {
addCloudInit(" - "+pk)
}
}
addCloudInit(" sudo: ALL=(ALL) NOPASSWD:ALL")
if (!isRpiosCloudInit) {
addCloudInit(" sudo: ALL=(ALL) NOPASSWD:ALL")
} else {
addCloudInit("\nrpi_interfaces:")
addCloudInit(" ssh: true")
}
}
addCloudInit("")

Expand Down Expand Up @@ -779,6 +814,14 @@ Window {
addFirstRun("fi")
}
addCloudInit("")

/*if (chkSetUser.checked) {
addCloudInit("final_message: \"Setup wizard has been skiped.\"")
addCloudInit("power_state:")
addCloudInit(" mode: reboot")
addCloudInit(" message: Rebooting machine")
addCloudInit("")
}*/
}
if (chkWifi.checked) {
var wpaconfig = "country="+fieldWifiCountry.editText+"\n"
Expand Down Expand Up @@ -814,21 +857,36 @@ Window {
addFirstRun("fi")


cloudinitnetwork = "version: 2\n"
cloudinitnetwork += "wifis:\n"
cloudinitnetwork += " renderer: networkd\n"
cloudinitnetwork += " wlan0:\n"
cloudinitnetwork += " dhcp4: true\n"
cloudinitnetwork += " optional: true\n"
cloudinitnetwork += " access-points:\n"
cloudinitnetwork += " \""+fieldWifiSSID.text+"\":\n"
cloudinitnetwork += " password: \""+cryptedPsk+"\"\n"
cloudinitnetwork = "network:\n"
cloudinitnetwork += " version: 2\n"
cloudinitnetwork += " renderer: " + (isRpiosCloudInit ? "NetworkManager" : "networkd") + "\n"
cloudinitnetwork += " wifis:\n"
cloudinitnetwork += " wlan0:\n"
cloudinitnetwork += " dhcp4: true\n"
cloudinitnetwork += " optional: true\n"
cloudinitnetwork += " access-points:\n"
cloudinitnetwork += " \""+fieldWifiSSID.text+"\":\n"
cloudinitnetwork += " password: \""+cryptedPsk+"\"\n"
if (chkWifiSSIDHidden.checked) {
cloudinitnetwork += " hidden: true\n"
cloudinitnetwork += " hidden: true\n"
}

addCmdline("cfg80211.ieee80211_regdom="+fieldWifiCountry.editText)
}
if (chkUSBEther.checked) {
// keep parity with cli.cpp
addConfig("dtoverlay=dwc2,dr_mode=peripheral")

enableEtherGadget = true;

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

if (chkLocale.checked) {
var kbdconfig = "XKBMODEL=\"pc105\"\n"
kbdconfig += "XKBLAYOUT=\""+fieldKeyboardLayout.editText+"\"\n"
Expand All @@ -854,17 +912,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 @@ -873,7 +920,7 @@ Window {
addCloudInit("runcmd:\n"+cloudinitrun+"\n")
}

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

function saveSettings()
Expand Down
17 changes: 14 additions & 3 deletions src/cli.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,19 +58,22 @@ 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 (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"},
});

parser.addVersionOption();
parser.addHelpOption();
parser.addPositionalArgument("src", "Image file/URL");
parser.addPositionalArgument("dst", "Destination device");
parser.process(*_app);

const QStringList args = parser.positionalArguments();
if (args.count() != 2)
{
std::cerr << "Usage: --cli [--disable-verify] [--disable-eject] [--sha256 <expected hash> [--cache-file <cache file>]] [--first-run-script <script>] [--debug] [--quiet] <image file to write> <destination drive device>" << std::endl;
std::cerr << "Usage: --cli [--disable-verify] [--disable-eject] [--sha256 <expected hash> [--cache-file <cache file>]] [--first-run-script <script>] [--usb-ether-gadget] [--debug] [--quiet] <image file to write> <destination drive device>" << std::endl;
return 1;
}

Expand Down Expand Up @@ -148,6 +151,8 @@ int Cli::run()
}
}

bool isEtherGadgetEnabled = parser.isSet("usb-ether-gadget");

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

_imageWriter->setImageCustomization("", "", "", userData, networkConfig);
_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 @@ -208,11 +218,12 @@ int Cli::run()
return 1;
}

_imageWriter->setImageCustomization("", "", firstRunScript, "", "");
_imageWriter->setImageCustomization("", "", firstRunScript, "", "", true, isEtherGadgetEnabled);
}

_imageWriter->setDst(args[1]);
_imageWriter->setVerifyEnabled(!parser.isSet("disable-verify"));
_imageWriter->setEtherGadgetEnabled(isEtherGadgetEnabled);
_imageWriter->setSetting("eject", !parser.isSet("disable-eject"));

/* Run startWrite() in event loop (otherwise calling _app->exit() on error does not work) */
Expand Down
59 changes: 55 additions & 4 deletions src/downloadthread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -855,6 +855,10 @@ void DownloadThread::setVerifyEnabled(bool verify)
_verifyEnabled = verify;
}

void DownloadThread::setEtherGadgetEnabled(bool etherGadget) {
_enableEtherGadget = etherGadget;
}

bool DownloadThread::isImage()
{
return true;
Expand Down Expand Up @@ -885,14 +889,16 @@ 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)
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;
}

bool DownloadThread::_customizeImage()
Expand Down Expand Up @@ -968,10 +974,55 @@ bool DownloadThread::_customizeImage()
}
}

if (!_firstrun.isEmpty() && _initFormat == "systemd")
if (_enableEtherGadget) {
// load files from disk and write
QByteArray networkConfig = _fileGetContentsTrimmed("://extraFiles/10-usb.network");
fat->writeFile("10usb.net", networkConfig);
// 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 constrained systems
modprobeConf.clear();

QByteArray modulesConf = _fileGetContentsTrimmed("://extraFiles/usb-ether-gadget.conf");
fat->writeFile("gemod.cnf", modulesConf);
// little optimization for memory constrained systems
modulesConf.clear();

QByteArray controllScript = _fileGetContentsTrimmed("://extraFiles/rpi-usb-ether-gadget.sh");
fat->writeFile("uethc.sh", controllScript);
controllScript.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");
_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");
_firstrun.append("mv /boot/firmware/uethc.sh /usr/bin/rpi-usb-ether-gadget\n");
_firstrun.append("chmod +x /usr/bin/rpi-usb-ether-gadget\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())
{
fat->writeFile("firstrun.sh", _firstrun);
_cmdline += " systemd.run=/boot/firstrun.sh systemd.run_success_action=reboot systemd.unit=kernel-command-line.target";
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
8 changes: 7 additions & 1 deletion src/downloadthread.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,11 @@ class DownloadThread : public QThread
*/
void setVerifyEnabled(bool verify);

/*
* Enable/disable USB Ethernet Gadget mode config
*/
void setEtherGadgetEnabled(bool etherGadget);

/*
* Enable disk cache
*/
Expand All @@ -116,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);
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 @@ -171,6 +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 _userDefinedFirstRun, _enableEtherGadget;
char *_firstBlock;
size_t _firstBlockSize;
static QByteArray _proxy;
Expand Down
8 changes: 8 additions & 0 deletions src/extraFiles.qrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<RCC>
<qresource prefix="/">
<file>extraFiles/10-usb.network</file>
<file>extraFiles/g_ether.conf</file>
<file>extraFiles/usb-ether-gadget.conf</file>
<file>extraFiles/rpi-usb-ether-gadget.sh</file>
</qresource>
</RCC>
Loading