-
Notifications
You must be signed in to change notification settings - Fork 0
/
chroot-manager.sh
140 lines (122 loc) · 3.64 KB
/
chroot-manager.sh
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#!/bin/bash
# Function to list available operating systems
list_os() {
if [ -f "$HOME/.cross/listofoses.txt" ]; then
echo "Available operating systems:"
cat "$HOME/.cross/listofoses.txt"
else
echo "No operating systems found."
fi
}
# Function to add or install an operating system
add_os() {
echo "Enter the name of the operating system:"
read os_name
if [ -z "$os_name" ]; then
echo "Invalid input. Please provide a valid operating system name."
return
fi
if [ -f "$HOME/.cross/listofoses.txt" ]; then
if grep -Fxq "$os_name" "$HOME/.cross/listofoses.txt"; then
echo "Operating system already exists in the list."
else
echo "$os_name" >> "$HOME/.cross/listofoses.txt"
echo "Operating system '$os_name' added to the list."
fi
else
echo "$os_name" > "$HOME/.cross/listofoses.txt"
echo "Operating system '$os_name' added to the list."
fi
# Set the distribution and architecture information
distribution="$os_name"
release="22.04"
arch="amd64"
# Set the specific URL for the distribution
case "$distribution" in
"ubuntu")
url="http://archive.ubuntu.com/ubuntu"
;;
"debian")
url="http://deb.debian.org/debian"
;;
# Add necessary URLs for other distributions
*)
echo "Unsupported distribution: $distribution"
exit 1
;;
esac
# Install necessary packages
sudo apt-get update
sudo apt-get install -y qemu-user-static schroot debootstrap
# Create the chroot environment
sudo debootstrap --arch "$arch" --foreign "$release" "/srv/chroot/$distribution-$arch" "$url"
# Copy qemu-user-static (required for supported architectures)
case "$arch" in
"i386" | "amd64" | "armhf" | "arm64" | "aarch64" | "x86_64")
sudo cp "/usr/bin/qemu-$arch-static" "/srv/chroot/$distribution-$arch/usr/bin"
;;
# Add necessary commands for other architectures
esac
# Perform the second stage of debootstrap
sudo chroot "/srv/chroot/$distribution-$arch" /debootstrap/debootstrap --second-stage
# Create the chroot configuration file
sudo sh -c "echo '[$distribution-$arch]
description=$distribution $release $arch chroot
aliases=$distribution-$arch
type=directory
directory=/srv/chroot/$distribution-$arch
profile=desktop
personality=linux
preserve-environment=true' > /etc/schroot/chroot.d/$distribution-$arch.conf"
# Enter the chroot environment
sudo schroot -c "$distribution-$arch"
}
# Function to clean up existing operating systems not in the list
clean_up_os() {
echo "Cleaning up existing operating systems not in the list..."
if [ -f "$HOME/.cross/listofoses.txt" ]; then
while IFS= read -r os; do
if ! grep -Fxq "$os" "$HOME/.cross/listofoses.txt"; then
sudo rm -rf "/srv/chroot/$os-amd64"
sudo rm -f "/etc/schroot/chroot.d/$os-amd64.conf"
echo "Operating system '$os' removed."
fi
done < <(ls -d /srv/chroot/*-amd64 2>/dev/null | grep -oP "(?<=/srv/chroot/).*?(?=-amd64)")
fi
echo "Cleanup completed."
}
# Main menu
while true; do
echo
echo "Cross-Platform Chroot Manager"
echo "-----------------------------"
echo "1. List available operating systems"
echo "2. Add or install an operating system"
echo "3. Remove an operating system"
echo "4. Clean up existing operating systems"
echo "0. Exit"
echo
echo -n "Enter your choice: "
read choice
case "$choice" in
1)
list_os
;;
2)
add_os
;;
3)
remove_os
;;
4)
clean_up_os
;;
0)
break
;;
*)
echo "Invalid choice. Please enter a valid option."
;;
esac
echo
done