XP-rience

Something that I'd like to share with you!

Thursday, October 28, 2021

Mounting a Clonezilla image to a running OS

No comments :

I have a Clonezilla image of Ubuntu OS and wanted to extract just a directory of important files from it. I'm trying to avoid restoring the whole image into a physical or virtual machine. What I can do is to extract the raw image file from the Clonezilla image itself and mount it onto a running OS. Below are the steps.

Extract the compressed raw image file(s) from the Clonezilla image. The easiest way to do this is by using 7-zip, open and extract under Windows (open and drag). Example below shows that I have 2 partitions stored in my Clonezilla image, "sdb1" & "sdb2". Copy out the compressed raw image partition file that you are interested in. For my case is "sdb2". Note that if you partition is too big, Clonezilla might split it and store more than than 1 file. They are differentiate by the same base name but different extension such as "*.aa", "*.ab" & "*.ac".

Next is to decompress the file by issuing command below. [ ref = link ].

# cat sdb2.ext4-ptcl-img.gz.aa | gzip -d > sdb2.ext4-ptcl-img

If you have more than 1 file, please issue command below. [ref = link ].

# cat sdb2.ext4-ptcl-img.gz.* | gzip -c -d > sdb2.ext4-ptcl-img

Next is to restore the decompressed file back into raw image format. Use "partclone.restore" tool to do this.

# partclone.restore -s sdb2.ext4-ptcl-img -o sdb2.ext4-ptcl-img_restore
Partclone v0.2.86 http://partclone.org
Starting to restore image (-) to device (file.img)
device (file.img) is mounted at
error exit

If you encounter error above, it is probably because of the "partclone.restore" version mismatch. On the current OS we have "partclone.restore" v0.2.86. But when the ISO was created with Clonezilla, it was using different version of "partclone" and having incompatibility issue. You can try this trick by find the exact version of the "partclone.restore" copy from the Clonezilla image itself. It turns out for my case the "partclone.restore" version is newer which is v0.3.11.

Copy the "partclone.restore" into the current OS and "chmod +x" it to make it executable. Run the extracted "partclone.restore". Use option -C to disable size checking, -O to override output file if it is already exists and -W to create special raw file for loop device.

#./partclone.restore -C -s sdb2.ext4-ptcl-img -O sdb2.ext4-ptcl-img_restore
Partclone v0.3.11 http://partclone.org
Starting to restore image (sdb2.ext4-ptcl-img) to device (sdb2.ext4-ptcl-img_restore)
Calculating bitmap... Please wait...
done!
File system:  EXTFS
Device size:   10.5 GB = 2560000 Blocks
Space in use:   6.9 GB = 1695256 Blocks
Free Space:     3.5 GB = 864744 Blocks
Block size:   4096 Byte
Elapsed: 00:00:20, Remaining: 00:00:00, Completed: 100.00%, Rate:  20.83GB/min,
current block:    2558079, total block:    2560000, Complete: 100.00%
Total Time: 00:00:20, Ave. Rate:   20.8GB/min, 100.00% completed!
Syncing... OK!
Partclone successfully restored the image (sdb2.ext4-ptcl-img) to the device (sdb2.ext4-ptcl-img_restore)
Cloned successfully.

Now try to mount it with "-o loop" and "-t ext4" options.

# mount -o loop -t ext4 sdb2.ext4-ptcl-img_restore temp

If you encounter below error, look for a clue from "/var/log/syslog".

mount: wrong fs type, bad option, bad superblock on /dev/loop0,
missing codepage or helper program, or other error
In some cases useful info is found in syslog - try
dmesg | tail or so.

# tail /var/log/syslog
Oct 20 05:10:18 xxx kernel: [34532.566686] EXT4-fs (loop0): bad geometry: block count 2560000 exceeds size of device (2558079 blocks)

I've no exact answer for this but you can refer to this link for more answer on this [ link ]. I believe because of the "sdb2.ext4-ptcl-img_restore" raw image file is not reaching the actual block count of 2560000. Thus from the URL, issue was solved by truncating it to 2560000 with -o option to treat SIZE as number of IO blocks instead of bytes.

# truncate -o -s 2560000 sdb2.ext4-ptcl-img_restore

Now, try to mount again.

# mount -o loop -t ext4 sdb2.ext4-ptcl-img_restore temp

Finnally, the image is now mounted.

# lsblk
NAME   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
loop0    7:0    0   9.8G  0 loop /mnt/disk/temp

Wednesday, October 27, 2021

Ubuntu .desktop file shortcut or launchers not working

No comments :

I encounter this issue while installing Intel Thermal Analysis Tool (TAT) under Ubuntu 20.04. The desktop launchers seems didn't installed correctly.


It will open an editor showing the content of the .desktop file if you try to launch it. To fix this, simply right click at the .desktop file and select "Allow Launching".


Now as you can see, the icon has changed indicating the .desktop file is now launchable.


There you go, the Intel TAT is now running fine.


Friday, August 27, 2021

Passwordless SSH login under Linux using RSA public-private key pairs

No comments :

For some reason, we might want to have passwordless SSH access. This will be useful for automation scripts that are running without user intervention.

In order to do this, the remote SSH server needs to be able to identify the SSH client. This is where the “~/.ssh/authorized_keys” file comes into the picture. The idea is to generate an RSA key from the client side and copy it to the remote server “~/.ssh/authorized_keys” file. If everything is done correctly, the remote SSH server will then be able to identify the SSH client that matched the RSA key and allow passwordless SSH connection.


For example below we are using 2 OS for testing, local client is running CentOS and remote server is running Ubuntu. Normal SSH as below will require the user to enter a password.

leorick@localhost ~]$ ssh leorick@192.168.100.17
leorick@192.168.100.17's password: 
Welcome to Ubuntu 20.04.1 LTS (GNU/Linux 5.11.0-25-generic x86_64)
...
Your Hardware Enablement Stack (HWE) is supported until April 2025.
Last login: Fri Aug 27 04:49:52 2021 from 192.168.100.223
leorick@ubuntu:~$

First step, generate local "id_rsa.pub" from local CentOS client by issuing “ssh-keygen -t rsa” command.

[leorick@localhost ~]$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/leorick/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /home/leorick/.ssh/id_rsa.
Your public key has been saved in /home/leorick/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:+7Ar4vN/uQlYinKVgP/UifRWYqGOaT+sysMST0vZbz0 leorick@localhost.localdomain
The key's randomart image is:
+---[RSA 3072]----+
|        .        |
|   .   o         |
|  . . o o .      |
|   . * * +       |
|   o= * S        |
|. +..B = .       |
| *.oo.B +  .     |
|..=o=.o. =o.     |
| .o=o+E++o+.     |
+----[SHA256]-----+

Next, send the “id_rsa.pub” file to the remote Ubuntu server using any method available. Example below is how to send the file using SCP.

leorick@localhost ~]$ scp /home/leorick/.ssh/id_rsa.pub  leorick@192.168.100.17:/home/leorick/.ssh/
leorick@192.168.100.17's password: 
id_rsa.pub                                    100%  583     1.0MB/s   00:00    
[leorick@localhost ~]$

On the remote Ubuntu server, copy the content of the “id_rsa.pub” file into the “~/.ssh/authorized_keys” file of the particular user home directory that you are going to login with.

leorick@ubuntu:~/.ssh$ pwd
/home/leorick/.ssh
leorick@ubuntu:~/.ssh$ cat id_rsa.pub 
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQCrLZsQkR2Sev5s/9cxf64vI1BiId+4CcHxmMIA5B+T2n+R1kZ4iA1u+tut3tKlWI9G9NQnH+yC6nJlBwtjf3szYyn66fIf3mxCwZ+ikFQt9eaOg7h2Wz/WKFx0MHnol3WgVkJwwJqWB0EUaLikJgU4IDjatBpp7X7U2nvVzDHMIIBnCuDYG3NL4nT1JacxZ4KeccRewJN7qWsb+kCJfcwKZRRyiSEvn+Fnk6RDrenSefm6mnxTQJ35kiVGQ2gkpiNzUcR+yrXkZQJyLQso7nEzjmob/ecRbxxj8nZtP2MbPxtU8tIEQjYwkbRuh7acFUw0I8Y4qBS4aKP01zZXrWz7WVoA86a2JvOgwIup0F8+sjXNxzbRR3rgQqV5AitpcwI7zhRVWsHQoL2+mCkIES0hHqEDHwuwnFYk81AOKIEoFRoPSk7wMons+C28uHH9ks5VJRYtty58Vrc6AMWjtVKSaiPRildUZSI7b/HQkQ3DVofpEPnmSJKhiuBEF4Fbk30= leorick@localhost.localdomain
leorick@ubuntu:~/.ssh$ cat id_rsa.pub > authorized_keys

Now login can be done without password.

[leorick@localhost ~]$ ssh leorick@192.168.100.17
Welcome to Ubuntu 20.04.1 LTS (GNU/Linux 5.11.0-25-generic x86_64)
...
Your Hardware Enablement Stack (HWE) is supported until April 2025.
Last login: Fri Aug 27 04:50:57 2021 from 192.168.100.223
leorick@ubuntu:~$

.

Saturday, August 21, 2021

Bash - progress bar

No comments :



Example on using bash loop to draw an ASCII progress bar with BASH.

The dispProgBar function takes 2 arguments, current progress and total progress number. In this example it starts with 1 until 10000. 

dispProgBar will then calculate percentage of progress and current bar length.

1st loop will draw current progress bar with a dark block (ASCII 219).

2nd loop will draw remaining progress bar with a light graphic character (ASCII 176).

At the end of the progress bar it will send "\r" to go back to the beginning of the line without printing a newline.
#!/bin/bash

function dispProgBar(){
	curProg=$1
	totalProg=$2
	barFullLen=50
	((perc = $curProg * 100 / $totalProg))
	((barLen = $curProg * $barFullLen / $totalProg))
	printf "%3s%% [" $perc

	for (( c=1; c<=$barLen; c++ )); do
		echo -ne "█"
	done

	for (( c=$barLen; c<$barFullLen; c++ )); do
		echo -ne "░"
	done

	echo -en "]\r"

	if [ $curProg -ge $totalProg ]; then
		echo ""
	fi
}

for i in {1..10000}
do
	dispProgBar $i 10000
done

Friday, April 23, 2021

Bash trap - signal trapping with Unix shell

No comments :

Linux/Unix shell/Bash trap command "Trap" can be used to execute a function/command on receiving certain signal. To list down all available signals, do "trap -l"
# trap -l
 1) SIGHUP       2) SIGINT       3) SIGQUIT      4) SIGILL       5) SIGTRAP
 6) SIGABRT      7) SIGBUS       8) SIGFPE       9) SIGKILL     10) SIGUSR1
11) SIGSEGV     12) SIGUSR2     13) SIGPIPE     14) SIGALRM     15) SIGTERM
16) SIGSTKFLT   17) SIGCHLD     18) SIGCONT     19) SIGSTOP     20) SIGTSTP
21) SIGTTIN     22) SIGTTOU     23) SIGURG      24) SIGXCPU     25) SIGXFSZ
26) SIGVTALRM   27) SIGPROF     28) SIGWINCH    29) SIGIO       30) SIGPWR
31) SIGSYS      34) SIGRTMIN    35) SIGRTMIN+1  36) SIGRTMIN+2  37) SIGRTMIN+3
38) SIGRTMIN+4  39) SIGRTMIN+5  40) SIGRTMIN+6  41) SIGRTMIN+7  42) SIGRTMIN+8
43) SIGRTMIN+9  44) SIGRTMIN+10 45) SIGRTMIN+11 46) SIGRTMIN+12 47) SIGRTMIN+13
48) SIGRTMIN+14 49) SIGRTMIN+15 50) SIGRTMAX-14 51) SIGRTMAX-13 52) SIGRTMAX-12
53) SIGRTMAX-11 54) SIGRTMAX-10 55) SIGRTMAX-9  56) SIGRTMAX-8  57) SIGRTMAX-7
58) SIGRTMAX-6  59) SIGRTMAX-5  60) SIGRTMAX-4  61) SIGRTMAX-3  62) SIGRTMAX-2
63) SIGRTMAX-1  64) SIGRTMAX
Please refer to https://www.computerhope.com/unix/utrap.htm for more information on this. Example common usage in a shell script, to perform cleanup when EXIT signal detected. Maybe to delete certain temporary files upon receiving CTRL+C. Example script
#!/bin/bash
cleanup(){
	echo
	echo "running cleanup here"
	echo "delete temporary files?"
	echo
}
trap cleanup EXIT
read -p "Press enter to continue"
read -p "Press enter to continue again"
read -p "Press enter to continue again"
When run, on receiving CTRL+C while the script is running (before the last line) will trigger the "cleanup" function.
# ./test.sh
Press enter to continue
Press enter to continue again^C
running cleanup here
delete temporary files?

Tuesday, April 20, 2021

Bash select - Creating simple menu with the Unix shell "select" loop

No comments :

Bash select command creates a menu from a list of items. The syntax is quite similar as the "for loop". Please refer to link below for more info https://linuxize.com/post/bash-select/ https://linuxhint.com/bash_select_command/ Lets do a quick test. Create a simple for loop as below and run it.
#!/bin/bash
for i in the quick brown fox jumps over the lazy dog
do
	echo $i
done
You will get a list of word as below.
# ./test.sh
the
quick
brown
fox
jumps
over
the
lazy
dog
Now modify the script as below. Replace "for" with "select". Add "break" just under the "echo" line.
#!/bin/bash
select i in the quick brown fox jumps over the lazy dog
do
	echo word selection = $i
	break
done
Now run the script and you will get below output.
# ./test.sh
1) the
2) quick
3) brown
4) fox
5) jumps
6) over
7) the
8) lazy
9) dog
#? 6
word selection = over
Note that, without "break", the script will keep on prompting the selection until you hit CTRL+C.

Monday, April 19, 2021

Bash array - how to use arrays in Unix shell

No comments :


Unix shell do support array. Yes, it does. Please refer to https://opensource.com/article/18/5/you-dont-know-bash-intro-bash-arrays OR https://www.tutorialspoint.com/unix/unix-using-arrays.htm for more info on this. To define an array, do;
myArray=(the quick brown fox jumps over the lazy dog)
But how to read an array?. Lets try this;
#!/bin/bash
myArray=(the quick brown fox jumps over the lazy dog)
echo $myArray
Run it ...
# ./test.sh
the
It will only echo the 1st word from the array. Lets try another example.
#!/bin/bash
myArray=(the quick brown fox jumps over the lazy dog)
echo ${myArray[0]}
echo ${myArray[1]}
echo ${myArray[2]}
echo ${myArray[3]}
echo ${myArray[4]}
echo ${myArray[5]}
echo ${myArray[6]}
echo ${myArray[7]}
echo ${myArray[8]}
echo ${myArray[9]}
Run it ...
# ./test.sh
the
quick
brown
fox
jumps
over
the
lazy
dog
Now, It will print all array content. Note that, it won't give any alert if the index is out of bound, for this example = "${myArray[9]}" Lets do a proper way to read an array by using "for loop";
#!/bin/bash
myArray=(the quick brown fox jumps over the lazy dog)
for i in ${myArray[@]}
do
    echo $i
done
Run it ...
# ./test.sh
the
quick
brown
fox
jumps
over
the
lazy
dog
You will get above output. Adding an "!" to the array name will change the loop from looping through the values to looping through the indices.
#!/bin/bash
myArray=(the quick brown fox jumps over the lazy dog)
for i in ${!myArray[@]}
do
    echo $i
done
Run it ...
# ./test.sh
0
1
2
3
4
5
6
7
8
Example below is using "while loop" to loop through the array.
#!/bin/bash
myArray=(the quick brown fox jumps over the lazy dog)
index=0
while [ $index -lt ${#myArray[@]} ]
do
    echo $index = ${myArray[index]}
    index=`expr $index + 1`
done
Run it ...
# ./test.sh
0 = the
1 = quick
2 = brown
3 = fox
4 = jumps
5 = over
6 = the
7 = lazy
8 = dog
Here are some syntax list that might be useful.
Syntax Result
myArray=() Create an empty array
myArray=(1 2 3) Initialize array
${myArray[2]} Retrieve all elements
${myArray[@]} Retrieve all elements
${!myArray[@]} Retrieve array indices
${#myArray[@]} Calculate array size
myArray[0]=3 Overwrite 1st element
myArray+=(4) Append value(s)
${arr[@]:s:n} Retrieve n elements starting at index s