-
Notifications
You must be signed in to change notification settings - Fork 310
/
Copy pathgenerate_hdd.sh
executable file
·147 lines (115 loc) · 4.22 KB
/
generate_hdd.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
141
142
143
144
145
146
#!/bin/sh
set -e
# Create sparse file of 20MB which can be used by QEMU.
if [ "$1" = "-e" -o "$1" = "--empty" ] ; then
# Create new hard disk image file.
rm -f hdd.img
truncate -s 20M hdd.img
echo "Created new hard disk image file 'hdd.img' with 20MB size."
elif [ "$1" = "-f" -o "$1" = "--folder" ] ; then
if [ ! "$(id -u)" = "0" ] ; then
echo "Using option '-f' (or '--folder') requires root permissions."
exit 1
fi
rm -f hdd.img
truncate -s 20M hdd.img
echo "Created new hard disk image file 'hdd.img' with 20MB size."
LOOP_DEVICE=$(losetup -f)
losetup $LOOP_DEVICE hdd.img
echo "Attached hard disk image file to loop device."
mkfs.ext2 $LOOP_DEVICE
echo "Hard disk image file has been formatted with Ext2 filesystem."
mkdir folder
mount hdd.img folder
echo "Mounted hard disk image file to temporary folder."
mkdir -p folder/minimal/rootfs
mkdir -p folder/minimal/work
echo "Overlay structure has been created."
echo "This file is on external hard disk." > folder/minimal/rootfs/overlay.txt
echo "Created sample text file."
sync
umount folder
sync
rm -rf folder
echo "Unmounted hard disk image file."
losetup -d $LOOP_DEVICE
echo "Detached hard disk image file from loop device."
# Find the original user. Note that this may not always be correct.
ORIG_USER=`who | awk '{print \$1}'`
chown $ORIG_USER hdd.img
echo "Applied original ownership to hard disk image file."
elif [ "$1" = "-s" -o "$1" = "--sparse" ] ; then
if [ ! "$(id -u)" = "0" ] ; then
echo "Using option '-s' (or '--sparse') requires root permissions."
exit 1
fi
rm -f hdd.img
truncate -s 20M hdd.img
echo "Created new hard disk image file 'hdd.img' with 20MB size."
LOOP_DEVICE_HDD=$(losetup -f)
losetup $LOOP_DEVICE_HDD hdd.img
echo "Attached hard disk image file to loop device."
mkfs.vfat $LOOP_DEVICE_HDD
echo "Hard disk image file has been formatted with FAT filesystem."
rm -rf sparse
mkdir sparse
mount hdd.img sparse
echo "Mounted hard disk image file to temporary folder."
rm -f sparse/minimal.img
truncate -s 3M sparse/minimal.img
echo "Created new overlay image file with 3MB size."
LOOP_DEVICE_OVL=$(losetup -f)
losetup $LOOP_DEVICE_OVL sparse/minimal.img
echo "Attached overlay image file to loop device."
mkfs.ext2 $LOOP_DEVICE_OVL
echo "Overlay image file has been formatted with Ext2 filesystem."
mkdir ovl
mount sparse/minimal.img ovl
echo "Mounted overlay image file to temporary folder."
mkdir -p ovl/rootfs
mkdir -p ovl/work
echo "Overlay structure has been created."
echo "Create sample text file."
echo "This file is on external hard disk." > ovl/rootfs/overlay.txt
chown -R root:root ovl
echo "Applied root ownership to overlay content."
sync
umount ovl
sync
sleep 1
rm -rf ovl
echo "Unmounted overlay image file."
losetup -d $LOOP_DEVICE_OVL
sleep 1
echo "Overlay image file has been detached from loop device."
sync
umount sparse
sync
sleep 1
rm -rf sparse
echo "Unmounted hard disk image file."
losetup -d $LOOP_DEVICE_HDD
sleep 1
echo "Hard disk image file has been detached from loop device."
# Find the original user. Note that this may not always be correct.
ORIG_USER=`who | awk '{print \$1}'`
chown $ORIG_USER hdd.img
echo "Applied original ownership to hard disk image file."
elif [ "$1" = "-h" -o "$1" = "--help" ] ; then
cat << CEOF
Usage: $0 [OPTION]
This utility generates 20MB sparse file 'hdd.img' which can be used as QEMU
disk image where all filesystem changes from the live session are persisted.
-e, --empty Create empty sparse image file which is not formatted.
-f, --folder Create sparse image file formatted with Ext2 filesystem which
contains compatible overlay folder structure.
-h, --help Prints this help information.
-s, --sparse Create sparse image file formatted with FAT filesystem which
contains sparse image file 'minimal.img' (3MB) formatted with
Ext2 filesystem which contains the actual overlay structure.
CEOF
elif [ "$1" = "" ] ; then
echo "No option specified. Use '-h' or '--help' for more info."
else
echo "Option '$1' is not recognized. Use '-h' or '--help' for more info."
fi