-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdisk.sh
More file actions
executable file
·81 lines (67 loc) · 2.06 KB
/
Copy pathdisk.sh
File metadata and controls
executable file
·81 lines (67 loc) · 2.06 KB
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
#!/bin/sh
# disk.sh - Mount/unmount disk.img on the host to inspect its contents
#
# Usage: ./disk.sh <command>
#
# Commands:
# mount Mount disk.img (loopback) at ./disk-mnt
# umount Unmount ./disk-mnt
# status Show whether disk.img is currently mounted
# help Display help info
#
# Configuration (edit variables below):
# DISK Path to the disk image
# MNT Mount point directory
set -eu
DISK="$PWD/disk.img"
MNT="$PWD/disk-mnt"
# Prepare arguments
cmd="${1:-}" # first argument is the subcommand (default: empty)
case "$cmd" in
mount)
if [ ! -f "$DISK" ]; then
echo "Error: $DISK not found. Run ./setup.sh first." >&2
exit 1
fi
if mountpoint -q "$MNT" 2>/dev/null; then
echo "$MNT is already mounted"
exit 0
fi
# disk.img is also used as a Firecracker block device (see
# baremetal.sh) -- mounting it on the host at the same time
# the VM is running risks the loop mount and the VM writing
# through stale/conflicting views of the same blocks.
if [ -S /tmp/firecracker.socket ] && curl -sf --unix-socket /tmp/firecracker.socket 'http://localhost/machine-config' > /dev/null 2>&1; then
echo "Error: VM is running (./baremetal.sh status). Stop it first to avoid corrupting $DISK." >&2
exit 1
fi
mkdir -p "$MNT"
# ext2 has its own on-disk uid/gid per inode (unlike FAT/
# ISO9660) -- passing mount(8)'s uid=/gid= remap options to it
# is an invalid option the ext2 driver rejects outright, which
# mount(8) reports as the generic (and misleading) "wrong fs
# type, bad option, bad superblock" error. Browsing/editing
# under $MNT after this needs sudo (or `sudo chown -R`) as a
# result, same as any other root-owned ext2 loop mount.
sudo mount -o loop "$DISK" "$MNT"
echo "Mounted $DISK at $MNT"
;;
umount)
if ! mountpoint -q "$MNT" 2>/dev/null; then
echo "$MNT is not mounted"
exit 0
fi
sudo umount "$MNT"
echo "Unmounted $MNT"
;;
status)
if mountpoint -q "$MNT" 2>/dev/null; then
echo "$MNT is mounted"
else
echo "$MNT is not mounted"
fi
;;
help|*)
sed -n '2,14p' "$0" | sed 's/^# \?//'
;;
esac