aboutsummaryrefslogtreecommitdiffstats
path: root/rpi2-debian-stdkernel/format-sdcard.py
blob: ea08728fc437eee5098a201760056e88cb449812 (plain)
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
#!/usr/bin/python3

import json
import subprocess
import sys

def run(*l, **d):
    print('run:', *l, d)
    return subprocess.run(*l, **d)

def main():
    if len(sys.argv) < 2:
        print('Usage: {} <device>'.format(sys.argv[0]), file=sys.stderr)
        raise SystemExit(1)
    device = sys.argv[1]
    # first, clean possible gpt tables
    run(['sudo', 'sgdisk', '--zap-all', device])
    part_commands = '''
        mklabel msdos
        mkpart primary fat32 1M    99M
        mkpart primary ext4  100M  -1M
        set 1 boot on
        print
    '''
    run(['sudo', 'parted', device],
        check=True, input=part_commands.encode('utf8'))
    run(['sudo', 'mkfs.vfat', '-n', 'boot', device + '1', ], check=True)
    run(['sudo', 'mkfs.ext4', '-q', '-F', '-L', 'root', device + '2'],
        check=True)


if __name__ == '__main__':
    main()