committed by
GitHub
8 changed files with 331 additions and 82 deletions
@ -0,0 +1,47 @@ |
|||
# |
|||
# Copyright 2020 Northern.tech AS |
|||
# |
|||
# Licensed under the Apache License, Version 2.0 (the "License"); |
|||
# you may not use this file except in compliance with the License. |
|||
# You may obtain a copy of the License at |
|||
# |
|||
# http://www.apache.org/licenses/LICENSE-2.0 |
|||
# |
|||
# Unless required by applicable law or agreed to in writing, software |
|||
# distributed under the License is distributed on an "AS IS" BASIS, |
|||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
# See the License for the specific language governing permissions and |
|||
# limitations under the License. |
|||
|
|||
function parse_cli_options () { |
|||
while (( "$#" )); do |
|||
case "$1" in |
|||
-o | --overlay) |
|||
overlays+=("${2}") |
|||
shift 2 |
|||
;; |
|||
-c | --config) |
|||
configs+=("${2}") |
|||
shift 2 |
|||
;; |
|||
-d | --disk-image) |
|||
disk_image="${2}" |
|||
shift 2 |
|||
;; |
|||
*) |
|||
log_fatal "Sorry but the provided option is not supported: $1" |
|||
;; |
|||
esac |
|||
done |
|||
|
|||
if [ -z "${disk_image}" ]; then |
|||
log_warn "Sorry, but '--disk-image' is a mandatory option" |
|||
log_warn "See ./mender-convert --help for more information" |
|||
exit 1 |
|||
fi |
|||
|
|||
if [ ! -e ${disk_image} ]; then |
|||
log_fatal "File not found: ${disk_image}" |
|||
fi |
|||
|
|||
} |
@ -0,0 +1,88 @@ |
|||
# |
|||
# Copyright 2020 Northern.tech AS |
|||
# |
|||
# Licensed under the Apache License, Version 2.0 (the "License"); |
|||
# you may not use this file except in compliance with the License. |
|||
# You may obtain a copy of the License at |
|||
# |
|||
# http://www.apache.org/licenses/LICENSE-2.0 |
|||
# |
|||
# Unless required by applicable law or agreed to in writing, software |
|||
# distributed under the License is distributed on an "AS IS" BASIS, |
|||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
# See the License for the specific language governing permissions and |
|||
# limitations under the License. |
|||
|
|||
source modules/zip.sh |
|||
source modules/log.sh |
|||
|
|||
# compression_type |
|||
# |
|||
# $1 - Path to the compressed disk image |
|||
# |
|||
# @return - The MENDER_COMPRESS_IMAGE compression type |
|||
# |
|||
function compression_type () { |
|||
if [[ $# -ne 1 ]]; then |
|||
log_fatal "compression_type() requires one argument" |
|||
fi |
|||
local -r disk_image="${1}" |
|||
case "${disk_image}" in |
|||
*.img | *.sdimg) |
|||
echo "none" |
|||
;; |
|||
*.gz) |
|||
echo "gzip" |
|||
;; |
|||
*.zip) |
|||
echo "zip" |
|||
;; |
|||
*.xz ) |
|||
echo "lzma" |
|||
;; |
|||
* ) |
|||
log_fatal "Unsupported compression type: ${disk_image}. Please uncompress the image yourself." |
|||
;; |
|||
esac |
|||
} |
|||
|
|||
# Decompresses the given input image |
|||
# |
|||
# $1 - Path to the compressed image |
|||
# $2 - Path to the output directory |
|||
# |
|||
# @return - Name of the uncompressed image |
|||
# |
|||
function decompress_image () { |
|||
if [[ $# -ne 2 ]]; then |
|||
log_fatal "decompress_image() requires an image argument and an output directory" |
|||
fi |
|||
local -r input_image="${1}" |
|||
local -r output_dir="${2}" |
|||
local disk_image="${output_dir}/$(basename ${input_image})" |
|||
case "$(compression_type ${disk_image})" in |
|||
none ) |
|||
: |
|||
;; |
|||
gzip ) |
|||
log_info "Decompressing ${disk_image}..." |
|||
disk_image=${disk_image%.gz} |
|||
zcat "${input_image}" > "${disk_image}" |
|||
;; |
|||
zip ) |
|||
log_info "Decompressing ${disk_image}..." |
|||
filename="$(zip_get_imgname ${input_image})" |
|||
unzip "${input_image}" -d "${output_dir}" &>/dev/null |
|||
disk_image="$(dirname ${disk_image})/${filename}" |
|||
;; |
|||
lzma ) |
|||
log_info "Decompressing ${disk_image}..." |
|||
disk_image=${disk_image%.xz} |
|||
xzcat "${input_image}" > "${disk_image}" |
|||
;; |
|||
* ) |
|||
log_fatal "Unsupported input image type: ${input_image}" |
|||
;; |
|||
esac |
|||
echo "${disk_image}" |
|||
} |
@ -0,0 +1,34 @@ |
|||
# |
|||
# Copyright 2020 Northern.tech AS |
|||
# |
|||
# Licensed under the Apache License, Version 2.0 (the "License"); |
|||
# you may not use this file except in compliance with the License. |
|||
# You may obtain a copy of the License at |
|||
# |
|||
# http://www.apache.org/licenses/LICENSE-2.0 |
|||
# |
|||
# Unless required by applicable law or agreed to in writing, software |
|||
# distributed under the License is distributed on an "AS IS" BASIS, |
|||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
# See the License for the specific language governing permissions and |
|||
# limitations under the License. |
|||
|
|||
# |
|||
# Parse the filename from the zipped output |
|||
# |
|||
# $1 - Zip archive path |
|||
# |
|||
# @return - Name of the img contained in the archive |
|||
# |
|||
function zip_get_imgname () { |
|||
if [[ $# -ne 1 ]]; then |
|||
log_fatal "zip_get_imgname requires one argument" |
|||
fi |
|||
local -r disk_image="${1}" |
|||
# Assert that the archive holds only one file |
|||
nfiles="$(unzip -l ${disk_image} | awk '{nfiles=$2} END {print nfiles}')" |
|||
[[ "$nfiles" -ne 1 ]] && log_fatal "Zip archive has more than one file. Needs to be unzipped by a human. nfiles: $nfiles" |
|||
local -r filename="$(unzip -lq ${disk_image} | awk 'NR==3 {filename=$NF} END {print filename}')" |
|||
[[ ${filename} == *.img ]] || log_fatal "no img file found in the zip archive ${disk_image}." |
|||
echo "$(basename ${filename})" |
|||
} |
Loading…
Reference in new issue