-
Notifications
You must be signed in to change notification settings - Fork 1
/
setup.go
616 lines (498 loc) · 22.8 KB
/
setup.go
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
package main
import (
"bytes"
"errors"
"io"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
"time"
"github.com/AspieSoft/go-regex-re2/v2"
"github.com/AspieSoft/goutil/bash"
"github.com/AspieSoft/goutil/fs/v2"
"github.com/AspieSoft/goutil/v7"
)
func setupInstall(diskParts diskPartList, cpu cpuType, tarName string) error {
waitToCool(false)
// setup fstab
err := setupFStab(diskParts)
if err != nil {
return err
}
installProgress += 1000
os.WriteFile("/mnt/gentoo/etc/profile.d/bash_ps.sh", regex.JoinBytes(
`if [ "$PS1" ]; then`, '\n',
` PS1="\[\e[m\][\[\e[1;32m\]\u@\h\[\e[m\]:\[\e[1;34m\]\W\[\e[m\]]\[\e[0;31m\](\$?)\[\e[1;0m\]\\$ \[\e[m\]"`, '\n',
` #PS1="\[\e[1;33m\][\@]${PS1}" # show clock`, '\n',
`fi`, '\n',
'\n',
`if [ "$EUID" = "0" -a "$(stat -c %d:%i /)" != "$(stat -c %d:%i /proc/1/root/. 2>/dev/null)" ]; then`, '\n',
` PS1="\[\e[1;35m\](chroot)\[\e[m\]${PS1}"`, '\n',
`fi`,
), 0644)
bash.Run([]string{`ln`, `-sr`, `../../profile.d/bash_ps.sh`, `bash_ps.sh`}, "/mnt/gentoo/etc/bash/bashrc.d", nil)
installProgress += 500
//todo: consider adding cpu specific flags (https://wiki.gentoo.org/wiki/Safe_CFLAGS#Intel)
// also remember the cpu_flags.yml file in the Empoleos-v5-gentoo project
// regex.RepFileStr("/mnt/gentoo/etc/portage/make.conf", `(?m)^COMMON_FLAGS="(.*)"$`, []byte(`COMMON_FLAGS="-march=native $1"`), false)
regex.Comp(`(?m)^COMMON_FLAGS="(.*)"$`).RepFileFunc("/mnt/gentoo/etc/portage/make.conf", func(data func(int) []byte) []byte {
flags := bytes.Split(data(1), []byte{' '})
if !goutil.Contains(flags, []byte("-march=native")) {
flags = append([][]byte{[]byte("-march=native")}, flags...)
}
for i, v := range flags {
if regex.Comp(`^-O[0-9]+$`).Match(v) {
//todo: remember to change this back to '-O2' after the install
flags[i] = []byte("-O3")
}
}
return regex.JoinBytes(`COMMON_FLAGS="`, bytes.Join(flags, []byte{' '}), '"')
}, false)
//todo: experiment with setting '-O3' in place of '-O2' to speed up the install
// note: this option can sometimes cause compile issues
installProgress += 500
var cpuCores uint64
if cpuInfo, err := bash.Run([]string{`lscpu`}, "", nil); err == nil {
regex.Comp(`(?mi)^\s*CPU(?:\(s\)|)\s*:\s*([0-9]+)\s*$`).RepFunc(cpuInfo, func(data func(int) []byte) []byte {
if i, err := strconv.Atoi(string(data(1))); err == nil {
cpuCores = uint64(i)
}
return nil
}, true)
/* regex.Comp(`(?mi)^\s*Thread(?:\(s\)|)\s+per\s+core(?:\(s\)|)\s*:\s*([0-9]+)\s*$`).RepFunc(cpuInfo, func(data func(int) []byte) []byte {
if i, err := strconv.Atoi(string(data(1))); err == nil {
cpuCores /= uint64(i)
}
return nil
}, true) */
if cpuCores == 0 {
regex.Comp(`(?mi)^\s*CPU(?:\(s\)|)\s+family\s*:\s*([0-9]+)\s*$`).RepFunc(cpuInfo, func(data func(int) []byte) []byte {
if i, err := strconv.Atoi(string(data(1))); err == nil {
cpuCores = uint64(i)
}
return nil
}, true)
}
// limit to 75% of the cpu threads
cpuCores = uint64(float64(cpuCores) * 0.75)
}
installProgress += 200
var memTotal uint64
if out, err := bash.Run([]string{`grep`, `MemTotal`, `/proc/meminfo`}, "/", nil); err == nil {
out = regex.Comp(`^.*?\s*([0-9]+)\s*([A-Za-z]*)\s*.*$`).RepStr(out, []byte("$1 $2"))
data := bytes.SplitN(out, []byte{' '}, 2)
if len(data) > 0 {
if len(data) == 1 {
data = append(data, []byte{'b'})
}
if mem, err := strconv.Atoi(string(data[0])); err == nil {
t := string(bytes.ToLower(data[1]))
if mbInd, err := goutil.IndexOf(dataSizeChars, "mb"); err == nil {
if i, err := goutil.IndexOf(dataSizeChars, t); err == nil {
for i > mbInd {
mem *= 1000
i--
}
for i < mbInd {
mem /= 1000
i++
}
if mem > 0 {
memTotal = uint64(mem)
}
}
}
}
}
memTotal /= 1000
memTotal++
}
installProgress += 200
if cpuCores == 0 || memTotal < cpuCores {
cpuCores = memTotal
}
if cpuCores != 0 {
setPortageOpt("MAKEOPT", regex.JoinBytes(`-j`, cpuCores, ` -l`, cpuCores), false)
setPortageOpt("EMERGE_DEFAULT_OPTS", regex.JoinBytes(`--jobs=`, cpuCores, ` --load-average=`, cpuCores), false)
}
//todo: remember to change this to '1' after the install
setPortageOpt("PORTAGE_NICENESS", []byte(`-2`), false)
setPortageOpt("FEATURES", []byte(`${FEATURES} parallel-fetch parallel-install sign`), true)
setPortageOpt("ACCEPT_KEYWORDS", []byte(`~amd64`), true)
setPortageOpt("ACCEPT_LICENSE", []byte(`*`), false)
installProgress += 200
// add use flags
// useFlags := []byte("boot gnuefi gcc cryptsetup curl openssl pcre policykit resolvconf seccomp sysv-utils tpm xfsprogs btrfs-progs device-mapper efiemu mount nls sdl truetype python -apparmor -qtwebenging -webenging")
// useFlags := []byte("boot gnuefi selinux gcc cryptsetup curl openssl pcre policykit resolvconf seccomp sysv-utils tpm xfsprogs btrfs-progs device-mapper efiemu mount nls sdl truetype python -apparmor -qtwebenging -webenging")
// useFlags := []byte("boot gnuefi selinux gcc make cmake cryptsetup curl openssl pcre policykit resolvconf seccomp sysv-utils tpm xfsprogs btrfs-progs device-mapper efiemu mount nls sdl dns network tools truetype overlay container-init git python python3 sql go nodejs java brotli gzip zlib dist-kernel kernel-open modules modules-sign -apparmor -qt5 -qtwayland -qtwebenging -webenging")
//todo: try without gcc use flag
// useFlags := []byte("boot gnuefi selinux gcc make cmake cryptsetup curl openssl pcre policykit resolvconf seccomp sysv-utils tpm xfsprogs btrfs-progs device-mapper efiemu mount nls sdl dns network tools truetype overlay container-init git python python3 sql go nodejs java brotli gzip zlib dist-kernel kernel-open modules modules-sign ffmpeg multimedia opengl video sound png jpeg webp webm weba mp3 mp4 ogg wav svg xml corefonts fontconfig -apparmor -qt5 -qtwayland -qtwebenging -webenging")
// useFlags := []byte("boot gnuefi selinux make cmake cryptsetup curl openssl pcre policykit resolvconf seccomp sysv-utils tpm xfsprogs btrfs-progs device-mapper efiemu mount nls sdl dns network tools truetype overlay container-init git python python3 sql go nodejs java brotli gzip zlib dist-kernel kernel-open modules modules-sign ffmpeg multimedia opengl video sound png jpeg webp webm weba mp3 mp4 ogg wav svg xml corefonts fontconfig -apparmor -qt5 -qtwayland -qtwebenging -webenging")
//? or try with rust
// useFlags := []byte("boot gnuefi selinux gcc make cmake rust cryptsetup curl openssl pcre policykit resolvconf seccomp sysv-utils tpm xfsprogs btrfs-progs device-mapper efiemu mount nls sdl dns network tools truetype overlay container-init git python python3 sql go nodejs java brotli gzip zlib dist-kernel kernel-open modules modules-sign ffmpeg multimedia opengl video sound png jpeg webp webm weba mp3 mp4 ogg wav svg xml corefonts fontconfig -apparmor -qt5 -qtwayland -qtwebenging -webenging")
//*added kernel-install
// useFlags := []byte("boot gnuefi selinux gcc make cmake rust cryptsetup curl openssl pcre policykit resolvconf seccomp sysv-utils tpm xfsprogs btrfs-progs device-mapper efiemu mount nls sdl dns network tools truetype overlay container-init git python python3 sql go nodejs java brotli gzip zlib kernel-install dist-kernel kernel-open modules modules-sign ffmpeg multimedia opengl video sound png jpeg webp webm weba mp3 mp4 ogg wav svg xml corefonts fontconfig -apparmor -qt5 -qtwayland -qtwebenging -webenging")
//*removed cmake gcc rust
// useFlags := []byte("boot gnuefi selinux make cryptsetup curl openssl pcre policykit resolvconf seccomp sysv-utils tpm xfsprogs btrfs-progs device-mapper efiemu mount nls sdl dns network tools truetype overlay container-init git python python3 sql go nodejs java brotli gzip zlib kernel-install dist-kernel kernel-open modules modules-sign ffmpeg multimedia opengl video sound png jpeg webp webm weba mp3 mp4 ogg wav svg xml corefonts fontconfig -apparmor -qt5 -qtwayland -qtwebenging -webenging")
//?removed kernel-install dist-kernel kernel-open modules modules-sign
// useFlags := []byte("boot gnuefi selinux make cryptsetup curl openssl pcre policykit resolvconf seccomp sysv-utils tpm xfsprogs btrfs-progs device-mapper efiemu mount nls sdl dns network tools truetype overlay container-init git python python3 sql go nodejs java brotli gzip zlib ffmpeg multimedia opengl video sound png jpeg webp webm weba mp3 mp4 ogg wav svg xml corefonts fontconfig -apparmor -qt5 -qtwayland -qtwebenging -webenging")
//?added -gcc -cmake
// useFlags := []byte("-gcc -cmake boot gnuefi selinux make cryptsetup curl openssl pcre policykit resolvconf seccomp sysv-utils tpm xfsprogs btrfs-progs device-mapper efiemu mount nls sdl dns network tools truetype overlay container-init git python python3 sql go nodejs java brotli gzip zlib kernel-install dist-kernel kernel-open modules modules-sign ffmpeg multimedia opengl video sound png jpeg webp webm weba mp3 mp4 ogg wav svg xml corefonts fontconfig -apparmor -qt5 -qtwayland -qtwebenging -webenging")
useFlags := regex.JoinBytes(
// core
// "boot kernel-install dist-kernel kernel-open modules modules-sign gnuefi efiemu mount nls sdl selinux hardened policykit resolvconf seccomp sysv-utils tpm xfsprogs btrfs-progs device-mapper tools truetype overlay container-init -apparmor -qt5 -qtwayland -qtwebenging -webenging",
"hardened nls sdl policykit resolvconf seccomp sysv-utils tpm xfsprogs btrfs-progs lvm tools truetype overlay container-init -qt -qt5 -qt6 -webengine",
// lang
// " gcc-symlinks make -cmake pcre curl git python python3 sql sqlite go nodejs java javascript ruby lua webkit docker",
" gcc-symlinks make -cmake pcre curl git python python3 sql sqlite go nodejs java javascript lua webkit docker",
// security
" cryptsetup openssl clamav ufw crypt ssl session strip tcmalloc tcpd gcr skey -telemetry -dmraid -device-mapper",
// performance
" smp sockets dri atm adns vc lto graphite ccache",
// net
" dns network ssh ftp sockets soap snmp ipv6 libwww sctp idn",
// tools
" utils encode emacs bash-completion geoip geolocation hddtemp scanner readline spell inotify fanotify dv",
// media
" ffmpeg multimedia opengl video sound png jpeg webp webm weba mp3 mp4 ogg wav svga xml corefonts fontconfig gif wavpack cgi fastcgi iconv aac flac tiff",
// compression
" brotli gzip zlib 7zip bzip2",
// other
" rpm symlink cxx cvs magic icu acpi djvu expat exif tidy xattr",
//! -tiff -emacs +qt6
//? -ruby
)
//todo: config clamav https://wiki.gentoo.org/wiki/ClamAV
//? secureboot
// jit javascript ruby lua webkit
// hardened clamav
// utils -svg svga
// magic cgi fastcgi
// crypt encode emacs bash-completion geoip geolocation hddtemp wavpack ssl
// scanner readline
// session strip symlink
// smp snmp soap sockets spell sqlite
// ftp cxx cvs gif iconv icu idn inotify ipv6 libwww
// dri atm adns acpi aac djvu expat exif dv
// flac sctp
// ssh tidy tiff xattr vc
//security: tcmalloc tcpd -telemetry gcr skey
//?desktop: dga calendar alsa accessibility aalib cups dvd dvdr jack lm-sensors portaudio samba screencast wifi wmf vulkan libnotify djvu ggi vorbis
//?desktop: wine-proton wine
/* if !installServer {
// useFlags = append(useFlags, []byte(" xorg x11extras wayland gui joystick dbus bluetooth printsupport location ffmpeg multimedia opengl video sound png jpeg webp mp3 mp4 ogg wav svg xml corefonts fontconfig nvidia-drivers nvenc nsplugin")...)
// useFlags = append(useFlags, []byte(" X xorg x11extras wayland xwayland gtk gui joystick dbus bluetooth printsupport location nvidia-drivers nvenc nsplugin")...)
//* added positioning
useFlags = append(useFlags, []byte(" X xorg x11extras wayland xwayland gtk gui joystick dbus bluetooth printsupport location positioning nvidia-drivers nvenc nsplugin")...)
//!Note: This TODO may be done
//todo: fix 'X gtk' USE flags not working
// may try adding flag '--autounmask-write' to emerge
// ACCEPT_KEYWORDS="~amd64"
// /etc/portage/package.accept_keywords
// X/X ~amd64
// x11-base/xorg-drivers x11-base/xorg-apps gui-libs/gtk
// dispatch-conf (u)
}else{
useFlags = append(useFlags, []byte(" monit nginx")...)
} */
if strings.Contains(tarName, "openrc") {
useFlags = append(useFlags, []byte(" -systemd")...)
}else if strings.Contains(tarName, "systemd") {
useFlags = append(useFlags, []byte(" systemd")...)
}
installProgress += 200
//todo: handle adding desktop flags (may add these later)
// useFlags = append(useFlags, []byte("dbus png jpeg webp gui gtk X libnotify -qt5 joystick opengl sound video bluetooth network multimedia printsupport location")...)
// consider adding -netboot to desktop
setPortageOpt("USE", useFlags, true)
installProgress += 200
// add grub platforms
/* grubPlatforms := []byte("emu efi-32 efi-64 pc")
hasOpt = false
regex.Comp(`(?m)^GRUB_PLATFORMS="(.*)"$`).RepFileFunc("/mnt/gentoo/etc/portage/make.conf", func(data func(int) []byte) []byte {
hasOpt = true
newPlat := bytes.Split(data(1), []byte{' '})
for _, val := range bytes.Split(grubPlatforms, []byte{' '}) {
if !goutil.Contains(newPlat, val) {
newPlat = append(newPlat, val)
}
}
return regex.JoinBytes(`GRUB_PLATFORMS="`, bytes.Join(newPlat, []byte{' '}), '"')
}, false)
if !hasOpt {
regex.Comp(`(?m)^USE=".*"$`).RepFileFunc("/mnt/gentoo/etc/portage/make.conf", func(data func(int) []byte) []byte {
hasOpt = true
return regex.JoinBytes(data(0), '\n', `GRUB_PLATFORMS="`, grubPlatforms, '"')
}, false)
}
if !hasOpt {
appendToFile("/mnt/gentoo/etc/portage/make.conf", regex.JoinBytes('\n', `GRUB_PLATFORMS="`, grubPlatforms, '"', '\n'))
} */
// copy dns info
bash.Run([]string{`cp`, `--dereference`, `/etc/resolv.conf`, `/mnt/gentoo/etc/`}, "", nil)
installProgress += 300
return nil
}
func setPortageOpt(opt string, val []byte, merge bool){
hasOpt := false
regex.Comp(`(?m)^%1="(.*)"$`, opt).RepFileFunc("/mnt/gentoo/etc/portage/make.conf", func(data func(int) []byte) []byte {
if hasOpt {
return data(0)
}
hasOpt = true
if merge {
newVal := bytes.Split(data(1), []byte{' '})
for _, val := range bytes.Split(val, []byte{' '}) {
if !goutil.Contains(newVal, val) {
newVal = append(newVal, val)
}
}
return bytes.Join(newVal, []byte{' '})
}
return regex.JoinBytes(opt, `="`, val, '"')
}, false)
for _, flag := range []string{`ACCEPT_LICENSE`, `MAKEOPT`, `FFLAGS`, `FCFLAGS`, `COMMON_FLAGS`} {
if hasOpt {
break
}
regex.Comp(`(?m)^%1=".*"$`, flag).RepFileFunc("/mnt/gentoo/etc/portage/make.conf", func(data func(int) []byte) []byte {
if hasOpt {
return data(0)
}
hasOpt = true
return regex.JoinBytes(data(0), '\n', opt, `="`, val, '"')
}, false)
}
if !hasOpt {
appendToFile("/mnt/gentoo/etc/portage/make.conf", regex.JoinBytes('\n', opt, `="`, val, '"', '\n'))
}
}
func setupFStab(diskParts diskPartList) error {
fstab := regex.JoinBytes(
`# /etc/fstab: static file system information.`, '\n',
`#`, '\n',
`# See the manpage fstab(5) for more information.`, '\n',
'\n',
)
bootUUID, err := bash.RunRaw(`lsblk /dev/`+diskParts.boot+` -lino UUID | head -n1`, "", nil)
if err != nil || len(bootUUID) == 0 {
return errors.New("error: failed to find uuid of boot partition")
}
fstab = regex.JoinBytes(fstab, `UUID=`, bytes.Trim(bootUUID, " \r\n"), "\t\t\t\t\t", `/boot`, "\t\t", `vfat`, "\t\t", `noatime`, "\t\t", `0 2`, '\n')
if diskParts.swap != "" {
swapUUID, err := bash.RunRaw(`lsblk /dev/`+diskParts.swap+` -lino UUID | head -n1`, "", nil)
if err != nil || len(swapUUID) == 0 {
return errors.New("error: failed to find uuid of swap partition")
}
fstab = regex.JoinBytes(fstab, `UUID=`, bytes.Trim(swapUUID, " \r\n"), '\t', `none`, "\t\t", `swap`, "\t\t", `sw`, "\t\t", `0 0`, '\n')
}
rootUUID, err := bash.RunRaw(`lsblk /dev/`+diskParts.root+` -lino UUID | head -n1`, "", nil)
if err != nil || len(rootUUID) == 0 {
return errors.New("error: failed to find uuid of root partition")
}
fstab = regex.JoinBytes(fstab, `UUID=`, bytes.Trim(rootUUID, " \r\n"), '\t', `/`, "\t\t", `xfs`, "\t\t", `noatime`, "\t\t", `0 1`, '\n')
if diskParts.home != "" {
homeUUID, err := bash.RunRaw(`lsblk /dev/`+diskParts.home+` -lino UUID | head -n1`, "", nil)
if err != nil || len(homeUUID) == 0 {
return errors.New("error: failed to find uuid of home partition")
}
fstab = regex.JoinBytes(fstab, `UUID=`, bytes.Trim(homeUUID, " \r\n"), '\t', `/var`, "\t\t", `btrfs`, "\t\t", `noatime`, "\t\t", `0 2`, '\n')
}
if diskParts.games != "" {
gamesUUID, err := bash.RunRaw(`lsblk /dev/`+diskParts.games+` -lino UUID | head -n1`, "", nil)
if err != nil || len(gamesUUID) == 0 {
return errors.New("error: failed to find uuid of games partition")
}
fstab = regex.JoinBytes(fstab, `UUID=`, bytes.Trim(gamesUUID, " \r\n"), '\t', `/games`, "\t\t", `ext4`, "\t\t", `noatime`, "\t\t", `0 2`, '\n')
}
if diskParts.home != "" {
cacheSize := "2G"
if out, err := bash.Run([]string{`lsblk`, `-linbo`, `size`, `/dev/`+diskParts.home}, "", nil); err == nil {
if size := goutil.Conv.ToUint(out); size != 0 {
size /= 1000 // bytes to kilobytes
size /= 1000 // kilobytes to megabytes
if size > 128000 {
cacheSize = "16G"
}else if size > 64000 {
cacheSize = "8G"
}else if size > 32000 {
cacheSize = "4G"
}else if size < 32000 {
cacheSize = ""
}
}
}
if cacheSize != "" {
fstab = append(fstab, []byte("\ntmpfs /tmp tmpfs rw,nosuid,noatime,nodev,size=16G,mode=1777 0 0\n")...)
}
}
err = os.WriteFile("/mnt/gentoo/etc/fstab", fstab, 0644)
if err != nil {
return errors.New(`error: failed to write fstab file`)
}
return nil
}
func setupChroot(diskParts diskPartList, locale localeInfo, cpu cpuType, installDisk string, tarName string) error {
waitToCool(false)
// get root dir
rootDir, err := filepath.Abs(".")
if err != nil {
rootDir = "."
}
os.MkdirAll("/mnt/gentoo/gentoo-installer", 0644)
_, err = fs.Copy("gentoo-installer", "/mnt/gentoo/gentoo-installer/gentoo-installer")
if err != nil {
return err
}
installProgress += 100
_, err = bash.Run([]string{`cp`, `-r`, `assets`, `/mnt/gentoo/gentoo-installer`}, rootDir, nil)
if err != nil {
return err
}
installProgress += 1000
json, err := goutil.JSON.Stringify(map[string]interface{}{
"installDisk": installDisk,
"cpuType": cpu.cpu,
"cpuType2": cpu.cpu2,
"cpuReal": cpu.real,
"timezone": locale.timezone,
"continent": locale.continent,
"locale": locale.locale,
"keymap": locale.keymap,
"tarName": tarName,
"disk_boot": diskParts.boot,
"disk_swap": diskParts.swap,
"disk_root": diskParts.root,
"disk_rootB": diskParts.rootB,
"disk_home": diskParts.home,
"disk_games": diskParts.games,
"disk_mem": diskParts.mem,
"installServer": installServer,
"installUSB": installUSB,
})
if err != nil {
return err
}
os.WriteFile("/mnt/gentoo/gentoo-installer/var.json", json, 0755)
installProgress += 500
_, err = bash.Run([]string{`chmod`, `+x`, `/mnt/gentoo/gentoo-installer/gentoo-installer`}, "", nil)
if err != nil {
return err
}
// use bash file for chroot to ensure bash environment is available for commands
err = os.WriteFile("/mnt/gentoo/gentoo-installer/run.sh", regex.JoinBytes(
`#!/bin/bash`, '\n',
'\n',
`/gentoo-installer/gentoo-installer --chroot`,
), 0755)
if err != nil {
return err
}
installProgress += 200
waitToCool(false)
// mounting necessary filesystems
bash.Run([]string{`mount`, `--types`, `proc`, `/proc`, `/mnt/gentoo/proc`}, "", nil)
bash.Run([]string{`mount`, `--rbind`, `/sys`, `/mnt/gentoo/sys`}, "", nil)
bash.Run([]string{`mount`, `--make-rslave`, `/mnt/gentoo/sys`}, "", nil)
bash.Run([]string{`mount`, `--rbind`, `/dev`, `/mnt/gentoo/dev`}, "", nil)
bash.Run([]string{`mount`, `--make-rslave`, `/mnt/gentoo/dev`}, "", nil)
bash.Run([]string{`mount`, `--bind`, `/run`, `/mnt/gentoo/run`}, "", nil)
bash.Run([]string{`mount`, `--make-slave`, `/mnt/gentoo/run`}, "", nil)
installProgress += 300
// for non-gentoo install media
bash.RunRaw(`test -L /dev/shm && rm /dev/shm && mkdir /dev/shm`, "", nil)
bash.RunRaw(`mount --types tmpfs --options nosuid,nodev,noexec shm /dev/shm`, "", nil)
bash.RunRaw(`chmod 1777 /dev/shm /run/shm`, "", nil)
installProgress += 200
waitToCool(false)
// enter new environment and run chroot script
cmd := exec.Command(`bash`, `-c`, `chroot /mnt/gentoo /bin/bash /gentoo-installer/run.sh`)
cmd.Dir = "/mnt/gentoo"
cmd.Stdin = os.Stdin
stdout, err := cmd.StdoutPipe()
if err != nil {
return err
}
defer stdout.Close()
var chErr error
running := true
go func(){
for {
buf := make([]byte, 1024)
n, err := stdout.Read(buf)
if err != nil {
chErr = err
running = false
break
}
buf = buf[:n]
buf = regex.Comp(`(?m)^CHROOT_PROGRESS:([A-Za-z0-9]+)\r?\n?`).RepFunc(buf, func(data func(int) []byte) []byte {
if i, err := strconv.ParseUint(string(data(1)), 36, 64); err == nil {
installProgress += uint(i)
}
return []byte{}
})
buf = regex.Comp(`(?m)^CHROOT_PROGRESS_RM:([A-Za-z0-9]+)\r?\n?`).RepFunc(buf, func(data func(int) []byte) []byte {
if i, err := strconv.ParseUint(string(data(1)), 36, 64); err == nil {
installProgress -= uint(i)
}
return []byte{}
})
buf = regex.Comp(`(?m)^CHROOT_PROGRESS_PRINT:([01])\r?\n?`).RepFunc(buf, func(data func(int) []byte) []byte {
if b := data(1); len(b) != 0 {
if b[0] == '1' {
installProgressPrintEnabled = true
}else if b[0] == '0' {
installProgressPrintEnabled = false
}
}
return []byte{}
})
if len(buf) != 0 || bytes.Equal(buf, []byte("\n")) || bytes.Equal(buf, []byte("\r\n")) {
logData(string(buf), true)
}
}
}()
err = cmd.Run()
if err != nil {
return err
}
for running {
time.Sleep(10 * time.Millisecond)
}
time.Sleep(10 * time.Millisecond)
if chErr != nil && chErr != io.EOF {
return chErr
}
stdout.Close()
waitToCool(false)
// after chroot exits
os.RemoveAll("/mnt/gentoo/gentoo-installer")
installProgress += 100
// unmount filesystems
bash.RunRaw(`umount -l /mnt/gentoo/dev{/shm,/pts,}`, "", nil)
bash.Run([]string{`umount`, `/mnt/gentoo/proc`}, "", nil)
bash.Run([]string{`umount`, `/mnt/gentoo/sys`}, "", nil)
bash.Run([]string{`umount`, `/mnt/gentoo/dev`}, "", nil)
bash.Run([]string{`umount`, `/mnt/gentoo/run`}, "", nil)
time.Sleep(1 * time.Second)
installProgress += 100
waitToCool(false)
// cache new build
/* bash.Run([]string{`tar`, `-cpf`, `cache/`+distroName+`.tar.xz`, `--xattrs-include='*.*'`, `--numeric-owner`, `--exclude=.cache`, `--exclude=.tmp`, `--exclude=.history`, `--exclude=lost+found`, `--exclude=/tmp/*`, `--exclude-caches-all`, `-C`, `/mnt/gentoo`, `.`}, rootDir, nil)
time.Sleep(1 * time.Second)
installProgress += 5000
// add checksum
if out, err := bash.RunRaw(`sha256sum `+distroName+`.tar.xz`, "cache", nil); err == nil && len(out) != 0 {
os.WriteFile("cache/"+distroName+".tar.xz.sum", out, 0644)
} */
time.Sleep(1 * time.Second)
installProgress += 500
waitToCool(false)
// unmount disk
unmountDisk(installDisk)
installProgress += 100
return nil
}