Linux + how to capture the OS disk device [closed]
Clash Royale CLAN TAG#URR8PPP
up vote
-1
down vote
favorite
we want to capture the disk device that belong to the OS ( Linux )
since each linux machine have list of disks that are not the OS , we want to capture the disk that belong to the OS
so by fdisk we can see that boot is on sda1
# sfdisk -l | grep Linux
/dev/sda1 * 0+ 63- 64- 512000 83 Linux
/dev/sda2 63+ 19581- 19518- 156773376 8e Linux LVM
so according to that I created the following command , in order to capture the disk that belong to the OS ( linux )
# OS_DISK=` sfdisk -l | grep Linux | awk '$2 == "*" print $1' | sed s'/// /g' | awk 'print $2' | sed 's/[0-9]*//g' `
# echo $OS_DISK
sda
seems the command do the Job
but Ifeel that this cli is too long and little clumsy
linux scripting disk fdisk lsblk
closed as unclear what you're asking by n.st, Thomas, Rui F Ribeiro, Jeff Schaller, JigglyNaga Nov 26 at 9:58
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
add a comment |
up vote
-1
down vote
favorite
we want to capture the disk device that belong to the OS ( Linux )
since each linux machine have list of disks that are not the OS , we want to capture the disk that belong to the OS
so by fdisk we can see that boot is on sda1
# sfdisk -l | grep Linux
/dev/sda1 * 0+ 63- 64- 512000 83 Linux
/dev/sda2 63+ 19581- 19518- 156773376 8e Linux LVM
so according to that I created the following command , in order to capture the disk that belong to the OS ( linux )
# OS_DISK=` sfdisk -l | grep Linux | awk '$2 == "*" print $1' | sed s'/// /g' | awk 'print $2' | sed 's/[0-9]*//g' `
# echo $OS_DISK
sda
seems the command do the Job
but Ifeel that this cli is too long and little clumsy
linux scripting disk fdisk lsblk
closed as unclear what you're asking by n.st, Thomas, Rui F Ribeiro, Jeff Schaller, JigglyNaga Nov 26 at 9:58
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
2
What's your defintion of "belonging to the OS"? The root partition?/boot
//usr
//var
/… partitions (in case they're separate)?/home
(if it is local)? All local partitions that are mounted? Or all local partitions, even if they're not mounted? There are many special cases (e.g. multiboot systems) and it isn't clear how you would like to handle those.
– n.st
Nov 25 at 15:42
yes boot partitions
– yael
Nov 25 at 16:38
Regardless of the problematic definition of "the OS", there is no single, general, simple, command to get the physical device that hosts a mount point. In this specific case, supposing that you are looking for the physical disk that hosts the partition mounted as/boot
,lsblk -n -o PKNAME "$(findmnt -n -T /boot -o SOURCE)"
will probably work (PKNAME
forlsblk
is the "internal parent kernel device name").
– fra-san
Nov 25 at 18:47
add a comment |
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
we want to capture the disk device that belong to the OS ( Linux )
since each linux machine have list of disks that are not the OS , we want to capture the disk that belong to the OS
so by fdisk we can see that boot is on sda1
# sfdisk -l | grep Linux
/dev/sda1 * 0+ 63- 64- 512000 83 Linux
/dev/sda2 63+ 19581- 19518- 156773376 8e Linux LVM
so according to that I created the following command , in order to capture the disk that belong to the OS ( linux )
# OS_DISK=` sfdisk -l | grep Linux | awk '$2 == "*" print $1' | sed s'/// /g' | awk 'print $2' | sed 's/[0-9]*//g' `
# echo $OS_DISK
sda
seems the command do the Job
but Ifeel that this cli is too long and little clumsy
linux scripting disk fdisk lsblk
we want to capture the disk device that belong to the OS ( Linux )
since each linux machine have list of disks that are not the OS , we want to capture the disk that belong to the OS
so by fdisk we can see that boot is on sda1
# sfdisk -l | grep Linux
/dev/sda1 * 0+ 63- 64- 512000 83 Linux
/dev/sda2 63+ 19581- 19518- 156773376 8e Linux LVM
so according to that I created the following command , in order to capture the disk that belong to the OS ( linux )
# OS_DISK=` sfdisk -l | grep Linux | awk '$2 == "*" print $1' | sed s'/// /g' | awk 'print $2' | sed 's/[0-9]*//g' `
# echo $OS_DISK
sda
seems the command do the Job
but Ifeel that this cli is too long and little clumsy
linux scripting disk fdisk lsblk
linux scripting disk fdisk lsblk
edited Nov 25 at 15:31
Jeff Schaller
37k1052121
37k1052121
asked Nov 25 at 14:29
yael
2,3341955
2,3341955
closed as unclear what you're asking by n.st, Thomas, Rui F Ribeiro, Jeff Schaller, JigglyNaga Nov 26 at 9:58
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
closed as unclear what you're asking by n.st, Thomas, Rui F Ribeiro, Jeff Schaller, JigglyNaga Nov 26 at 9:58
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
2
What's your defintion of "belonging to the OS"? The root partition?/boot
//usr
//var
/… partitions (in case they're separate)?/home
(if it is local)? All local partitions that are mounted? Or all local partitions, even if they're not mounted? There are many special cases (e.g. multiboot systems) and it isn't clear how you would like to handle those.
– n.st
Nov 25 at 15:42
yes boot partitions
– yael
Nov 25 at 16:38
Regardless of the problematic definition of "the OS", there is no single, general, simple, command to get the physical device that hosts a mount point. In this specific case, supposing that you are looking for the physical disk that hosts the partition mounted as/boot
,lsblk -n -o PKNAME "$(findmnt -n -T /boot -o SOURCE)"
will probably work (PKNAME
forlsblk
is the "internal parent kernel device name").
– fra-san
Nov 25 at 18:47
add a comment |
2
What's your defintion of "belonging to the OS"? The root partition?/boot
//usr
//var
/… partitions (in case they're separate)?/home
(if it is local)? All local partitions that are mounted? Or all local partitions, even if they're not mounted? There are many special cases (e.g. multiboot systems) and it isn't clear how you would like to handle those.
– n.st
Nov 25 at 15:42
yes boot partitions
– yael
Nov 25 at 16:38
Regardless of the problematic definition of "the OS", there is no single, general, simple, command to get the physical device that hosts a mount point. In this specific case, supposing that you are looking for the physical disk that hosts the partition mounted as/boot
,lsblk -n -o PKNAME "$(findmnt -n -T /boot -o SOURCE)"
will probably work (PKNAME
forlsblk
is the "internal parent kernel device name").
– fra-san
Nov 25 at 18:47
2
2
What's your defintion of "belonging to the OS"? The root partition?
/boot
//usr
//var
/… partitions (in case they're separate)? /home
(if it is local)? All local partitions that are mounted? Or all local partitions, even if they're not mounted? There are many special cases (e.g. multiboot systems) and it isn't clear how you would like to handle those.– n.st
Nov 25 at 15:42
What's your defintion of "belonging to the OS"? The root partition?
/boot
//usr
//var
/… partitions (in case they're separate)? /home
(if it is local)? All local partitions that are mounted? Or all local partitions, even if they're not mounted? There are many special cases (e.g. multiboot systems) and it isn't clear how you would like to handle those.– n.st
Nov 25 at 15:42
yes boot partitions
– yael
Nov 25 at 16:38
yes boot partitions
– yael
Nov 25 at 16:38
Regardless of the problematic definition of "the OS", there is no single, general, simple, command to get the physical device that hosts a mount point. In this specific case, supposing that you are looking for the physical disk that hosts the partition mounted as
/boot
, lsblk -n -o PKNAME "$(findmnt -n -T /boot -o SOURCE)"
will probably work (PKNAME
for lsblk
is the "internal parent kernel device name").– fra-san
Nov 25 at 18:47
Regardless of the problematic definition of "the OS", there is no single, general, simple, command to get the physical device that hosts a mount point. In this specific case, supposing that you are looking for the physical disk that hosts the partition mounted as
/boot
, lsblk -n -o PKNAME "$(findmnt -n -T /boot -o SOURCE)"
will probably work (PKNAME
for lsblk
is the "internal parent kernel device name").– fra-san
Nov 25 at 18:47
add a comment |
2 Answers
2
active
oldest
votes
up vote
1
down vote
accepted
- I find the simplest command to identify the operating system disk
to bedf /
.
Unfortunately, it produces a lot of output
(by which I mean a header line and many fields),
so you would still need to do some filtering
to get just the device name. You're right; your command is overly long and somewhat clumsy.
awk
is a very powerful program; you rarely need
to combine it withgrep
and/orsed
,
and having multipleawk
commands in the same pipeline
is almost never necessary.
Your pipeline can be replaced withsfdisk -l | awk '/Linux/ && $2 == "*" gsub("[0-9]", "", $1); split($1, a, "/"); print a[3]; '
OK, it's only about a dozen characters shorter,
but it's one command instead of five.P.S.
sed 's/[0-9]*//g'
is a slightly dangerous command.
Because of theg
,
it doesn't really make sense to have the*
also.
To see what I mean, trysed 's/[0-9]*/X/g'
with various inputs,
and compare tos/[0-9]/X/g
ands/[0-9]+/X/g
.OS_DISK=` command `
can be changed
toOS_DISK=$(command)
,
and the second form (with the parentheses) is preferred.
add a comment |
up vote
2
down vote
Commands fdisk
and sfdisk
are included in package util-linux
.
In the same package there is also findmnt
command, that you can use like bellow:
findmnt / -no SOURCE
/dev/sda6
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
- I find the simplest command to identify the operating system disk
to bedf /
.
Unfortunately, it produces a lot of output
(by which I mean a header line and many fields),
so you would still need to do some filtering
to get just the device name. You're right; your command is overly long and somewhat clumsy.
awk
is a very powerful program; you rarely need
to combine it withgrep
and/orsed
,
and having multipleawk
commands in the same pipeline
is almost never necessary.
Your pipeline can be replaced withsfdisk -l | awk '/Linux/ && $2 == "*" gsub("[0-9]", "", $1); split($1, a, "/"); print a[3]; '
OK, it's only about a dozen characters shorter,
but it's one command instead of five.P.S.
sed 's/[0-9]*//g'
is a slightly dangerous command.
Because of theg
,
it doesn't really make sense to have the*
also.
To see what I mean, trysed 's/[0-9]*/X/g'
with various inputs,
and compare tos/[0-9]/X/g
ands/[0-9]+/X/g
.OS_DISK=` command `
can be changed
toOS_DISK=$(command)
,
and the second form (with the parentheses) is preferred.
add a comment |
up vote
1
down vote
accepted
- I find the simplest command to identify the operating system disk
to bedf /
.
Unfortunately, it produces a lot of output
(by which I mean a header line and many fields),
so you would still need to do some filtering
to get just the device name. You're right; your command is overly long and somewhat clumsy.
awk
is a very powerful program; you rarely need
to combine it withgrep
and/orsed
,
and having multipleawk
commands in the same pipeline
is almost never necessary.
Your pipeline can be replaced withsfdisk -l | awk '/Linux/ && $2 == "*" gsub("[0-9]", "", $1); split($1, a, "/"); print a[3]; '
OK, it's only about a dozen characters shorter,
but it's one command instead of five.P.S.
sed 's/[0-9]*//g'
is a slightly dangerous command.
Because of theg
,
it doesn't really make sense to have the*
also.
To see what I mean, trysed 's/[0-9]*/X/g'
with various inputs,
and compare tos/[0-9]/X/g
ands/[0-9]+/X/g
.OS_DISK=` command `
can be changed
toOS_DISK=$(command)
,
and the second form (with the parentheses) is preferred.
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
- I find the simplest command to identify the operating system disk
to bedf /
.
Unfortunately, it produces a lot of output
(by which I mean a header line and many fields),
so you would still need to do some filtering
to get just the device name. You're right; your command is overly long and somewhat clumsy.
awk
is a very powerful program; you rarely need
to combine it withgrep
and/orsed
,
and having multipleawk
commands in the same pipeline
is almost never necessary.
Your pipeline can be replaced withsfdisk -l | awk '/Linux/ && $2 == "*" gsub("[0-9]", "", $1); split($1, a, "/"); print a[3]; '
OK, it's only about a dozen characters shorter,
but it's one command instead of five.P.S.
sed 's/[0-9]*//g'
is a slightly dangerous command.
Because of theg
,
it doesn't really make sense to have the*
also.
To see what I mean, trysed 's/[0-9]*/X/g'
with various inputs,
and compare tos/[0-9]/X/g
ands/[0-9]+/X/g
.OS_DISK=` command `
can be changed
toOS_DISK=$(command)
,
and the second form (with the parentheses) is preferred.
- I find the simplest command to identify the operating system disk
to bedf /
.
Unfortunately, it produces a lot of output
(by which I mean a header line and many fields),
so you would still need to do some filtering
to get just the device name. You're right; your command is overly long and somewhat clumsy.
awk
is a very powerful program; you rarely need
to combine it withgrep
and/orsed
,
and having multipleawk
commands in the same pipeline
is almost never necessary.
Your pipeline can be replaced withsfdisk -l | awk '/Linux/ && $2 == "*" gsub("[0-9]", "", $1); split($1, a, "/"); print a[3]; '
OK, it's only about a dozen characters shorter,
but it's one command instead of five.P.S.
sed 's/[0-9]*//g'
is a slightly dangerous command.
Because of theg
,
it doesn't really make sense to have the*
also.
To see what I mean, trysed 's/[0-9]*/X/g'
with various inputs,
and compare tos/[0-9]/X/g
ands/[0-9]+/X/g
.OS_DISK=` command `
can be changed
toOS_DISK=$(command)
,
and the second form (with the parentheses) is preferred.
edited Nov 25 at 23:53
answered Nov 25 at 23:20
G-Man
12.3k92961
12.3k92961
add a comment |
add a comment |
up vote
2
down vote
Commands fdisk
and sfdisk
are included in package util-linux
.
In the same package there is also findmnt
command, that you can use like bellow:
findmnt / -no SOURCE
/dev/sda6
add a comment |
up vote
2
down vote
Commands fdisk
and sfdisk
are included in package util-linux
.
In the same package there is also findmnt
command, that you can use like bellow:
findmnt / -no SOURCE
/dev/sda6
add a comment |
up vote
2
down vote
up vote
2
down vote
Commands fdisk
and sfdisk
are included in package util-linux
.
In the same package there is also findmnt
command, that you can use like bellow:
findmnt / -no SOURCE
/dev/sda6
Commands fdisk
and sfdisk
are included in package util-linux
.
In the same package there is also findmnt
command, that you can use like bellow:
findmnt / -no SOURCE
/dev/sda6
edited Nov 25 at 19:11
answered Nov 25 at 18:16
George Vasiliou
5,57531028
5,57531028
add a comment |
add a comment |
2
What's your defintion of "belonging to the OS"? The root partition?
/boot
//usr
//var
/… partitions (in case they're separate)?/home
(if it is local)? All local partitions that are mounted? Or all local partitions, even if they're not mounted? There are many special cases (e.g. multiboot systems) and it isn't clear how you would like to handle those.– n.st
Nov 25 at 15:42
yes boot partitions
– yael
Nov 25 at 16:38
Regardless of the problematic definition of "the OS", there is no single, general, simple, command to get the physical device that hosts a mount point. In this specific case, supposing that you are looking for the physical disk that hosts the partition mounted as
/boot
,lsblk -n -o PKNAME "$(findmnt -n -T /boot -o SOURCE)"
will probably work (PKNAME
forlsblk
is the "internal parent kernel device name").– fra-san
Nov 25 at 18:47