Skip to contentSkip to conversation

Second Distro-agnostic series taken from Fedora Rawhide

PATCH Superseded Leo Sandoval 3 participants 21 messages Oct 11, 2024 – Oct 14, 2024 Kbuild

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover Second Distro-agnostic series taken from Fedora Rawhide

This is the second patch series, taken from Fedora Rawhide spec [1] that is distro-agnostic. The goal is to merge most of them so all the community/distros would benefit.

Changes since v1:
- delete 0024-efinet-and-bootp-add-support-for-dhcpv6.patch build issue, so for the moment discarting it

- delete 0025-bootp-New-net_bootp6-command.patch patch will be sent separately by mchang

- delete 0027-Make-grub_fatal-also-backtrace.patch not accepted

- delete 0028-Make-our-info-pages-say-grub2-where-appropriate.patch non-distro agnostic

- delete 0034-grub2-btrfs-04-grub2-install.patch patch will be revisited by author (mchang)

- delete 0036-grub2-btrfs-06-subvol-mount.patch not accepted

Andrei Borzenkov (1):
btrfs: Fallback to old subvol name scheme to support old snapshot
config

Jeff Mahoney (1):
btrfs: Add ability to boot from subvolumes

Josef Bacik (1):
net/tcp: add window scaling support

Michael Chang (4):
btrfs: export btrfs_subvol and btrfs_subvolid
btrfs: grub2-btrfs-03-follow_default
btdfs: grub2-btrfs-05-grub2-mkconfig
btrfs: Grub not working correctly with btrfs snapshots (bsc#1026511)

Peter Jones (6):
10_linux.in: Make grub2-mkconfig construct titles that look like the
ones we want elsewhere.
grub-get-kernel-settings.in: Add grub-get-kernel-settings and use it

    in 10_linux

30_os-prober.in: just build chainloader entries, don't try any xnu
xnu.
btrfs: fix a bad null check
efi: Add grub_efi_allocate_pool() and grub_efi_free_pool() wrappers.
chainloader: Use grub_efi_...() memory helpers where reasonable.

Robert Marshall (1):
grub-set-password.in: Add friendly grub2 password config tool

    (#985962)
 Makefile.util.def                             |  20 +
 configure.ac                                  |   2 +
 docs/man/grub-get-kernel-settings.h2m         |   2 +
 docs/man/grub-set-password.h2m                |   2 +
 grub-core/fs/btrfs.c                          | 895 +++++++++++++++++-
 grub-core/loader/efi/chainloader.c            |   4 +-
 grub-core/net/tcp.c                           |  42 +-
 include/grub/btrfs.h                          |   1 +
 include/grub/efi/efi.h                        |  36 +-
 .../bash-completion.d/grub-completion.bash.in |  22 +
 util/grub-get-kernel-settings.in              |  88 ++
 util/grub-mkconfig.in                         |   8 +-
 util/grub-mkconfig_lib.in                     |   4 +
 util/grub-set-password.in                     | 121 +++
 util/grub.d/00_header.in                      |  26 +-
 util/grub.d/01_users.in                       |  11 +
 util/grub.d/10_linux.in                       |  61 +-
 util/grub.d/20_linux_xen.in                   |   4 +
 util/grub.d/30_os-prober.in                   |  78 +-
 19 files changed, 1300 insertions(+), 127 deletions(-)
 create mode 100644 docs/man/grub-get-kernel-settings.h2m
 create mode 100644 docs/man/grub-set-password.h2m
 create mode 100644 util/grub-get-kernel-settings.in
 create mode 100644 util/grub-set-password.in
 create mode 100644 util/grub.d/01_users.in
Signature · 7 lines
2.46.2


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
#1 lore raw

PATCH v2 17/30 10_linux.in: Make grub2-mkconfig construct titles that look like the ones we want elsewhere.

From: Peter Jones <[email protected]>

Resolves: rhbz#1215839

---
 util/grub.d/10_linux.in | 34 +++++++++++++++++++++++++++-------
 1 file changed, 27 insertions(+), 7 deletions(-)
util/grub.d/10_linux.in +27 -7
index 3105c31e4..0115dd0c0 100644
@@ -80,6 +80,32 @@ case x"$GRUB_FS" in
 	;;
 esac
 
+mktitle ()
+{
+  local title_type
+  local version
+  local OS_NAME
+  local OS_VERS
+
+  title_type=$1 && shift
+  version=$1 && shift
+
+  OS_NAME="$(eval $(grep ^NAME= /etc/os-release) ; echo ${NAME})"
+  OS_VERS="$(eval $(grep ^VERSION= /etc/os-release) ; echo ${VERSION})"
+
+  case $title_type in
+    recovery)
+      title=$(printf '%s (%s) %s (recovery mode)' \
+                     "${OS_NAME}" "${version}" "${OS_VERS}")
+      ;;
+    *)
+      title=$(printf '%s (%s) %s' \
+                     "${OS_NAME}" "${version}" "${OS_VERS}")
+      ;;
+  esac
+  echo -n ${title}
+}
+
 title_correction_code=
 
 linux_entry ()
@@ -93,17 +119,11 @@ linux_entry ()
       boot_device_id="$(grub_get_device_id "${GRUB_DEVICE}")"
   fi
   if [ x$type != xsimple ] ; then
-      case $type in
-	  recovery)
-	      title="$(gettext_printf "%s, with Linux %s (recovery mode)" "${os}" "${version}")" ;;
-	  *)
-	      title="$(gettext_printf "%s, with Linux %s" "${os}" "${version}")" ;;
-      esac
+      title=$(mktitle "$type" "$version")
       if [ x"$title" = x"$GRUB_ACTUAL_DEFAULT" ] || [ x"Previous Linux versions>$title" = x"$GRUB_ACTUAL_DEFAULT" ]; then
 	  replacement_title="$(echo "Advanced options for ${OS}" | sed 's,>,>>,g')>$(echo "$title" | sed 's,>,>>,g')"
 	  quoted="$(echo "$GRUB_ACTUAL_DEFAULT" | grub_quote)"
 	  title_correction_code="${title_correction_code}if [ \"x\$default\" = '$quoted' ]; then default='$(echo "$replacement_title" | grub_quote)'; fi;"
-	  grub_warn "$(gettext_printf "Please don't use old title \`%s' for GRUB_DEFAULT, use \`%s' (for versions before 2.00) or \`%s' (for 2.00 or later)" "$GRUB_ACTUAL_DEFAULT" "$replacement_title" "gnulinux-advanced-$boot_device_id>gnulinux-$version-$type-$boot_device_id")"
       fi
       echo "menuentry '$(echo "$title" | grub_quote)' ${CLASS} \$menuentry_id_option 'gnulinux-$version-$type-$boot_device_id' {" | sed "s/^/$submenu_indentation/"
   else
Signature · 7 lines
2.46.2


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
#2 lore raw

PATCH v2 18/30 grub-set-password.in: Add friendly grub2 password config tool (#985962)

In reply to Leo Sandoval

From: Robert Marshall <[email protected]>

Provided a tool for users to reset the grub2 root user password without having to alter the grub.cfg. The hashed password now lives in a root-only-readable configuration file.

Resolves: rhbz#985962

[pjones: fix the efidir in grub-setpassword and rename tool]
[luto: fix grub-setpassword -o's output path]
[rharwood: migrate man page to h2m, context]
---
 Makefile.util.def              |  13 ++++
 configure.ac                   |   1 +
 docs/man/grub-set-password.h2m |   2 +
 util/grub-mkconfig.in          |   2 +
 util/grub-set-password.in      | 121 +++++++++++++++++++++++++++++++++
 util/grub.d/01_users.in        |  11 +++
 6 files changed, 150 insertions(+)
 create mode 100644 docs/man/grub-set-password.h2m
 create mode 100644 util/grub-set-password.in
 create mode 100644 util/grub.d/01_users.in
Makefile.util.def +13 -0
index 09bfcadd9..95884956a 100644
@@ -455,6 +455,12 @@ script = {
   installdir = grubconf;
 };
 
+script = {
+  name = '01_users';
+  common = util/grub.d/01_users.in;
+  installdir = grubconf;
+};
+
 script = {
   name = '10_windows';
   common = util/grub.d/10_windows.in;
@@ -733,6 +739,13 @@ script = {
   installdir = sbin;
 };
 
+script = {
+  name = grub-set-password;
+  common = util/grub-set-password.in;
+  mansection = 8;
+  installdir = sbin;
+};
+
 script = {
   name = grub-mkconfig_lib;
   common = util/grub-mkconfig_lib.in;
configure.ac +1 -0
index ae3a49321..493e336ec 100644
@@ -77,6 +77,7 @@ grub_TRANSFORM([grub-mkrelpath])
 grub_TRANSFORM([grub-mkrescue])
 grub_TRANSFORM([grub-probe])
 grub_TRANSFORM([grub-reboot])
+grub_TRANSFORM([grub-set-password])
 grub_TRANSFORM([grub-script-check])
 grub_TRANSFORM([grub-set-default])
 grub_TRANSFORM([grub-sparc64-setup])
docs/man/grub-set-password.h2m new file +2 -0
new file mode 100644
index 000000000..10ee82f4d
@@ -0,0 +1,2 @@
+[NAME]
+grub-set-password \- generate the user.cfg file containing the hashed grub bootloader password
util/grub-mkconfig.in +2 -0
index d1bf4983b..9d5d7964b 100644
@@ -283,6 +283,8 @@ for i in "${grub_mkconfig_dir}"/* ; do
     *~) ;;
     # emacsen autosave files. FIXME: support other editors
     */\#*\#) ;;
+    # rpm config files of yore.
+    *.rpmsave|*.rpmnew|*.rpmorig) ;;
     *)
       if grub_file_is_not_garbage "$i" && test -x "$i" ; then
         echo
util/grub-set-password.in new file +121 -0

121 lines not on this page · raw message

util/grub.d/01_users.in new file +11 -0
new file mode 100644
index 000000000..db2f44bfb
@@ -0,0 +1,11 @@
+#!/bin/sh -e
+cat << EOF
+if [ -f \${prefix}/user.cfg ]; then
+  source \${prefix}/user.cfg
+  if [ -n "\${GRUB2_PASSWORD}" ]; then
+    set superusers="root"
+    export superusers
+    password_pbkdf2 root \${GRUB2_PASSWORD}
+  fi
+fi
+EOF
Signature · 7 lines
2.46.2


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
#3 lore raw

PATCH v2 19/30 net/tcp: add window scaling support

In reply to Leo Sandoval

From: Josef Bacik <[email protected]>

Sometimes we have to provision boxes across regions, such as California to Sweden. The http server has a 10 minute timeout, so if we can't get our 250mb image transferred fast enough our provisioning fails, which is not ideal. So add tcp window scaling on open connections and set the window size to 1mb. With this change we're able to get higher sustained transfers between regions and can transfer our image in well below 10 minutes. Without this patch we'd time out every time halfway through the transfer. Thanks,

---
 grub-core/net/tcp.c | 42 +++++++++++++++++++++++++++++-------------
 1 file changed, 29 insertions(+), 13 deletions(-)
grub-core/net/tcp.c +29 -13
index 93dee0caa..902ab7e8a 100644
@@ -106,6 +106,18 @@ struct tcphdr
   grub_uint16_t urgent;
 } GRUB_PACKED;
 
+struct tcp_scale_opt {
+  grub_uint8_t kind;
+  grub_uint8_t length;
+  grub_uint8_t scale;
+} GRUB_PACKED;
+
+struct tcp_synhdr {
+  struct tcphdr tcphdr;
+  struct tcp_scale_opt scale_opt;
+  grub_uint8_t padding;
+};
+
 struct tcp_pseudohdr
 {
   grub_uint32_t src;
@@ -571,7 +583,7 @@ grub_net_tcp_open (char *server,
   grub_net_tcp_socket_t socket;
   static grub_uint16_t in_port = 21550;
   struct grub_net_buff *nb;
-  struct tcphdr *tcph;
+  struct tcp_synhdr *tcph;
   int i;
   grub_uint8_t *nbd;
   grub_net_link_level_address_t ll_target_addr;
@@ -640,20 +652,24 @@ grub_net_tcp_open (char *server,
     }
 
   tcph = (void *) nb->data;
+  grub_memset(tcph, 0, sizeof (*tcph));
   socket->my_start_seq = grub_get_time_ms ();
   socket->my_cur_seq = socket->my_start_seq + 1;
-  socket->my_window = 8192;
-  tcph->seqnr = grub_cpu_to_be32 (socket->my_start_seq);
-  tcph->ack = grub_cpu_to_be32_compile_time (0);
-  tcph->flags = grub_cpu_to_be16_compile_time ((5 << 12) | TCP_SYN);
-  tcph->window = grub_cpu_to_be16 (socket->my_window);
-  tcph->urgent = 0;
-  tcph->src = grub_cpu_to_be16 (socket->in_port);
-  tcph->dst = grub_cpu_to_be16 (socket->out_port);
-  tcph->checksum = 0;
-  tcph->checksum = grub_net_ip_transport_checksum (nb, GRUB_NET_IP_TCP,
-						   &socket->inf->address,
-						   &socket->out_nla);
+  socket->my_window = 32768;
+  tcph->tcphdr.seqnr = grub_cpu_to_be32 (socket->my_start_seq);
+  tcph->tcphdr.ack = grub_cpu_to_be32_compile_time (0);
+  tcph->tcphdr.flags = grub_cpu_to_be16_compile_time ((6 << 12) | TCP_SYN);
+  tcph->tcphdr.window = grub_cpu_to_be16 (socket->my_window);
+  tcph->tcphdr.urgent = 0;
+  tcph->tcphdr.src = grub_cpu_to_be16 (socket->in_port);
+  tcph->tcphdr.dst = grub_cpu_to_be16 (socket->out_port);
+  tcph->tcphdr.checksum = 0;
+  tcph->scale_opt.kind = 3;
+  tcph->scale_opt.length = 3;
+  tcph->scale_opt.scale = 5;
+  tcph->tcphdr.checksum = grub_net_ip_transport_checksum (nb, GRUB_NET_IP_TCP,
+							  &socket->inf->address,
+							  &socket->out_nla);
 
   tcp_socket_register (socket);
 
Signature · 7 lines
2.46.2


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
#4 lore raw

PATCH v2 20/30 grub-get-kernel-settings.in: Add grub-get-kernel-settings and use it in 10_linux

In reply to Leo Sandoval

From: Peter Jones <[email protected]>

This patch adds grub-get-kernel-settings, which reads the system kernel installation configuration from /etc/sysconfig/kernel, and outputs ${GRUB_...} variables suitable for evaluation by grub-mkconfig. Those variables are then used by 10_linux to choose whether or not to create debug stanzas.

Resolves: rhbz#1226325
[rharwood: migrate man page to h2m]

---
 Makefile.util.def                             |  7 ++
 configure.ac                                  |  1 +
 docs/man/grub-get-kernel-settings.h2m         |  2 +
 .../bash-completion.d/grub-completion.bash.in | 22 +++++
 util/grub-get-kernel-settings.in              | 88 +++++++++++++++++++
 util/grub-mkconfig.in                         |  3 +
 util/grub.d/10_linux.in                       | 23 +++--
 7 files changed, 141 insertions(+), 5 deletions(-)
 create mode 100644 docs/man/grub-get-kernel-settings.h2m
 create mode 100644 util/grub-get-kernel-settings.in
Makefile.util.def +7 -0
index 95884956a..84a17ef5c 100644
@@ -725,6 +725,13 @@ script = {
   installdir = sbin;
 };
 
+script = {
+  name = grub-get-kernel-settings;
+  common = util/grub-get-kernel-settings.in;
+  mansection = 3;
+  installdir = sbin;
+};
+
 script = {
   name = grub-set-default;
   common = util/grub-set-default.in;
configure.ac +1 -0
index 493e336ec..3602b4ab6 100644
@@ -70,6 +70,7 @@ grub_TRANSFORM([grub-install])
 grub_TRANSFORM([grub-mkconfig])
 grub_TRANSFORM([grub-mkfont])
 grub_TRANSFORM([grub-mkimage])
+grub_TRANSFORM([grub-get-kernel-settings])
 grub_TRANSFORM([grub-glue-efi])
 grub_TRANSFORM([grub-mklayout])
 grub_TRANSFORM([grub-mkpasswd-pbkdf2])
docs/man/grub-get-kernel-settings.h2m new file +2 -0
new file mode 100644
index 000000000..b8051f01f
@@ -0,0 +1,2 @@
+[NAME]
+grub-get-kernel-settings \- Evaluate the system's kernel installation settings for use while making a grub configuration file
util/bash-completion.d/grub-completion.bash.in +22 -0
index 213ce1e57..da7fca5ef 100644
@@ -275,6 +275,28 @@ have ${__grub_sparc64_setup_program} && \
 unset __grub_sparc64_setup_program
 
 
+#
+# grub-get-kernel-settings
+#
+_grub_get_kernel_settings () {
+    local cur
+
+    COMPREPLY=()
+    cur=`_get_cword`
+
+    if [[ "$cur" == -* ]]; then
+        __grubcomp "$(__grub_get_options_from_help)"
+    else
+        # Default complete with a filename
+        _filedir
+    fi
+}
+__grub_get_kernel_settings_program="@grub_get_kernel_settings@"
+have ${__grub_get_kernel_settings_program} && \
+ complete -F _grub_get_kernel_settings -o filenames ${__grub_get_kernel_settings_program}
+unset __grub_get_kernel_settings_program
+
+
 #
 # grub-install
 #
util/grub-get-kernel-settings.in new file +88 -0

88 lines not on this page · raw message

util/grub-mkconfig.in +3 -0
index 9d5d7964b..91f761331 100644
@@ -45,6 +45,7 @@ grub_probe="${sbindir}/@grub_probe@"
 grub_file="${bindir}/@grub_file@"
 grub_editenv="${bindir}/@grub_editenv@"
 grub_script_check="${bindir}/@grub_script_check@"
+grub_get_kernel_settings="${sbindir}/@grub_get_kernel_settings@"
 
 export TEXTDOMAIN=@PACKAGE@
 export TEXTDOMAINDIR="@localedir@"
@@ -161,6 +162,8 @@ if test -f ${sysconfdir}/default/grub ; then
   . ${sysconfdir}/default/grub
 fi
 
+eval "$("${grub_get_kernel_settings}")" || true
+
 if [ "x${GRUB_DISABLE_UUID}" = "xtrue" ]; then
   if [ -z "${GRUB_DISABLE_LINUX_UUID}" ]; then
     GRUB_DISABLE_LINUX_UUID="true"
util/grub.d/10_linux.in +18 -5
index 0115dd0c0..b94af4a8d 100644
@@ -113,7 +113,8 @@ linux_entry ()
   os="$1"
   version="$2"
   type="$3"
-  args="$4"
+  isdebug="$4"
+  args="$5"
 
   if [ -z "$boot_device_id" ]; then
       boot_device_id="$(grub_get_device_id "${GRUB_DEVICE}")"
@@ -125,6 +126,9 @@ linux_entry ()
 	  quoted="$(echo "$GRUB_ACTUAL_DEFAULT" | grub_quote)"
 	  title_correction_code="${title_correction_code}if [ \"x\$default\" = '$quoted' ]; then default='$(echo "$replacement_title" | grub_quote)'; fi;"
       fi
+      if [ x$isdebug = xdebug ]; then
+	  title="$title${GRUB_LINUX_DEBUG_TITLE_POSTFIX}"
+      fi
       echo "menuentry '$(echo "$title" | grub_quote)' ${CLASS} \$menuentry_id_option 'gnulinux-$version-$type-$boot_device_id' {" | sed "s/^/$submenu_indentation/"
   else
       echo "menuentry '$(echo "$os" | grub_quote)' ${CLASS} \$menuentry_id_option 'gnulinux-simple-$boot_device_id' {" | sed "s/^/$submenu_indentation/"
@@ -320,11 +324,15 @@ for linux in ${reverse_sorted_list}; do
   fi
 
   if [ "x$is_top_level" = xtrue ] && [ "x${GRUB_DISABLE_SUBMENU}" != xtrue ]; then
-    linux_entry "${OS}" "${version}" simple \
+    linux_entry "${OS}" "${version}" simple standard \
     "${GRUB_CMDLINE_LINUX} ${GRUB_CMDLINE_LINUX_DEFAULT}"
+    if [ "x$GRUB_LINUX_MAKE_DEBUG" = "xtrue" ]; then
+      linux_entry "${OS}" "${version}" simple debug \
+        "${GRUB_CMDLINE_LINUX} ${GRUB_CMDLINE_LINUX_DEFAULT} ${GRUB_CMDLINE_LINUX_DEBUG}"
+    fi
 
     submenu_indentation="$grub_tab"
-    
+
     if [ -z "$boot_device_id" ]; then
 	boot_device_id="$(grub_get_device_id "${GRUB_DEVICE}")"
     fi
@@ -333,10 +341,15 @@ for linux in ${reverse_sorted_list}; do
     is_top_level=false
   fi
 
-  linux_entry "${OS}" "${version}" advanced \
+  linux_entry "${OS}" "${version}" advanced standard \
               "${GRUB_CMDLINE_LINUX} ${GRUB_CMDLINE_LINUX_DEFAULT}"
+  if [ "x$GRUB_LINUX_MAKE_DEBUG" = "xtrue" ]; then
+    linux_entry "${OS}" "${version}" advanced debug \
+                "${GRUB_CMDLINE_LINUX} ${GRUB_CMDLINE_LINUX_DEFAULT} ${GRUB_CMDLINE_LINUX_DEBUG}"
+  fi
+
   if [ "x${GRUB_DISABLE_RECOVERY}" != "xtrue" ]; then
-    linux_entry "${OS}" "${version}" recovery \
+    linux_entry "${OS}" "${version}" recovery standard \
                 "${GRUB_CMDLINE_LINUX_RECOVERY} ${GRUB_CMDLINE_LINUX}"
   fi
 done
Signature · 7 lines
2.46.2


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
#5 lore raw

PATCH v2 21/30 30_os-prober.in: just build chainloader entries, don't try any xnu xnu.

In reply to Leo Sandoval

From: Peter Jones <[email protected]>

Since our bugs tell us that the xnu boot entries really just don't work most of the time, and they create piles of extra boot entries, because they can't quite figure out 32-vs-64 and other stuff like that.

It's rediculous, and we should just boot their bootloader through the chainloader instead.

So this patch does that.

Resolves: rhbz#893179

---
 util/grub.d/30_os-prober.in | 78 +++++++++----------------------------
 1 file changed, 18 insertions(+), 60 deletions(-)
util/grub.d/30_os-prober.in +18 -60
index 656301eaf..1aac097c1 100644
@@ -45,68 +45,25 @@ if [ -z "${OSPROBED}" ] ; then
 fi
 
 osx_entry() {
-    if [ x$2 = x32 ]; then
-        # TRANSLATORS: it refers to kernel architecture (32-bit)
-	bitstr="$(gettext "(32-bit)")"
-    else
-        # TRANSLATORS: it refers to kernel architecture (64-bit)
-	bitstr="$(gettext "(64-bit)")"
-    fi
     # TRANSLATORS: it refers on the OS residing on device %s
     onstr="$(gettext_printf "(on %s)" "${DEVICE}")"
-        cat << EOF
-menuentry '$(echo "${LONGNAME} $bitstr $onstr" | grub_quote)' --class osx --class darwin --class os \$menuentry_id_option 'osprober-xnu-$2-$(grub_get_device_id "${DEVICE}")'  {
+    hints=""
+    for hint in `"${grub_probe}" --device ${device} --target=efi_hints 2> /dev/null` ; do
+      hints="${hints} --hint=${hint}"
+    done
+    cat << EOF
+menuentry '$(echo "${LONGNAME} $onstr" | grub_quote)' --class osx --class darwin --class os \$menuentry_id_option 'osprober-xnu-$2-$(grub_get_device_id "${DEVICE}")'  {
 EOF
 	save_default_entry | grub_add_tab
 	prepare_grub_to_access_device ${DEVICE} | grub_add_tab
 	cat << EOF
+	set gfxpayload=keep
         load_video
-        set do_resume=0
-        if [ /var/vm/sleepimage -nt10 / ]; then
-           if xnu_resume /var/vm/sleepimage; then
-             set do_resume=1
-           fi
-        fi
-        if [ \$do_resume = 0 ]; then
-           xnu_uuid ${OSXUUID} uuid
-           if [ -f /Extra/DSDT.aml ]; then
-              acpi -e /Extra/DSDT.aml
-           fi
-           if [ /kernelcache -nt /System/Library/Extensions ]; then
-              $1 /kernelcache boot-uuid=\${uuid} rd=*uuid
-           elif [ -f /System/Library/Kernels/kernel ]; then
-              $1 /System/Library/Kernels/kernel boot-uuid=\${uuid} rd=*uuid
-              xnu_kextdir /System/Library/Extensions
-           else
-              $1 /mach_kernel boot-uuid=\${uuid} rd=*uuid
-              if [ /System/Library/Extensions.mkext -nt /System/Library/Extensions ]; then
-                xnu_mkext /System/Library/Extensions.mkext
-              else
-                xnu_kextdir /System/Library/Extensions
-              fi
-           fi
-           if [ -f /Extra/Extensions.mkext ]; then
-              xnu_mkext /Extra/Extensions.mkext
-           fi
-           if [ -d /Extra/Extensions ]; then
-              xnu_kextdir /Extra/Extensions
-           fi
-           if [ -f /Extra/devprop.bin ]; then
-              xnu_devprop_load /Extra/devprop.bin
-           fi
-           if [ -f /Extra/splash.jpg ]; then
-              insmod jpeg
-              xnu_splash /Extra/splash.jpg
-           fi
-           if [ -f /Extra/splash.png ]; then
-              insmod png
-              xnu_splash /Extra/splash.png
-           fi
-           if [ -f /Extra/splash.tga ]; then
-              insmod tga
-              xnu_splash /Extra/splash.tga
-           fi
-        fi
+	insmod part_gpt
+	insmod hfsplus
+	search --no-floppy --fs-uuid --set=root ${hints} $(grub_get_device_id "${DEVICE}")
+	chainloader (\$root)/System/Library/CoreServices/boot.efi
+	boot
 }
 EOF
 }
@@ -299,11 +256,12 @@ EOF
       echo "$title_correction_code"
     ;;
     macosx)
-      if [ "${UUID}" ]; then
-	OSXUUID="${UUID}"
-	osx_entry xnu_kernel 32
-	osx_entry xnu_kernel64 64
-      fi
+      for subdevice in ${DEVICE%[[:digit:]]*}* ; do
+	parttype="`"${grub_probe}" --device ${device} --target=gpt_parttype "${subdevice}" 2> /dev/null`"
+	if [[ "$parttype" = "426f6f74-0000-11aa-aa11-00306543ecac" ]]; then
+	  DEVICE="${subdevice}" osx_entry
+	fi
+      done
     ;;
     hurd)
       onstr="$(gettext_printf "(on %s)" "${DEVICE}")"
Signature · 7 lines
2.46.2


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
#6 lore raw

PATCH v2 22/30 btrfs: Add ability to boot from subvolumes

In reply to Leo Sandoval

From: Jeff Mahoney <[email protected]>

This patch adds the ability to specify a different root on a btrfs filesystem too boot from other than the default one.

btrfs-list-snapshots <dev> will list the subvolumes available on the filesystem.

set btrfs_subvol=<path> and set btrfs_subvolid=<subvolid> will specify which subvolume to use and any pathnames provided with either of those variables set will start using that root. If the subvolume or subvolume id doesn't exist, then an error case will result.

It is possible to boot into a separate GRUB instance by exporting the variable and loading the config file from the subvolume.

---
 grub-core/fs/btrfs.c | 554 +++++++++++++++++++++++++++++++++++++++++--
 include/grub/btrfs.h |   1 +
 2 files changed, 534 insertions(+), 21 deletions(-)
grub-core/fs/btrfs.c +533 -21
index ba0c58352..f14fe9c1b 100644
@@ -38,6 +38,9 @@
 #include <zstd.h>
 #include <grub/i18n.h>
 #include <grub/btrfs.h>
+#include <grub/command.h>
+#include <grub/env.h>
+#include <grub/extcmd.h>
 #include <grub/crypto.h>
 #include <grub/diskfilter.h>
 #include <grub/safemath.h>
@@ -79,9 +82,11 @@ struct grub_btrfs_superblock
   grub_uint64_t generation;
   grub_uint64_t root_tree;
   grub_uint64_t chunk_tree;
-  grub_uint8_t dummy2[0x20];
+  grub_uint8_t dummy2[0x18];
+  grub_uint64_t bytes_used;
   grub_uint64_t root_dir_objectid;
-  grub_uint8_t dummy3[0x41];
+  grub_uint64_t num_devices;
+  grub_uint8_t dummy3[0x39];
   struct grub_btrfs_device this_device;
   char label[0x100];
   grub_uint8_t dummy4[0x100];
@@ -121,6 +126,7 @@ struct grub_btrfs_data
   grub_uint64_t exttree;
   grub_size_t extsize;
   struct grub_btrfs_extent_data *extent;
+  grub_uint64_t fs_tree;
 };
 
 struct grub_btrfs_chunk_item
@@ -191,6 +197,14 @@ struct grub_btrfs_leaf_descriptor
   } *data;
 };
 
+struct grub_btrfs_root_ref
+{
+  grub_uint64_t dirid;
+  grub_uint64_t sequence;
+  grub_uint16_t name_len;
+  const char name[0];
+} __attribute__ ((packed));
+
 struct grub_btrfs_time
 {
   grub_int64_t sec;
@@ -236,6 +250,14 @@ struct grub_btrfs_extent_data
 
 #define GRUB_BTRFS_OBJECT_ID_CHUNK 0x100
 
+#define GRUB_BTRFS_ROOT_TREE_OBJECTID 1ULL
+#define GRUB_BTRFS_FS_TREE_OBJECTID 5ULL
+#define GRUB_BTRFS_ROOT_REF_KEY     156
+#define GRUB_BTRFS_ROOT_ITEM_KEY     132
+
+static grub_uint64_t btrfs_default_subvolid = 0;
+static char *btrfs_default_subvol = NULL;
+
 static grub_disk_addr_t superblock_sectors[] = { 64 * 2, 64 * 1024 * 2,
   256 * 1048576 * 2, 1048576ULL * 1048576ULL * 2
 };
@@ -1252,6 +1274,62 @@ grub_btrfs_read_logical (struct grub_btrfs_data *data, grub_disk_addr_t addr,
   return GRUB_ERR_NONE;
 }
 
+static grub_err_t
+get_fs_root(struct grub_btrfs_data *data, grub_uint64_t tree,
+            grub_uint64_t objectid, grub_uint64_t offset,
+            grub_uint64_t *fs_root);
+
+static grub_err_t
+lookup_root_by_id(struct grub_btrfs_data *data, grub_uint64_t id)
+{
+  grub_err_t err;
+  grub_uint64_t tree;
+
+  err = get_fs_root(data, data->sblock.root_tree, id, -1, &tree);
+  if (!err)
+    data->fs_tree = tree;
+  return err;
+}
+
+static grub_err_t
+find_path (struct grub_btrfs_data *data,
+	   const char *path, struct grub_btrfs_key *key,
+	   grub_uint64_t *tree, grub_uint8_t *type);
+
+static grub_err_t
+lookup_root_by_name(struct grub_btrfs_data *data, const char *path)
+{
+  grub_err_t err;
+  grub_uint64_t tree = 0;
+  grub_uint8_t type;
+  struct grub_btrfs_key key;
+
+  err = find_path (data, path, &key, &tree, &type);
+  if (err)
+      return grub_error(GRUB_ERR_FILE_NOT_FOUND, "couldn't locate %s\n", path);
+
+  if (key.object_id != grub_cpu_to_le64_compile_time (GRUB_BTRFS_OBJECT_ID_CHUNK) || tree == 0)
+    return grub_error(GRUB_ERR_BAD_FILE_TYPE, "%s: not a subvolume\n", path);
+
+  data->fs_tree = tree;
+  return GRUB_ERR_NONE;
+}
+
+static grub_err_t
+btrfs_handle_subvol(struct grub_btrfs_data *data __attribute__ ((unused)))
+{
+  if (btrfs_default_subvol)
+    return lookup_root_by_name(data, btrfs_default_subvol);
+
+  if (btrfs_default_subvolid)
+    return lookup_root_by_id(data, btrfs_default_subvolid);
+
+  data->fs_tree = 0;
+
+  return GRUB_ERR_NONE;
+}
+
+
 static struct grub_btrfs_data *
 grub_btrfs_mount (grub_device_t dev)
 {
@@ -1287,6 +1365,13 @@ grub_btrfs_mount (grub_device_t dev)
   data->devices_attached[0].dev = dev;
   data->devices_attached[0].id = data->sblock.this_device.device_id;
 
+  err = btrfs_handle_subvol (data);
+  if (err)
+    {
+      grub_free (data);
+      return NULL;
+    }
+
   return data;
 }
 
@@ -1784,6 +1869,91 @@ get_root (struct grub_btrfs_data *data, struct grub_btrfs_key *key,
   return GRUB_ERR_NONE;
 }
 
+static grub_err_t
+find_pathname(struct grub_btrfs_data *data, grub_uint64_t objectid,
+              grub_uint64_t fs_root, const char *name, char **pathname)
+{
+  grub_err_t err;
+  struct grub_btrfs_key key = {
+    .object_id = objectid,
+    .type = GRUB_BTRFS_ITEM_TYPE_INODE_REF,
+    .offset = 0,
+  };
+  struct grub_btrfs_key key_out;
+  struct grub_btrfs_leaf_descriptor desc;
+  char *p = grub_strdup (name);
+  grub_disk_addr_t elemaddr;
+  grub_size_t elemsize;
+  grub_size_t alloc = grub_strlen(name) + 1;
+
+  err = lower_bound(data, &key, &key_out, fs_root,
+                    &elemaddr, &elemsize, &desc, 0);
+  if (err)
+    return grub_error(err, "lower_bound caught %d\n", err);
+
+  if (key_out.type != GRUB_BTRFS_ITEM_TYPE_INODE_REF)
+    next(data, &desc, &elemaddr, &elemsize, &key_out);
+
+  if (key_out.type != GRUB_BTRFS_ITEM_TYPE_INODE_REF)
+    {
+      return grub_error(GRUB_ERR_FILE_NOT_FOUND,
+                        "Can't find inode ref for {%"PRIuGRUB_UINT64_T
+                        ", %u, %"PRIuGRUB_UINT64_T"} %"PRIuGRUB_UINT64_T
+                        "/%"PRIuGRUB_SIZE"\n",
+                        key_out.object_id, key_out.type,
+                        key_out.offset, elemaddr, elemsize);
+    }
+
+
+  while (key_out.type == GRUB_BTRFS_ITEM_TYPE_INODE_REF &&
+         key_out.object_id != key_out.offset) {
+    struct grub_btrfs_inode_ref *inode_ref;
+    char *new;
+
+    inode_ref = grub_malloc(elemsize + 1);
+    if (!inode_ref)
+      return grub_error(GRUB_ERR_OUT_OF_MEMORY,
+                        "couldn't allocate memory for inode_ref (%"PRIuGRUB_SIZE")\n", elemsize);
+
+    err = grub_btrfs_read_logical(data, elemaddr, inode_ref, elemsize, 0);
+    if (err)
+      return grub_error(err, "read_logical caught %d\n", err);
+
+    alloc += grub_le_to_cpu16 (inode_ref->n) + 2;
+    new = grub_malloc(alloc);
+    if (!new)
+      return grub_error(GRUB_ERR_OUT_OF_MEMORY,
+                        "couldn't allocate memory for name (%"PRIuGRUB_SIZE")\n", alloc);
+
+    grub_memcpy(new, inode_ref->name, grub_le_to_cpu16 (inode_ref->n));
+    if (p)
+      {
+        new[grub_le_to_cpu16 (inode_ref->n)] = '/';
+        grub_strcpy (new + grub_le_to_cpu16 (inode_ref->n) + 1, p);
+        grub_free(p);
+      }
+    else
+      new[grub_le_to_cpu16 (inode_ref->n)] = 0;
+    grub_free(inode_ref);
+
+    p = new;
+
+    key.object_id = key_out.offset;
+
+    err = lower_bound(data, &key, &key_out, fs_root, &elemaddr,
+                      &elemsize, &desc, 0);
+    if (err)
+      return grub_error(err, "lower_bound caught %d\n", err);
+
+    if (key_out.type != GRUB_BTRFS_ITEM_TYPE_INODE_REF)
+      next(data, &desc, &elemaddr, &elemsize, &key_out);
+
+  }
+
+  *pathname = p;
+  return 0;
+}
+
 static grub_err_t
 find_path (struct grub_btrfs_data *data,
 	   const char *path, struct grub_btrfs_key *key,
@@ -1802,14 +1972,26 @@ find_path (struct grub_btrfs_data *data,
   char *origpath = NULL;
   unsigned symlinks_max = 32;
 
-  err = get_root (data, key, tree, type);
-  if (err)
-    return err;
-
   origpath = grub_strdup (path);
   if (!origpath)
     return grub_errno;
 
+  if (data->fs_tree)
+    {
+      *type = GRUB_BTRFS_DIR_ITEM_TYPE_DIRECTORY;
+      *tree = data->fs_tree;
+      /* This is a tree root, so everything starts at objectid 256 */
+      key->object_id = grub_cpu_to_le64_compile_time (GRUB_BTRFS_OBJECT_ID_CHUNK);
+      key->type = GRUB_BTRFS_ITEM_TYPE_DIR_ITEM;
+      key->offset = 0;
+    }
+  else
+    {
+      err = get_root (data, key, tree, type);
+      if (err)
+	return err;
+    }
+
   while (1)
     {
       while (path[0] == '/')
@@ -1982,13 +2164,25 @@ find_path (struct grub_btrfs_data *data,
 	  path = path_alloc = tmp;
 	  if (path[0] == '/')
 	    {
-	      err = get_root (data, key, tree, type);
-	      if (err)
+	      if (data->fs_tree)
+		{
+		  *type = GRUB_BTRFS_DIR_ITEM_TYPE_DIRECTORY;
+		  *tree = data->fs_tree;
+		  /* This is a tree root, so everything starts at objectid 256 */
+		  key->object_id = grub_cpu_to_le64_compile_time (GRUB_BTRFS_OBJECT_ID_CHUNK);
+		  key->type = GRUB_BTRFS_ITEM_TYPE_DIR_ITEM;
+		  key->offset = 0;
+		}
+	      else
 		{
-		  grub_free (direl);
-		  grub_free (path_alloc);
-		  grub_free (origpath);
-		  return err;
+		  err = get_root (data, key, tree, type);
+		  if (err)
+                    {
+                      grub_free (direl);
+                      grub_free (path_alloc);
+                      grub_free (origpath);
+                      return err;
+                    }
 		}
 	    }
 	  continue;
@@ -2256,6 +2450,20 @@ grub_btrfs_read (grub_file_t file, char *buf, grub_size_t len)
 				 data->tree, file->offset, buf, len);
 }
 
+static char *
+btrfs_unparse_uuid(struct grub_btrfs_data *data)
+{
+  return  grub_xasprintf ("%04x%04x-%04x-%04x-%04x-%04x%04x%04x",
+			  grub_be_to_cpu16 (data->sblock.uuid[0]),
+			  grub_be_to_cpu16 (data->sblock.uuid[1]),
+			  grub_be_to_cpu16 (data->sblock.uuid[2]),
+			  grub_be_to_cpu16 (data->sblock.uuid[3]),
+			  grub_be_to_cpu16 (data->sblock.uuid[4]),
+			  grub_be_to_cpu16 (data->sblock.uuid[5]),
+			  grub_be_to_cpu16 (data->sblock.uuid[6]),
+			  grub_be_to_cpu16 (data->sblock.uuid[7]));
+}
+
 static grub_err_t
 grub_btrfs_uuid (grub_device_t device, char **uuid)
 {
@@ -2267,15 +2475,7 @@ grub_btrfs_uuid (grub_device_t device, char **uuid)
   if (!data)
     return grub_errno;
 
-  *uuid = grub_xasprintf ("%04x%04x-%04x-%04x-%04x-%04x%04x%04x",
-			  grub_be_to_cpu16 (data->sblock.uuid[0]),
-			  grub_be_to_cpu16 (data->sblock.uuid[1]),
-			  grub_be_to_cpu16 (data->sblock.uuid[2]),
-			  grub_be_to_cpu16 (data->sblock.uuid[3]),
-			  grub_be_to_cpu16 (data->sblock.uuid[4]),
-			  grub_be_to_cpu16 (data->sblock.uuid[5]),
-			  grub_be_to_cpu16 (data->sblock.uuid[6]),
-			  grub_be_to_cpu16 (data->sblock.uuid[7]));
+  *uuid = btrfs_unparse_uuid(data);
 
   grub_btrfs_unmount (data);
 
@@ -2396,6 +2596,242 @@ grub_btrfs_embed (grub_device_t device __attribute__ ((unused)),
 }
 #endif
 
+static grub_err_t
+grub_cmd_btrfs_info (grub_command_t cmd __attribute__ ((unused)), int argc,
+		     char **argv)
+{
+  grub_device_t dev;
+  char *devname;
+  struct grub_btrfs_data *data;
+  char *uuid;
+
+  if (argc < 1)
+    return grub_error (GRUB_ERR_BAD_ARGUMENT, "device name required");
+
+  devname = grub_file_get_device_name(argv[0]);
+
+  if (!devname)
+    return grub_errno;
+
+  dev = grub_device_open (devname);
+  grub_free (devname);
+  if (!dev)
+    return grub_errno;
+
+  data = grub_btrfs_mount (dev);
+  if (!data)
+    {
+      grub_device_close(dev);
+      return grub_error (GRUB_ERR_BAD_ARGUMENT, "failed to open fs");
+    }
+
+  if (data->sblock.label)
+    grub_printf("Label: '%s' ", data->sblock.label);
+  else
+    grub_printf("Label: none ");
+
+  uuid = btrfs_unparse_uuid(data);
+
+  grub_printf(" uuid: %s\n\tTotal devices %" PRIuGRUB_UINT64_T
+              " FS bytes used %" PRIuGRUB_UINT64_T "\n",
+	      uuid, grub_cpu_to_le64(data->sblock.num_devices),
+	      grub_cpu_to_le64(data->sblock.bytes_used));
+
+  grub_btrfs_unmount (data);
+
+  return 0;
+}
+
+static grub_err_t
+get_fs_root(struct grub_btrfs_data *data, grub_uint64_t tree,
+            grub_uint64_t objectid, grub_uint64_t offset,
+            grub_uint64_t *fs_root)
+{
+  grub_err_t err;
+  struct grub_btrfs_key key_in = {
+    .object_id = objectid,
+    .type = GRUB_BTRFS_ROOT_ITEM_KEY,
+    .offset = offset,
+  }, key_out;
+  struct grub_btrfs_leaf_descriptor desc;
+  grub_disk_addr_t elemaddr;
+  grub_size_t elemsize;
+  struct grub_btrfs_root_item ri;
+
+  err = lower_bound(data, &key_in, &key_out, tree,
+                    &elemaddr, &elemsize, &desc, 0);
+
+  if (err)
+    return err;
+
+  if (key_out.type != GRUB_BTRFS_ITEM_TYPE_ROOT_ITEM || elemaddr == 0)
+    return grub_error(GRUB_ERR_FILE_NOT_FOUND,
+                    N_("can't find fs root for subvol %"PRIuGRUB_UINT64_T"\n"),
+                    key_in.object_id);
+
+  err = grub_btrfs_read_logical (data, elemaddr, &ri, sizeof (ri), 0);
+  if (err)
+    return err;
+
+  *fs_root = ri.tree;
+
+  return GRUB_ERR_NONE;
+}
+
+static const struct grub_arg_option options[] = {
+  {"output", 'o', 0, N_("Output to a variable instead of the console."),
+   N_("VARNAME"), ARG_TYPE_STRING},
+  {"path-only", 'p', 0, N_("Show only the path of the subvolume."), 0, 0},
+  {"id-only", 'i', 0, N_("Show only the id of the subvolume."), 0, 0},
+  {0, 0, 0, 0, 0, 0}
+};
+
+static grub_err_t
+grub_cmd_btrfs_list_subvols (struct grub_extcmd_context *ctxt,
+			     int argc, char **argv)
+{
+  struct grub_btrfs_data *data;
+  grub_device_t dev;
+  char *devname;
+  grub_uint64_t tree;
+  struct grub_btrfs_key key_in = {
+    .object_id = grub_cpu_to_le64_compile_time (GRUB_BTRFS_FS_TREE_OBJECTID),
+    .type = GRUB_BTRFS_ROOT_REF_KEY,
+    .offset = 0,
+  }, key_out;
+  struct grub_btrfs_leaf_descriptor desc;
+  grub_disk_addr_t elemaddr;
+  grub_uint64_t fs_root = 0;
+  grub_size_t elemsize;
+  grub_size_t allocated = 0;
+  int r = 0;
+  grub_err_t err;
+  char *buf = NULL;
+  int print = 1;
+  int path_only = ctxt->state[1].set;
+  int num_only = ctxt->state[2].set;
+  char *varname = NULL;
+  char *output = NULL;
+
+  if (ctxt->state[0].set) {
+    varname = ctxt->state[0].arg;
+    print = 0;
+  }
+
+  if (argc < 1)
+    return grub_error (GRUB_ERR_BAD_ARGUMENT, "device name required");
+
+  devname = grub_file_get_device_name(argv[0]);
+  if (!devname)
+    return grub_errno;
+
+  dev = grub_device_open (devname);
+  grub_free (devname);
+  if (!dev)
+    return grub_errno;
+
+  data = grub_btrfs_mount(dev);
+  if (!data)
+    return grub_error (GRUB_ERR_BAD_ARGUMENT, "could not open device");
+
+  tree = data->sblock.root_tree;
+  err = get_fs_root(data, tree, grub_cpu_to_le64_compile_time (GRUB_BTRFS_FS_TREE_OBJECTID),
+                    0, &fs_root);
+  if (err)
+    goto out;
+
+  err = lower_bound(data, &key_in, &key_out, tree,
+                    &elemaddr, &elemsize, &desc, 0);
+
+  if (err)
+    {
+      grub_btrfs_unmount(data);
+      return err;
+    }
+
+  if (key_out.type != GRUB_BTRFS_ITEM_TYPE_ROOT_REF || elemaddr == 0)
+    {
+      r = next(data, &desc, &elemaddr, &elemsize, &key_out);
+    }
+
+  if (key_out.type != GRUB_BTRFS_ITEM_TYPE_ROOT_REF) {
+    err = GRUB_ERR_FILE_NOT_FOUND;
+    grub_error(GRUB_ERR_FILE_NOT_FOUND, N_("can't find root refs"));
+    goto out;
+  }
+
+  do
+    {
+      struct grub_btrfs_root_ref *ref;
+      char *p = NULL;
+
+      if (key_out.type != GRUB_BTRFS_ITEM_TYPE_ROOT_REF)
+        {
+          r = 0;
+          break;
+        }
+
+      if (elemsize > allocated)
+        {
+          grub_free(buf);
+          allocated = 2 * elemsize;
+          buf = grub_malloc(allocated + 1);
+          if (!buf)
+            {
+              r = -grub_errno;
+              break;
+            }
+        }
+      ref = (struct grub_btrfs_root_ref *)buf;
+
+      err = grub_btrfs_read_logical(data, elemaddr, buf, elemsize, 0);
+      if (err)
+        {
+          r = -err;
+          break;
+        }
+        buf[elemsize] = 0;
+
+      find_pathname(data, ref->dirid, fs_root, ref->name, &p);
+
+      if (print)
+        {
+          if (num_only)
+            grub_printf("ID %"PRIuGRUB_UINT64_T"\n", key_out.offset);
+          else if (path_only)
+            grub_printf("%s\n", p);
+          else
+            grub_printf("ID %"PRIuGRUB_UINT64_T" path %s\n", key_out.offset, p);
+        } else {
+          char *old = output;
+          if (num_only)
+            output = grub_xasprintf("%s%"PRIuGRUB_UINT64_T"\n",
+                                    old ?: "", key_out.offset);
+          else if (path_only)
+            output = grub_xasprintf("%s%s\n", old ?: "", p);
+          else
+            output = grub_xasprintf("%sID %"PRIuGRUB_UINT64_T" path %s\n",
+                                    old ?: "", key_out.offset, p);
+
+          if (old)
+            grub_free(old);
+        }
+
+      r = next(data, &desc, &elemaddr, &elemsize, &key_out);
+  } while(r > 0);
+
+  if (output)
+    grub_env_set(varname, output);
+
+out:
+  free_iterator(&desc);
+  grub_btrfs_unmount(data);
+
+  grub_device_close (dev);
+
+  return 0;
+}
+
 static struct grub_fs grub_btrfs_fs = {
   .name = "btrfs",
   .fs_dir = grub_btrfs_dir,
@@ -2411,12 +2847,88 @@ static struct grub_fs grub_btrfs_fs = {
 #endif
 };
 
+static grub_command_t cmd_info;
+static grub_extcmd_t cmd_list_subvols;
+
+static char *
+subvolid_set_env (struct grub_env_var *var __attribute__ ((unused)),
+                  const char *val)
+{
+  unsigned long long result = 0;
+
+  grub_errno = GRUB_ERR_NONE;
+  if (*val)
+    {
+      result = grub_strtoull(val, NULL, 10);
+      if (grub_errno)
+        return NULL;
+    }
+
+  grub_free (btrfs_default_subvol);
+  btrfs_default_subvol = NULL;
+  btrfs_default_subvolid = result;
+  return grub_strdup(val);
+}
+
+static const char *
+subvolid_get_env (struct grub_env_var *var __attribute__ ((unused)),
+                  const char *val __attribute__ ((unused)))
+{
+  if (btrfs_default_subvol)
+    return grub_xasprintf("subvol:%s", btrfs_default_subvol);
+  else if (btrfs_default_subvolid)
+    return grub_xasprintf("%"PRIuGRUB_UINT64_T, btrfs_default_subvolid);
+  else
+    return "";
+}
+
+static char *
+subvol_set_env (struct grub_env_var *var __attribute__ ((unused)),
+                const char *val)
+{
+  grub_free (btrfs_default_subvol);
+  btrfs_default_subvol = grub_strdup (val);
+  btrfs_default_subvolid = 0;
+  return grub_strdup(val);
+}
+
+static const char *
+subvol_get_env (struct grub_env_var *var __attribute__ ((unused)),
+                const char *val __attribute__ ((unused)))
+{
+  if (btrfs_default_subvol)
+    return btrfs_default_subvol;
+  else if (btrfs_default_subvolid)
+    return grub_xasprintf("subvolid:%" PRIuGRUB_UINT64_T,
+                          btrfs_default_subvolid);
+  else
+    return "";
+}
+
 GRUB_MOD_INIT (btrfs)
 {
   grub_fs_register (&grub_btrfs_fs);
+  cmd_info = grub_register_command("btrfs-info", grub_cmd_btrfs_info,
+				   "DEVICE",
+				   "Print BtrFS info about DEVICE.");
+  cmd_list_subvols = grub_register_extcmd("btrfs-list-subvols",
+					 grub_cmd_btrfs_list_subvols, 0,
+					 "[-p|-n] [-o var] DEVICE",
+					 "Print list of BtrFS subvolumes on "
+					 "DEVICE.", options);
+  grub_register_variable_hook ("btrfs_subvol", subvol_get_env,
+                               subvol_set_env);
+  grub_register_variable_hook ("btrfs_subvolid", subvolid_get_env,
+                               subvolid_set_env);
 }
 
 GRUB_MOD_FINI (btrfs)
 {
+  grub_register_variable_hook ("btrfs_subvol", NULL, NULL);
+  grub_register_variable_hook ("btrfs_subvolid", NULL, NULL);
+  grub_unregister_command (cmd_info);
+  grub_unregister_extcmd (cmd_list_subvols);
   grub_fs_unregister (&grub_btrfs_fs);
 }
+
+// vim: si et sw=2:
include/grub/btrfs.h +1 -0
index 9d93fb6c1..234ad9767 100644
@@ -29,6 +29,7 @@ enum
     GRUB_BTRFS_ITEM_TYPE_ROOT_ITEM = 0x84,
     GRUB_BTRFS_ITEM_TYPE_ROOT_BACKREF = 0x90,
     GRUB_BTRFS_ITEM_TYPE_DEVICE = 0xd8,
+    GRUB_BTRFS_ITEM_TYPE_ROOT_REF = 0x9c,
     GRUB_BTRFS_ITEM_TYPE_CHUNK = 0xe4
   };
 
Signature · 7 lines
2.46.2


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
#7 lore raw

PATCH v2 23/30 btrfs: fix a bad null check

In reply to Leo Sandoval

From: Peter Jones <[email protected]>

current gcc complains:

grub-core/fs/btrfs.c: In function ‘grub_cmd_btrfs_info’:
grub-core/fs/btrfs.c:2745:7: error: the comparison will always evaluate as ‘true’ for the address of ‘label’ will never be NULL [-Werror=address]
2745 | if (data->sblock.label)

        |       ^~~~
  grub-core/fs/btrfs.c:92:8: note: ‘label’ declared here
     92 |   char label[0x100];
        |        ^~~~~

cc1: all warnings being treated as errors

Obviously this check should be on the first data byte instead of the symbol itself.

---
 grub-core/fs/btrfs.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
grub-core/fs/btrfs.c +1 -1
index f14fe9c1b..8e2b1e9f7 100644
@@ -2625,7 +2625,7 @@ grub_cmd_btrfs_info (grub_command_t cmd __attribute__ ((unused)), int argc,
       return grub_error (GRUB_ERR_BAD_ARGUMENT, "failed to open fs");
     }
 
-  if (data->sblock.label)
+  if (data->sblock.label[0])
     grub_printf("Label: '%s' ", data->sblock.label);
   else
     grub_printf("Label: none ");
Signature · 7 lines
2.46.2


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
#8 lore raw

PATCH v2 24/30 btrfs: export btrfs_subvol and btrfs_subvolid

In reply to Leo Sandoval

From: Michael Chang <[email protected]>

We should export btrfs_subvol and btrfs_subvolid to have both visible to subsidiary configuration files loaded using configfile.

---
 grub-core/fs/btrfs.c | 2 ++
 1 file changed, 2 insertions(+)
grub-core/fs/btrfs.c +2 -0
index 8e2b1e9f7..14e38a4df 100644
@@ -2920,6 +2920,8 @@ GRUB_MOD_INIT (btrfs)
                                subvol_set_env);
   grub_register_variable_hook ("btrfs_subvolid", subvolid_get_env,
                                subvolid_set_env);
+  grub_env_export ("btrfs_subvol");
+  grub_env_export ("btrfs_subvolid");
 }
 
 GRUB_MOD_FINI (btrfs)
Signature · 7 lines
2.46.2


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
#9 lore raw

PATCH v2 25/30 btrfs: grub2-btrfs-03-follow_default

In reply to Leo Sandoval

From: Michael Chang <[email protected]>

---
 grub-core/fs/btrfs.c | 107 ++++++++++++++++++++++++++++++-------------
 1 file changed, 76 insertions(+), 31 deletions(-)
grub-core/fs/btrfs.c +76 -31
index 14e38a4df..d47f9ab03 100644
@@ -1335,6 +1335,7 @@ grub_btrfs_mount (grub_device_t dev)
 {
   struct grub_btrfs_data *data;
   grub_err_t err;
+  const char *relpath = grub_env_get ("btrfs_relative_path");
 
   if (!dev->disk)
     {
@@ -1365,11 +1366,14 @@ grub_btrfs_mount (grub_device_t dev)
   data->devices_attached[0].dev = dev;
   data->devices_attached[0].id = data->sblock.this_device.device_id;
 
-  err = btrfs_handle_subvol (data);
-  if (err)
+  if (relpath && (relpath[0] == '1' || relpath[0] == 'y'))
     {
-      grub_free (data);
-      return NULL;
+      err = btrfs_handle_subvol (data);
+      if (err)
+      {
+        grub_free (data);
+        return NULL;
+      }
     }
 
   return data;
@@ -1966,24 +1970,39 @@ find_path (struct grub_btrfs_data *data,
   grub_size_t allocated = 0;
   struct grub_btrfs_dir_item *direl = NULL;
   struct grub_btrfs_key key_out;
+  int follow_default;
   const char *ctoken;
   grub_size_t ctokenlen;
   char *path_alloc = NULL;
   char *origpath = NULL;
   unsigned symlinks_max = 32;
+  const char *relpath = grub_env_get ("btrfs_relative_path");
 
+  follow_default = 0;
   origpath = grub_strdup (path);
   if (!origpath)
     return grub_errno;
 
-  if (data->fs_tree)
+  if (relpath && (relpath[0] == '1' || relpath[0] == 'y'))
     {
-      *type = GRUB_BTRFS_DIR_ITEM_TYPE_DIRECTORY;
-      *tree = data->fs_tree;
-      /* This is a tree root, so everything starts at objectid 256 */
-      key->object_id = grub_cpu_to_le64_compile_time (GRUB_BTRFS_OBJECT_ID_CHUNK);
-      key->type = GRUB_BTRFS_ITEM_TYPE_DIR_ITEM;
-      key->offset = 0;
+      if (data->fs_tree)
+        {
+          *type = GRUB_BTRFS_DIR_ITEM_TYPE_DIRECTORY;
+          *tree = data->fs_tree;
+          /* This is a tree root, so everything starts at objectid 256 */
+          key->object_id = grub_cpu_to_le64_compile_time (GRUB_BTRFS_OBJECT_ID_CHUNK);
+          key->type = GRUB_BTRFS_ITEM_TYPE_DIR_ITEM;
+          key->offset = 0;
+        }
+      else
+        {
+          *type = GRUB_BTRFS_DIR_ITEM_TYPE_DIRECTORY;
+          *tree = data->sblock.root_tree;
+          key->object_id = data->sblock.root_dir_objectid;
+          key->type = GRUB_BTRFS_ITEM_TYPE_DIR_ITEM;
+          key->offset = 0;
+          follow_default = 1;
+        }
     }
   else
     {
@@ -1994,15 +2013,23 @@ find_path (struct grub_btrfs_data *data,
 
   while (1)
     {
-      while (path[0] == '/')
-	path++;
-      if (!path[0])
-	break;
-      slash = grub_strchr (path, '/');
-      if (!slash)
-	slash = path + grub_strlen (path);
-      ctoken = path;
-      ctokenlen = slash - path;
+      if (!follow_default)
+	{
+	  while (path[0] == '/')
+	    path++;
+	  if (!path[0])
+	    break;
+	  slash = grub_strchr (path, '/');
+	  if (!slash)
+	    slash = path + grub_strlen (path);
+	  ctoken = path;
+	  ctokenlen = slash - path;
+	}
+      else
+	{
+	  ctoken = "default";
+	  ctokenlen = sizeof ("default") - 1;
+	}
 
       if (*type != GRUB_BTRFS_DIR_ITEM_TYPE_DIRECTORY)
 	{
@@ -2013,7 +2040,9 @@ find_path (struct grub_btrfs_data *data,
 
       if (ctokenlen == 1 && ctoken[0] == '.')
 	{
-	  path = slash;
+	  if (!follow_default)
+	    path = slash;
+	  follow_default = 0;
 	  continue;
 	}
       if (ctokenlen == 2 && ctoken[0] == '.' && ctoken[1] == '.')
@@ -2044,8 +2073,9 @@ find_path (struct grub_btrfs_data *data,
 	  *type = GRUB_BTRFS_DIR_ITEM_TYPE_DIRECTORY;
 	  key->object_id = key_out.offset;
 
-	  path = slash;
-
+	  if (!follow_default)
+	    path = slash;
+	  follow_default = 0;
 	  continue;
 	}
 
@@ -2114,7 +2144,9 @@ find_path (struct grub_btrfs_data *data,
 	  return err;
 	}
 
-      path = slash;
+      if (!follow_default)
+	path = slash;
+      follow_default = 0;
       if (cdirel->type == GRUB_BTRFS_DIR_ITEM_TYPE_SYMLINK)
 	{
 	  struct grub_btrfs_inode inode;
@@ -2164,14 +2196,26 @@ find_path (struct grub_btrfs_data *data,
 	  path = path_alloc = tmp;
 	  if (path[0] == '/')
 	    {
-	      if (data->fs_tree)
+              if (relpath && (relpath[0] == '1' || relpath[0] == 'y'))
 		{
-		  *type = GRUB_BTRFS_DIR_ITEM_TYPE_DIRECTORY;
-		  *tree = data->fs_tree;
-		  /* This is a tree root, so everything starts at objectid 256 */
-		  key->object_id = grub_cpu_to_le64_compile_time (GRUB_BTRFS_OBJECT_ID_CHUNK);
-		  key->type = GRUB_BTRFS_ITEM_TYPE_DIR_ITEM;
-		  key->offset = 0;
+	          if (data->fs_tree)
+		    {
+		      *type = GRUB_BTRFS_DIR_ITEM_TYPE_DIRECTORY;
+		      *tree = data->fs_tree;
+		      /* This is a tree root, so everything starts at objectid 256 */
+		      key->object_id = grub_cpu_to_le64_compile_time (GRUB_BTRFS_OBJECT_ID_CHUNK);
+		      key->type = GRUB_BTRFS_ITEM_TYPE_DIR_ITEM;
+		      key->offset = 0;
+		    }
+		  else
+		    {
+	              *type = GRUB_BTRFS_DIR_ITEM_TYPE_DIRECTORY;
+	              *tree = data->sblock.root_tree;
+	              key->object_id = data->sblock.root_dir_objectid;
+	              key->type = GRUB_BTRFS_ITEM_TYPE_DIR_ITEM;
+	              key->offset = 0;
+	              follow_default = 1;
+		    }
 		}
 	      else
 		{
@@ -2922,6 +2966,7 @@ GRUB_MOD_INIT (btrfs)
                                subvolid_set_env);
   grub_env_export ("btrfs_subvol");
   grub_env_export ("btrfs_subvolid");
+  grub_env_export ("btrfs_relative_path");
 }
 
 GRUB_MOD_FINI (btrfs)
Signature · 7 lines
2.46.2


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
#10 lore raw

Re: [PATCH v2 25/30] btrfs: grub2-btrfs-03-follow_default

Quoted 5 lines · Leo Sandoval

From: Michael Chang <[email protected]>

This needs a real commit message describing what this does. Please work with Michael to come up with one. :)

Quoted 199 lines
---
 grub-core/fs/btrfs.c | 107 ++++++++++++++++++++++++++++++-------------
 1 file changed, 76 insertions(+), 31 deletions(-)
grub-core/fs/btrfs.c +76 -32
index 14e38a4df..d47f9ab03 100644
@@ -1335,6 +1335,7 @@ grub_btrfs_mount (grub_device_t dev)
 {
   struct grub_btrfs_data *data;
   grub_err_t err;
+  const char *relpath = grub_env_get ("btrfs_relative_path");
 
   if (!dev->disk)
     {
@@ -1365,11 +1366,14 @@ grub_btrfs_mount (grub_device_t dev)
   data->devices_attached[0].dev = dev;
   data->devices_attached[0].id = data->sblock.this_device.device_id;
 
-  err = btrfs_handle_subvol (data);
-  if (err)
+  if (relpath && (relpath[0] == '1' || relpath[0] == 'y'))
     {
-      grub_free (data);
-      return NULL;
+      err = btrfs_handle_subvol (data);
+      if (err)
+      {
+        grub_free (data);
+        return NULL;
+      }
     }
 
   return data;
@@ -1966,24 +1970,39 @@ find_path (struct grub_btrfs_data *data,
   grub_size_t allocated = 0;
   struct grub_btrfs_dir_item *direl = NULL;
   struct grub_btrfs_key key_out;
+  int follow_default;
   const char *ctoken;
   grub_size_t ctokenlen;
   char *path_alloc = NULL;
   char *origpath = NULL;
   unsigned symlinks_max = 32;
+  const char *relpath = grub_env_get ("btrfs_relative_path");
 
+  follow_default = 0;
   origpath = grub_strdup (path);
   if (!origpath)
     return grub_errno;
 
-  if (data->fs_tree)
+  if (relpath && (relpath[0] == '1' || relpath[0] == 'y'))
     {
-      *type = GRUB_BTRFS_DIR_ITEM_TYPE_DIRECTORY;
-      *tree = data->fs_tree;
-      /* This is a tree root, so everything starts at objectid 256 */
-      key->object_id = grub_cpu_to_le64_compile_time (GRUB_BTRFS_OBJECT_ID_CHUNK);
-      key->type = GRUB_BTRFS_ITEM_TYPE_DIR_ITEM;
-      key->offset = 0;
+      if (data->fs_tree)
+        {
+          *type = GRUB_BTRFS_DIR_ITEM_TYPE_DIRECTORY;
+          *tree = data->fs_tree;
+          /* This is a tree root, so everything starts at objectid 256 */
+          key->object_id = grub_cpu_to_le64_compile_time (GRUB_BTRFS_OBJECT_ID_CHUNK);
+          key->type = GRUB_BTRFS_ITEM_TYPE_DIR_ITEM;
+          key->offset = 0;
+        }
+      else
+        {
+          *type = GRUB_BTRFS_DIR_ITEM_TYPE_DIRECTORY;
+          *tree = data->sblock.root_tree;
+          key->object_id = data->sblock.root_dir_objectid;
+          key->type = GRUB_BTRFS_ITEM_TYPE_DIR_ITEM;
+          key->offset = 0;
+          follow_default = 1;
+        }
     }
   else
     {
@@ -1994,15 +2013,23 @@ find_path (struct grub_btrfs_data *data,
 
   while (1)
     {
-      while (path[0] == '/')
-       path++;
-      if (!path[0])
-       break;
-      slash = grub_strchr (path, '/');
-      if (!slash)
-       slash = path + grub_strlen (path);
-      ctoken = path;
-      ctokenlen = slash - path;
+      if (!follow_default)
+       {
+         while (path[0] == '/')
+           path++;
+         if (!path[0])
+           break;
+         slash = grub_strchr (path, '/');
+         if (!slash)
+           slash = path + grub_strlen (path);
+         ctoken = path;
+         ctokenlen = slash - path;
+       }
+      else
+       {
+         ctoken = "default";
+         ctokenlen = sizeof ("default") - 1;
+       }
 
       if (*type != GRUB_BTRFS_DIR_ITEM_TYPE_DIRECTORY)
        {
@@ -2013,7 +2040,9 @@ find_path (struct grub_btrfs_data *data,
 
       if (ctokenlen == 1 && ctoken[0] == '.')
        {
-         path = slash;
+         if (!follow_default)
+           path = slash;
+         follow_default = 0;
          continue;
        }
       if (ctokenlen == 2 && ctoken[0] == '.' && ctoken[1] == '.')
@@ -2044,8 +2073,9 @@ find_path (struct grub_btrfs_data *data,
          *type = GRUB_BTRFS_DIR_ITEM_TYPE_DIRECTORY;
          key->object_id = key_out.offset;
 
-         path = slash;
-
+         if (!follow_default)
+           path = slash;
+         follow_default = 0;
          continue;
        }
 
@@ -2114,7 +2144,9 @@ find_path (struct grub_btrfs_data *data,
          return err;
        }
 
-      path = slash;
+      if (!follow_default)
+       path = slash;
+      follow_default = 0;
       if (cdirel->type == GRUB_BTRFS_DIR_ITEM_TYPE_SYMLINK)
        {
          struct grub_btrfs_inode inode;
@@ -2164,14 +2196,26 @@ find_path (struct grub_btrfs_data *data,
          path = path_alloc = tmp;
          if (path[0] == '/')
            {
-             if (data->fs_tree)
+              if (relpath && (relpath[0] == '1' || relpath[0] == 'y'))
                {
-                 *type = GRUB_BTRFS_DIR_ITEM_TYPE_DIRECTORY;
-                 *tree = data->fs_tree;
-                 /* This is a tree root, so everything starts at objectid 256 */
-                 key->object_id = grub_cpu_to_le64_compile_time (GRUB_BTRFS_OBJECT_ID_CHUNK);
-                 key->type = GRUB_BTRFS_ITEM_TYPE_DIR_ITEM;
-                 key->offset = 0;
+                 if (data->fs_tree)
+                   {
+                     *type = GRUB_BTRFS_DIR_ITEM_TYPE_DIRECTORY;
+                     *tree = data->fs_tree;
+                     /* This is a tree root, so everything starts at objectid 256 */
+                     key->object_id = grub_cpu_to_le64_compile_time (GRUB_BTRFS_OBJECT_ID_CHUNK);
+                     key->type = GRUB_BTRFS_ITEM_TYPE_DIR_ITEM;
+                     key->offset = 0;
+                   }
+                 else
+                   {
+                     *type = GRUB_BTRFS_DIR_ITEM_TYPE_DIRECTORY;
+                     *tree = data->sblock.root_tree;
+                     key->object_id = data->sblock.root_dir_objectid;
+                     key->type = GRUB_BTRFS_ITEM_TYPE_DIR_ITEM;
+                     key->offset = 0;
+                     follow_default = 1;
+                   }
                }
              else
                {
@@ -2922,6 +2966,7 @@ GRUB_MOD_INIT (btrfs)
                                subvolid_set_env);
   grub_env_export ("btrfs_subvol");
   grub_env_export ("btrfs_subvolid");
+  grub_env_export ("btrfs_relative_path");
 }
 
 GRUB_MOD_FINI (btrfs)
--

2.46.2

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

Signature · 6 lines
真実はいつも一つ!/ Always, there's only one truth!

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
#11 lore raw

Re: [PATCH v2 25/30] btrfs: grub2-btrfs-03-follow_default

Quoted 9 lines · Neal Gompa
Quoted 5 lines · Leo Sandoval

From: Michael Chang <[email protected]>

This needs a real commit message describing what this does. Please work with Michael to come up with one. :)

I will provide the description to Leo. Same for other related patches.

Thanks,
Michael

Quoted 196 lines
Quoted 196 lines
---
 grub-core/fs/btrfs.c | 107 ++++++++++++++++++++++++++++++-------------
 1 file changed, 76 insertions(+), 31 deletions(-)
grub-core/fs/btrfs.c +76 -32
index 14e38a4df..d47f9ab03 100644
@@ -1335,6 +1335,7 @@ grub_btrfs_mount (grub_device_t dev)
 {
   struct grub_btrfs_data *data;
   grub_err_t err;
+  const char *relpath = grub_env_get ("btrfs_relative_path");
 
   if (!dev->disk)
     {
@@ -1365,11 +1366,14 @@ grub_btrfs_mount (grub_device_t dev)
   data->devices_attached[0].dev = dev;
   data->devices_attached[0].id = data->sblock.this_device.device_id;
 
-  err = btrfs_handle_subvol (data);
-  if (err)
+  if (relpath && (relpath[0] == '1' || relpath[0] == 'y'))
     {
-      grub_free (data);
-      return NULL;
+      err = btrfs_handle_subvol (data);
+      if (err)
+      {
+        grub_free (data);
+        return NULL;
+      }
     }
 
   return data;
@@ -1966,24 +1970,39 @@ find_path (struct grub_btrfs_data *data,
   grub_size_t allocated = 0;
   struct grub_btrfs_dir_item *direl = NULL;
   struct grub_btrfs_key key_out;
+  int follow_default;
   const char *ctoken;
   grub_size_t ctokenlen;
   char *path_alloc = NULL;
   char *origpath = NULL;
   unsigned symlinks_max = 32;
+  const char *relpath = grub_env_get ("btrfs_relative_path");
 
+  follow_default = 0;
   origpath = grub_strdup (path);
   if (!origpath)
     return grub_errno;
 
-  if (data->fs_tree)
+  if (relpath && (relpath[0] == '1' || relpath[0] == 'y'))
     {
-      *type = GRUB_BTRFS_DIR_ITEM_TYPE_DIRECTORY;
-      *tree = data->fs_tree;
-      /* This is a tree root, so everything starts at objectid 256 */
-      key->object_id = grub_cpu_to_le64_compile_time (GRUB_BTRFS_OBJECT_ID_CHUNK);
-      key->type = GRUB_BTRFS_ITEM_TYPE_DIR_ITEM;
-      key->offset = 0;
+      if (data->fs_tree)
+        {
+          *type = GRUB_BTRFS_DIR_ITEM_TYPE_DIRECTORY;
+          *tree = data->fs_tree;
+          /* This is a tree root, so everything starts at objectid 256 */
+          key->object_id = grub_cpu_to_le64_compile_time (GRUB_BTRFS_OBJECT_ID_CHUNK);
+          key->type = GRUB_BTRFS_ITEM_TYPE_DIR_ITEM;
+          key->offset = 0;
+        }
+      else
+        {
+          *type = GRUB_BTRFS_DIR_ITEM_TYPE_DIRECTORY;
+          *tree = data->sblock.root_tree;
+          key->object_id = data->sblock.root_dir_objectid;
+          key->type = GRUB_BTRFS_ITEM_TYPE_DIR_ITEM;
+          key->offset = 0;
+          follow_default = 1;
+        }
     }
   else
     {
@@ -1994,15 +2013,23 @@ find_path (struct grub_btrfs_data *data,
 
   while (1)
     {
-      while (path[0] == '/')
-       path++;
-      if (!path[0])
-       break;
-      slash = grub_strchr (path, '/');
-      if (!slash)
-       slash = path + grub_strlen (path);
-      ctoken = path;
-      ctokenlen = slash - path;
+      if (!follow_default)
+       {
+         while (path[0] == '/')
+           path++;
+         if (!path[0])
+           break;
+         slash = grub_strchr (path, '/');
+         if (!slash)
+           slash = path + grub_strlen (path);
+         ctoken = path;
+         ctokenlen = slash - path;
+       }
+      else
+       {
+         ctoken = "default";
+         ctokenlen = sizeof ("default") - 1;
+       }
 
       if (*type != GRUB_BTRFS_DIR_ITEM_TYPE_DIRECTORY)
        {
@@ -2013,7 +2040,9 @@ find_path (struct grub_btrfs_data *data,
 
       if (ctokenlen == 1 && ctoken[0] == '.')
        {
-         path = slash;
+         if (!follow_default)
+           path = slash;
+         follow_default = 0;
          continue;
        }
       if (ctokenlen == 2 && ctoken[0] == '.' && ctoken[1] == '.')
@@ -2044,8 +2073,9 @@ find_path (struct grub_btrfs_data *data,
          *type = GRUB_BTRFS_DIR_ITEM_TYPE_DIRECTORY;
          key->object_id = key_out.offset;
 
-         path = slash;
-
+         if (!follow_default)
+           path = slash;
+         follow_default = 0;
          continue;
        }
 
@@ -2114,7 +2144,9 @@ find_path (struct grub_btrfs_data *data,
          return err;
        }
 
-      path = slash;
+      if (!follow_default)
+       path = slash;
+      follow_default = 0;
       if (cdirel->type == GRUB_BTRFS_DIR_ITEM_TYPE_SYMLINK)
        {
          struct grub_btrfs_inode inode;
@@ -2164,14 +2196,26 @@ find_path (struct grub_btrfs_data *data,
          path = path_alloc = tmp;
          if (path[0] == '/')
            {
-             if (data->fs_tree)
+              if (relpath && (relpath[0] == '1' || relpath[0] == 'y'))
                {
-                 *type = GRUB_BTRFS_DIR_ITEM_TYPE_DIRECTORY;
-                 *tree = data->fs_tree;
-                 /* This is a tree root, so everything starts at objectid 256 */
-                 key->object_id = grub_cpu_to_le64_compile_time (GRUB_BTRFS_OBJECT_ID_CHUNK);
-                 key->type = GRUB_BTRFS_ITEM_TYPE_DIR_ITEM;
-                 key->offset = 0;
+                 if (data->fs_tree)
+                   {
+                     *type = GRUB_BTRFS_DIR_ITEM_TYPE_DIRECTORY;
+                     *tree = data->fs_tree;
+                     /* This is a tree root, so everything starts at objectid 256 */
+                     key->object_id = grub_cpu_to_le64_compile_time (GRUB_BTRFS_OBJECT_ID_CHUNK);
+                     key->type = GRUB_BTRFS_ITEM_TYPE_DIR_ITEM;
+                     key->offset = 0;
+                   }
+                 else
+                   {
+                     *type = GRUB_BTRFS_DIR_ITEM_TYPE_DIRECTORY;
+                     *tree = data->sblock.root_tree;
+                     key->object_id = data->sblock.root_dir_objectid;
+                     key->type = GRUB_BTRFS_ITEM_TYPE_DIR_ITEM;
+                     key->offset = 0;
+                     follow_default = 1;
+                   }
                }
              else
                {
@@ -2922,6 +2966,7 @@ GRUB_MOD_INIT (btrfs)
                                subvolid_set_env);
   grub_env_export ("btrfs_subvol");
   grub_env_export ("btrfs_subvolid");
+  grub_env_export ("btrfs_relative_path");
 }
 
 GRUB_MOD_FINI (btrfs)
--

2.46.2

Quoted 9 lines
Quoted 4 lines

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

Signature · 1 line
真実はいつも一つ!/ Always, there's only one truth!

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#12 lore raw

PATCH v2 26/30 btdfs: grub2-btrfs-05-grub2-mkconfig

In reply to Leo Sandoval

From: Michael Chang <[email protected]>

---
 util/grub-mkconfig.in       |  3 ++-
 util/grub-mkconfig_lib.in   |  4 ++++
 util/grub.d/00_header.in    | 26 +++++++++++++++++++++++++-
 util/grub.d/10_linux.in     |  4 ++++
 util/grub.d/20_linux_xen.in |  4 ++++
 5 files changed, 39 insertions(+), 2 deletions(-)
util/grub-mkconfig.in +2 -1
index 91f761331..ad5b778e3 100644
@@ -259,7 +259,8 @@ export GRUB_DEFAULT \
   GRUB_BADRAM \
   GRUB_OS_PROBER_SKIP_LIST \
   GRUB_DISABLE_SUBMENU \
-  GRUB_DEFAULT_DTB
+  GRUB_DEFAULT_DTB \
+  SUSE_BTRFS_SNAPSHOT_BOOTING
 
 if test "x${grub_cfg}" != "x"; then
   rm -f "${grub_cfg}.new"
util/grub-mkconfig_lib.in +4 -0
index 33e1750ae..0ba0e0e1c 100644
@@ -49,7 +49,11 @@ grub_warn ()
 
 make_system_path_relative_to_its_root ()
 {
+  if [ "x${SUSE_BTRFS_SNAPSHOT_BOOTING}" = "xtrue" ] ; then
+  "${grub_mkrelpath}" -r "$1"
+  else
   "${grub_mkrelpath}" "$1"
+  fi
 }
 
 is_path_readable_by_grub ()
util/grub.d/00_header.in +25 -1
index 6a316a5ba..4ed7975a9 100644
@@ -27,6 +27,15 @@ export TEXTDOMAINDIR="@localedir@"
 
 . "$pkgdatadir/grub-mkconfig_lib"
 
+if [ "x${SUSE_BTRFS_SNAPSHOT_BOOTING}" = "xtrue" ] &&
+   [ "x${GRUB_FS}" = "xbtrfs" ] ; then
+    cat <<EOF
+set btrfs_relative_path="y"
+export btrfs_relative_path
+EOF
+fi
+
+
 # Do this as early as possible, since other commands might depend on it.
 # (e.g. the `loadfont' command might need lvm or raid modules)
 for i in ${GRUB_PRELOAD_MODULES} ; do
@@ -43,7 +52,9 @@ if [ "x${GRUB_DEFAULT_BUTTON}" = "xsaved" ] ; then GRUB_DEFAULT_BUTTON='${saved_
 if [ "x${GRUB_TIMEOUT_BUTTON}" = "x" ] ; then GRUB_TIMEOUT_BUTTON="$GRUB_TIMEOUT" ; fi
 
 cat << EOF
-if [ -s \$prefix/grubenv ]; then
+if [ -f \${config_directory}/grubenv ]; then
+  load_env -f \${config_directory}/grubenv
+elif [ -s \$prefix/grubenv ]; then
   load_env
 fi
 EOF
@@ -354,3 +365,16 @@ fi
 if [ "x${GRUB_BADRAM}" != "x" ] ; then
   echo "badram ${GRUB_BADRAM}"
 fi
+
+if [ "x${SUSE_BTRFS_SNAPSHOT_BOOTING}" = "xtrue" ] &&
+   [ "x${GRUB_ENABLE_BLSCFG}" = "xtrue" ] &&
+   [ "x${GRUB_FS}" = "xbtrfs" ] ; then
+   # Note: No $snapshot_num on *read-only* rollback!  (bsc#901487)
+   cat <<EOF
+if [ -n "\$extra_cmdline" ]; then
+  submenu "Bootable snapshot #\$snapshot_num" {
+    menuentry "If OK, run 'snapper rollback' and reboot." { true; }
+  }
+fi
+EOF
+fi
util/grub.d/10_linux.in +4 -0
index b94af4a8d..192d7f594 100644
@@ -68,10 +68,14 @@ fi
 
 case x"$GRUB_FS" in
     xbtrfs)
+  if [ "x${SUSE_BTRFS_SNAPSHOT_BOOTING}" = "xtrue" ]; then
+  GRUB_CMDLINE_LINUX="${GRUB_CMDLINE_LINUX} \${extra_cmdline}"
+  else
 	rootsubvol="`make_system_path_relative_to_its_root /`"
 	rootsubvol="${rootsubvol#/}"
 	if [ "x${rootsubvol}" != x ]; then
 	    GRUB_CMDLINE_LINUX="rootflags=subvol=${rootsubvol} ${GRUB_CMDLINE_LINUX}"
+  fi
 	fi;;
     xzfs)
 	rpool=`${grub_probe} --device ${GRUB_DEVICE} --target=fs_label 2>/dev/null || true`
util/grub.d/20_linux_xen.in +4 -0
index 94dd8be13..a786c87ac 100644
@@ -75,10 +75,14 @@ fi
 
 case x"$GRUB_FS" in
     xbtrfs)
+  if [ "x${SUSE_BTRFS_SNAPSHOT_BOOTING}" = "xtrue" ]; then
+  GRUB_CMDLINE_LINUX="${GRUB_CMDLINE_LINUX} \${extra_cmdline}"
+  else
 	rootsubvol="`make_system_path_relative_to_its_root /`"
 	rootsubvol="${rootsubvol#/}"
 	if [ "x${rootsubvol}" != x ]; then
 	    GRUB_CMDLINE_LINUX="rootflags=subvol=${rootsubvol} ${GRUB_CMDLINE_LINUX}"
+  fi
 	fi;;
     xzfs)
 	rpool=`${grub_probe} --device ${GRUB_DEVICE} --target=fs_label 2>/dev/null || true`
Signature · 7 lines
2.46.2


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
#13 lore raw

Re: [PATCH v2 26/30] btdfs: grub2-btrfs-05-grub2-mkconfig

Quoted 4 lines · Leo Sandoval

From: Michael Chang <[email protected]>

This patch needs a real commit message. I know which series of patches this comes from, and they *all* need real commit messages. Can you and Michael work to figure out proper messages for these?

Quoted 13 lines
---
 util/grub-mkconfig.in       |  3 ++-
 util/grub-mkconfig_lib.in   |  4 ++++
 util/grub.d/00_header.in    | 26 +++++++++++++++++++++++++-
 util/grub.d/10_linux.in     |  4 ++++
 util/grub.d/20_linux_xen.in |  4 ++++
 5 files changed, 39 insertions(+), 2 deletions(-)
util/grub-mkconfig.in +0 -0
index 91f761331..ad5b778e3 100644

util/grub-mkconfig.in export GRUB_DEFAULT \ · line 264

Quoted 7 lines
util/grub-mkconfig.in +2 -1
@@ -259,7 +259,8 @@ export GRUB_DEFAULT \
   GRUB_BADRAM \
   GRUB_OS_PROBER_SKIP_LIST \
   GRUB_DISABLE_SUBMENU \
-  GRUB_DEFAULT_DTB
+  GRUB_DEFAULT_DTB \
+  SUSE_BTRFS_SNAPSHOT_BOOTING
 

This needs to drop the SUSE_ prefix for upstreaming. Same for the instances below.

Quoted 112 lines
util/grub-mkconfig.in +0 -0
 if test "x${grub_cfg}" != "x"; then
   rm -f "${grub_cfg}.new"
util/grub-mkconfig_lib.in +4 -0
index 33e1750ae..0ba0e0e1c 100644
@@ -49,7 +49,11 @@ grub_warn ()
 
 make_system_path_relative_to_its_root ()
 {
+  if [ "x${SUSE_BTRFS_SNAPSHOT_BOOTING}" = "xtrue" ] ; then
+  "${grub_mkrelpath}" -r "$1"
+  else
   "${grub_mkrelpath}" "$1"
+  fi
 }
 
 is_path_readable_by_grub ()
util/grub.d/00_header.in +25 -1
index 6a316a5ba..4ed7975a9 100644
@@ -27,6 +27,15 @@ export TEXTDOMAINDIR="@localedir@"
 
 . "$pkgdatadir/grub-mkconfig_lib"
 
+if [ "x${SUSE_BTRFS_SNAPSHOT_BOOTING}" = "xtrue" ] &&
+   [ "x${GRUB_FS}" = "xbtrfs" ] ; then
+    cat <<EOF
+set btrfs_relative_path="y"
+export btrfs_relative_path
+EOF
+fi
+
+
 # Do this as early as possible, since other commands might depend on it.
 # (e.g. the `loadfont' command might need lvm or raid modules)
 for i in ${GRUB_PRELOAD_MODULES} ; do
@@ -43,7 +52,9 @@ if [ "x${GRUB_DEFAULT_BUTTON}" = "xsaved" ] ; then GRUB_DEFAULT_BUTTON='${saved_
 if [ "x${GRUB_TIMEOUT_BUTTON}" = "x" ] ; then GRUB_TIMEOUT_BUTTON="$GRUB_TIMEOUT" ; fi
 
 cat << EOF
-if [ -s \$prefix/grubenv ]; then
+if [ -f \${config_directory}/grubenv ]; then
+  load_env -f \${config_directory}/grubenv
+elif [ -s \$prefix/grubenv ]; then
   load_env
 fi
 EOF
@@ -354,3 +365,16 @@ fi
 if [ "x${GRUB_BADRAM}" != "x" ] ; then
   echo "badram ${GRUB_BADRAM}"
 fi
+
+if [ "x${SUSE_BTRFS_SNAPSHOT_BOOTING}" = "xtrue" ] &&
+   [ "x${GRUB_ENABLE_BLSCFG}" = "xtrue" ] &&
+   [ "x${GRUB_FS}" = "xbtrfs" ] ; then
+   # Note: No $snapshot_num on *read-only* rollback!  (bsc#901487)
+   cat <<EOF
+if [ -n "\$extra_cmdline" ]; then
+  submenu "Bootable snapshot #\$snapshot_num" {
+    menuentry "If OK, run 'snapper rollback' and reboot." { true; }
+  }
+fi
+EOF
+fi
util/grub.d/10_linux.in +4 -0
index b94af4a8d..192d7f594 100644
@@ -68,10 +68,14 @@ fi
 
 case x"$GRUB_FS" in
     xbtrfs)
+  if [ "x${SUSE_BTRFS_SNAPSHOT_BOOTING}" = "xtrue" ]; then
+  GRUB_CMDLINE_LINUX="${GRUB_CMDLINE_LINUX} \${extra_cmdline}"
+  else
        rootsubvol="`make_system_path_relative_to_its_root /`"
        rootsubvol="${rootsubvol#/}"
        if [ "x${rootsubvol}" != x ]; then
            GRUB_CMDLINE_LINUX="rootflags=subvol=${rootsubvol} ${GRUB_CMDLINE_LINUX}"
+  fi
        fi;;
     xzfs)
        rpool=`${grub_probe} --device ${GRUB_DEVICE} --target=fs_label 2>/dev/null || true`
util/grub.d/20_linux_xen.in +4 -1
index 94dd8be13..a786c87ac 100644
@@ -75,10 +75,14 @@ fi
 
 case x"$GRUB_FS" in
     xbtrfs)
+  if [ "x${SUSE_BTRFS_SNAPSHOT_BOOTING}" = "xtrue" ]; then
+  GRUB_CMDLINE_LINUX="${GRUB_CMDLINE_LINUX} \${extra_cmdline}"
+  else
        rootsubvol="`make_system_path_relative_to_its_root /`"
        rootsubvol="${rootsubvol#/}"
        if [ "x${rootsubvol}" != x ]; then
            GRUB_CMDLINE_LINUX="rootflags=subvol=${rootsubvol} ${GRUB_CMDLINE_LINUX}"
+  fi
        fi;;
     xzfs)
        rpool=`${grub_probe} --device ${GRUB_DEVICE} --target=fs_label 2>/dev/null || true`
--

2.46.2

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

Signature · 6 lines
真実はいつも一つ!/ Always, there's only one truth!

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
#14 lore raw

PATCH v2 27/30 btrfs: Fallback to old subvol name scheme to support old snapshot config

In reply to Leo Sandoval

From: Andrei Borzenkov <[email protected]>

Ref: bsc#953538

---
 grub-core/fs/btrfs.c | 32 +++++++++++++++++++++++++++++++-
 1 file changed, 31 insertions(+), 1 deletion(-)
grub-core/fs/btrfs.c +31 -1
index d47f9ab03..c5b3fd116 100644
@@ -1315,11 +1315,41 @@ lookup_root_by_name(struct grub_btrfs_data *data, const char *path)
   return GRUB_ERR_NONE;
 }
 
+static grub_err_t
+lookup_root_by_name_fallback(struct grub_btrfs_data *data, const char *path)
+{
+  grub_err_t err;
+  grub_uint64_t tree = 0;
+  grub_uint8_t type;
+  struct grub_btrfs_key key;
+
+  err = find_path (data, path, &key, &tree, &type);
+  if (err)
+      return grub_error(GRUB_ERR_FILE_NOT_FOUND, "couldn't locate %s\n", path);
+
+  if (key.object_id != grub_cpu_to_le64_compile_time (GRUB_BTRFS_OBJECT_ID_CHUNK) || tree == 0)
+    return grub_error(GRUB_ERR_BAD_FILE_TYPE, "%s: not a subvolume\n", path);
+
+  data->fs_tree = tree;
+  return GRUB_ERR_NONE;
+}
+
 static grub_err_t
 btrfs_handle_subvol(struct grub_btrfs_data *data __attribute__ ((unused)))
 {
   if (btrfs_default_subvol)
-    return lookup_root_by_name(data, btrfs_default_subvol);
+    {
+      grub_err_t err;
+      err = lookup_root_by_name(data, btrfs_default_subvol);
+
+      /* Fallback to old schemes */
+      if (err == GRUB_ERR_FILE_NOT_FOUND)
+	{
+	  err = GRUB_ERR_NONE;
+	  return lookup_root_by_name_fallback(data, btrfs_default_subvol);
+	}
+      return err;
+    }
 
   if (btrfs_default_subvolid)
     return lookup_root_by_id(data, btrfs_default_subvolid);
Signature · 7 lines
2.46.2


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
#15 lore raw

PATCH v2 28/30 btrfs: Grub not working correctly with btrfs snapshots (bsc#1026511)

In reply to Leo Sandoval

From: Michael Chang <[email protected]>

---
 grub-core/fs/btrfs.c | 238 +++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 238 insertions(+)
grub-core/fs/btrfs.c +238 -0
index c5b3fd116..1c9e14008 100644
@@ -2906,6 +2906,238 @@ out:
   return 0;
 }
 
+static grub_err_t
+grub_btrfs_get_parent_subvol_path (struct grub_btrfs_data *data,
+		grub_uint64_t child_id,
+		const char *child_path,
+		grub_uint64_t *parent_id,
+		char **path_out)
+{
+  grub_uint64_t fs_root = 0;
+  struct grub_btrfs_key key_in = {
+    .object_id = child_id,
+    .type = GRUB_BTRFS_ITEM_TYPE_ROOT_BACKREF,
+    .offset = 0,
+  }, key_out;
+  struct grub_btrfs_root_ref *ref;
+  char *buf;
+  struct grub_btrfs_leaf_descriptor desc;
+  grub_size_t elemsize;
+  grub_disk_addr_t elemaddr;
+  grub_err_t err;
+  char *parent_path;
+
+  *parent_id = 0;
+  *path_out = 0;
+
+  err = lower_bound(data, &key_in, &key_out, data->sblock.root_tree,
+                    &elemaddr, &elemsize, &desc, 0);
+  if (err)
+    return err;
+
+  if (key_out.type != GRUB_BTRFS_ITEM_TYPE_ROOT_BACKREF || elemaddr == 0)
+    next(data, &desc, &elemaddr, &elemsize, &key_out);
+
+  if (key_out.type != GRUB_BTRFS_ITEM_TYPE_ROOT_BACKREF)
+    {
+      free_iterator(&desc);
+      return grub_error(GRUB_ERR_FILE_NOT_FOUND, N_("can't find root backrefs"));
+    }
+
+  buf = grub_malloc(elemsize + 1);
+  if (!buf)
+    {
+      free_iterator(&desc);
+      return grub_errno;
+    }
+
+  err = grub_btrfs_read_logical(data, elemaddr, buf, elemsize, 0);
+  if (err)
+    {
+      grub_free(buf);
+      free_iterator(&desc);
+      return err;
+    }
+
+  buf[elemsize] = 0;
+  ref = (struct grub_btrfs_root_ref *)buf;
+
+  err = get_fs_root(data, data->sblock.root_tree, grub_le_to_cpu64 (key_out.offset),
+                    0, &fs_root);
+  if (err)
+    {
+      grub_free(buf);
+      free_iterator(&desc);
+      return err;
+    }
+
+  find_pathname(data, grub_le_to_cpu64 (ref->dirid), fs_root, ref->name, &parent_path);
+
+  if (child_path)
+    {
+      *path_out = grub_xasprintf ("%s/%s", parent_path, child_path);
+      grub_free (parent_path);
+    }
+  else
+    *path_out = parent_path;
+
+  *parent_id = grub_le_to_cpu64 (key_out.offset);
+
+  grub_free(buf);
+  free_iterator(&desc);
+  return GRUB_ERR_NONE;
+}
+
+static grub_err_t
+grub_btrfs_get_default_subvolume_id (struct grub_btrfs_data *data, grub_uint64_t *id)
+{
+  grub_err_t err;
+  grub_disk_addr_t elemaddr;
+  grub_size_t elemsize;
+  struct grub_btrfs_key key, key_out;
+  struct grub_btrfs_dir_item *direl = NULL;
+  const char *ctoken = "default";
+  grub_size_t ctokenlen = sizeof ("default") - 1;
+
+  *id = 0;
+  key.object_id = data->sblock.root_dir_objectid;
+  key.type = GRUB_BTRFS_ITEM_TYPE_DIR_ITEM;
+  key.offset = grub_cpu_to_le64 (~grub_getcrc32c (1, ctoken, ctokenlen));
+  err = lower_bound (data, &key, &key_out, data->sblock.root_tree, &elemaddr, &elemsize,
+			 NULL, 0);
+  if (err)
+    return err;
+
+  if (key_cmp (&key, &key_out) != 0)
+    return grub_error (GRUB_ERR_FILE_NOT_FOUND, N_("file not found"));
+
+  struct grub_btrfs_dir_item *cdirel;
+  direl = grub_malloc (elemsize + 1);
+  err = grub_btrfs_read_logical (data, elemaddr, direl, elemsize, 0);
+  if (err)
+    {
+      grub_free (direl);
+      return err;
+    }
+  for (cdirel = direl;
+       (grub_uint8_t *) cdirel - (grub_uint8_t *) direl
+       < (grub_ssize_t) elemsize;
+       cdirel = (void *) ((grub_uint8_t *) (direl + 1)
+       + grub_le_to_cpu16 (cdirel->n)
+       + grub_le_to_cpu16 (cdirel->m)))
+    {
+      if (ctokenlen == grub_le_to_cpu16 (cdirel->n)
+        && grub_memcmp (cdirel->name, ctoken, ctokenlen) == 0)
+      break;
+    }
+  if ((grub_uint8_t *) cdirel - (grub_uint8_t *) direl
+      >= (grub_ssize_t) elemsize)
+    {
+      grub_free (direl);
+      err = grub_error (GRUB_ERR_FILE_NOT_FOUND, N_("file not found"));
+      return err;
+    }
+
+  if (cdirel->key.type != GRUB_BTRFS_ITEM_TYPE_ROOT_ITEM)
+    {
+      grub_free (direl);
+      err = grub_error (GRUB_ERR_FILE_NOT_FOUND, N_("file not found"));
+      return err;
+    }
+
+  *id = grub_le_to_cpu64 (cdirel->key.object_id);
+  return GRUB_ERR_NONE;
+}
+
+static grub_err_t
+grub_cmd_btrfs_get_default_subvol (struct grub_extcmd_context *ctxt,
+			     int argc, char **argv)
+{
+  char *devname;
+  grub_device_t dev;
+  struct grub_btrfs_data *data;
+  grub_err_t err;
+  grub_uint64_t id;
+  char *subvol = NULL;
+  grub_uint64_t subvolid = 0;
+  char *varname = NULL;
+  char *output = NULL;
+  int path_only = ctxt->state[1].set;
+  int num_only = ctxt->state[2].set;
+
+  if (ctxt->state[0].set)
+    varname = ctxt->state[0].arg;
+
+  if (argc < 1)
+    return grub_error (GRUB_ERR_BAD_ARGUMENT, "device name required");
+
+  devname = grub_file_get_device_name(argv[0]);
+  if (!devname)
+    return grub_errno;
+
+  dev = grub_device_open (devname);
+  grub_free (devname);
+  if (!dev)
+    return grub_errno;
+
+  data = grub_btrfs_mount(dev);
+  if (!data)
+    {
+      grub_device_close (dev);
+      grub_dprintf ("btrfs", "failed to open fs\n");
+      grub_errno = GRUB_ERR_NONE;
+      return 0;
+    }
+
+  err = grub_btrfs_get_default_subvolume_id (data, &subvolid);
+  if (err)
+    {
+      grub_btrfs_unmount (data);
+      grub_device_close (dev);
+      return err;
+    }
+
+  id = subvolid;
+  while (id != GRUB_BTRFS_ROOT_VOL_OBJECTID)
+    {
+      grub_uint64_t parent_id;
+      char *path_out;
+
+      err = grub_btrfs_get_parent_subvol_path (data, grub_cpu_to_le64 (id), subvol, &parent_id, &path_out);
+      if (err)
+	{
+	  grub_btrfs_unmount (data);
+	  grub_device_close (dev);
+	  return err;
+	}
+
+      if (subvol)
+        grub_free (subvol);
+      subvol = path_out;
+      id = parent_id;
+    }
+
+  if (num_only && path_only)
+      output = grub_xasprintf ("%"PRIuGRUB_UINT64_T" /%s", subvolid, subvol);
+  else if (num_only)
+      output = grub_xasprintf ("%"PRIuGRUB_UINT64_T, subvolid);
+  else
+      output = grub_xasprintf ("/%s", subvol);
+
+  if (varname)
+    grub_env_set(varname, output);
+  else
+    grub_printf ("%s\n", output);
+
+  grub_free (output);
+  grub_free (subvol);
+
+  grub_btrfs_unmount (data);
+  grub_device_close (dev);
+
+  return GRUB_ERR_NONE;
+}
+
 static struct grub_fs grub_btrfs_fs = {
   .name = "btrfs",
   .fs_dir = grub_btrfs_dir,
@@ -2923,6 +3155,7 @@ static struct grub_fs grub_btrfs_fs = {
 
 static grub_command_t cmd_info;
 static grub_extcmd_t cmd_list_subvols;
+static grub_extcmd_t cmd_get_default_subvol;
 
 static char *
 subvolid_set_env (struct grub_env_var *var __attribute__ ((unused)),
@@ -2990,6 +3223,11 @@ GRUB_MOD_INIT (btrfs)
 					 "[-p|-n] [-o var] DEVICE",
 					 "Print list of BtrFS subvolumes on "
 					 "DEVICE.", options);
+  cmd_get_default_subvol = grub_register_extcmd("btrfs-get-default-subvol",
+					 grub_cmd_btrfs_get_default_subvol, 0,
+					 "[-p|-n] [-o var] DEVICE",
+					 "Print default BtrFS subvolume on "
+					 "DEVICE.", options);
   grub_register_variable_hook ("btrfs_subvol", subvol_get_env,
                                subvol_set_env);
   grub_register_variable_hook ("btrfs_subvolid", subvolid_get_env,
Signature · 7 lines
2.46.2


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
#16 lore raw

Re: [PATCH v2 28/30] btrfs: Grub not working correctly with btrfs snapshots (bsc#1026511)

Quoted 5 lines · Leo Sandoval

From: Michael Chang <[email protected]>

Can we have a description of what this patch does for the commit message? The bug report[1] is private, so I have no context here.

[1]: https://bugzilla.suse.com/1026511

--
真実はいつも一つ!/ Always, there's only one truth!

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#17 lore raw

PATCH v2 29/30 efi: Add grub_efi_allocate_pool() and grub_efi_free_pool() wrappers.

In reply to Leo Sandoval

From: Peter Jones <[email protected]>

---
 include/grub/efi/efi.h | 36 ++++++++++++++++++++++++++++++++----
 1 file changed, 32 insertions(+), 4 deletions(-)
include/grub/efi/efi.h +32 -4
index a5cd99e5a..8d98203a7 100644
@@ -36,6 +36,10 @@ struct linux_arch_kernel_header {
   struct grub_pe_image_header pe_image_header;
 };
 
+/* Variables.  */
+extern grub_efi_system_table_t *EXPORT_VAR(grub_efi_system_table);
+extern grub_efi_handle_t EXPORT_VAR(grub_efi_image_handle);
+
 /* Functions.  */
 void *EXPORT_FUNC(grub_efi_locate_protocol) (grub_guid_t *protocol,
 					     void *registration);
@@ -71,6 +75,33 @@ EXPORT_FUNC(grub_efi_get_memory_map) (grub_efi_uintn_t *memory_map_size,
 				      grub_efi_uintn_t *descriptor_size,
 				      grub_efi_uint32_t *descriptor_version);
 void grub_efi_memory_fini (void);
+
+static inline grub_efi_status_t
+__attribute__((__unused__))
+grub_efi_allocate_pool (grub_efi_memory_type_t pool_type,
+			grub_efi_uintn_t buffer_size,
+			void **buffer)
+{
+  grub_efi_boot_services_t *b;
+  grub_efi_status_t status;
+
+  b = grub_efi_system_table->boot_services;
+  status = b->allocate_pool(pool_type, buffer_size, buffer);
+  return status;
+}
+
+static inline grub_efi_status_t
+__attribute__((__unused__))
+grub_efi_free_pool (void *buffer)
+{
+  grub_efi_boot_services_t *b;
+  grub_efi_status_t status;
+
+  b = grub_efi_system_table->boot_services;
+  status = b->free_pool(buffer);
+  return status;
+}
+
 grub_efi_loaded_image_t *EXPORT_FUNC(grub_efi_get_loaded_image) (grub_efi_handle_t image_handle);
 void EXPORT_FUNC(grub_efi_print_device_path) (grub_efi_device_path_t *dp);
 char *EXPORT_FUNC(grub_efi_get_filename) (grub_efi_device_path_t *dp);
@@ -140,10 +171,7 @@ void grub_efi_init (void);
 void grub_efi_fini (void);
 void grub_efi_set_prefix (void);
 
-/* Variables.  */
-extern grub_efi_system_table_t *EXPORT_VAR(grub_efi_system_table);
-extern grub_efi_handle_t EXPORT_VAR(grub_efi_image_handle);
-
+/* More variables.  */
 extern int EXPORT_VAR(grub_efi_is_finished);
 
 struct grub_net_card;
Signature · 7 lines
2.46.2


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
#18 lore raw

PATCH v2 30/30 chainloader: Use grub_efi_...() memory helpers where reasonable.

In reply to Leo Sandoval

From: Peter Jones <[email protected]>

This uses grub_efi_allocate_pool(), grub_efi_free_pool(), and grub_efi_free_pages() instead of open-coded efi_call_N() calls, so we get more reasonable type checking.

---
 grub-core/loader/efi/chainloader.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
grub-core/loader/efi/chainloader.c +2 -2
index 1de98f783..203692450 100644
@@ -95,7 +95,7 @@ grub_chainloader_boot (void *context)
     }
 
   if (exit_data)
-    b->free_pool (exit_data);
+    grub_efi_free_pool (exit_data);
 
   grub_loader_unset ();
 
@@ -419,7 +419,7 @@ grub_cmd_chainloader (grub_command_t cmd __attribute__ ((unused)),
   grub_free (file_path);
 
   if (address)
-    b->free_pages (address, pages);
+    grub_efi_free_pages (address, pages);
 
   if (image_handle != NULL)
     b->unload_image (image_handle);
Signature · 7 lines
2.46.2


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
#19 lore raw
In reply to Leo Sandoval
Quoted 51 lines · Leo Sandoval

This is the second patch series, taken from Fedora Rawhide spec [1] that is distro-agnostic. The goal is to merge most of them so all the community/distros would benefit.

Changes since v1:
- delete 0024-efinet-and-bootp-add-support-for-dhcpv6.patch build issue, so for the moment discarting it

- delete 0025-bootp-New-net_bootp6-command.patch patch will be sent separately by mchang

- delete 0027-Make-grub_fatal-also-backtrace.patch not accepted

- delete 0028-Make-our-info-pages-say-grub2-where-appropriate.patch non-distro agnostic

- delete 0034-grub2-btrfs-04-grub2-install.patch patch will be revisited by author (mchang)

- delete 0036-grub2-btrfs-06-subvol-mount.patch not accepted

Andrei Borzenkov (1):
btrfs: Fallback to old subvol name scheme to support old snapshot
config

Jeff Mahoney (1):
btrfs: Add ability to boot from subvolumes

Josef Bacik (1):
net/tcp: add window scaling support

Michael Chang (4):
btrfs: export btrfs_subvol and btrfs_subvolid
btrfs: grub2-btrfs-03-follow_default
btdfs: grub2-btrfs-05-grub2-mkconfig
btrfs: Grub not working correctly with btrfs snapshots (bsc#1026511)

Peter Jones (6):
10_linux.in: Make grub2-mkconfig construct titles that look like the
ones we want elsewhere.
grub-get-kernel-settings.in: Add grub-get-kernel-settings and use it

    in 10_linux

30_os-prober.in: just build chainloader entries, don't try any xnu
xnu.
btrfs: fix a bad null check
efi: Add grub_efi_allocate_pool() and grub_efi_free_pool() wrappers.
chainloader: Use grub_efi_...() memory helpers where reasonable.

Quoted 4 lines

Robert Marshall (1):
grub-set-password.in: Add friendly grub2 password config tool

    (#985962)

I appreciate that you're doing this, thank you! I've given some feedback on the btrfs patches, though alas I missed the v1 round where some of the patches were debated.

Signature · 6 lines
真実はいつも一つ!/ Always, there's only one truth!

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
#20 lore raw
Quoted 53 lines · Neal Gompa
Quoted 3 lines · Leo Sandoval

This is the second patch series, taken from Fedora Rawhide spec [1] that is distro-agnostic. The goal is to merge most of them so all the

community/distros

Quoted 48 lines

would benefit.

Changes since v1:
- delete 0024-efinet-and-bootp-add-support-for-dhcpv6.patch build issue, so for the moment discarting it

- delete 0025-bootp-New-net_bootp6-command.patch patch will be sent separately by mchang

- delete 0027-Make-grub_fatal-also-backtrace.patch not accepted

- delete 0028-Make-our-info-pages-say-grub2-where-appropriate.patch non-distro agnostic

- delete 0034-grub2-btrfs-04-grub2-install.patch patch will be revisited by author (mchang)

- delete 0036-grub2-btrfs-06-subvol-mount.patch not accepted

Andrei Borzenkov (1):
btrfs: Fallback to old subvol name scheme to support old snapshot
config

Jeff Mahoney (1):
btrfs: Add ability to boot from subvolumes

Josef Bacik (1):
net/tcp: add window scaling support

Michael Chang (4):
btrfs: export btrfs_subvol and btrfs_subvolid
btrfs: grub2-btrfs-03-follow_default
btdfs: grub2-btrfs-05-grub2-mkconfig
btrfs: Grub not working correctly with btrfs snapshots (bsc#1026511)

Peter Jones (6):
10_linux.in: Make grub2-mkconfig construct titles that look like the
ones we want elsewhere.
grub-get-kernel-settings.in: Add grub-get-kernel-settings and use it

    in 10_linux

30_os-prober.in: just build chainloader entries, don't try any xnu
xnu.
btrfs: fix a bad null check
efi: Add grub_efi_allocate_pool() and grub_efi_free_pool() wrappers.
chainloader: Use grub_efi_...() memory helpers where reasonable.

Quoted 9 lines
Quoted 4 lines

Robert Marshall (1):
grub-set-password.in: Add friendly grub2 password config tool

    (#985962)

I appreciate that you're doing this, thank you! I've given some feedback on the btrfs patches, though alas I missed the v1 round where some of the patches were debated.

Thanks for the feedback Neal. I am about to bump the series version with your comments and some descriptions provided by mchang.

Quoted 11 lines

--
真実はいつも一つ!/ Always, there's only one truth!

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel