-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate-blank-img.sh
More file actions
61 lines (51 loc) · 1.25 KB
/
create-blank-img.sh
File metadata and controls
61 lines (51 loc) · 1.25 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
#!/bin/sh
set -e -o pipefail
if [ $# -ne 2 ]; then
1>&2 echo "Error: two arguments expected: fs_type file_name"
exit 1
fi
fs_type="$1"
file_name="$2"
if [ -z "$DISK_SIZE_SECTORS" ]; then
DISK_SIZE_SECTORS=65536
fi
if [ -z "$PART_LABEL" ]; then
PART_LABEL="PolyDisk"
fi
part_start_sector=2048
part_size_sectors=$(($DISK_SIZE_SECTORS - $part_start_sector))
_fallocate() {
size="$1"
filename="$2"
if [ -f "$filename" ]; then
rm "$filename"
fi
if ! fallocate "$filename" 2>/dev/null; then
dd if=/dev/zero of="$filename" bs="$size" count=1 status=none
fi
}
create_files() {
_fallocate $(($DISK_SIZE_SECTORS * 512)) "$file_name"
_fallocate $(($part_size_sectors * 512)) "$file_name.part"
}
if [ "$fs_type" = "vfat" ]; then
create_files
mkfs.vfat -n "$PART_LABEL" "$file_name.part"
part_type=c
elif [ "$fs_type" = "exfat" ]; then
create_files
mkfs.exfat -L "$PART_LABEL" "$file_name.part"
part_type=7
else
1>&2 echo "Error: invalid filesystem type: $fs_type"
exit 1
fi
cat <<EOF | sfdisk "$file_name"
label: dos
device: /fakedev
unit: sectors
sector-size: 512
/fakedev1 : start=$part_start_sector, size=$part_size_sectors, type=$part_type
EOF
dd "if=$file_name.part" "of=$file_name" bs=$(($part_start_sector * 512)) seek=1 conv=notrunc
rm "$file_name.part"