Initrd and Initramfs Commands (Demo)

Video Activity
Join over 3 million cybersecurity professionals advancing their career
Sign up with
Required fields are marked with an *
or

Already have an account? Sign In »

Time
21 hours 25 minutes
Difficulty
Intermediate
CEU/CPE
21
Video Transcription
00:02
>> Hello Cybrarians and welcome back to
00:02
the Linux Plus course here at the Cybrary.
00:02
I'm your instructor, Rob Gills.
00:02
In today's lesson,
00:02
we will be discussing initrd and initramfs commands.
00:02
Upon completion of the lesson,
00:02
you will understand initrd and initramfs commands
00:02
and you will know when and how to use
00:02
these commands. Let's get to it.
00:02
Demo time. Today we're going to
00:02
cover the following commands: mkinitramfs and dracut.
00:02
With that being said, let's go ahead and
00:02
get on over to our demo environment.
00:02
Today we're going to start in Ubuntu.
00:02
Here we are in our Ubuntu environment and as you
00:02
can see, we're still in the boot directory over
00:02
here where we left off from our previous lesson.
00:02
Now, the mkinitrd command
00:02
is what you may see on the exam blueprint,
00:02
but that command is actually been
00:02
deprecated in my copy of Ubuntu,
00:02
which is a Ubuntu 20.
00:02
The command we're going to use today
00:02
is mkinitramfs and that command,
00:02
mkinitramfs, is used to reconstruct the initrd file.
00:02
If we look here,
00:02
we see initrd file, initrd.img-5.4.059 generic.
00:02
That corresponds to the Linux,
00:02
which is the compressed kernel, 5.4.0-59 generic.
00:02
These are file systems.
00:02
It has the executable and
00:02
drivers that are used to boot the kernels.
00:02
Initrd boots this kernel here.
00:02
You may have to use
00:02
the mkinitramfs command if
00:02
you've had a new hardware devices.
00:02
Now, normally those new hardware devices
00:02
get picked up by the kernel upon boot.
00:02
But if you're not planning to reboot the system anytime
00:02
soon and you've added some new hardware,
00:02
you may want to go ahead and re-initialize
00:02
the hardware drivers using this command.
00:02
Let's take a look at how you might do that.
00:02
The first thing we're going to want to
00:02
do is run the command "uname-r".
00:02
What this is, is
00:02
a process or a command that we could use
00:02
to display the current kernel version that's
00:02
running on the system and
00:02
we see that it's 5.8.0-48-generic.
00:02
One of the cool things we can do in Linux is
00:02
something called shell expansion.
00:02
One of the great shell expansion you can do
00:02
is actually running something like this.
00:02
What this will do is it'll pass it
00:02
into a command that you're running.
00:02
The command we just ran
00:02
will get passed into another command.
00:02
For instance, the first thing we're going to
00:02
probably want to do is back this up.
00:02
We can do that using the copy command.
00:02
We always want to back things up before we
00:02
make changes in case something blows
00:02
up and we need to revert that change back.
00:02
The first thing we want to do is
00:02
copy this and we always want to back
00:02
things up because you may have to reboot.
00:02
What we're going to want to do with this is actually
00:02
copy the existing initrd file, and we always
00:02
want to do this in case we run into issues,
00:02
in case we need to revert
00:02
our changes or anything that we did.
00:02
Remember, everything in Linux is a file.
00:02
If you have a copy of a file backed up,
00:02
it's really easy to restore the file that we
00:02
overrode or make changes to that caused issues.
00:02
We know that this file lives and our
00:02
>> boot directory here,
00:02
>> and this all start with initrd.image,
00:02
and then they have a dash,
00:02
and then they have the version.
00:02
This is 5.8.0-48 generic.
00:02
We find that with "uname-r" command.
00:02
By using this shell expansion here,
00:02
we can just do as a shortcut without actually having to
00:02
type out that long number.
00:02
What we're going to do is we're just going to
00:02
copy this, the cp command copy it.
00:02
We are going to copy it into root.
00:02
Because root is owned by the root user,
00:02
we need to elevate our privileges to do that.
00:02
We'll use the pseudo command and there we go.
00:02
Now if we do an LS or actually let's
00:02
do a pseudo LS on root,
00:02
we're going to see that we have
00:02
initrd image 5.8.0-48 generic.
00:02
That shell expansion allowed us to put that in
00:02
there without actually having to type
00:02
it. This string right here.
00:02
We can also do an ls-al in root, and then
00:02
what we can do is we can see a timestamp on it.
00:02
We see that this was created April 6th at 23:20,
00:02
and if we do a date command 11:20,
00:02
this is a local time here in
00:02
ADT 23:20 is the 24 hour time, so that is correct.
00:02
We just created that file.
00:02
It has been successfully backed up.
00:02
Now if we want to run mkinitramfs,
00:02
we can do that in our directory because we've already
00:02
got a backup of this file somewhere else.
00:02
We can do that with mkinitramfs-0.
00:02
That's going to tell the command to
00:02
output into this directory,
00:02
into the boot directory,
00:02
and we're going to call it initrd.img.
00:02
We're going to use that same trick as before.
00:02
We're going to say "uname-r",
00:02
and then we're going to use
00:02
the existing kernel version which we can use "uname-r"
00:02
again as the source to build this initrd image.
00:02
If we hit "Enter", this will run.
00:02
Apologies, we actually have to
00:02
run that as pseudo as well.
00:02
We have to elevate
00:02
our privileges in order to run this command.
00:02
We can run this now, and once it's done,
00:02
we can take a look at the timestamp
00:02
of the initrd file in this directory.
00:02
What we can do here is we can do
00:02
ls-l boot/Initrd/image-(uname-r)
00:02
and hit "Enter".
00:02
What we can see is the timestamp on that there
00:02
is 23:21, and we're at 11:21.
00:02
That file was just created.
00:02
We successfully created a new initramfs,
00:02
a new initrd file actually.
00:02
That's what we would do over there in Ubuntu.
00:02
Ubuntu uses initrd files.
00:02
But let's go over to CentOS and let's
00:02
go ahead and get logged in here.
00:02
Over here and CentOS, we're still
00:02
>> in our boot directory.
00:02
>> We can do an LS here.
00:02
What you'll notice is that initramfs files
00:02
are here in CentOS
00:02
and initrd files are
00:02
used in Ubuntu. They're the same thing.
00:02
The initramfs is
00:02
initial random access memory file system.
00:02
It has the same job as initrd.
00:02
It's a file system that has the executables and
00:02
drivers required to boot the kernel.
00:02
Just as we saw before,
00:02
we're going to see a version of initramfs with
00:02
the same kernel revision here as
00:02
>> we see for the VM Linux,
00:02
>> the compressed kernel file.
00:02
For 4.18.0193, we're going to see a
00:02
4.18.0193 here as well
00:02
because they need to be associated
00:02
every time you install a new kernel,
00:02
a new initramfs is created.
00:02
The dracut command itself will
00:02
>> create an initramfs image.
00:02
>> What we can do is we can run dracut and it's going to
00:02
yell at us because it's going to say, it cannot
00:02
overwrite this existing version without force.
00:02
But what we could do is we could
00:02
create a new version of the file.
00:02
I'm going to do this pseudo,
00:02
I'm going to elevate my privileges just to be safe.
00:02
What I'm going to do is I'm going to run dracut
00:02
and I'll say newimage.image.
00:02
We'll hit "Enter" and I'll type in
00:02
my password here to get elevated privileges.
00:02
Then we wait a couple of seconds
00:02
here to about a minute and we should
00:02
be able to see that this file has been created.
00:02
Now that has finished.
00:02
We can do an LS in this directory.
00:02
We will see right here newimage.img was created.
00:02
Now, one of the other commands that you need to
00:02
know for the exam is lsinitrd.
00:02
What we can do is we can actually get
00:02
information on that new image we just created.
00:02
You have to run this with elevated privileges as well.
00:02
But you can pass in the name of the image.
00:02
Then what we can do is we can actually
00:02
do a redirection trick.
00:02
This is a pipe and what will happen is
00:02
this output here of the lsinitrd of
00:02
the image we just created
00:02
gets passed over to less and less is a pager.
00:02
What that means is it allows us to see
00:02
the output and then go up and
00:02
down in the output using the page up and page
00:02
down commands, hence pager.
00:02
This is going to run and as it loads up here,
00:02
it'll just kinda keep loading information.
00:02
It's going to pause right here and we won't see
00:02
anything else until we can get the page up and .
00:02
page down but you know, it's just telling us
00:02
information about what it's building.
00:02
It's going to build this image or image
00:02
was built in the CPIO format.
00:02
It puts information on
00:02
the arguments that are used to build the image.
00:02
It has information on all the different files included
00:02
and the permission structure for
00:02
those and, so on and so forth.
00:02
This is really far down in the weeds,
00:02
but I just wanted to demonstrate what this command
00:02
does and some of the things that
00:02
you may need to know for
00:02
the exam about the lsinitrd tool.
00:02
One more thing that you might need to
00:02
know with dracut is that you can
00:02
actually add specific drivers with the command.
00:02
For instance, if you wanted to add
00:02
a specific driver with dracut,
00:02
you could do dracut dash dash add drivers,
00:02
and if you knew the driver name,
00:02
you could put the driver right here,
00:02
and then you would just tell it what the image name is.
00:02
We can say newimage.img again, for instance.
00:02
This would have to be run, as elevated privileges,
00:02
but this would add that driver into
00:02
a new image and then you could use
00:02
that image on your system.
00:02
With that being said in today's lesson,
00:02
we covered the initrd and the initramfs
00:02
commands from the Linux Plus exam blueprint.
00:02
That is the mkinitramfs command,
00:02
which is used on Ubuntu formerly mkinitrd.
00:02
The dracut command is used on CentOS
00:02
to work with initramfs files.
00:02
We also covered when you would use these commands.
00:02
Thank you so much for being here and I look
00:02
forward to seeing you in the next lesson.
Up Next