-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathburn-gpt.js
137 lines (110 loc) · 3.68 KB
/
burn-gpt.js
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
const Promise = require('bluebird')
const path = require('path')
const fs = Promise.promisifyAll(require('fs'))
const child = Promise.promisifyAll(require('child_process'))
const execAsync = Promise.promisify(require('./lib/exec'))
const partUUID = require('./lib/partUUID')
// let size = parseInt(fs.readFileSync(`/sys/block/${devname}/size`).toString().trim()) * 512
const fdiskInput = [
'o', // create a new empty DOS partition table
'n', // add a new partition
'p', // primary
'1', // number 1
'', // first sector
'+1G', // last sector
'n', // add a new partition
'p', // primary
'2', // number 2
'', // first sector
'+3G', // last sector
'n', // add a new partition
'p', // primary
'3', // number 3
'', // first sector
'+3G', // last sector
'n', // add a new partition
'e', // extended
'4', // number 4
'', // first sector
'', // last sector
'n', // new logic partition
'', // first sector
'+2G', // last sector
'n', // new logic partition
'', // first sector
'', // last sector
'w' // final write
].join('\n')
const gdiskInput = [
'o', // create new GPT partition table
'Y', // confirm
'n', // add a new partition
'', // number 1
'', // first sector
'+2G', // last sector (by size)
'', // code, default 8300
'n', // add a new partition
'', // number 2
'', // first sector
'+4G', // last sector (by size)
'', // code, default 8300
'n', // add a new partition
'', // number 3
'', // first sector
'+4G', // last sector (by size)
'', // code, default 8300
'n', // add a new partition
'', // number 4
'', // first sector
'+4G', // last sector (by size)
'8200', // code, linux swap 8200
'n', // add a new partition
'', // number 5
'', // first sector
'', // last sector (all left space)
'', // code, default 8300
'w', // final write
'Y\n', // final confirm
].join('\n')
const args = process.argv.slice(2)
if (args.length !== 1) {
console.log('ERROR: this script requires exactly one argument')
process.exit(1)
}
let devpath = args[0]
if (!/^\/dev\/sd[b-g]$/.test(devpath)) {
console.log(`ERROR: invalid argument ${devpath}`)
process.exit(1)
}
let devname = devpath.split('/').pop()
;(async () => {
await execAsync('partprobe')
for (let i = 1; i < 7; i++) {
try { await child.execAsync(`umount ${devpath}${i}`) } catch (e) {}
try { await child.execAsync(`wipefs -a ${devpath}${i}`) } catch (e) {}
}
// TODO
await execAsync(`gdisk ${devpath}`, gdiskInput)
await execAsync(`mkfs.ext4 -F -U ${partUUID.p} /dev/${devname}1`)
await execAsync(`mkfs.ext4 -F -U ${partUUID.a} /dev/${devname}2`)
await execAsync(`mkfs.ext4 -F -U ${partUUID.b} /dev/${devname}3`)
// TODO
await execAsync(`mkswap -U ${partUUID.s} /dev/${devname}4`)
// TODO
await execAsync(`mkfs.btrfs -f /dev/${devname}5`)
await execAsync(`partprobe`)
await execAsync('mkimage -C none -A arm -T script -d assets/boot.cmd assets/boot.scr')
await execAsync('rm -rf mnt')
await execAsync('mkdir -p mnt/p')
await execAsync('mkdir -p mnt/a')
await execAsync('mkdir -p mnt/b')
await execAsync(`mount /dev/${devname}1 mnt/p`)
await execAsync('tar xf out/p.tar.gz -C mnt/p')
await execAsync('umount -l mnt/p')
await execAsync(`mount /dev/${devname}2 mnt/a`)
await execAsync(`tar xf out/rootfs.tar.gz -C mnt/a`)
await execAsync(`umount -l mnt/a`)
await execAsync(`mount /dev/${devname}3 mnt/b`)
await execAsync(`tar xf out/rootfs.tar.gz -C mnt/b`)
await execAsync(`umount -l mnt/b`)
})().then(() => {}).catch(e => console.log(e))