Why is my initramfs script not being run? - linux

Running Ubuntu 18.04 LTS. I've put a script in the following location:
/usr/share/initramfs-tools/scripts/init-bottom/dothis
dothis is set as +x. I've run sudo update-initramfs -u which appears to update the initramfs contents just fine. I've looked at /boot/grub/grub.cfg and see the expected initrd file configured.
Running lsinitramfs on the file DOES show the script as having been added.
I cannot however find any evidence that dothis is being run on boot. As per documentation online, the root filesystem SHOULD be in place by the time the /init-bottom scripts are run, which should be the only thing required for my 'hello world' to work, as it outputs to a file on the root file system.
Is there some other step that needs to be done in order to get this script to run, or show whether it's being run? I've tried to simply output text to a file in /tmp but this is not showing up either.

Since you did not provide more details, I have to guess. Does your dothis script write to /tmp inside the initramfs? The root file system is mounted at /root and the initramfs uses switch_root to switch to it (as last step of the initramfs). Then all files written to /tmp are lost. If you let your script write to /root/root/log, this file will appear in /root/log on the booted system.
To further debug your issue, you can boot your system with the kernel parameter break=bottom. Then you get a shell where you can run your script manually. For even faster debug cycles, you can use a virtual machine.

Related

Issues setting $PATH on Bash on Ubuntu on Windows (Linux Subsystem)

I am using the "Bash on Ubuntu on Windows" (Linux Subsystem) and want to add Terraform to my $PATH. Since Terraform can't be installed via apt-get, I did the following steps:
Navigated to this directory, where I wanted to install Terraform:
cd /usr/local
In the above path, I used wget to download Terraform:
wget
https://releases.hashicorp.com/terraform/0.9.8/terraform_0.9.8_linux_amd64.zip
Terraform successfully unzips! When I open the file in VIM it is all good:
unzip terraform_0.9.8_linux_amd64.zip
I then enter this command to check to see if the Terraform binary is accessible from the command line:
terraform -version
However the following message gets returned:
terraform: command not found
This tells me that the Terraform downloaded location needs to be added to my $PATH.
Already being logged in as the root user ("sudo su") I enter the following command to access ".profile":
vim ~/.profile
The following is already in this file, which I leave untouched:
# ~/.profile: executed by Bourne-compatible login shells.
if [ "$BASH" ]; then
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
fi
mesg n
Immediately below this text, I add the following, and successfully save the file using :wq!:
export PATH=/usr/local/bin:$PATH
export PATH=$PATH:/usr/local/terraform
6.
I then again enter the following command to check to see if terraform is detected
terraform -version
Still the same "terraform: command not found" message is returned. I even tried closing out and starting a new command line session and even restarting my computer. Still no change.
Anyone have any ideas on how to resolve this? Again, note that I am using "Bash on Ubuntu on Windows" (Linux Subsystem). Any input would be appreciated!
The direct answer to your problem is at the end.
But I think it will make more sense if you keep reading from here.
Before trying to add to PATH,
I recommend to test a program first.
In your case I would do like this:
wget https://releases.hashicorp.com/terraform/0.9.8/terraform_0.9.8_linux_amd64.zip
unzip terraform_0.9.8_linux_amd64.zip
./terraform
Notice the last line ./terraform.
The zip file contains a single file, terraform,
which now should be in the current directory,
so I can run it with ./terraform.
If it's executable.
If it's not executable then confirm it:
ls -l terraform
And make it executable if needed:
chmod +x terraform
Now let's add it to PATH.
But first,
let's decide where to put this executable.
/usr/local/bin seems a reasonable location.
So let's move the terraform executable into that directory.
Usually /usr/local/bin is already on PATH,
so you might not need to change anything.
Now you can try your check, and there's a good chance it already works:
terraform -version
If it doesn't, then /usr/local/bin is not on the PATH.
To add it, add this line in ~/.profile:
export PATH=$PATH:/usr/local/bin
Two things looked fundamentally wrong with your approach:
Adding /usr/local/terraform to PATH. This is fishy, because the entries on PATH must be directories, and in your post nothing indicates that you created a directory at /usr/local/terraform.
You cd into /usr/local, and then unzip the zip file of terraform. The linked zip contains a single file named terraform, so /usr/local/terraform in your example should be a file.
If it is a file, then you could make it executable as terraform by adding to add to PATH its base directory. But adding /usr/local to PATH would not be a good idea. It's conventional to put binaries into /usr/local/bin, not directly into /usr/local
You did not mention how you reloaded ~/.profile. After editing this file, the new commands you added do not get automatically executed in your current shell. They will get executed when you open a new shell. Or you could manually execute the added commands in the current shell.
Hit below command
export PATH=$PATH:/usr/local/bin

Create a file in a filesystem

I've been requested to run a code on a file in a file system I had to create.
(I created the fs using mkfs and then mounted it with another directory: /home/may/new_place (the original fs appears on my desktop - 8.6GB filesystem)
My question is, can you even create a file in a filesystem? I can't even transfer a file into it, so can't execute my code.
I'm really new to this.. thank you all
(P.S. I'm using linux xubunto OS)
are you able to touch any files -- with touch command -- also open a sample file -- write echo $HOSTNAME - save it - chmod u+x and run it -- see if you are able to execute the file

In Linux (Ubuntu), how do I determine which specific file was not found when I see "No such file or directory"

In Ubuntu, I give these commands and obtain this output:
soujanya#LLN-Ubuntu:~/workspace/openEAR-0.1.0$ ls -l SMILExtract
-rwxr-xr-x 1 soujanya soujanya 3789876 Aug 20 2009 SMILExtract
soujanya#LLN-Ubuntu:~/workspace/openEAR-0.1.0$ whoami
soujanya
soujanya#LLN-Ubuntu:~/workspace/openEAR-0.1.0$ ./SMILExtract
bash: ./SMILExtract: No such file or directory
soujanya#LLN-Ubuntu:~/workspace/openEAR-0.1.0$
SMILExtract is an executable file (not shell script) and I do not have access to the source code of this file. Maybe it calls some system() or maybe not, no way for me to know.
I have heard that this error might be if the file is 32-bit and I run it on a 64-bit system, so No such file or directory refers to the loader and not this file. I think this is not the cause in my case, but anyway, my question is:
Is there a way to find out WHICH file is No such file or directory? Maybe a special variable in Bash or something like this.
You can run programs with strace, a tool that shows you which system calls are used by a program. It'll produce a lot of output, but you can see the files your program attempts to open. Run your program like this:
strace ./SMILExtract
To be sure about the 32/64 bit question you could 'file ./SMILExtract'

How to assign execute permission to a .sh file in windows to be executed in linux

Here is my problem,
In Windows I am making a zip file in which there is a text .sh file which is supposed to be executed in Linux.
The user on the other end opens the zip file in Linux and tries to execute the .sh file but the execute permission is gone. So the user has to do it manually ( like explained here:add execute permission.
How can I in Windows make the .sh executable and add it to a zip file so that when the zip file opens in linux the .sh file still retains its execute permission ( so that user doesn't have to do it manually)
As far as I know the permission system in Linux is set up in such a way to prevent exactly what you are trying to accomplish.
I think the best you can do is to give your Linux user a custom unzip one-liner to run on the prompt:
unzip zip_name.zip && chmod +x script_name.sh
If there are multiple scripts that you need to give execute permission to, write a grant_perms.sh as follows:
#!/bin/bash
# file: grant_perms.sh
chmod +x script_1.sh
chmod +x script_2.sh
...
chmod +x script_n.sh
(You can put the scripts all on one line for chmod, but I found separate lines easier to work with in vim and with shell script commands.)
And now your unzip one-liner becomes:
unzip zip_name.zip && source grant_perms.sh
Note that since you are using source to run grant_perms.sh, it doesn't need execute permission
The ZIP file format does allow to store the permission bits, but Windows programs normally ignore it.
The zip utility on Cygwin however does preserve the x bit, just like it does on Linux.
If you do not want to use Cygwin, you can take a source code and tweak it so that all *.sh files get the executable bit set.
Or write a script like explained here
This is possible using the Info-Zip open-source Zip utilities. If unzip is run with the -X parameter, it will attempt to preserve the original permissions. If the source filesystem was NTFS and the destination is a Unix one, it will attempt to translate from one to the other. I do not have a Windows system available right now to test the translation, so you will have to experiment with which group needs to be awarded execute permissions. It'll be something like "Users" or "Any user"
Use my windows command line utility zip_exec.zip to set the executable flag for linux/unix and mac (tested on files created with Windows Explorer and 7zip). The cpp source is also available. I searched the internet a lot before making my own utility. It can be modified to set any file attribute.
This is not possible. Linux permissions and windows permissions do not translate. They are machine specific. It would be a security hole to allow permissions to be set on files before they even arrive on the target system.

shell script cd fails even though the path is correct

I need to do a script to extract a tar at a specified location.
I did something simple like:
cp test.tar /var/www/html
cd /var/www/html
tar xvf test.tar
If I execute the commands by hand everything is OK. If I save them in a .sh then use #bash script.sh, I get the following error ": Not a directory cd: /var/www/html". Any ideea why?
Ty for your time.
Notes: I tried the script version on a virtual machine (CentOS 5.5) and the script worked fine, the problem occurs on the real machine where I want to use it (I used same OS disk image, same configurations as on the virtual machine... this makes it really really odd for me).
Added: Also I try invoking something like service mysqld start... this also fails saying that a dir doesn't exist (still if I run by hand it works.).
I solved the problem - it is quite interesting).
I created the script on a virtual machine running on windows with a centos os, the enter in windows is "\r\n" while in linux is "\n".
The script worked on the vm because the code for enter was correct, while on the second computer, with native linux it was incorrect. I created exactly the same script on linux and everything went back 2 normal ;).
Note... the mkdir part worked because I used another, simplified script written on linux.
On a related note, I have found that the "~" character does not seem to work in bash, so if you are using that, try replacing it with the full path.
It looks like your cp might be coping test.jar to the file html under the www directory. Make sure that html exists and is a directory before you try to cp.
mkdir -p /var/www/html
cp test.tar /var/www/html
cd /var/www/html
tar xvf test.tar

Resources