@@ -29,23 +29,28 @@ log_error() {
2929 echo -e " ${RED} ❌ $1 ${NC} "
3030}
3131
32+ # Helper function to run commands on host via nsenter
33+ hostrun () {
34+ nsenter -t 1 -m -p -n " $@ "
35+ }
36+
3237# Check if already installed
3338check_existing () {
3439 log_info " Checking existing installation..."
3540
3641 # Check if systemd services exist (in either /etc or /run)
37- if chroot /host systemctl list-unit-files | grep -q " sysbox-mgr.service" ||
42+ if hostrun systemctl list-unit-files | grep -q " sysbox-mgr.service" ||
3843 [ -f /host/run/systemd/system/sysbox-mgr.service ] ||
3944 [ -f /host/etc/systemd/system/sysbox-mgr.service ]; then
4045 log_warning " Sysbox services already installed - skipping installation"
4146
4247 # Show service status
4348 echo " Service status:"
44- chroot /host systemctl status sysbox-mgr.service --no-pager 2> /dev/null | head -5 || true
45- chroot /host systemctl status sysbox-fs.service --no-pager 2> /dev/null | head -5 || true
49+ hostrun systemctl status sysbox-mgr.service --no-pager 2> /dev/null | head -5 || true
50+ hostrun systemctl status sysbox-fs.service --no-pager 2> /dev/null | head -5 || true
4651
4752 # Check if actually running
48- if chroot /host systemctl is-active sysbox-mgr.service > /dev/null 2>&1 ; then
53+ if hostrun systemctl is-active sysbox-mgr.service > /dev/null 2>&1 ; then
4954 log_success " Sysbox is installed and running"
5055 else
5156 log_info " Sysbox is installed but not running. Start with:"
@@ -71,15 +76,15 @@ copy_binaries() {
7176 chmod +x /host/tmp/rsync-static /host/tmp/sysbox-*
7277
7378 # Create symlinks for dependencies
74- chroot /host ln -sf /tmp/rsync-static /usr/bin/rsync 2> /dev/null || true
75- chroot /host ln -sf /usr/sbin/modprobe /usr/bin/modprobe 2> /dev/null || true
76- chroot /host ln -sf /usr/sbin/iptables /usr/bin/iptables 2> /dev/null || true
79+ hostrun ln -sf /tmp/rsync-static /usr/bin/rsync 2> /dev/null || true
80+ hostrun ln -sf /usr/sbin/modprobe /usr/bin/modprobe 2> /dev/null || true
81+ hostrun ln -sf /usr/sbin/iptables /usr/bin/iptables 2> /dev/null || true
7782
7883 # Handle fusermount/fusermount3 (Alpine has fusermount3, sysbox expects fusermount)
79- if ! chroot /host which fusermount > /dev/null 2>&1 ; then
80- if chroot /host which fusermount3 > /dev/null 2>&1 ; then
84+ if ! hostrun which fusermount > /dev/null 2>&1 ; then
85+ if hostrun which fusermount3 > /dev/null 2>&1 ; then
8186 log_info " Creating symlink: fusermount -> fusermount3"
82- chroot /host ln -sf /usr/bin/fusermount3 /usr/bin/fusermount
87+ hostrun ln -sf /usr/bin/fusermount3 /usr/bin/fusermount
8388 else
8489 log_warning " Neither fusermount nor fusermount3 found - FUSE operations may fail"
8590 fi
@@ -88,17 +93,28 @@ copy_binaries() {
8893 log_success " Binaries copied and dependencies linked"
8994}
9095
91- # Setup /etc configuration (subuid/subgid)
92- setup_etc_config () {
93- log_info " Setting up /etc configuration..."
96+ # Setup /etc overlay and configuration
97+ setup_etc_overlay () {
98+ log_info " Setting up /etc overlay..."
99+
100+ # Create persistent overlay directories
101+ hostrun mkdir -p /dstack/persistent/sysbox-etc-overlay/upper /dstack/persistent/sysbox-etc-overlay/work
94102
95- # Create subuid/subgid files
96- echo " sysbox:200000:65536" > /host/tmp/subuid.tmp
97- echo " sysbox:200000:65536" > /host/tmp/subgid.tmp
103+ # Check if main overlay already exists
104+ if hostrun mount | grep -q " /etc.*overlay.*sysbox-etc-overlay" ; then
105+ log_warning " /etc already has sysbox overlay mounted"
106+ else
107+ # Mount main /etc overlay
108+ hostrun mount -t overlay overlay \
109+ -o lowerdir=/etc,upperdir=/dstack/persistent/sysbox-etc-overlay/upper,workdir=/dstack/persistent/sysbox-etc-overlay/work \
110+ /etc
111+ log_success " Main /etc overlay mounted"
112+ fi
98113
99- # Note: The actual /etc overlay will be handled by systemd service
100- log_success " Created subuid/subgid configuration files"
101- log_info " These will be applied when the overlay service starts"
114+ # Create subuid/subgid
115+ hostrun sh -c ' echo "sysbox:200000:65536" > /etc/subuid'
116+ hostrun sh -c ' echo "sysbox:200000:65536" > /etc/subgid'
117+ log_success " Created subuid/subgid mappings"
102118}
103119
104120# Configure Docker runtime
@@ -109,12 +125,12 @@ configure_docker() {
109125 # Currently overwrites daemon.json - should merge with existing runtimes/settings
110126
111127 # Backup existing daemon.json if it exists
112- if chroot /host [ -f /etc/docker/daemon.json ]; then
113- chroot /host cp /etc/docker/daemon.json /etc/docker/daemon.json.backup
128+ if hostrun [ -f /etc/docker/daemon.json ]; then
129+ hostrun cp /etc/docker/daemon.json /etc/docker/daemon.json.backup
114130 log_info " Backed up existing Docker daemon.json (will be overwritten)"
115131 fi
116132
117- chroot /host tee /etc/docker/daemon.json > /dev/null << 'DOCKEREOF '
133+ hostrun tee /etc/docker/daemon.json > /dev/null << 'DOCKEREOF '
118134{
119135 "log-driver": "json-file",
120136 "log-opts": {
@@ -137,31 +153,13 @@ create_systemd_services() {
137153 log_info " Creating systemd services..."
138154
139155 # Use /run/systemd/system for runtime units (doesn't require persistent storage)
140- chroot /host mkdir -p /run/systemd/system
156+ hostrun mkdir -p /run/systemd/system
141157
142- # Copy all service files from container to host runtime directory
143- cp /usr/local/share/sysbox-etc-overlay.service /host/run/systemd/system/
158+ # Copy service files from container to host runtime directory
144159 cp /usr/local/share/sysbox-mgr.service /host/run/systemd/system/
145160 cp /usr/local/share/sysbox-fs.service /host/run/systemd/system/
146161
147- # Create a setup script for subuid/subgid
148- cat > /host/tmp/sysbox-setup.sh << 'EOF '
149- #!/bin/sh
150- # Apply subuid/subgid configuration after overlay mount
151- if [ -f /tmp/subuid.tmp ]; then
152- cat /tmp/subuid.tmp > /etc/subuid
153- cat /tmp/subgid.tmp > /etc/subgid
154- rm -f /tmp/subuid.tmp /tmp/subgid.tmp
155- fi
156- EOF
157- chmod +x /host/tmp/sysbox-setup.sh
158-
159162 # Verify files were copied
160- if [ ! -f /host/run/systemd/system/sysbox-etc-overlay.service ]; then
161- log_error " Failed to copy sysbox-etc-overlay.service to /run/systemd/system/"
162- return 1
163- fi
164-
165163 if [ ! -f /host/run/systemd/system/sysbox-mgr.service ]; then
166164 log_error " Failed to copy sysbox-mgr.service to /run/systemd/system/"
167165 return 1
@@ -175,47 +173,36 @@ EOF
175173 log_success " Service files copied to /run/systemd/system/"
176174
177175 # Reload systemd to pick up new service files
178- chroot /host systemctl daemon-reload
176+ hostrun systemctl daemon-reload
179177
180- # Note: We don't enable services as that requires writing to /etc/systemd/system/*.wants/
181- # Services in /run are transient and will be lost on reboot anyway
182178 log_success " Systemd services created (transient until reboot)"
183- log_info " Services: sysbox-etc-overlay, sysbox-mgr, sysbox-fs"
184- log_info " Services will be started without enabling (read-only /etc)"
179+ log_info " Services: sysbox-mgr, sysbox-fs"
185180}
186181
187182# Start Sysbox services
188183start_sysbox () {
189184 log_info " Starting Sysbox services..."
190185
191186 # Create data directory
192- chroot /host mkdir -p /dstack/persistent/sysbox-data
193-
194- # Start services in order: overlay first, then sysbox-mgr, then sysbox-fs
195- log_info " Starting /etc overlay service..."
196- chroot /host systemctl start sysbox-etc-overlay.service
197- sleep 2
198-
199- # Apply subuid/subgid configuration
200- chroot /host /tmp/sysbox-setup.sh
187+ hostrun mkdir -p /dstack/persistent/sysbox-data
201188
189+ # Start services in order
202190 log_info " Starting Sysbox manager..."
203- chroot /host systemctl start sysbox-mgr.service
191+ hostrun systemctl start sysbox-mgr.service
204192 sleep 3
205193
206194 log_info " Starting Sysbox filesystem..."
207- chroot /host systemctl start sysbox-fs.service
195+ hostrun systemctl start sysbox-fs.service
208196 sleep 2
209197
210198 # Verify services are running
211- if chroot /host systemctl is-active sysbox-etc-overlay.service > /dev/null &&
212- chroot /host systemctl is-active sysbox-mgr.service > /dev/null &&
213- chroot /host systemctl is-active sysbox-fs.service > /dev/null; then
214- log_success " All Sysbox services started successfully"
199+ if hostrun systemctl is-active sysbox-mgr.service > /dev/null &&
200+ hostrun systemctl is-active sysbox-fs.service > /dev/null; then
201+ log_success " Sysbox services started successfully"
215202 else
216203 log_warning " Some services may not have started correctly"
217- log_info " Check status with: systemctl status sysbox-etc-overlay sysbox- mgr sysbox-fs"
218- log_info " Check logs with: journalctl -u sysbox-etc-overlay -u sysbox- mgr -u sysbox-fs"
204+ log_info " Check status with: systemctl status sysbox-mgr sysbox-fs"
205+ log_info " Check logs with: journalctl -u sysbox-mgr -u sysbox-fs"
219206 fi
220207}
221208
@@ -227,8 +214,8 @@ show_status() {
227214 echo " =========================================="
228215 echo
229216 echo " 📊 Status:"
230- echo " • Sysbox Manager: $( chroot /host systemctl is-active sysbox-mgr.service) "
231- echo " • Sysbox FS: $( chroot /host systemctl is-active sysbox-fs.service) "
217+ echo " • Sysbox Manager: $( hostrun systemctl is-active sysbox-mgr.service) "
218+ echo " • Sysbox FS: $( hostrun systemctl is-active sysbox-fs.service) "
232219 echo " • Docker Runtime: Configured (restart required)"
233220 echo
234221 echo -e " ${YELLOW} ⚠️ IMPORTANT: Restart Docker to enable sysbox-runc runtime:${NC} "
@@ -253,7 +240,7 @@ show_status() {
253240main () {
254241 check_existing
255242 copy_binaries
256- setup_etc_config
243+ setup_etc_overlay
257244 configure_docker
258245 create_systemd_services
259246 start_sysbox
0 commit comments