Skip to content
TheChatArchiveThe Chat Archive
gpt-4o-2024-05-13· OpenAIAi2 WildChat-4.8M (AI2)

Make systemd service to run script by root user in chroot environment. Take attention to…

  1. 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

  2. Assistant

    Creating a systemd service 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:

    1. 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.

    2. Create the systemd Service File:

      Create a new service file in /etc/systemd/system/. For example, you can call it chroot-reconfig.service.

      sudo nano /etc/systemd/system/chroot-reconfig.service
      

      Add 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.target
      

      Explanation of the service file:

      • [Unit]:
        • Description: A short description of the service.
        • After and Wants: 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.
    3. Reload systemd Daemon:

      After creating the service file, reload the systemd manager configuration to apply the new service.

      sudo systemctl daemon-reload
      
    4. 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
      
    5. 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 systemd service 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.

  3. User

    Half this service to two different: chroot-mount and script. Script requires chroot-mount

  4. Assistant

    To split the functionality into two different systemd services, 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.service

    Create a new service file for mounting the necessary directories under /var/chroot.

    sudo nano /etc/systemd/system/chroot-mount.service
    

    Add 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.target
    

    Step 2: Create script.service

    Create 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.service
    

    Add 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.target
    

    Step 3: Reload systemd Daemon

    After creating both service files, reload the systemd manager configuration to apply the new services.

    sudo systemctl daemon-reload
    

    Step 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.service
    

    Step 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.service
    

    With these steps, you have successfully split the original systemd service 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.
  5. User

    Why in first case you used ExecStopPost for umount, but in second case there is ExecStop?

  6. Assistant

    I apologize for any confusion caused by the inconsistency. Typically, ExecStop is used to specify commands that run when a service is being stopped, while ExecStopPost runs after ExecStop. In most cases, for a simple service, you can use ExecStop directly 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 ExecStop is appropriate for unmounting as we want to ensure these actions are taken when the service stops.

    Step 1: Update chroot-mount.service

    Correct the service file to use ExecStop for unmounting the directories.

    sudo nano /etc/systemd/system/chroot-mount.service
    

    Update 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.target
    

    Step 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.service
    

    Ensure 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.target
    

    Step 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.service
    

    Step 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.service
    

    This ensures consistency in using ExecStop for unmounting directories when the service stops. The approach ensures that mount points are only active when they are needed and properly cleaned up afterward.

  7. 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

  8. Assistant

    To create a systemd service unit that binds the directories as specified in your /etc/fstab file, you will need to list all the necessary mount and unmount commands in ExecStart and ExecStop. 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.target
    

    Explanation:

    • [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 separate ExecStart line.
      • 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 separate ExecStop line.
      • RemainAfterExit=yes: Keeps the service active even after the ExecStart commands have completed, ensuring that ExecStop will 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:

    1. Reload systemd Daemon:

      After creating or modifying the service file, reload the systemd daemon to apply the changes:

      sudo systemctl daemon-reload
      
    2. 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
      
    3. 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.