11 EXIT_COMMAND_NOT_FOUND
=127
21 IMAGE_RELEASE
=${FALSE}
30 echo "[${level}] ${message}"
37 log DEBUG
"Running command: ${cmd}"
47 log DEBUG
"Returned with code '${ret}'"
55 local mounted_dir
=${1}
57 if [ ! -d ${mounted_dir} ]; then
58 log ERROR
"Is not a directory ${mounted_dir}"
61 mountpoint
${mounted_dir}
69 local return_value
=${EXIT_OK}
71 for mounted_dir
in "/proc" "/sys" "/dev/pts" "/dev/shm" "/dev" "/run" "/tmp" ""
73 path
="${IMAGE_MOUNT_DIR}${mounted_dir}"
75 if is_mounted
"${path}"; then
76 log DEBUG
"Unmounting ${path}"
79 if is_mounted
"${path}"; then
80 return_value
=${EXIT_ERROR}
83 log DEBUG
"${path} is not mounted"
96 # Remove partition from the kernel table.
99 # Remove loopback device
100 log DEBUG
" Remove loopback device"
105 # Cleanup Config file for the local repo
106 if [ -f "${LOCAL_REPO_FILE}" ]; then
107 rm -f "${LOCAL_REPO_FILE}"
114 if unmount_dirs
; then
116 if [ -d "${WORKING_DIR}" ]; then
117 #rm -dfR "${WORKING_DIR}"
128 generate_image_filename
() {
133 echo "${distro}-${version}-${arch}"
136 check_for_pakfire
() {
140 # Check that pakfire-server binary is available
141 # Check that pakfire binary is available
143 # Check that repo files are installed. (pakfire need to know which repos exist)
144 local repo_dir
="/etc/pakfire/repos"
146 if [ ! -d "${repo_dir}" ]; then
147 log ERROR
"Could not find respository directory ${repo_dir}"
151 return ${return_value}
155 local compression
=${1}
156 local image_file
=${2}
159 log debug
"Compressing ${image_file} with ${compression}"
161 case "${compression}" in
163 # Check that the file does not exist yet
164 if [ -f "${image_file}.xz" ]; then
165 log ERROR
"Failed to compress the image. The file already exists"
168 cmd xz
"-${level}" "${image_file}"
171 # Check that the file does not exist yet
172 if [ -f "${image_file}.zip" ]; then
173 log ERROR
"Failed to compress the image. The file already exists"
177 cmd
zip "-${level}" "${image_file}.zip" "${image_file}"
178 # Remove the file which we compressed+
179 rm -f "${image_file}"
185 reset_root_password
() {
188 cp -avf ${root_dir}/etc
/passwd
${root_dir}/etc
/passwd.orig
190 # Drop root password.
191 sed -e "s/^\(root:\)[^:]*:/\1:/" ${root_dir}/etc
/passwd.orig
> ${root_dir}/etc
/passwd
193 # Remove passwd backup file.
194 rm -rvf ${root_dir}/etc
/passwd.orig
198 # Dir where the repos should be located
209 # Clone git repositories.
210 for repo
in ${repos}; do
216 # This function is used to publish the produced images
220 # The image we created usually a img. file
221 local image_base_file
=${2}
223 local image_name_final
="$(generate_image_filename "${DISTRO}" "${VERSION}" "${ARCH}")"
226 # Do these steps for every image format we like to publish
227 for image_type
in ${IMAGE_TYPES_PUBLISH}; do
228 # Convert images to the type specified in IMAGE_TYPES_PUBLISH
229 convert_image
"${image_type}" "${image_base_file}" "${image_name_final}"
232 if [[ ${IMAGE_RELEASE} -eq ${TRUE} ]]; then
233 local compression_type
234 local compression_level
237 # Get compressioon type
238 compression_type
="$(get_compression_type ${image_type})"
241 compress_image
"${compression_type}" "${image_name_final}.${image_type}" ${compression_level}
243 # Move images to this path
244 mv -f "${image_name_final}.${image_type}.${compression_type}" ${path}
246 # point the latest links to these images
247 ln -s -f "${path}/${image_name_final}.${image_type}.${compression_type}" \
248 "${path}/$(generate_image_filename "${DISTRO}" "latest" "${ARCH}").${image_type}.${compression_type}"
251 # Move images to this path
252 mv -f "${image_name_final}.${image_type}" ${path}
263 if [[ ${type} = "img
" ]]; then
264 # We do not need to convert the image here but we need to rename
265 mv -f ${from} ${to}.${type}
269 if [[ ${type} = "qcow2
" ]]; then
270 local command="qemu-img convert
-c -O ${type} ${from} ${to}.${type}"
272 local command="qemu-img convert
-O ${type} ${from} ${to}.${type}"
278 get_compression_type() {
279 local image_type=${1}
281 case "${image_type}" in
283 # These types can be used only under Unix so we use xz as compression
287 # These types can be also under Windows so we use zip as compression
294 check_for_free_space() {
297 local space_in_path=0
299 space_in_path=$(df -h -B MB --output=avail ${path} | tail -n 1)
300 space_in_path=${space_in_path%MB}
301 log debug ${space_in_path}
304 if [ ${space_in_path} -lt ${space} ]; then
305 log error "Not enough free space available under
${path}"
306 log error "Free space is
${space_in_path}MB but we need
at least
${space}MB
"
312 while [ $# -gt 0 ]; do
315 IMAGE_RELEASE=${TRUE}
319 error "Invalid argument
: ${1}"
320 return ${EXIT_CONF_ERROR}
328 local chroot_dir="${1}"
334 if [ ! -d ${chroot_dir} ]; then
335 log ERROR "Cannot chroot
in a non directory
${chroot_dir}"
338 mount proc "${chroot_dir}/proc
" -t proc -o nosuid,noexec,nodev
339 mount sys "${chroot_dir}/sys
" -t sysfs -o nosuid,noexec,nodev,ro
340 mount udev "${chroot_dir}/dev
" -t devtmpfs -o mode=0755,nosuid
341 mount devpts "${chroot_dir}/dev
/pts
" -t devpts -o mode=0620,gid=5,nosuid,noexec
342 mount shm "${chroot_dir}/dev
/shm
" -t tmpfs -o mode=1777,nosuid,nodev
343 mount /run "${chroot_dir}/run
" --bind
344 mount tmp "${chroot_dir}/tmp
" -t tmpfs -o mode=1777,strictatime,nodev,nosuid
348 cmd chroot "${chroot_dir}" "${command}"
351 umount "${chroot_dir}/proc
"
352 umount "${chroot_dir}/sys
"
353 umount "${chroot_dir}/dev
/pts
"
354 umount "${chroot_dir}/dev
/shm
"
355 umount "${chroot_dir}/dev
"
356 umount "${chroot_dir}/run
"
357 umount "${chroot_dir}/tmp
"