{"id":37,"date":"2025-04-26T18:48:52","date_gmt":"2025-04-26T18:48:52","guid":{"rendered":"https:\/\/8p8c.org\/?page_id=37"},"modified":"2025-04-27T13:20:20","modified_gmt":"2025-04-27T13:20:20","slug":"chroot-a-menu-system","status":"publish","type":"page","link":"https:\/\/8p8c.org\/?page_id=37","title":{"rendered":"chroot &#8211; a menu system?"},"content":{"rendered":"\n<p><\/p>\n\n\n\n<p>Below is an example of a complete Bash script that provides a text\u2010based \u201cGUI\u201d (menu) for managing chroot environments on Debian 12. This script assumes you\u2019re running on a build machine (with most of the tools already installed) and will handle basic tasks such as creating, entering, listing, and removing chroot jails using debootstrap. Error trapping and simple prompts are built in so you don\u2019t have to manually type out every command.<\/p>\n\n\n\n<p>You can save this as, say, <code>chroot_frontend.sh<\/code>, make it executable, and run it as root. (It uses debootstrap to install a minimal Debian Bookworm environment into each chroot.)<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>Yes &#8211; it&#8217;s probably lacking everywhere &#8211; but &#8211; it&#8217;s here, and comments are turned off for a reason.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#!\/bin\/bash\n# chroot_frontend.sh - A minimal Bash frontend for managing chroot environments on Debian 12 (Bookworm)\n\n# Enable error trapping and exit immediately if a command fails.\nset -e\ntrap 'error_handler ${LINENO}' ERR\n\n# Error handler: prints the line number where the error occurred.\nfunction error_handler {\n    echo \"Error occurred at line ${1}. Exiting.\"\n    exit 1\n}\n\n# Ensure the script is being run as root.\nif &#91; \"$EUID\" -ne 0 ]; then\n    echo \"Please run this script as root.\"\n    exit 1\nfi\n\n# Global variable: Base directory where all chroot environments will reside.\nCHROOT_BASE=\"\/var\/chroots\"\n\n# Create the base directory if it doesn't exist.\nif &#91; ! -d \"$CHROOT_BASE\" ]; then\n    mkdir -p \"$CHROOT_BASE\"\nfi\n\n# Check that required commands are installed.\nfunction check_prerequisites {\n    for cmd in debootstrap chroot mount umount; do\n        command -v $cmd &gt;\/dev\/null 2&gt;&amp;1 || { echo \"Required command '$cmd' not found. Please install it.\"; exit 1; }\n    done\n}\ncheck_prerequisites\n\n# --------------------------------------------------------------------\n# Function: create_chroot\n# Description: Uses debootstrap to create a minimal Debian Bookworm environment.\n# --------------------------------------------------------------------\nfunction create_chroot {\n    read -p \"Enter a name for the new chroot environment: \" CHROOT_NAME\n    CHROOT_DIR=\"${CHROOT_BASE}\/${CHROOT_NAME}\"\n    \n    if &#91; -d \"$CHROOT_DIR\" ]; then\n        echo \"Error: Chroot environment '$CHROOT_NAME' already exists.\"\n        return 1\n    fi\n\n    mkdir -p \"$CHROOT_DIR\"\n    echo \"Creating chroot environment in ${CHROOT_DIR}...\"\n    \n    # Using debootstrap to install a minimal Debian (Bookworm) system\n    debootstrap --arch=amd64 bookworm \"$CHROOT_DIR\" http:\/\/deb.debian.org\/debian || { echo \"debootstrap failed.\"; return 1; }\n    echo \"Chroot '$CHROOT_NAME' created successfully.\"\n}\n\n# --------------------------------------------------------------------\n# Function: enter_chroot\n# Description: Binds necessary system directories and drops you into a shell inside the chroot.\n# --------------------------------------------------------------------\nfunction enter_chroot {\n    read -p \"Enter the name of the chroot environment to enter: \" CHROOT_NAME\n    CHROOT_DIR=\"${CHROOT_BASE}\/${CHROOT_NAME}\"\n    \n    if &#91; ! -d \"$CHROOT_DIR\" ]; then\n        echo \"Error: Chroot environment '$CHROOT_NAME' does not exist.\"\n        return 1\n    fi\n\n    # Bind mount system directories for a functional chroot.\n    mount --bind \/proc \"$CHROOT_DIR\/proc\" || { echo \"Failed to bind \/proc\"; return 1; }\n    mount --bind \/sys \"$CHROOT_DIR\/sys\" || { echo \"Failed to bind \/sys\"; return 1; }\n    mount --bind \/dev \"$CHROOT_DIR\/dev\" || { echo \"Failed to bind \/dev\"; return 1; }\n    \n    echo \"Entering chroot environment '$CHROOT_NAME'. Type 'exit' when finished.\"\n    chroot \"$CHROOT_DIR\" \/bin\/bash # Launches a new Bash shell inside the chroot.\n\n    # Unmount the bound directories after exiting the chroot.\n    umount \"$CHROOT_DIR\/proc\" || true\n    umount \"$CHROOT_DIR\/sys\" || true\n    umount \"$CHROOT_DIR\/dev\" || true\n}\n\n# --------------------------------------------------------------------\n# Function: remove_chroot\n# Description: Deletes a specified chroot environment.\n# --------------------------------------------------------------------\nfunction remove_chroot {\n    read -p \"Enter the name of the chroot environment to remove: \" CHROOT_NAME\n    CHROOT_DIR=\"${CHROOT_BASE}\/${CHROOT_NAME}\"\n    \n    if &#91; ! -d \"$CHROOT_DIR\" ]; then\n        echo \"Error: Chroot environment '$CHROOT_NAME' does not exist.\"\n        return 1\n    fi\n\n    read -p \"Are you sure you want to remove '$CHROOT_NAME'? This will delete its contents. (y\/N): \" confirm\n    if &#91;&#91; \"$confirm\" =~ ^&#91;Yy]$ ]]; then\n        rm -rf \"$CHROOT_DIR\"\n        echo \"Chroot environment '$CHROOT_NAME' has been removed.\"\n    else\n        echo \"Removal cancelled.\"\n    fi\n}\n\n# --------------------------------------------------------------------\n# Function: list_chroots\n# Description: Lists all chroot environments under CHROOT_BASE.\n# --------------------------------------------------------------------\nfunction list_chroots {\n    echo \"Available chroot environments in ${CHROOT_BASE}:\"\n    if &#91; -d \"$CHROOT_BASE\" ]; then\n        ls -1 \"$CHROOT_BASE\"\n    else\n        echo \"No chroot environments found.\"\n    fi\n}\n\n# --------------------------------------------------------------------\n# Main Menu\n# Description: Provides a simple menu and calls the appropriate functions.\n# --------------------------------------------------------------------\nfunction main_menu {\n    while true; do\n        clear\n        echo \"==================================\"\n        echo \"Chroot Management Frontend (Debian 12)\"\n        echo \"==================================\"\n        echo \"1) Create a new chroot environment\"\n        echo \"2) Enter an existing chroot environment\"\n        echo \"3) Remove a chroot environment\"\n        echo \"4) List available chroot environments\"\n        echo \"5) Exit\"\n        echo \"==================================\"\n        read -p \"Choose an option &#91;1-5]: \" option\n        case $option in\n            1)\n                create_chroot\n                read -p \"Press Enter to continue...\"\n                ;;\n            2)\n                enter_chroot\n                read -p \"Press Enter to continue...\"\n                ;;\n            3)\n                remove_chroot\n                read -p \"Press Enter to continue...\"\n                ;;\n            4)\n                list_chroots\n                read -p \"Press Enter to continue...\"\n                ;;\n            5)\n                echo \"Exiting.\"\n                exit 0\n                ;;\n            *)\n                echo \"Invalid option. Try again.\"\n                read -p \"Press Enter to continue...\"\n                ;;\n        esac\n    done\n}\n\n# Start the program\nmain_menu\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Below is an example of a complete Bash script that provides a text\u2010based \u201cGUI\u201d (menu) for managing chroot environments on Debian 12. This script assumes you\u2019re running on a build machine (with most of the tools already installed) and will handle basic tasks such as creating, entering, listing, and removing chroot jails using debootstrap. Error [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-37","page","type-page","status-publish","hentry"],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/8p8c.org\/index.php?rest_route=\/wp\/v2\/pages\/37","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/8p8c.org\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/8p8c.org\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/8p8c.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/8p8c.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=37"}],"version-history":[{"count":2,"href":"https:\/\/8p8c.org\/index.php?rest_route=\/wp\/v2\/pages\/37\/revisions"}],"predecessor-version":[{"id":57,"href":"https:\/\/8p8c.org\/index.php?rest_route=\/wp\/v2\/pages\/37\/revisions\/57"}],"wp:attachment":[{"href":"https:\/\/8p8c.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=37"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}