Skip to content

Commit 6b09a95

Browse files
authored
Merge pull request #4 from ravindu644/test
Fixes issues related to cleaning up the mounted images after unpacking
1 parent d7d8a31 commit 6b09a95

2 files changed

Lines changed: 47 additions & 17 deletions

File tree

.bin/unpack-erofs.sh

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
#!/bin/bash
23
# EROFS Image Unpacker Script with File Attribute Preservation
34
# Usage: ./unpack_erofs.sh <image_file> [output_directory] [--no-banner]
@@ -120,7 +121,15 @@ cleanup() {
120121
echo -e "\n${YELLOW}Cleaning up...${RESET}"
121122
if mountpoint -q "$MOUNT_DIR" 2>/dev/null; then
122123
echo -e "Unmounting ${MOUNT_DIR}..."
123-
umount "$MOUNT_DIR" 2>/dev/null || fusermount -u "$MOUNT_DIR" 2>/dev/null || true
124+
125+
# Try different unmount methods in order of preference
126+
if [ "$MOUNT_METHOD" = "fuse" ]; then
127+
# For FUSE mounts, try fusermount first, then fallback to umount
128+
fusermount -u "$MOUNT_DIR" 2>/dev/null || umount "$MOUNT_DIR" 2>/dev/null || true
129+
else
130+
# For kernel mounts, try umount first, then fallback to fusermount
131+
umount "$MOUNT_DIR" 2>/dev/null || fusermount -u "$MOUNT_DIR" 2>/dev/null || true
132+
fi
124133
fi
125134

126135
# Remove raw image if it was created
@@ -129,13 +138,21 @@ cleanup() {
129138
rm -f "$RAW_IMAGE" 2>/dev/null || true
130139
fi
131140

132-
# Remove mount directory
141+
# Remove mount directory and all contents
133142
if [ -d "$MOUNT_DIR" ]; then
134143
echo -e "Removing mount directory..."
135-
rm -rf "$MOUNT_DIR" 2>/dev/null || true
144+
# Use a more aggressive approach to handle stubborn files
145+
if ! rm -rf "$MOUNT_DIR" 2>/dev/null; then
146+
echo -e "${YELLOW}Warning: Some files in mount directory may still be in use${RESET}"
147+
# Try to kill any processes using the mount point
148+
fuser -km "$MOUNT_DIR" 2>/dev/null || true
149+
# Wait a moment then try again
150+
sleep 1
151+
rm -rf "$MOUNT_DIR" 2>/dev/null || true
152+
fi
136153
fi
137154

138-
echo -e "Cleanup completed."
155+
echo -e "${GREEN}Cleanup completed.${RESET}"
139156
}
140157

141158
# Function to explicitly handle 'needs journal recovery' state
@@ -192,7 +209,7 @@ get_fs_param() {
192209
}
193210

194211
# Register cleanup function to run on script exit or interrupt
195-
# trap cleanup EXIT INT TERM
212+
trap cleanup EXIT INT TERM
196213

197214
# Create or recreate mount directory
198215
if [ -d "$MOUNT_DIR" ]; then
@@ -523,9 +540,7 @@ else
523540
exit 1
524541
fi
525542

526-
# Unmount the image
527-
cleanup
528-
543+
# Script completion
529544
if [ "$INTERACTIVE_MODE" = true ]; then
530545
echo -e "\n${GREEN}${BOLD}Done!${RESET}"
531546
fi

android_image_tools.sh

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,7 @@ cleanup_workspace() {
414414

415415
# --- Single Image Tools ---
416416
run_unpack_interactive() {
417+
local quiet_mode="$1"
417418
local input_image
418419
local output_dir
419420
local step=1
@@ -449,7 +450,9 @@ run_unpack_interactive() {
449450

450451
echo -e "\n${RED}${BOLD}Starting unpack. DO NOT INTERRUPT...${RESET}\n"
451452
trap '' INT
452-
set -e; bash "$UNPACK_SCRIPT_PATH" "$input_image" "$output_dir" --no-banner --quiet; set +e
453+
local quiet_flag=""
454+
[ "$quiet_mode" = true ] && quiet_flag="--quiet"
455+
set -e; bash "$UNPACK_SCRIPT_PATH" "$input_image" "$output_dir" --no-banner $quiet_flag; set +e
453456
trap 'cleanup_and_exit' INT TERM EXIT
454457

455458
echo -e "\n${GREEN}${BOLD}Unpack successful. Files are in: $output_dir${RESET}"
@@ -461,6 +464,7 @@ run_unpack_interactive() {
461464
}
462465

463466
run_repack_interactive() {
467+
local quiet_mode="$1"
464468
local source_dir output_image fs repack_mode erofs_comp erofs_level create_sparse overhead_percent
465469
local step=1
466470
while true; do
@@ -524,7 +528,9 @@ run_repack_interactive() {
524528
echo -e "\n${RED}${BOLD}Starting repack. DO NOT INTERRUPT...${RESET}"; trap '' INT; local repack_args=("--fs" "$fs")
525529
if [ "$fs" == "erofs" ]; then repack_args+=("--erofs-compression" "$erofs_comp"); if [ -n "$erofs_level" ]; then repack_args+=("--erofs-level" "$erofs_level"); fi; else repack_args+=("--ext4-mode" "$repack_mode"); if [ "$repack_mode" == "flexible" ]; then repack_args+=("--ext4-overhead-percent" "$overhead_percent"); fi; fi
526530

527-
set -e; bash "$REPACK_SCRIPT_PATH" "$source_dir" "$output_image" "${repack_args[@]}" --no-banner --quiet; set +e; trap 'cleanup_and_exit' INT TERM EXIT; echo
531+
local quiet_flag=""
532+
[ "$quiet_mode" = true ] && quiet_flag="--quiet"
533+
set -e; bash "$REPACK_SCRIPT_PATH" "$source_dir" "$output_image" "${repack_args[@]}" --no-banner $quiet_flag; set +e; trap 'cleanup_and_exit' INT TERM EXIT; echo
528534

529535
local final_image_path="$output_image"
530536
if [ -f "$output_image" ]; then
@@ -541,6 +547,7 @@ run_repack_interactive() {
541547

542548
# --- Super Kitchen Functions ---
543549
run_super_unpack_interactive() {
550+
local quiet_mode="$1"
544551
local super_image session_name project_dir metadata_dir logical_dir extracted_dir
545552

546553
select_item "Select super image to unpack:" "INPUT_IMAGES" "image_file"
@@ -594,7 +601,9 @@ run_super_unpack_interactive() {
594601

595602
# Run the unpack in the background so we can show a spinner
596603
# We redirect output to /dev/null because we only care about success or failure.
597-
bash "$UNPACK_SCRIPT_PATH" "${logical_dir}/${part_name}.img" "${extracted_dir}/${part_name}" --no-banner --quiet >/dev/null 2>&1 &
604+
local quiet_flag=""
605+
[ "$quiet_mode" = true ] && quiet_flag="--quiet"
606+
bash "$UNPACK_SCRIPT_PATH" "${logical_dir}/${part_name}.img" "${extracted_dir}/${part_name}" --no-banner $quiet_flag >/dev/null 2>&1 &
598607
local pid=$!
599608

600609
while kill -0 $pid 2>/dev/null; do
@@ -789,6 +798,7 @@ run_super_create_config_interactive() {
789798

790799
run_super_repack_interactive() {
791800

801+
local quiet_mode="$1"
792802
local project_dir metadata_dir part_config_file logical_dir extracted_dir
793803

794804
select_item "Select project to repack:" "SUPER_TOOLS" "dir"
@@ -847,8 +857,13 @@ run_super_repack_interactive() {
847857
fi
848858
fi
849859

850-
# Use a default of 'false' if the variable isn't in the config file
851-
if [ "${ENABLE_VERBOSE_LOGS:-false}" == "true" ]; then
860+
# Use project config ENABLE_VERBOSE_LOGS first, then fallback to command line quiet_mode
861+
local use_verbose_logs="${ENABLE_VERBOSE_LOGS:-false}"
862+
if [ "$use_verbose_logs" != "true" ] && [ "$quiet_mode" = true ]; then
863+
use_verbose_logs="false"
864+
fi
865+
866+
if [ "$use_verbose_logs" == "true" ]; then
852867
# --- VERBOSE LOGGING PATH ---
853868
echo -e "\n${YELLOW}--- (${current}/${total}) Repacking: ${BOLD}${part_name}${RESET} ---${RESET}"
854869
bash "$REPACK_SCRIPT_PATH" "${project_dir}/extracted_content/${part_name}" "${logical_dir}/${part_name}.img" "${repack_args[@]}" --no-banner
@@ -922,9 +937,9 @@ run_super_kitchen_menu() {
922937
select_option "Super Image Kitchen:" "${kitchen_options[@]}"
923938

924939
case $AIT_CHOICE_INDEX in
925-
0) run_super_unpack_interactive ;;
940+
0) run_super_unpack_interactive "$QUIET_MODE" ;;
926941
1) run_super_create_config_interactive ;;
927-
2) run_super_repack_interactive ;;
942+
2) run_super_repack_interactive "$QUIET_MODE" ;;
928943
3) break ;;
929944
esac
930945
done
@@ -1167,8 +1182,8 @@ while true; do
11671182
select_option "Select an action:" "${main_options[@]}"; choice=$AIT_CHOICE_INDEX
11681183

11691184
case $choice in
1170-
0) run_unpack_interactive;;
1171-
1) run_repack_interactive;;
1185+
0) run_unpack_interactive "$QUIET_MODE";;
1186+
1) run_repack_interactive "$QUIET_MODE";;
11721187
2) generate_config_file; read -rp $'\nPress Enter to continue...';;
11731188
3) run_advanced_tools_menu;;
11741189
4) cleanup_workspace;;

0 commit comments

Comments
 (0)