-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpartition-sticks.sh
executable file
·94 lines (76 loc) · 2.57 KB
/
partition-sticks.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
#!/bin/bash
# We handle partitions here. Best to set -e to not cause harm.
set -e
stick="$1"
size="4G"
forbidden="/dev/sda"
if [ "$stick" == "$forbidden" ]; then
echo "Cannot use /dev/sda as this would be the operating system."
exit 1
fi
devices="`echo /dev/sd* /dev/hd* | grep -o -e '/dev/..[^*]' | uniq`"
if [ -z "$stick" ]; then
echo "Possilbe first argument:"
for dev in $devices; do
[ "$dev" == "$forbidden" ] && continue
echo " \"$0\" \"$dev\""
done
exit 0
fi
echo "Computing partition"
echo "See http://rainbow.chard.org/2013/01/30/how-to-align-partitions-for-best-performance-using-parted/"
optimal_io_size="`cat /sys/block/sdb/queue/optimal_io_size`"
minimum_io_size="`cat /sys/block/sdb/queue/minimum_io_size`"
alignment_offset="`cat /sys/block/sdb/alignment_offset`"
physical_block_size="`cat /sys/block/sdb/queue/physical_block_size`"
echo "optimal_io_size = $optimal_io_size"
echo "minimum_io_size = $minimum_io_size"
echo "alignment_offset = $alignment_offset"
echo "physical_block_size = $physical_block_size"
if [ "$minimum_io_size" -gt "$optimal_io_size" ]; then
echo "minimum io size is greater than the optimal io size"
io_size="$minimum_io_size"
else
io_size="$optimal_io_size"
fi
start_sector="$(((io_size + alignment_offset) / physical_block_size))"
echo "start_sector = $start_sector"
echo "Printing partitions:"
sudo parted -s "$stick" print || true
partitions="`sudo parted -s \"$stick\" print|awk '/^ / {print $1}'`"
echo "partitions: $partitions"
echo "Deleting partitions"
for i in $partitions; do
sudo umount "$stick$i" || true
sudo parted -s "$stick" rm "$i"
done
part_num="1"
partition="$stick$part_num"
sudo partprobe || true
echo "Waiting for partition to disappear"
timeout 1s bash -c "while [ -e \"$partition\" ]; do echo -n .; sleep 0.2; done" || {
echo "$partition exists"
exit 1
}
echo " $partition vanished."
echo "Creating partition:"
sudo parted -s "$stick" mkpart primary "$start_sector"s "$size" || true
echo "Rereading the partition table"
echo "see http://serverfault.com/a/36047"
sudo partprobe || true
echo "Waiting for partition to appear"
timeout 1s bash -c "while ! [ -e \"$partition\" ]; do echo -n .; sleep 0.2; done" || {
echo "$partition does not exist"
exit 1
}
echo " $partition exists."
echo "Formatting the partition"
if [ -z "`which mkdosfs`" ]; then
sudo apt-get -y install dosfstools
fi
echo "Partition: $partition"
echo "Formatting with fat32"
sudo mkdosfs -F 32 -I "$partition"
echo "setting flags"
sudo parted -s "$stick" set "$part_num" boot on || true
sudo parted -s "$stick" set "$part_num" lba on || true