linux-image-manager/scripts/image/base.sh

119 lines
4.3 KiB
Bash
Raw Normal View History

2020-05-15 12:00:03 +02:00
#!/bin/bash
#
# Offers base functions for the image management
#
# shellcheck disable=SC2034 #Deactivate checking of unused variables
2020-05-19 11:10:06 +02:00
# shellcheck disable=SC2010 # ls | grep allowed
2020-05-15 12:00:03 +02:00
# shellcheck source=/dev/null # Deactivate SC1090
2020-05-19 11:10:06 +02:00
# shellcheck disable=SC2015 # Deactivate bools hints
2020-06-12 11:07:57 +02:00
# shellcheck disable=SC2154 # Deactivate referenced but not assigned hints
2020-05-15 12:00:03 +02:00
source "$(dirname "$(readlink -f "${0}")")/../base.sh" || (echo "Loading base.sh failed." && exit 1)
# Writes the full partition name
# @parameter $1 is device path
# @parameter $2 is the partition number
echo_partition_name(){
if [ "${device_path:5:1}" != "s" ]
2020-05-15 12:00:03 +02:00
then
2020-05-15 14:13:47 +02:00
echo "$device_path""p""$1"
2020-05-15 12:00:03 +02:00
else
2020-05-15 14:13:47 +02:00
echo "$device_path$1"
2020-05-15 12:00:03 +02:00
fi
}
2020-05-15 12:23:07 +02:00
set_partition_paths(){
2020-10-01 17:42:35 +02:00
info "Setting partition and mapper paths..."
boot_partition_path=$(echo_partition_name "1")
root_partition_path=$(echo_partition_name "2")
2020-10-01 17:42:35 +02:00
root_mapper_path=$root_partition_path
}
2020-05-15 14:10:24 +02:00
make_mount_folders(){
info "Preparing mount paths..." &&
boot_mount_path="$working_folder_path""boot/" &&
root_mount_path="$working_folder_path""root/" &&
mkdir -v "$boot_mount_path" &&
mkdir -v "$root_mount_path" ||
error
}
make_working_folder(){
working_folder_path="/tmp/raspberry-pi-tools-$(date +%s)/" &&
info "Create temporary working folder in $working_folder_path" &&
mkdir -v "$working_folder_path" ||
error
}
2020-10-01 21:01:06 +02:00
set_root_variables(){
info "Setting root variables..." &&
root_partition_uuid=$(blkid "$root_partition_path" -s UUID -o value) &&
2020-10-04 12:53:37 +02:00
boot_partition_uuid=$(blkid "$boot_partition_path" -s UUID -o value) &&
2020-10-01 21:01:06 +02:00
root_mapper_name="arch-root-$root_partition_uuid" &&
root_mapper_path="/dev/mapper/$root_mapper_name" || error
}
2020-10-01 21:16:08 +02:00
decrypt_root(){
2020-10-01 19:35:01 +02:00
if [ "$(blkid "$root_partition_path" -s TYPE -o value)" == "crypto_LUKS" ]
then
2020-10-01 21:01:06 +02:00
set_root_variables &&
2020-10-01 19:35:01 +02:00
info "Decrypting of $root_partition_path is neccessary..." &&
sudo cryptsetup -v luksOpen "$root_partition_path" "$root_mapper_name" ||
error
fi
2020-10-01 21:16:08 +02:00
}
mount_partitions(){
2020-05-15 14:10:24 +02:00
info "Mount boot and root partition..." &&
mount -v "$boot_partition_path" "$boot_mount_path" &&
2020-10-01 17:42:35 +02:00
mount -v "$root_mapper_path" "$root_mount_path" &&
2020-05-15 14:10:24 +02:00
info "The following mounts refering this setup exist:" && mount | grep "$working_folder_path" ||
error
}
2020-06-11 15:09:28 +02:00
destructor(){
2020-06-11 12:19:19 +02:00
info "Cleaning up..."
info "Unmounting everything..."
umount -lv "$chroot_dev_pts_mount_path" || warning "Umounting $chroot_dev_pts_mount_path failed!"
umount -lv "$chroot_dev_mount_path" || warning "Umounting $chroot_dev_mount_path failed!"
umount -v "$chroot_proc_mount_path" || warning "Umounting $chroot_proc_mount_path failed!"
umount -v "$chroot_sys_mount_path" || warning "Umounting $chroot_sys_mount_path failed!"
umount -v "$root_mount_path""boot/" || warning "Umounting $root_mount_path""boot/ failed!"
umount -v "$root_mount_path" || warning "Umounting $root_mount_path failed!"
umount -v "$boot_mount_path" || warning "Umounting $boot_mount_path failed!"
info "Deleting mount folders..."
rmdir -v "$root_mount_path" || warning "Removing $root_mount_path failed!"
rmdir -v "$boot_mount_path" || warning "Removing $boot_mount_path failed!"
rmdir -v "$working_folder_path" || warning "Removing $working_folder_path failed!"
2020-10-01 19:35:01 +02:00
if [ "$(blkid "$root_partition_path" -s TYPE -o value)" == "crypto_LUKS" ]
then
info "Trying to close decrypted $root_mapper_name..." &&
2020-10-01 21:01:06 +02:00
sudo cryptsetup -v luksClose "$root_mapper_name" || warning "Failed."
2020-10-01 19:35:01 +02:00
fi
2020-06-11 11:41:51 +02:00
}
2020-05-21 19:22:14 +02:00
mount_chroot_binds(){
2020-05-15 14:10:24 +02:00
info "Mount chroot environments..." &&
chroot_sys_mount_path="$root_mount_path""sys/" &&
chroot_proc_mount_path="$root_mount_path""proc/" &&
chroot_dev_mount_path="$root_mount_path""dev/" &&
chroot_dev_pts_mount_path="$root_mount_path""dev/pts" &&
2020-06-08 17:45:11 +02:00
mount --bind "$boot_mount_path" "$root_mount_path""boot" &&
2020-05-15 14:10:24 +02:00
mount --bind /dev "$chroot_dev_mount_path" &&
mount --bind /sys "$chroot_sys_mount_path" &&
mount --bind /proc "$chroot_proc_mount_path" &&
mount --bind /dev/pts "$chroot_dev_pts_mount_path" ||
error
}
2020-05-21 19:22:14 +02:00
copy_qemu(){
info "Copy qemu binary..." &&
cp -v /usr/bin/qemu-arm-static "$root_mount_path""usr/bin/" ||
error
}
copy_resolve_conf(){
info "Copy resolve.conf..." &&
2020-06-11 10:13:54 +02:00
cp --remove-destination -v /etc/resolv.conf "$root_mount_path""etc/" ||
2020-05-21 19:22:14 +02:00
warning "Failed. Propably there is no internet connection available."
}