-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecreate_container_as_privileged.sh
executable file
·60 lines (51 loc) · 1.65 KB
/
recreate_container_as_privileged.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
#!/bin/bash
# List all containers with their IDs and names
echo "Available containers:"
pct list
# Prompt user to enter a container ID
read -p "Enter the container ID you want to recreate as privileged: " CONTAINER_ID
# Prompt user to enter storage pool to use
# List storage pools
echo ""
echo "Available storage pools"
pvesm status
read -p "Enter the storage pool to restore container: " STORAGE_POOL
# Validate that the container ID is valid
if ! pct status $CONTAINER_ID &>/dev/null; then
echo "Container ID $CONTAINER_ID is not valid."
exit 1
fi
# Stop the container
echo "Stopping container $CONTAINER_ID..."
pct stop $CONTAINER_ID
if [ $? -ne 0 ]; then
echo "Failed to stop the container $CONTAINER_ID."
exit 1
fi
# Backup the container
BACKUP_DIR="/var/lib/vz/dump/$CONTAINER_ID"
mkdir -p $BACKUP_DIR && rm -f $BACKUP_DIR/*
echo "Backing up container $CONTAINER_ID to $BACKUP_DIR..."
vzdump $CONTAINER_ID --dumpdir $BACKUP_DIR
if [ $? -ne 0 ]; then
echo "Failed to backup the container $CONTAINER_ID."
exit 1
fi
# Destroy the container
echo "Destroying container $CONTAINER_ID..."
pct destroy $CONTAINER_ID
if [ $? -ne 0 ]; then
echo "Failed to destroy the container $CONTAINER_ID."
exit 1
fi
# Restore the container as privileged
echo "Restoring container $CONTAINER_ID as privileged..."
BACKUP_FILE=$( ls /var/lib/vz/dump/$CONTAINER_ID/*.tar)
pct restore $CONTAINER_ID $BACKUP_FILE --storage $STORAGE_POOL --unprivileged 0
if [ $? -ne 0 ]; then
echo "Failed to restore the container $CONTAINER_ID as privileged."
exit 1
else
echo "Container $CONTAINER_ID has been successfully recreated as a privileged container."
exit 0
fi