@@ -65,12 +65,29 @@ func New() *Config {
6565}
6666
6767// ProbeWithCallback probes the sd-boot bootloader, and calls the callback function with the Config.
68+ //
69+ //nolint:gocyclo
6870func ProbeWithCallback (disk string , options options.ProbeOptions , callback func (* Config ) error ) (* Config , error ) {
6971 // if not UEFI boot, nothing to do
7072 if ! IsUEFIBoot () {
7173 return nil , nil
7274 }
7375
76+ // here we need to read the EFI vars to see if we have any defaults
77+ // and populate config accordingly
78+ // https://www.freedesktop.org/software/systemd/man/systemd-boot.html#LoaderEntryDefault
79+ // this should be set on install/upgrades
80+ efiCtx := efivario .NewDefaultContext ()
81+
82+ bootedEntry , err := ReadVariable (efiCtx , LoaderEntrySelectedName )
83+ if err != nil {
84+ return nil , err
85+ }
86+
87+ log .Printf ("booted entry: %q" , bootedEntry )
88+
89+ config := & Config {}
90+
7491 // read /boot/EFI and find if sd-boot is already being used
7592 // this is to make sure sd-boot from Talos is being used and not sd-boot from another distro
7693 if err := mount .PartitionOp (
@@ -93,6 +110,28 @@ func ProbeWithCallback(disk string, options options.ProbeOptions, callback func(
93110 return fmt .Errorf ("no boot*.efi files found in %q" , filepath .Join (constants .EFIMountPoint , "EFI" , "boot" ))
94111 }
95112
113+ // list existing UKIs, and check if the current one is present
114+ ukiFiles , err := filepath .Glob (filepath .Join (constants .EFIMountPoint , "EFI" , "Linux" , "Talos-*.efi" ))
115+ if err != nil {
116+ return err
117+ }
118+
119+ for _ , ukiFile := range ukiFiles {
120+ if strings .EqualFold (filepath .Base (ukiFile ), bootedEntry ) {
121+ config .Default = bootedEntry
122+ }
123+ }
124+
125+ // here we handle a case when we boot of just kernel+initrd/uki and we don't have a booted entry
126+ if bootedEntry == "" && len (ukiFiles ) == 1 {
127+ // we have only one UKI, so we can assume it's the default
128+ config .Default = filepath .Base (ukiFiles [0 ])
129+ }
130+
131+ if callback != nil {
132+ return callback (config )
133+ }
134+
96135 return nil
97136 },
98137 options .BlockProbeOptions ,
@@ -111,7 +150,7 @@ func ProbeWithCallback(disk string, options options.ProbeOptions, callback func(
111150 return nil , err
112151 }
113152
114- return & Config {} , nil
153+ return config , nil
115154}
116155
117156// Probe for existing sd-boot bootloader.
@@ -355,26 +394,13 @@ func (c *Config) Revert(disk string) error {
355394}
356395
357396func (c * Config ) revert () error {
358- // read current entry from EFI variables
359- efiCtx := efivario .NewDefaultContext ()
360-
361- // if we can't read the current entry, we can't revert
362- bootedEntry , err := ReadVariable (efiCtx , LoaderEntrySelectedName )
363- if err != nil {
364- return fmt .Errorf ("failed to read current UKI, cannot revert: %w" , err )
365- }
366-
367- if bootedEntry == "" {
368- return errors .New ("no UKI selected, nothing to revert" )
369- }
370-
371397 files , err := filepath .Glob (filepath .Join (constants .EFIMountPoint , "EFI" , "Linux" , "Talos-*.efi" ))
372398 if err != nil {
373399 return err
374400 }
375401
376402 for _ , file := range files {
377- if strings .EqualFold (filepath .Base (file ), bootedEntry ) {
403+ if strings .EqualFold (filepath .Base (file ), c . Default ) {
378404 continue
379405 }
380406
0 commit comments