[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: check if file exist on ntfs partition to decide default boot order

From: Jarek
Subject: Re: check if file exist on ntfs partition to decide default boot order
Date: Mon, 26 Apr 2021 16:13:38 +0200
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.10.0

Hi Hans! Thanks for fast reply!

I did some tests and ended up with creating new file 01_winfile in /etc/grub.d:

/cat << EOF
search --file --set jarekwin /win.txt
if [ -n "\$jarekwin" ]; then

now I will try do similar on my real server which boot awfully long (bios and raid controller part) and most of time machine is closed in small room because of fans noise, without attached screen and keyboard, but with vnc, rdp and ssh.

I also learn something about shell syntax, thanks!


W dniu 24.04.2021 o 04:17, Hans Gruber pisze:
Hello Jarek,

It's possible to do with grub what you want to do.
You will need to script a little using grub which use a shell like syntax.

All commands from bash are not available but you can create function, variable (no array), use if .. then, elif ... then, else, fi and for .. in.. do .. done, while, until ... $#, $@, $* and so on
it's very powerful.
for example, if requires single [ ... ] not [[ ... ]], "or" "and" operators are not && || but "-o" "-a".

See "6.3 Writing full configuration files directly" in Grub manual for more information.
Some module as regexp, tr, ... can be loaded if you need additional command.

About ntfs you will need to load ntfs module (+reqs if needed)
You have a really interresting file inside /boot/grub/${arch}/moddep.lst which list all required dependencies for each module.

About searching a file you will need the search module which will return the root of drive if file is found. you can search file system uuid or file system label if you want.
eg: search --file [--set=root] /path/win.txt
will return the root of drive if absolute path /path/win.txt is found; else nothing ([--set=root] if not specified, root variable is used but you can use another variable to get result and test if variable exists or not)

search --file --set=winroot "/path/win.txt"
if [ -n "${winroot}" ]; then
    echo "root found"
    set root="${winroot}"
    set prefix=
    echo "oops no windows"

or this work too

if search --file --set=winroot "/path/win.txt"; then
    echo "windows root found"
    set root="${winroot}"
    set prefix=
elif search --file --set=linroot "/boot/grub/grub.cfg"; then
    echo "oops no windows but linux"
    set root="${linroot}"
    set prefix=
    echo "oops nothing found :("

Here is the manual with all command.

Hope this help.


Le samedi 24 avril 2021 à 02:50:07 UTC+2, Jarek <> a écrit :


I want to check if specific file (let's say win.txt) exist in root
directory at ntfs WIndows partition and if does, then boot Windows (or
better: make it default choice but still display menu and timeout), and
if not boot Linux (or leave default menu with default Linux and
timeout). I've seen command test -e file and insmod ntfs, but I don't
know what next. Does "grub works" like shell scripts? Are entries from
grub.cfg are just interpreted and executed from top to bottom or is it
compiled on update?


reply via email to

[Prev in Thread] Current Thread [Next in Thread]