forked from pxlrbt/move-wsl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmove-wsl
79 lines (62 loc) · 1.78 KB
/
move-wsl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/bin/bash
cleanup() {
# Remove temporary file
echo "Cleaning up ..."
rm -f "$TEMP_FILE"
}
# Allow old and new way
if [ $# -eq 2 ]; then
WSL_NAME=$1
WSL_TARGET=$2
else
# Select distro
WSL_DISTROS=($(wsl -l -q | iconv -f UTF-16LE -t UTF-8 | sed "s/\r\n/\n/g"))
PS3="Select distro to move: "
select WSL_NAME in ${WSL_DISTROS[@]}; do
if [ 1 -le "$REPLY" ] && [ "$REPLY" -le ${#WSL_DISTROS[@]} ];
then
break;
else
echo "Wrong selection: Select any number from 1-${#WSL_DISTROS[@]}"
fi
done
read -p "Enter WSL target directory: " WSL_TARGET
fi
# Safety check
echo
read -p "Move WSL '$WSL_NAME' to $WSL_TARGET? (Y|n) " -n 1 PROMPT
echo
if [ "$PROMPT" != 'Y' ]; then
exit 1;
fi
TEMP_FILE="$WSL_TARGET/$WSL_NAME.tar"
# Create target dir if non existent
if [ ! -d "$WSL_TARGET" ]; then
echo "Creating target dir \"$WSL_TARGET\" ..."
mkdir -p $WSL_TARGET
fi
# Export WSL image to tar file
echo "Exporting VHDX to \"$TEMP_FILE\" ..."
wsl --export $WSL_NAME $TEMP_FILE
if [ $? -ne 0 ] || [ ! -f $TEMP_FILE ]; then
echo "ERROR: Export failed"
cleanup
exit 2
fi
# Unregister WSL so we can register it again at new location
echo "Unregistering WSL ..."
wsl --unregister $WSL_NAME &>/dev/null
# Importing WSL at new location
echo "Importing \"$WSL_NAME\" at \"$WSL_TARGET\" ..."
wsl --import $WSL_NAME $WSL_TARGET $TEMP_FILE
# Validate everything went well
if [ -z $(wsl --list | iconv -f UTF-16LE -t UTF-8 | grep -E "(^|\s)$WSL_NAME($|\s)") ]; then
echo "ERROR: Import failed. Distro not found. Export file at $TEMP_FILE"
exit 3
fi
if [ ! -f "$WSL_TARGET/ext4.vhdx" ]; then
echo "ERROR: Import failed. Target file not found. Export file at $TEMP_FILE"
exit 4
fi
cleanup
echo "Done!"