-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathburn.js
95 lines (76 loc) · 2.76 KB
/
burn.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
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 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) {}
}
await execAsync(`fdisk ${devpath}`, fdiskInput)
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`)
await execAsync(`mkswap -U ${partUUID.s} /dev/${devname}5`)
await execAsync(`mkfs.btrfs -f /dev/${devname}6`)
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))