Skip to content

Commit f1b5b89

Browse files
mirkobrombinaxtloss
andcommittedMay 5, 2023
move mount points to a custom init and implement etc as a overlay, so the user can still change its content even with root set as read only
Co-authored-by: axtloss <[email protected]>
1 parent 3374a09 commit f1b5b89

File tree

3 files changed

+94
-2
lines changed

3 files changed

+94
-2
lines changed
 

‎core/integrity.go

+18
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ package core
1414
*/
1515

1616
import (
17+
"fmt"
1718
"os"
1819
"path/filepath"
1920

@@ -25,11 +26,17 @@ type IntegrityCheck struct {
2526
systemPath string
2627
standardLinks []string
2728
rootPaths []string
29+
etcPaths []string
2830
}
2931

3032
// NewIntegrityCheck creates a new IntegrityCheck instance
3133
func NewIntegrityCheck(root ABRootPartition, repair bool) (*IntegrityCheck, error) {
3234
systemPath := filepath.Join(root.Partition.MountPoint, "/.system")
35+
etcPath := filepath.Join("/var/lib/abroot/etc", root.IdentifiedAs)
36+
etcWorkPath := filepath.Join(
37+
"/var/lib/abroot/etc",
38+
fmt.Sprintf("%s-work", root.IdentifiedAs),
39+
)
3340
ic := &IntegrityCheck{
3441
rootPath: root.Partition.MountPoint,
3542
systemPath: systemPath,
@@ -61,6 +68,10 @@ func NewIntegrityCheck(root ABRootPartition, repair bool) (*IntegrityCheck, erro
6168
"/var",
6269
settings.Cnf.LibPathStates,
6370
},
71+
etcPaths: []string{
72+
etcPath,
73+
etcWorkPath,
74+
},
6475
}
6576

6677
if err := ic.check(repair); err != nil {
@@ -97,6 +108,13 @@ func (ic *IntegrityCheck) check(repair bool) error {
97108
}
98109
}
99110

111+
// check if etc paths exist
112+
for _, path := range ic.etcPaths {
113+
if !fileExists(path) {
114+
repairPaths = append(repairPaths, path)
115+
}
116+
}
117+
100118
if repair {
101119
for _, path := range repairPaths {
102120
PrintVerbose("IntegrityCheck: Repairing path %s", path)

‎core/system.go

+57-2
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ UUID=%s /var %s defaults 0 0
231231
/var/home /home x-systemd.after=/var bind 0 0
232232
/var/opt /opt x-systemd.after=/var bind 0 0
233233
/.system/usr /.system/usr none bind,ro 0 0
234-
}`
234+
`
235235
fstab := fmt.Sprintf(
236236
template,
237237
root.Partition.Uuid,
@@ -250,6 +250,55 @@ UUID=%s /var %s defaults 0 0
250250
return nil
251251
}
252252

253+
// GenerateSbinInit generates a usr/sbin/init file for the future root
254+
func (s *ABSystem) GenerateSbinInit(rootPath string, root ABRootPartition) error {
255+
PrintVerbose("ABSystem.GenerateSbinInit: generating init")
256+
257+
template := `#!/usr/bin/bash
258+
echo "ABRoot: Initializing mount points..."
259+
260+
# /var mount
261+
mount -U %s /var
262+
263+
# /etc overlay
264+
mount -t overlay overlay -o lowerdir=/.system/etc,upperdir=/var/lib/abroot/etc/%s,workdir=/var/lib/abroot/etc/%s-work /etc
265+
266+
# /var binds
267+
mount -o bind /var/home /home
268+
mount -o bind /var/opt /opt
269+
mount -o bind,ro /.system/usr /usr
270+
271+
echo "ABRoot: Starting systemd..."
272+
273+
# Start systemd
274+
exec /lib/systemd/systemd
275+
`
276+
277+
init := fmt.Sprintf(
278+
template,
279+
s.RootM.VarPartition.Uuid,
280+
root.Label,
281+
root.Label,
282+
)
283+
284+
os.Remove(rootPath + "/usr/sbin/init")
285+
286+
err := ioutil.WriteFile(rootPath+"/usr/sbin/init", []byte(init), 0755)
287+
if err != nil {
288+
PrintVerbose("ABSystem.GenerateSbinInit:err: %s", err)
289+
return err
290+
}
291+
292+
err = os.Chmod(rootPath+"/usr/sbin/init", 0755)
293+
if err != nil {
294+
PrintVerbose("ABSystem.GenerateSbinInit:err(2): %s", err)
295+
return err
296+
}
297+
298+
PrintVerbose("ABSystem.GenerateSbinInit: init generated")
299+
return nil
300+
}
301+
253302
// Upgrade upgrades the system to the latest available image
254303
func (s *ABSystem) Upgrade() error {
255304
PrintVerbose("ABSystem.Upgrade: starting upgrade")
@@ -353,7 +402,7 @@ func (s *ABSystem) Upgrade() error {
353402
return err
354403
}
355404

356-
// Stage 6: Generate /etc/fstab
405+
// Stage 6: Generate /etc/fstab and /usr/sbin/init
357406
// ------------------------------------------------
358407
PrintVerbose("[Stage 6] -------- ABSystemUpgrade")
359408

@@ -363,6 +412,12 @@ func (s *ABSystem) Upgrade() error {
363412
return err
364413
}
365414

415+
err = s.GenerateSbinInit(systemNew, partFuture)
416+
if err != nil {
417+
PrintVerbose("ABSystem.Upgrade:err(6.1): %s", err)
418+
return err
419+
}
420+
366421
// Stage 7: Update the bootloader
367422
// ------------------------------------------------
368423
PrintVerbose("[Stage 7] -------- ABSystemUpgrade")

‎samples/sbin/init

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/usr/bin/bash
2+
3+
echo "ABRoot: Initializing mount points..."
4+
5+
# /var mount
6+
mount -U a834618a-39a6-415a-b9a7-31d30f2db2e2 /var
7+
8+
# /etc overlay
9+
mount -t overlay overlay -o lowerdir=/.system/etc,upperdir=/var/lib/abroot/etc/a,workdir=/var/lib/abroot/etc/a-work /etc
10+
11+
# /var binds
12+
mount -o bind /var/home /home
13+
mount -o bind /var/opt /opt
14+
mount -o bind,ro /.system/usr /usr
15+
16+
echo "ABRoot: Starting systemd..."
17+
18+
# Start systemd
19+
exec /lib/systemd/systemd

0 commit comments

Comments
 (0)
Please sign in to comment.