Compare commits

...

13 Commits

Author SHA1 Message Date
Ettore Di Giacinto
361e1ab817 Merge pull request #7 from kairos-io/fix/umount
Do not umount oem if we have found it out
2022-11-23 16:25:16 +01:00
Ettore Di Giacinto
4234ae7034 Incorporating feedback review 2022-11-23 15:11:48 +00:00
Ettore Di Giacinto
193a7da9ee Update main.go
Co-authored-by: Dimitris Karakasilis <jimmykarily@gmail.com>
2022-11-23 16:05:44 +01:00
Ettore Di Giacinto
e0316ade0a Add earthly.sh 2022-11-23 15:16:04 +01:00
Ettore Di Giacinto
1c3a3ac510 Do not fail if we can't find a partition file 2022-11-23 14:09:44 +01:00
Ettore Di Giacinto
641fc6ffa7 Try to run kcrypt directly 2022-11-23 14:01:46 +01:00
Ettore Di Giacinto
214ae51333 Bind it to sysroot 2022-11-23 14:01:46 +01:00
Ettore Di Giacinto
7f397a3459 Try to wait for oem mount instead 2022-11-23 14:01:46 +01:00
Ettore Di Giacinto
b3d7b86591 Do not umount oem if we have found it out
Signed-off-by: Ettore Di Giacinto <mudler@users.noreply.github.com>
2022-11-23 14:01:46 +01:00
Ettore Di Giacinto
24240b6421 Merge pull request #8 from kairos-io/dont-hide-errors
Don't hide there error when it's not about file not existing
2022-11-23 14:00:57 +01:00
Dimitris Karakasilis
bb144f204a Don't hide there error when it's not about file not existing
Signed-off-by: Dimitris Karakasilis <dimitris@karakasilis.me>
2022-11-23 14:53:02 +02:00
Dimitris Karakasilis
7d077c9353 Merge pull request #6 from kairos-io/dont-let-mapping-be-nil
Don't let the mapping be nil when reading an empty file
2022-11-16 09:30:31 +02:00
Dimitris Karakasilis
0e278a89f0 Don't let the mapping be nil when reading an empty file
e.g. when we first create it

because it throws this error:

```
panic: assignment to entry in nil map

goroutine 1 [running]:
github.com/kairos-io/kcrypt/pkg/partition_info.PartitionInfo.UpdateMapping({{0xa60325?, 0x18?}, 0x0?}, {0xc000302000?, 0x1?})
	/go/pkg/mod/github.com/kairos-io/kcrypt@v0.4.2/pkg/partition_info/partition_info.go:69 +0x99

```

Signed-off-by: Dimitris Karakasilis <dimitris@karakasilis.me>
2022-11-15 19:45:44 +02:00
8 changed files with 71 additions and 59 deletions

View File

@@ -7,31 +7,32 @@ GENERATOR_DIR="$2"
[ -z "$GENERATOR_DIR" ] && exit 1
[ -d "$GENERATOR_DIR" ] || mkdir "$GENERATOR_DIR"
if getargbool 0 rd.neednet; then
{
oem_label=$(getarg rd.cos.oemlabel=)
# See https://github.com/kairos-io/packages/blob/d12b12b043a71d8471454f7b4fc84c3181d2bf60/packages/system/dracut/immutable-rootfs/30cos-immutable-rootfs/cos-generator.sh#L29
{
echo "[Unit]"
echo "DefaultDependencies=no"
echo "Description=kcrypt online mount"
echo "Before=cos-immutable-rootfs.service"
echo "After=network-online.target"
echo "Conflicts=initrd-switch-root.target"
if getargbool 0 rd.neednet; then
echo "Wants=network-online.target"
echo "[Service]"
echo "Type=oneshot"
echo "RemainAfterExit=no"
echo "ExecStart=/sbin/kcrypt-mount-local"
} > "$GENERATOR_DIR"/kcrypt.service
else
{
echo "[Unit]"
echo "DefaultDependencies=no"
echo "After=network-online.target"
echo "Description=kcrypt online mount"
else
echo "Description=kcrypt mount"
echo "Before=cos-immutable-rootfs.service"
fi
# OEM is special as kcrypt plugins might need that in order to unlock other partitions and plugins can reside in /oem as well and kcrypt needs to find them
if [ -n "${oem_label}" ]; then
echo "After=oem.mount"
fi
echo "After=sysroot.mount"
echo "[Service]"
echo "Type=oneshot"
echo "RemainAfterExit=no"
echo "ExecStart=/sbin/kcrypt-mount-local"
} > "$GENERATOR_DIR"/kcrypt.service
fi
echo "ExecStart=/usr/bin/kcrypt unlock-all"
} > "$GENERATOR_DIR"/kcrypt.service
if [ ! -e "$GENERATOR_DIR/initrd-fs.target.requires/kcrypt.service" ]; then
mkdir -p "$GENERATOR_DIR"/initrd-fs.target.requires

View File

@@ -27,8 +27,7 @@ install() {
inst_multiple \
kcrypt
inst_script "${moddir}/mount-local.sh" "/sbin/kcrypt-mount-local"
#inst_hook pre-trigger 10 "$moddir/mount-local.sh"
inst_script "${moddir}/generator.sh" \
"${systemdutildir}/system-generators/dracut-kcrypt-generator"

View File

@@ -1,19 +0,0 @@
#!/bin/sh
# -*- mode: shell-script; indent-tabs-mode: nil; sh-basic-offset: 4; -*-
# ex: ts=8 sw=4 sts=4 et filetype=sh
type getarg > /dev/null 2>&1 || . /lib/dracut-lib.sh
PATH=/usr/sbin:/usr/bin:/sbin:/bin
OEM=$(blkid -L COS_OEM)
if [ "$OEM" != "" ]; then
mkdir /oem
mount $OEM /oem
fi
kcrypt unlock-all
if [ "$OEM" != "" ]; then
umount /oem
fi

3
earthly.sh Executable file
View File

@@ -0,0 +1,3 @@
#!/bin/bash
docker run --privileged -v /var/run/docker.sock:/var/run/docker.sock --rm -t -v $(pwd):/workspace -v earthly-tmp:/tmp/earthly:rw earthly/earthly:v0.6.21 --allow-privileged $@

14
main.go
View File

@@ -283,15 +283,22 @@ func unlockAll() error {
partitionInfo, _, err := pi.NewPartitionInfoFromFile(pi.DefaultPartitionInfoFile)
if err != nil {
return err
fmt.Printf("Warning: Partition file not found '%s' \n", pi.DefaultPartitionInfoFile)
}
block, err := ghw.Block()
if err == nil {
if err != nil {
fmt.Printf("Warning: Error reading partitions '%s \n", err.Error())
return nil
}
for _, disk := range block.Disks {
for _, p := range disk.Partitions {
if p.Type == "crypto_LUKS" {
if partitionInfo != nil {
p.Label = partitionInfo.LookupLabelForUUID(p.UUID)
}
fmt.Printf("Unmounted Luks found at '%s' LABEL '%s' \n", p.Name, p.Label)
err = multierror.Append(err, unlockDisk(p))
if err != nil {
@@ -301,8 +308,7 @@ func unlockAll() error {
}
}
}
}
return err
return nil
}
func main() {

View File

@@ -28,7 +28,7 @@ type Bus struct {
func (b *Bus) LoadProviders() {
wd, _ := os.Getwd()
b.Manager.Autoload("kcrypt-discovery", "/system/discovery", "/oem/kcrypt", "/oem/system/discovery", wd).Register()
b.Manager.Autoload("kcrypt-discovery", "/sysroot/system/discovery", "/system/discovery", "/oem/kcrypt", "/oem/system/discovery", wd).Register()
}
func (b *Bus) Initialize() {

View File

@@ -71,6 +71,10 @@ func (pi PartitionInfo) UpdateMapping(partitionData string) error {
return pi.save()
}
func (pi PartitionInfo) IsMappingNil() bool {
return pi.mapping == nil
}
func (pi PartitionInfo) save() error {
data, err := yaml.Marshal(&pi.mapping)
if err != nil {
@@ -107,6 +111,9 @@ func ParsePartitionInfoFile(file string) (map[string]string, error) {
if err != nil {
return result, errors.Wrap(err, "unmarshalling partition info file")
}
if result == nil {
result = map[string]string{}
}
return result, nil
}
@@ -120,6 +127,8 @@ func createInfoFileIfNotExists(fileName string) (bool, error) {
return false, err
}
return false, nil
} else if err != nil {
return false, err
}
return true, nil
}

View File

@@ -38,13 +38,26 @@ var _ = Describe("Partition Info file parsing", func() {
fmt.Sprintf("partition-info-%d.yaml", time.Now().UnixNano()))
})
It("creates the file and returns 'false' and an (empty) mapping", func() {
When("there is some error other than the file doesn't exist", func() {
It("returns 'false' and the error", func() {
// We are trying to write to a path that doesn't exist (not the file, the path).
// https://stackoverflow.com/a/66808328
fileName = "\000x"
_, _, err := pi.NewPartitionInfoFromFile(fileName)
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("stat \000x: invalid argument"))
})
})
It("creates the file and returns 'false' and a non nil mapping", func() {
result, existed, err := pi.NewPartitionInfoFromFile(fileName)
Expect(err).ToNot(HaveOccurred())
Expect(result).ToNot(BeNil())
Expect(existed).To(BeFalse())
_, err = os.Stat(fileName)
Expect(err).ToNot(HaveOccurred())
Expect(result).ToNot(BeNil())
Expect(result.IsMappingNil()).To(BeFalse())
})
})
})