diff --git a/docker/docker_tar_to_chroot.sh b/docker/docker_tar_to_chroot.sh index 6ba4080..705bc81 100755 --- a/docker/docker_tar_to_chroot.sh +++ b/docker/docker_tar_to_chroot.sh @@ -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!" diff --git a/docker/docker_to_chroot.bat b/docker/docker_to_chroot.bat index 2af99a9..6d1cde0 100755 --- a/docker/docker_to_chroot.bat +++ b/docker/docker_to_chroot.bat @@ -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! @@ -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 diff --git a/docker/docker_to_chroot.sh b/docker/docker_to_chroot.sh index 49391fb..d17ae73 100755 --- a/docker/docker_to_chroot.sh +++ b/docker/docker_to_chroot.sh @@ -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 ------------------------------------------------------------ @@ -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}