Pages

Monday, December 30, 2019

PXE server directly from Arch Linux ISO

Without installing, we can boot Arch Linux installation ISO and set it as a pxe server and boot the arch linux installer to another machine using bios/UEFI Network booting client of the machine.

Using grub4dos ( we may need memdisk as Kernel) we could boot the Arch Linux ISO.

Grub4Dos menu entry for booting arch iso

kernel /memdisk iso raw
initrd /archlinux_<some suffix>.iso
boot

The live installer will be booted from ISO. Without installation we can even setup PXE server. [The Arch PXE guide]

Since it comes from a read only iso, all files will be written in RAM so everyting will be lost on subsequent reboot. So we need to mount a hard disk partition to keep our necessary files for further uses.

e.g. mount /dev/sda5 /mnt


We need to do 3 works to up and run  the Arch Linux PXE server:
  1. Set IP address
  2. Setup and start the dnsmasq service [the core PXE server]
  3. Setup and launch either NBD or NFS  [to supply arch installer to remote client]


SETUP IP Address:

At first we need to add IP address. [ e.g. ip addr add 192.168.0.1/24 dev enp1s0 ]


SETUP dnsmasq service:

We shall setup and start the dnsmasq service that provides essential DHCP and TFTP services for PXE booting.

We can use everything as is by default from /run/archiso/bootmnt or we can customize the pxe boot configuration file.

For the default, the dnsmasq configuration file looks like:

port=0
interface=enp1s0
bind-interfaces
dhcp-range=192.168.0.5,192.168.0.10,12h
#This is a separate entry, relatively it begins after tftp-root
# tftp-root + dhcp-boot
dhcp-boot=arch/boot/syslinux/lpxelinux.0
#These 2 are pair 210 = foldre after tfpt-root; 209 is the config file
# tftp-root + 210 directory + 209 config file
dhcp-option-force=209,boot/syslinux/archiso.cfg
dhcp-option-force=210,/arch/
dhcp-option-force=66,192.168.0.1
enable-tftp
tftp-root=/run/archiso/bootmnt
For customized pxe boot configuration, we need to copy the syslinux/ and x86_64/ directories maintaining a certain path hierarchy, it is important to make it work properly.

Suppose, our TFTP home is /home directory, then the dnsmasq. conf file will be:

port=0
interface=enp1s0
bind-interfaces
dhcp-range=192.168.0.5,192.168.0.10,12h
#This is a separate entry, relatively it begins after tftp-root
# tftp-root + dhcp-boot
dhcp-boot=arch/boot/syslinux/lpxelinux.0
#These 2 are pair 210 = foldre after tfpt-root; 209 is the config file
# tftp-root + 210 directory + 209 config file
dhcp-option-force=209,boot/syslinux/archiso.cfg
dhcp-option-force=210,/arch/
dhcp-option-force=66,192.168.0.1
enable-tftp
tftp-root=/home

The issue of maintaining a certain path hierarchy:

dhcp-boot, the main boot loader takes an absolute path and it is fine.

The configuration file (dhcp-option-force=209) path is complicated. The full path is
      tftp-root + 210 directory + 209 config file

Here,   /home            + /arch/                    +  boot/syslinux/archiso.cfg ( instead of boot/syslinux we can put the config file anywhere BUT! .)

But the most complicated is the path of other files like 'whichsys.c32' etc. How they are formed?

      tftp-root + 210 directory + boot/syslinux/<filename>  boot/syslinux/ is a MUST, in this way they asks the TFTPD to send files.

That's why we need to create the directory hierarchy 

/TFTP root/ <something - for 210>/boot/ and inside /boot, copied the syslinux/ and x86_64/ directories. syslinux for pxe boot up and x86_64/ for Arch Linux booting.

cp -r /run/archiso/bootmnt/arch/boot/syslinux /home/arch/boot
cp -r  /run/archiso/bootmnt/arch/boot/x86_64 /home/arch/boot

We can not comment out or erase the option 210, the directory option, it does not work.

The customized arch Linux pxe boot menu takes in the /home/arch/boot/syslinux/archiso_pxe.cfg file. We removed the AMD and Intel specific initrd files and adding coytoram=n as kernel parameter. Our old machine could not boot otherwise.

The GRUB edit seemed a tedious job, and the default archiso mount folder /run/archiso/bootmnt is read-only, we do this work around.
----------------------------------

SETUP NBD:

Once it is done, we can either use NBD - Network Block Device ( to load archlinux in PXE client from ISO directly); configuration file location for NBD is /etc/nbd-server/config

Sample config file content:

[generic]
# The [generic] section is required, even if nothing is specified there.
[archiso]
readonly = true
exportname = /path/to/archlinux-2019.08.01-x86_64.iso

Now, we can launch the NBD service: systemctl start nbd.service
----------------------------------

SETUP NFS:
Or, we can setup NFS - Network File System that will use the arch live directory /run/archiso/bootmnt/ ; configuration file is /etc/exports

In /etc/exports we appended the line:
/run/archiso/bootmnt 192.168.0.1/24(ro,no_subtree_check)
To launch the NFS service: systemctl start nfs-server.service
----------------------------------


SHARING the internet with PXE client:

Since Arch Linux pacman needs internet to download packages, we shared the internet connection. We connected the server with internet and share connection with client. Server is equipped with 2 NIC cards.

Arch pxe wiki provides the commands to be executed in the server:

iptables -t nat -A POSTROUTING -s 192.168.0.0/24 -j MASQUERADE
We also made the internet gateway as default gateway:

route add default gw <inet v4 IP address of gateway> <inet interface name>
----------------------------------

ALL in a single run:

This is script that composes all necessary commands. So my job is even easier now, mounting the partition in /mnt where all my scripts are stored, run the commands by 

zsh command_file_name
and done. PXE server is ready. Here is the commands file content:




ip addr add 192.168.0.1/24 dev enp1s0 # For default boot options cp -f /mnt/dnsmasq.conf /etc/dnsmasq.conf # For customized boot parameters. cp -f /mnt/dnsmasq.customconf.conf /etc/dnsmasq.conf mkdir -p /home/arch/boot/ cp -r /run/archiso/bootmnt/arch/boot/syslinux /home/arch/boot cp -r /run/archiso/bootmnt/arch/boot/x86_64 /home/arch/boot # Modify Arch Kernel parameters sed 's/^INITRD.*,/INITRD /;s/^APPEND/APPEND copytoram=n /' /home/arch/boot/syslinux/archiso_pxe.cfg -i # Now start the service systemctl start dnsmasq.service # For NFS - since a single line we do not created a file echo '/run/archiso/bootmnt 192.168.0.1/24(ro,no_subtree_check)' >> /etc/exports systemctl start nfs-server.service # For NBD systemctl start nbd.service # To setup internet connection in PXE server and share with PXE client: ip addr add <inet v4 IP address>/28 dev <inet interface name> route add default gw <inet v4 IP address of gateway> <inet interface name> # Routing the client IP iptables -t nat -A POSTROUTING -s 192.168.0.0/24 -j MASQUERADE

For a low configuration (lower RAM) machine, NBD seems work better than NFS

Grub4Dos in Windows XP/2003 and Vista/2008

From Run, open %systemroot%\System32\.

Find cmd.exe, right click and Run as administrator, [ otherwise bcdedit in Vista+ will not be able to write/edit].

bcdedit commands to create entry, set boot device or partition grldr.mbr


For XP/2k3, edit boot.ini and load grldr directly.

Grldr requires menu.lst.

Thursday, November 7, 2019

lusrmgr - Removing Windows user password

It works  for 7, 8, server 2003, 2008 and so on.

If the Windows need password to log in, we need boot disk (CD, DVD or usb ).

But enabling auto-login will bring to to the default user desktop. If the user is administrator, and we do not know the password, we can remove or repass using lusrmgr - Local User Manager.

On "Run" - write lusrmgr.msc

Or on cmd console - just write lusrmgr

Rest is simple:

Select Users -> Right click on a user -> Set Password



Click Proceed if this below message or something similar appears.



To remove passwords keep the fields blank and click OK.



Sunday, September 22, 2019

Phishing vulnerability on using target="_blank"; also performance downgrade

target="_blank" and window.open keep the window.opener reference towards parent/source site. Thus it opens the possibility of phishing attack.

Therefore we should use in HTML : rel="noopener noreferrer"

and in Javascript:
const newWindow = window.open('https://gosink.in');
newWindow.opener = null;

For details: here

Source: here

Saturday, September 14, 2019

C# .net file Prepend

In C# .net, there are no built-in feature for file prepend . However, if we want to write a file reversely we can read the lines from backward and write usually in a new file. this will generate a reverse copy of the main file.

We can read reversely like https://www.blakepell.com/2010-11-29-backward-file-reader-vb-csharp-source  - only issue is the the line feed and carriage return characters need to give a backslash (\) in the code.

If we try to write using FileStream and use Seek(0, SeekOrigin.Begin) to position write cursor at the begin ( or in a random position manipulating the offset and SeekOrigin) the contents will be overwritten.

We can use the insertion sort's trick then, we copy-shift the contents down upto the size of new content's size and write to the determined positon, it will overwrite the repeated contents created due to our "copy-shift" operation




So you understand the pain point for a large file of this raw technique.

Windows batch file - non-blocking command, function/subroutine and date wise file name

Let's go through the below batch file, say ping.bat:

REM REM means remark, it is the comment in windows batch file
REM Here we are extracting year, month and day separately from %date% and deriving sub-string

set year=%date:~10,4%
REM  %date:~Begin from, count of letters upto%

set month=%date:~4,2%
set day=%date:~7,2%

REM We are naming a file along with the current year, month and day with the data derived above
set LOGFILE=pingtest-%year%-%month%-%day%.log

REM A subroutine LOG is called and output redirected to a file
call :LOG > %LOGFILE%

REM Non-blocking program execution, start /b will launch the command as a new process and our batch file will continue execution and finish separately
start /b notepad.exe %LOGFILE%

exit /B

:LOG
ping 192.168.1.1
ping 192.168.1.2
ping 192.168.1.3
ping google.com



Saturday, April 20, 2019

Nodejs handling uncaught exceptions globally

For unhandled exceptions:
process.on('uncaughtException', err => {
  console.error('There was an uncaught error', err)
  process.exit(1) //mandatory (as per the Node.js docs)
})

Here  uncaughtException event listener will be called for every such scenario.

Source: here

For unhandled Promise rejection:


process.on('unhandledRejection', err => { console.error('There was an uncaught error', err) process.exit(1) //mandatory (as per the Node.js docs) })


Here unhandledRejection event listener will also be called for every such scenario.

Monday, April 8, 2019

SemVer := Semantic Versioning

Here we are limiting our discussion for node.js semver

For nodejs itself and other npm packages: we see the versioning

j.n.p [ 3 group of digits ] e.g. 2.12.34

j = Major
n = Minor
p = Patch

For a package author the convention of version management is as follow:


  • Increment of Major version: Incompatible API Changes
  • Increment of Minor version: Added functionality with backward compatibility
  • Increment of Patch version: backward compatible bug fixing is added


Presiding Symbols of SemVer:

When npm install/update is run, package versions prepended with

~ will update only patches of the specified major and minor

^ will update only minors of the specified Major

|| logical or operation; either of the packages will be updated for example use 1.0.0 || >=1.1.0 <1.2.0 to either use 1.0.0 or one release from 1.1.0 up, but lower than 1.2.0.

= or no symbol - exactly mentioned package version

<   less than this Major, minor and patch
<= less than or equal to this Major, minor and patch
>   more than this Major, minor and patch
>= more than or equal to this Major, minor and patch


Only latest will update to the latest version.



Monday, April 1, 2019

MySQL master-slave replication configuration

Changes in configuration (mysqld.cnf) file:

For Master:


bind-address =0.0.0.0 [to listen all interfaces]
server-id = 1
For Salve:


bind-address =0.0.0.0 [to listen all interfaces]
server-id = 2

Both Master and Salve MySQL servers need to restart to make changes effective.

Replication commnads in mysql commnad prompt as root user/super admin

For Master:


-- user creation is optional; an existing user can be granted but 
-- need need to be publicly accessible.

mysql > create user 'replication_user'@'%' identified by 'MASTER_PASSWORD';
mysql > GRANT REPLICATION SLAVE ON *.* TO 'replication_user'@'%';
mysql > FLUSH PRIVILEGES;

-- After granting privilege the below command is the mean to collect 
-- master info to be used in salve


mysql> show master status; 

+------------------+----------+--------------+------------------+-------------------+

| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |

+------------------+----------+--------------+------------------+-------------------+

| mysql-bin.000001 |      154 |              |                  |                   |

+------------------+----------+--------------+------------------+-------------------+

1 row in set (0.00 sec)


For Slave:


mysql> stop slave; 
mysql> CHANGE MASTER TO MASTER_HOST = '<MASTER_HOST_IP_OR_ADDRESS>',MASTER_USER = 'replication_user', MASTER_PASSWORD = 'MASTER_PASSWORD'MASTER_LOG_FILE = 'mysql-bin.000001', MASTER_LOG_POS = 154;
mysql > start slave;



** MASTER_LOG_FILE and MASTER_LOG_POS will be according to the values found by " show master status "
Source and Details: here

Thursday, March 28, 2019

Javascript ASCII code to character and vice versa

For capital letter,

65 = A
66 = B
...
89 = Y
90 = Z

String.fromCharCode(An integer); 
e.g. 
String.fromCharCode(66) will return B
String.fromCharCode(105) will return i (lower case i )

For small letter,

a = 97
b = 98
...
z = 122

'a'.charCodeAt(0) will return 97
'cdef'.charCodeAt(3) will return 102

Thursday, March 14, 2019

Javascript for/in for/of forEach and for(;;)

Javascript has different variants of iteration with different feature. Below is a summary of different for loop iteration with their features:



For /in
For / of
Obj.forEach
Traditional for
Object Key type
All (numeric + non-numeric)
Numeric only
Numeric only
Numeric only
Index vs Value
Index only
Value only

To get index Array#entries()
Index + Value 

Index as the second param of callback fn
Index only
Empty element
const arr = ['a',, 'c'];

Ignore empty element
Consider empty element
Ignore empty element
Consider empty element
Function Context (context of this )
this of the outer context
this of the outer context
Own version of
this
this of the outer context
Async / Await + Generator

Yes
Y
With caveats and not straight forward
Y

Side topics emerged here:
Ø  ESLint
Ø  Generator
Ø  Yield
Ø  Async / Await

Source and Details: here


Thursday, February 7, 2019

SQL for Product recommendation

In a shopping cart we can use the following logic to propose recommended products for a certain customer Customer 1.

In SQL, this is a scenario of recursive join.

We have

Customer Table (Entity)
Product Table (Entity)

and Customer_Product_Mapping (nicknamed cpm) (Relationship)

To derive a recommendation list we need to work on the relationship cpm, a ternary join (self join or recursive join)

1. We shall find the product list of our target customer : Customer 1 (cpm)

2. [cpm2] We shall then find the fellow customers who purchased at least 1 of the product Customer 1 has bought.

3. [cpm3] We shall find the rest of product list Customer 1 has not yet bought.

4. By joining the cpm2 and cpm3 we shall get the products peer/fellow customers has bought but not Customer 1, this the recommendation list for customer 1.




The SQL statement is:

SELECT cpm3.product_id,
cpm3.customer_id FROM Customer_Product_Mapping as cpm, Customer_Product_Mapping as cpm2,
Customer_Product_Mapping as cpm3
WHERE

cpm.customer_id = 1  -- produces the cpm

and cpm.product_id = cpm2.product_id and cpm2.customer_id <> 1  -- produces cpm2


and cpm3.product_id not in
(select distinct product_id FROM Table1 cpm WHERE cpm.customer_id = 1)  --produces cpm3

and cpm3.customer_id = cpm2.customer_id -- finally derives recommendation list



Tuesday, January 22, 2019

RESTful API methods: a typicl practice

When we are building RESTful API services, we can use the following methods for a certain actions.

Below we are mentioning a table of API methods, for example, for a Product entity.


Method
Example EndPoint
Meaning
GET
api/products
List of Products
GET
api/products/{id}
View a product
POST
api/products
Create New product
PUT
api/products/{id}
Update a product
DELETE
api/products/{id}
Delete a product



Friday, January 18, 2019

Mapping a constructed Javascript array

Array(100) produces vacant array. Execution of map function will lead undefined.
 
 We need to use spread operator to use map function successfully.
 
 
const arr = [...Array(100)].map((_, i) => i); 
 
 
Source: Shawn Reisner' blog entry