Debian Codegraph @

tools/make-short-partition

raw
1 #!/bin/sh
2 #
3 # just quickly format a disk with a partition table
4 # in gpt or dos, and give the first partition ~ 1/2 the size
5
6 fmt="$1"
7 disk="$2"
8 set -e
9
10 fail() { echo "$@" 1>&2; exit 1; }
11
12 if [ ! -e "$disk" ]; then
13 truncate -s 1G "$disk"
14 fi
15
16 if [ -b "$disk" ]; then
17 if [ "${_FORCE_PARTITION:-0}" != "0" ]; then
18 echo "must set _FORCE_PARTITION=1 to work with block device";
19 exit 1;
20 fi
21 blocks=$(awk '$4 == name { print $3 }' "name=${disk#/dev/}" /proc/partitions)
22 [ -n "$blocks" ] || fail "did not find $disk in /proc/partitions"
23 size=$(($blocks*1024))
24 else
25 size=$(stat --printf="%s" "$disk")
26 fi
27
28 wipefs --force --all "$disk"
29
30 pt1sectors="$(($size/1024))" # roughly half
31 sfdisk_in="2048,$pt1sectors"
32 if [ "$fmt" = "gpt" ]; then
33 if command -v sgdisk; then
34 sgdisk --new "1:2048:$pt1sectors" "$disk"
35 else
36 echo "$sfdisk_in" | sfdisk --force --unit=S --label=gpt "$disk"
37 fi
38 else
39 echo "$sfdisk_in" | sfdisk --force --unit=S "$disk"
40 fi
41
42 # vi: ts=4 noexpandtab