This script creates an isolated environment using Linux namespaces and cgroups, executes a provided Bash script, and then cleans up the environment. It ensures that any changes made within the isolated environment do not affect the host system.
- Argument Check: The script checks if a Bash script file is provided as an argument.
- File Existence Check: It verifies if the provided file exists.
- Cgroup Creation: A cgroup is created to limit the number of processes to 50.
- Namespace Creation: The script creates new PID and UTS namespaces using
unshare
. - Hostname Change: The hostname is changed to
isolated_env
. - Temporary File System Isolation: A new
tmpfs
is mounted to isolate the temporary file system. - Script Execution: The provided script is executed within the isolated environment.
- Cleanup: The temporary file system is unmounted and the cgroup is removed after execution.
-
Save the
isolate.sh
script and make it executable:chmod +x isolate.sh
-
Create a sample Bash script, e.g.,
test_script.sh
:#!/bin/bash echo "This is a script running in an isolated environment" touch /tmp/testfile ls /tmp
-
Execute the
isolate.sh
script, passing the sample script as an argument:./isolate.sh test_script.sh
Given a script test_script.sh
:
#!/bin/bash
echo "This is a script running in an isolated environment"
touch /tmp/testfile
ls /tmp
Running the following command:
./isolate.sh test_script.sh
Will output:
This is a script running in an isolated environment
testfile
Isolated environment finished and cleaned up, including cgroup.
The changes made within the isolated environment (like creating /tmp/testfile
) will not affect the host system's /tmp
directory.
The script uses unshare to create new namespaces and mount to create an isolated file system. Cgroups are used to limit the number of processes to 50. You can adjust this limit as needed. The isolated environment is cleaned up after the script execution, ensuring no residual changes affect the host system.