@@ -30,7 +30,7 @@ log_error() {
3030}
3131
3232# Helper function to run commands on host via nsenter
33- hostrun () {
33+ host_run () {
3434 nsenter -t 1 -m -p -n " $@ "
3535}
3636
@@ -39,18 +39,18 @@ check_existing() {
3939 log_info " Checking existing installation..."
4040
4141 # Check if systemd services exist (in either /etc or /run)
42- if hostrun systemctl list-unit-files | grep -q " sysbox-mgr.service" ||
42+ if host_run systemctl list-unit-files | grep -q " sysbox-mgr.service" ||
4343 [ -f /host/run/systemd/system/sysbox-mgr.service ] ||
4444 [ -f /host/etc/systemd/system/sysbox-mgr.service ]; then
4545 log_warning " Sysbox services already installed - skipping installation"
4646
4747 # Show service status
4848 echo " Service status:"
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
49+ host_run systemctl status sysbox-mgr.service --no-pager 2> /dev/null | head -5 || true
50+ host_run systemctl status sysbox-fs.service --no-pager 2> /dev/null | head -5 || true
5151
5252 # Check if actually running
53- if hostrun systemctl is-active sysbox-mgr.service > /dev/null 2>&1 ; then
53+ if host_run systemctl is-active sysbox-mgr.service > /dev/null 2>&1 ; then
5454 log_success " Sysbox is installed and running"
5555 else
5656 log_info " Sysbox is installed but not running. Start with:"
@@ -76,14 +76,14 @@ copy_binaries() {
7676 chmod +x /host/usr/bin/rsync /host/usr/bin/sysbox-*
7777
7878 # Create symlinks for dependencies
79- hostrun ln -sf /usr/sbin/modprobe /usr/bin/modprobe 2> /dev/null || true
80- hostrun ln -sf /usr/sbin/iptables /usr/bin/iptables 2> /dev/null || true
79+ host_run ln -sf /usr/sbin/modprobe /usr/bin/modprobe 2> /dev/null || true
80+ host_run ln -sf /usr/sbin/iptables /usr/bin/iptables 2> /dev/null || true
8181
8282 # Handle fusermount/fusermount3 (Alpine has fusermount3, sysbox expects fusermount)
83- if ! hostrun which fusermount > /dev/null 2>&1 ; then
84- if hostrun which fusermount3 > /dev/null 2>&1 ; then
83+ if ! host_run which fusermount > /dev/null 2>&1 ; then
84+ if host_run which fusermount3 > /dev/null 2>&1 ; then
8585 log_info " Creating symlink: fusermount -> fusermount3"
86- hostrun ln -sf /usr/bin/fusermount3 /usr/bin/fusermount
86+ host_run ln -sf /usr/bin/fusermount3 /usr/bin/fusermount
8787 else
8888 log_warning " Neither fusermount nor fusermount3 found - FUSE operations may fail"
8989 fi
@@ -94,54 +94,54 @@ copy_binaries() {
9494
9595setup_subuid_subgid () {
9696 log_info " Setting up subuid/subgid..."
97- hostrun sh -c ' echo "sysbox:200000:65536" > /etc/subuid'
98- hostrun sh -c ' echo "sysbox:200000:65536" > /etc/subgid'
97+ host_run sh -c ' echo "sysbox:200000:65536" > /etc/subuid'
98+ host_run sh -c ' echo "sysbox:200000:65536" > /etc/subgid'
9999 log_success " Created subuid/subgid mappings"
100100}
101101
102102# Setup /etc overlay and configuration
103103setup_etc_overlay () {
104104 # Check if main overlay already exists
105- if hostrun mount | grep -q " /etc .*overlay" ; then
105+ if host_run mount | grep -q " /etc .*overlay" ; then
106106 log_warning " /etc already has overlay mounted - skipping mount"
107107 return
108108 fi
109109
110110 log_info " Setting up /etc overlay..."
111111
112112 # Create volatile overlay directories for /etc
113- hostrun mkdir -p /var/volatile/overlay/etc/sysbox/upper /var/volatile/overlay/etc/sysbox/work
113+ host_run mkdir -p /var/volatile/overlay/etc/sysbox/upper /var/volatile/overlay/etc/sysbox/work
114114
115115 # Preserve wireguard config if it exists in volatile storage
116- if [ -f /host /var/volatile/overlay/etc/wireguard/upper/wg0.conf ]; then
116+ if host_run [ -f /var/volatile/overlay/etc/wireguard/upper/wg0.conf ]; then
117117 log_info " Preserving existing wireguard configuration..."
118- mkdir -p /host /var/volatile/overlay/etc/sysbox/upper/wireguard
119- cp /host/ var/volatile/overlay/etc/wireguard/upper/* /host/ var/volatile/overlay/etc/sysbox/upper/wireguard/ 2> /dev/null || true
118+ host_run mkdir -p /var/volatile/overlay/etc/sysbox/upper/wireguard
119+ host_run bash -c ' cp /var/volatile/overlay/etc/wireguard/upper/* /var/volatile/overlay/etc/sysbox/upper/wireguard/ 2>/dev/null' || true
120120 fi
121121
122122 # Preserve docker config if it exists in volatile storage
123- if [ -d /host/ var/volatile/overlay/etc/docker/upper ]; then
123+ if host_run [ -d /var/volatile/overlay/etc/docker/upper/daemon.json ]; then
124124 log_info " Preserving existing Docker configuration..."
125- mkdir -p /host /var/volatile/overlay/etc/sysbox/upper/docker
126- cp -r /host/ var/volatile/overlay/etc/docker/upper/* /host/ var/volatile/overlay/etc/sysbox/upper/docker/ 2> /dev/null || true
125+ host_run mkdir -p /var/volatile/overlay/etc/sysbox/upper/docker
126+ host_run bash -c ' cp -r /var/volatile/overlay/etc/docker/upper/* /var/volatile/overlay/etc/sysbox/upper/docker/ 2>/dev/null' || true
127127 fi
128128
129129 # Unmount existing individual overlays (except /etc/users which should remain persistent)
130130 log_info " Unmounting individual overlays..."
131- hostrun umount /etc/wireguard 2> /dev/null || true
132- hostrun umount /etc/docker 2> /dev/null || true
131+ host_run umount /etc/wireguard 2> /dev/null || true
132+ host_run umount /etc/docker 2> /dev/null || true
133133
134134 # Mount volatile /etc overlay
135- hostrun mount -t overlay overlay \
135+ host_run mount -t overlay overlay \
136136 -o lowerdir=/etc,upperdir=/var/volatile/overlay/etc/sysbox/upper,workdir=/var/volatile/overlay/etc/sysbox/work \
137137 /etc
138138 log_success " Volatile /etc overlay mounted"
139139
140140 # Remount /etc/users as persistent (if it exists) to override the volatile /etc mount
141- if [ -d /host /dstack/persistent/overlay/etc/users ]; then
141+ if host_run [ -d /dstack/persistent/overlay/etc/users ]; then
142142 log_info " Remounting /etc/users as persistent overlay..."
143- hostrun mkdir -p /dstack/persistent/overlay/etc/users/upper /dstack/persistent/overlay/etc/users/work
144- hostrun mount -t overlay overlay \
143+ host_run mkdir -p /dstack/persistent/overlay/etc/users/upper /dstack/persistent/overlay/etc/users/work
144+ host_run mount -t overlay overlay \
145145 -o lowerdir=/etc/users,upperdir=/dstack/persistent/overlay/etc/users/upper,workdir=/dstack/persistent/overlay/etc/users/work \
146146 /etc/users
147147 log_success " /etc/users mounted as persistent overlay"
@@ -150,41 +150,36 @@ setup_etc_overlay() {
150150
151151# Configure Docker runtime
152152configure_docker () {
153- log_info " Configuring Docker runtime..."
154-
155- # TODO: Implement proper JSON merging to preserve existing Docker configuration
156- # Currently overwrites daemon.json - should merge with existing runtimes/settings
153+ log_info " Configuring Docker runtime using Sysbox's docker-cfg script..."
157154
158155 # Backup existing daemon.json if it exists
159- if hostrun [ -f /etc/docker/daemon.json ]; then
160- hostrun cp /etc/docker/daemon.json /etc/docker/daemon.json.backup
161- log_info " Backed up existing Docker daemon.json (will be overwritten) "
156+ if host_run [ -f /etc/docker/daemon.json ]; then
157+ host_run cp /etc/docker/daemon.json /etc/docker/daemon.json.backup
158+ log_info " Backed up existing Docker daemon.json"
162159 fi
163160
164- hostrun tee /etc/docker/daemon.json > /dev/null << 'DOCKEREOF '
165- {
166- "log-driver": "json-file",
167- "log-opts": {
168- "max-size": "100m",
169- "max-file": "10"
170- },
171- "runtimes": {
172- "sysbox-runc": {
173- "path": "/usr/bin/sysbox-runc"
174- }
175- }
176- }
177- DOCKEREOF
161+ # Use Sysbox's official docker-cfg script to configure Docker
162+ cp /usr/local/bin/sysbox-docker-cfg /usr/bin/
163+ if host_run bash /usr/bin/sysbox-docker-cfg --sysbox-runtime=enable; then
164+ log_success " Docker configuration updated with Sysbox runtime"
165+ else
166+ log_error " Failed to configure Docker with docker-cfg script"
167+ if host_run [ -f /etc/docker/daemon.json.backup ]; then
168+ host_run mv /etc/docker/daemon.json.backup /etc/docker/daemon.json
169+ log_info " Restored backup Docker configuration"
170+ fi
171+ return 1
172+ fi
178173
179- log_success " Docker configuration updated "
174+ log_success " Docker configuration completed "
180175}
181176
182177# Create systemd services
183178create_systemd_services () {
184179 log_info " Creating systemd services..."
185180
186181 # Use /run/systemd/system for runtime units (doesn't require persistent storage)
187- hostrun mkdir -p /run/systemd/system
182+ host_run mkdir -p /run/systemd/system
188183
189184 # Copy service files from container to host runtime directory
190185 cp /usr/local/share/sysbox-mgr.service /host/run/systemd/system/
@@ -204,7 +199,7 @@ create_systemd_services() {
204199 log_success " Service files copied to /run/systemd/system/"
205200
206201 # Reload systemd to pick up new service files
207- hostrun systemctl daemon-reload
202+ host_run systemctl daemon-reload
208203
209204 log_success " Systemd services created (transient until reboot)"
210205 log_info " Services: sysbox-mgr, sysbox-fs"
@@ -215,20 +210,20 @@ start_sysbox() {
215210 log_info " Starting Sysbox services..."
216211
217212 # Create data directory
218- hostrun mkdir -p /dstack/persistent/sysbox-data
213+ host_run mkdir -p /dstack/persistent/sysbox-data
219214
220215 # Start services in order
221216 log_info " Starting Sysbox manager..."
222- hostrun systemctl start sysbox-mgr.service
217+ host_run systemctl start sysbox-mgr.service
223218 sleep 3
224219
225220 log_info " Starting Sysbox filesystem..."
226- hostrun systemctl start sysbox-fs.service
221+ host_run systemctl start sysbox-fs.service
227222 sleep 2
228223
229224 # Verify services are running
230- if hostrun systemctl is-active sysbox-mgr.service > /dev/null &&
231- hostrun systemctl is-active sysbox-fs.service > /dev/null; then
225+ if host_run systemctl is-active sysbox-mgr.service > /dev/null &&
226+ host_run systemctl is-active sysbox-fs.service > /dev/null; then
232227 log_success " Sysbox services started successfully"
233228 else
234229 log_warning " Some services may not have started correctly"
@@ -245,8 +240,8 @@ show_status() {
245240 echo " =========================================="
246241 echo
247242 echo " 📊 Status:"
248- echo " • Sysbox Manager: $( hostrun systemctl is-active sysbox-mgr.service) "
249- echo " • Sysbox FS: $( hostrun systemctl is-active sysbox-fs.service) "
243+ echo " • Sysbox Manager: $( host_run systemctl is-active sysbox-mgr.service) "
244+ echo " • Sysbox FS: $( host_run systemctl is-active sysbox-fs.service) "
250245 echo " • Docker Runtime: Configured (restart required)"
251246 echo
252247 echo -e " ${YELLOW} ⚠️ IMPORTANT: Restart Docker to enable sysbox-runc runtime:${NC} "
0 commit comments