Skip to content

Commit

Permalink
Add chroot buffer size as an optional argument to docker_to_chroot sc…
Browse files Browse the repository at this point in the history
…ripts
  • Loading branch information
kspaceKelvin committed Nov 22, 2024
1 parent 6b78e7c commit 280bfa5
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 14 deletions.
13 changes: 7 additions & 6 deletions docker/docker_tar_to_chroot.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,25 @@
# This script takes a Docker container export (.tar) creates a chroot image (.img)
# Note that root privileges are required to mount the loopback images

# Syntax: ./docker_tar_to_chroot.sh docker-export.tar chroot.img
# Syntax: ./docker_tar_to_chroot.sh docker-export.tar chroot.img optional_buffer_size_in_mb

EXPORT_FILE=${1}
CHROOT_FILE=${2}
BUFFER_MB=${3:-50}

# Files have a minimum storage size of 4k due to block size
exportSize=$(tar -tvf "${EXPORT_FILE}" | awk '{s+=int($3/4096+0.99999)*4096} END{printf "%.0f\n", s}')

# Add a minimum buffer of free space to account for filesystem overhead
chrootMinSize=$(( exportSize/(1024*1024) * 115/100 + 50))
chrootMinSize=$(( exportSize/(1024*1024) * 115/100 + ${BUFFER_MB}))

# Round up to the nearest 100 MB
chrootSize=$(( ((${chrootMinSize%.*})/100+1)*100 ))

echo ------------------------------------------------------------
echo Total size of files from Docker image is $(( exportSize/(1024*1024) )) MB
echo Creating chroot file ${CHROOT_FILE} of size $chrootSize MB
echo ------------------------------------------------------------
echo ----------------------------------------------------------------------
echo Total size of files from Docker image is $(( exportSize/(1024*1024) )) MB with ${BUFFER_MB} MB of buffer
echo Creating chroot file ${CHROOT_FILE} of size ${chrootSize} MB
echo ----------------------------------------------------------------------

if test -f "${CHROOT_FILE}"; then
echo "Warning -- ${CHROOT_FILE} exists and will be overwritten!"
Expand Down
15 changes: 11 additions & 4 deletions docker/docker_to_chroot.bat
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
@echo off
setlocal enabledelayedexpansion

rem This script takes a Docker image and creates a chroot image (.img)
rem Note that this script also requires docker_tar_to_chroot.sh to be located in the same folder

rem Syntax: docker_to_chroot.bat kspacekelvin/fire-python fire-python-chroot.img
rem Syntax: docker_to_chroot.bat kspacekelvin/fire-python fire-python-chroot.img optional_buffer_size_in_mb

if "%1"=="" GOTO wrongargnum
if "%2"=="" GOTO wrongargnum
if not "%3"=="" GOTO wrongargnum

set DOCKER_NAME=%1
set CHROOT_FILE=%2
set EXPORT_FILE=docker-export.tar

if "%3"=="" (
set BUFFER_SIZE=50
) else (
set BUFFER_SIZE=%3
)

if exist %EXPORT_FILE% (
echo Warning -- %EXPORT_FILE% exists and will be overwritten!
Expand All @@ -31,12 +38,12 @@ docker run -it --rm ^
--privileged=true ^
-v "%cd%":/share ^
ubuntu ^
/bin/bash -c "sed -i -e 's/\r//g' /share/docker_tar_to_chroot.sh && /share/docker_tar_to_chroot.sh /share/%EXPORT_FILE% /share/%CHROOT_FILE%"
/bin/bash -c "sed -i -e 's/\r//g' /share/docker_tar_to_chroot.sh && /share/docker_tar_to_chroot.sh /share/%EXPORT_FILE% /share/%CHROOT_FILE% !BUFFER_SIZE!"

del %EXPORT_FILE%
goto eof

:wrongargnum
echo Syntax: docker_to_chroot.bat docker_image_name chroot_file_name
echo Syntax: docker_to_chroot.bat docker_image_name chroot_file_name optional_buffer_size_in_mb

:eof
9 changes: 5 additions & 4 deletions docker/docker_to_chroot.sh
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
#!/bin/bash
# This script takes a Docker image and creates a chroot image (.img)

# Syntax: ./docker_to_chroot.sh docker_image_name chroot_file_name
# Syntax: ./docker_to_chroot.sh docker_image_name chroot_file_name optional_buffer_size_in_mb

if [[ $# -ne 2 ]]; then
if [[ $# -gt 3 ]]; then
echo "Wrong number of arguments" >&2
echo "Syntax: ./docker_to_chroot.sh docker_image_name chroot_file_name" >&2
echo "Syntax: ./docker_to_chroot.sh docker_image_name chroot_file_name optional_buffer_size_in_mb" >&2
exit 2
fi

DOCKER_NAME=${1}
CHROOT_FILE=${2}
EXPORT_FILE=docker-export.tar
BUFFER_MB=${3:-50}

# Create a Docker container and export to a .tar file
echo ------------------------------------------------------------
Expand All @@ -32,6 +33,6 @@ docker run -it --rm \
--privileged=true \
-v $(pwd):/share \
ubuntu \
/bin/bash -c "sed -i -e 's/\r//g' /share/docker_tar_to_chroot.sh && /share/docker_tar_to_chroot.sh /share/${EXPORT_FILE} /share/${CHROOT_FILE}"
/bin/bash -c "sed -i -e 's/\r//g' /share/docker_tar_to_chroot.sh && /share/docker_tar_to_chroot.sh /share/${EXPORT_FILE} /share/${CHROOT_FILE} ${BUFFER_MB}"

rm ${EXPORT_FILE}

0 comments on commit 280bfa5

Please sign in to comment.