Home > Blockchain >  Cannot reboot system from C
Cannot reboot system from C

Time:11-11

I've a C program running in a docker container. I want to reboot container via my program but I cannot.

Dockerfile:

FROM gcc

WORKDIR /client
COPY . .

RUN apt-get update && apt-get install qt5-qmake qtbase5-dev libmosquitto-dev -y

RUN qmake mainapp.pro && make
RUN chmod  x docker-entrypoint.sh

docker-entrypoint.sh

#!/bin/bash
./mainapp -e

Here is my reboot function (cleared from unnecessary codes):

    Logger::getInstance()->write(QString("Sync"));
    sync();
    Logger::getInstance()->write(QString("Setuid"));
    setuid(0);
    Logger::getInstance()->write(QString("RB_AUTOBOOT"));
    reboot(RB_AUTOBOOT);
    Logger::getInstance()->write(QString("Reboot"));
    system("reboot");

I can see ALL the outputs and then it prints an error message. Here is the error message:

System has not been booted with systemd as init system (PID 1). Can't operate.
Failed to connect to bus: Host is down
Failed to talk to init daemon.

I tried to reboot from container but it doesn't work as well:

root@93501f6d7fc8:/client# reboot
System has not been booted with systemd as init system (PID 1). Can't operate.
Failed to connect to bus: Host is down
Failed to talk to init daemon.
root@93501f6d7fc8:/client# shutdown -r
System has not been booted with systemd as init system (PID 1). Can't operate.
Failed to connect to bus: Host is down

I've searched this problem and tried most of the suggestions but they didn't solved my problem.

I am open to all your ideas and suggestions.

CodePudding user response:

system("reboot") tries to use the shell (which may not exist in your docker container) to run the reboot command (which may not exist in your docker container). Besides, "rebooting" is not a well-defined concept. You can't reboot the kernel, since that's shared. A docker container is an app-level container.

To restart your app, and by extension the container, just use docker restart=always and then call std::exit(0) to exit your app normally.

CodePudding user response:

Thanks for the answers. I added this to my docker compose yaml file:

    privileged: true
    restart: always

After that, reboot(RB_AUTOBOOT); rebooted my container and started the app successfully.

  • Related