summaryrefslogtreecommitdiff
path: root/setup_sdcard.sh
blob: d15d778e61464e6c0067f5c4491fa58b9455fb65 (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
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
#!/bin/bash

print_help()
{
    echo ""
    echo "$ ./setup_sdcard.sh [option]"
    echo ""
    echo "Options:"
    echo "-b [device]      -- Create bootable image on device"
    echo ""

    exit 1
}

parse_args()
{
    while getopts ":b:" opt
    do
        case "$opt" in
        b)
            DEV=$OPTARG
            export DEV
            ;;
        *)
            print_help
            ;;
        esac
    done
    shift $((OPTIND-1))
}

parse_args $@

if ! [[ -a $DEV ]]; then
    echo "No valid device found, \"${DEV}\" is not valid"
    print_help
fi

# E.g.:
#   on mmcblk0, we have mmcblk0p1, mmcblk0p2...
#   on sdb, we have sdb1, sdb2...
[[ $(basename $DEV) = "mmcblk"* ]] && PPREFIX="p"

sudo umount ${DEV}*
(
    # Create a new empty DOS partition table
    echo o
    # Add a 200MB boot partition
    echo n
    echo p
    echo 1
    echo
    echo +200M
    echo t
    echo b
    # Add a 3500MB system partition
    echo n
    echo p
    echo 2
    echo
    echo +3500M
    # Add a 500MB cache partition
    echo n
    echo p
    echo 3
    echo
    echo +500M
    # Add a data partition
    echo n
    echo p
    echo 4
    echo
    echo
    # Write changes
    echo w
) | sudo fdisk "${DEV}"

# Refresh partition lists for devices
sudo partprobe

sudo mkfs.vfat -F 32 "${DEV}${PPREFIX}1"
sudo fatlabel        "${DEV}${PPREFIX}1" BOOT

sudo mkfs.ext4  "${DEV}${PPREFIX}2" -F -L system
sudo mkfs.ext4  "${DEV}${PPREFIX}3" -F -L cache
sudo mkfs.ext4  "${DEV}${PPREFIX}4" -F -L data
sync