Make systemd service to run script by root user in chroot environment. Take attention to…
- User
Make systemd service to run script by root user in chroot environment. Take attention to run later after successfully binding chroot mountpoins and multiuser target. Chroot directory is /var/chroot. Script directory in chroot is /root/reconfig.sh
- Assistant
Creating a
systemdservice to run a script by the root user in a chroot environment requires a series of steps, including ensuring that the chroot environment is properly set up and that all necessary filesystems are mounted. Additionally, the service must be configured to run after the necessary targets are reached.Below is a step-by-step guide to accomplish this:
-
Ensure Chroot Environment is Prepared: Make sure your chroot environment (
/var/chroot) has all necessary directories and files. Common subdirectories that need to be bound include/proc,/sys, and/dev. -
Create the systemd Service File:
Create a new service file in
/etc/systemd/system/. For example, you can call itchroot-reconfig.service.sudo nano /etc/systemd/system/chroot-reconfig.serviceAdd the following configuration to the file:
[Unit] Description=Run script in chroot environment After=multi-user.target local-fs.target Wants=multi-user.target local-fs.target [Service] Type=oneshot ExecStartPre=/usr/bin/mount --bind /proc /var/chroot/proc ExecStartPre=/usr/bin/mount --bind /sys /var/chroot/sys ExecStartPre=/usr/bin/mount --bind /dev /var/chroot/dev ExecStart=/usr/sbin/chroot /var/chroot /bin/bash -c '/root/reconfig.sh' ExecStopPost=/usr/bin/umount /var/chroot/proc ExecStopPost=/usr/bin/umount /var/chroot/sys ExecStopPost=/usr/bin/umount /var/chroot/dev RemainAfterExit=yes [Install] WantedBy=multi-user.targetExplanation of the service file:
- [Unit]:
Description: A short description of the service.AfterandWants: Ensures this service starts after the multi-user target and local file systems are mounted.
- [Service]:
Type=oneshot: The service will perform a single start-up task and then exit.ExecStartPre: Commands to execute before starting the script. These commands bind the necessary directories for the chroot environment.ExecStart: The main command to run the script in the chroot environment.ExecStopPost: Commands to execute after the main command. These commands unbind the directories.RemainAfterExit=yes: Ensures the service is considered active even after the script has finished execution.
- [Install]:
WantedBy: Indicates that this service should be started when multi-user.target is reached.
- [Unit]:
-
Reload systemd Daemon:
After creating the service file, reload the
systemdmanager configuration to apply the new service.sudo systemctl daemon-reload -
Enable and Start the Service:
Enable the service so that it starts on boot, and then start it immediately.
sudo systemctl enable chroot-reconfig.service sudo systemctl start chroot-reconfig.service -
Check the Status:
You can check the status of your service to ensure it is running as expected.
sudo systemctl status chroot-reconfig.service
With these steps, you've created a
systemdservice that runs a script by the root user within a chroot environment. The script will only run after the necessary targets and file systems are available. -
- User
Half this service to two different: chroot-mount and script. Script requires chroot-mount
- Assistant
To split the functionality into two different
systemdservices, one for handling the chroot mounts and another for running the script inside the chroot, you'll need to create two separate service files. The script service will depend on the chroot mount service.Step 1: Create
chroot-mount.serviceCreate a new service file for mounting the necessary directories under
/var/chroot.sudo nano /etc/systemd/system/chroot-mount.serviceAdd the following configuration:
[Unit] Description=Mount directories for chroot environment After=local-fs.target [Service] Type=oneshot ExecStart=/usr/bin/mount --bind /proc /var/chroot/proc ExecStart=/usr/bin/mount --bind /sys /var/chroot/sys ExecStart=/usr/bin/mount --bind /dev /var/chroot/dev RemainAfterExit=yes ExecStop=/usr/bin/umount /var/chroot/proc ExecStop=/usr/bin/umount /var/chroot/sys ExecStop=/usr/bin/umount /var/chroot/dev [Install] WantedBy=multi-user.targetStep 2: Create
script.serviceCreate another service file that runs the script inside the chroot environment. This service will depend on
chroot-mount.service.sudo nano /etc/systemd/system/script.serviceAdd the following configuration:
[Unit] Description=Run script inside chroot environment Requires=chroot-mount.service After=chroot-mount.service [Service] Type=oneshot ExecStart=/usr/sbin/chroot /var/chroot /bin/bash -c '/root/reconfig.sh' [Install] WantedBy=multi-user.targetStep 3: Reload systemd Daemon
After creating both service files, reload the
systemdmanager configuration to apply the new services.sudo systemctl daemon-reloadStep 4: Enable and Start the Services
Enable both services so they start on boot. Start them in the correct order where the script service depends on the chroot mount service.
sudo systemctl enable chroot-mount.service sudo systemctl enable script.service sudo systemctl start script.serviceStep 5: Verify the Status
Check the status of both services to ensure they are running as expected.
sudo systemctl status chroot-mount.service sudo systemctl status script.serviceWith these steps, you have successfully split the original
systemdservice into two separate services:chroot-mount.service, which handles binding the necessary directories for the chroot environment.script.service, which runs the script inside the chroot environment and depends on the chroot mounts being in place.
- User
Why in first case you used ExecStopPost for umount, but in second case there is ExecStop?
- Assistant
I apologize for any confusion caused by the inconsistency. Typically,
ExecStopis used to specify commands that run when a service is being stopped, whileExecStopPostruns afterExecStop. In most cases, for a simple service, you can useExecStopdirectly unless you have more specific timing or sequence requirements.Let’s make this consistent. Both approaches should ideally be uniform.
Correcting the First Case with
ExecStop:In both services, using
ExecStopis appropriate for unmounting as we want to ensure these actions are taken when the service stops.Step 1: Update
chroot-mount.serviceCorrect the service file to use
ExecStopfor unmounting the directories.sudo nano /etc/systemd/system/chroot-mount.serviceUpdate the file to include
ExecStop:[Unit] Description=Mount directories for chroot environment After=local-fs.target [Service] Type=oneshot ExecStart=/usr/bin/mount --bind /proc /var/chroot/proc ExecStart=/usr/bin/mount --bind /sys /var/chroot/sys ExecStart=/usr/bin/mount --bind /dev /var/chroot/dev RemainAfterExit=yes ExecStop=/usr/bin/umount /var/chroot/proc ExecStop=/usr/bin/umount /var/chroot/sys ExecStop=/usr/bin/umount /var/chroot/dev [Install] WantedBy=multi-user.targetStep 2: Script Service Stays the Same
The script service remains unchanged since its main job is to run the chroot script and it doesn't need to handle unmounting:
sudo nano /etc/systemd/system/script.serviceEnsure it's still looking like this:
[Unit] Description=Run script inside chroot environment Requires=chroot-mount.service After=chroot-mount.service [Service] Type=oneshot ExecStart=/usr/sbin/chroot /var/chroot /bin/bash -c '/root/reconfig.sh' [Install] WantedBy=multi-user.targetStep 3: Reload and Restart Services
After making the changes, reload the systemd daemon and restart the services.
sudo systemctl daemon-reload sudo systemctl restart chroot-mount.service sudo systemctl restart script.serviceStep 4: Verify the Status
Check the status to ensure the services are running as expected:
sudo systemctl status chroot-mount.service sudo systemctl status script.serviceThis ensures consistency in using
ExecStopfor unmounting directories when the service stops. The approach ensures that mount points are only active when they are needed and properly cleaned up afterward. - User
Help me to write correct unit from fstab entries:
[Unit] Description=VIRT0 Chroot mount service After=local-fs.target
[Service] Type=oneshot
#PUT DIRECTORIES HERE ExecStart=/usr/bin/mount --bind /sys /opt/virt0/sys ...
#AND HERE ExecStop=/usr/bin/umount /opt/virt0/dev ...
RemainAfterExit=yes
[Install] WantedBy=multi-user.target
THIS DIRECTORIES FROM FSTAB
/sys /opt/virt0/sys none bind 0 0 /proc /opt/virt0/proc none bind 0 0 /dev /opt/virt0/dev none bind 0 0 /dev/pts /opt/virt0/dev/pts none bind 0 0
/home/lin /opt/virt0/home/lin none bind 0 0 /root /opt/virt0/root none bind 0 0
Pulseaudio & Xorg
/tmp /opt/virt0/tmp none bind 0 0
/run /opt/virt0/run none bind 0 0
/var/lib/dbus /opt/virt0/var/lib/dbus none bind 0 0
/dev/shm /opt/virt0/dev/shm none bind 0 0 - Assistant
To create a
systemdservice unit that binds the directories as specified in your/etc/fstabfile, you will need to list all the necessary mount and unmount commands inExecStartandExecStop. This will ensure that those directories are properly mounted when the service starts and unmounted when it stops. Here's the corrected service unit file:[Unit] Description=VIRT0 Chroot mount service After=local-fs.target [Service] Type=oneshot ExecStart=/usr/bin/mount --bind /sys /opt/virt0/sys ExecStart=/usr/bin/mount --bind /proc /opt/virt0/proc ExecStart=/usr/bin/mount --bind /dev /opt/virt0/dev ExecStart=/usr/bin/mount --bind /dev/pts /opt/virt0/dev/pts ExecStart=/usr/bin/mount --bind /home/lin /opt/virt0/home/lin ExecStart=/usr/bin/mount --bind /root /opt/virt0/root ExecStart=/usr/bin/mount --bind /tmp /opt/virt0/tmp ExecStart=/usr/bin/mount --bind /run /opt/virt0/run ExecStart=/usr/bin/mount --bind /var/lib/dbus /opt/virt0/var/lib/dbus ExecStart=/usr/bin/mount --bind /dev/shm /opt/virt0/dev/shm ExecStop=/usr/bin/umount /opt/virt0/sys ExecStop=/usr/bin/umount /opt/virt0/proc ExecStop=/usr/bin/umount /opt/virt0/dev/shm ExecStop=/usr/bin/umount /opt/virt0/dev/pts ExecStop=/usr/bin/umount /opt/virt0/dev ExecStop=/usr/bin/umount /opt/virt0/home/lin ExecStop=/usr/bin/umount /opt/virt0/root ExecStop=/usr/bin/umount /opt/virt0/tmp ExecStop=/usr/bin/umount /opt/virt0/run ExecStop=/usr/bin/umount /opt/virt0/var/lib/dbus RemainAfterExit=yes [Install] WantedBy=multi-user.targetExplanation:
-
[Unit] Section:
Description: Provides a description of the service.After=local-fs.target: Ensures this service starts after local filesystems are mounted.
-
[Service] Section:
Type=oneshot: Indicates that the service performs a single action and then exits.ExecStart: Specifies the commands to mount all the necessary directories. Each mount operation is listed as a separateExecStartline.ExecStop: Specifies the commands to unmount the directories when the service stops. Each unmount operation corresponds to a previously mounted directory and is listed as a separateExecStopline.RemainAfterExit=yes: Keeps the service active even after theExecStartcommands have completed, ensuring thatExecStopwill be called when the service is stopped.
-
[Install] Section:
WantedBy=multi-user.target: Specifies that this service should be started when the multi-user target is reached.
Additional Steps:
-
Reload systemd Daemon:
After creating or modifying the service file, reload the systemd daemon to apply the changes:
sudo systemctl daemon-reload -
Enable and Start the Service:
Enable the service so that it starts automatically at boot and then start it immediately:
sudo systemctl enable chroot-mount.service sudo systemctl start chroot-mount.service -
Check the Status:
Verify that the service is running as expected by checking its status:
sudo systemctl status chroot-mount.service
This setup ensures that your chroot environment is correctly set up with all necessary mounts when the system boots and properly cleaned up when the service stops.
-