-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmk-firecracker
executable file
·94 lines (80 loc) · 1.71 KB
/
mk-firecracker
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
#!/bin/bash
usage() {
echo -e "mk-firecracker -s <SIZE> -i <QCOW2_IMG> -p <ROOT_PART>"
echo -e "options:"
echo -e "\t-s : size of final ext4 file in GB (eg -s 4 == 4GB)"
echo -e "\t-i : QCOW2 image to transform"
echo -e "\t-p : Partition where the filesystem is kept in QCOW2_IMG"
echo -e "\n\texample:\n\t$ mk-firecracker -s 10 -i arch.qcow2 -p 3\n"
}
SIZE=""
QEMU_IMG=""
PART=""
while [[ ! -z "$1" ]]; do
case "$1" in
-h|--help)
usage
exit 1
;;
-s|--size)
SIZE=$2
shift
;;
-i|--img)
QEMU_IMG=$2
shift
;;
-p|--partition)
PART=$2
shift
;;
esac
shift
done
ID=$(id | awk '{ print $1; }' | cut -b 5-8 | sed 's/[^0-9]*//g')
if [[ $ID != "0" ]]; then
echo -e "Error: Must be root"
exit 1
fi
if [[ $SIZE == "" || $QEMU_IMG == "" || $PART == "" ]]; then
echo -e "ERROR: Missing parameter"
usage
exit 1
else
echo -e "size = $SIZE image = $QEMU_IMG partition = $PART"
fi
BASE=`pwd`
EXT=$BASE/ext4-dir
umount ext4-dir 2> /dev/null
umount raw-dir 2> /dev/null
rm -rf ext4-dir raw-dir 2> /dev/null
mkdir ext4-dir raw-dir
rm OS.ext4 2> /dev/null
dd if=/dev/zero of=OS.ext4 bs=1G count=$SIZE
mkfs.ext4 OS.ext4
LOOP_0=`losetup -f`
losetup -fP OS.ext4
mount $LOOP_0 ext4-dir
LOOP_1=`losetup -f`
LOOP_1+="p$PART"
isqemu=$(file $QEMU_IMG | grep -i "qemu qcow")
if [[ ! -z "$isqemu" ]]; then
rm $QEMU_IMG.raw 2> /dev/null
qemu-img convert -f qcow2 -O raw $QEMU_IMG $QEMU_IMG.raw
QEMU_IMG="$QEMU_IMG.raw"
fi
losetup -fP $QEMU_IMG
mount "$LOOP_1" raw-dir
cd raw-dir
#cd root
tar c . | tar x -C $EXT
cd $BASE
rm -rf ext4-dir/lost+found
rm ext4-dir/etc/fstab
touch ext4-dir/.autorelabel
ls -l ext4-dir
umount raw-dir
umount ext4-dir
losetup -d $LOOP_1
losetup -d $LOOP_0
rm -rf raw-dir ext4-dir