Understanding write-through, write-around and write-back caching

A cache is a hardware or software component that stores data so future requests for that data can be served faster 


Write-through#
Using the write-through policy, data is written to the cache and the backing store location at the same time. The significance here is not the order in which it happens or whether it happens in parallel. The significance is that I/O completion is only confirmed once the data has been written to both places.

Advantage: Ensures fast retrieval while making sure the data is in the backing store and is not lost in case the cache is disrupted.

Disadvantage: Writing data will experience latency as you have to write to two places every time.

What is it good for?#
The write-through policy is good for applications that write and then re-read data frequently. This will result in slightly higher write latency but low read latency. So, it’s ok to spend a bit longer writing once, but then benefit from reading frequently with low latency.

Write-around#
Using the write-around policy, data is written only to the backing store without writing to the cache. So, I/O completion is confirmed as soon as the data is written to the backing store.

Advantage: Good for not flooding the cache with data that may not subsequently be re-read.

Disadvsntage: Reading recently written data will result in a cache miss (and so a higher latency) because the data can only be read from the slower backing store.

What is it good for?#

The write-around policy is good for applications that don’t frequently re-read recently written data. This will result in lower write latency but higher read latency which is a acceptable trade-off for these scenarios.

Write-back#
Using the write-back policy, data is written to the cache and Then I/O completion is confirmed. The data is then typically also written to the backing store in the background but the completion confirmation is not blocked on that.

Advantage: Low latency and high throughput for write-intensive applications.

Disadvantage: There is data availability risk because the cache could fail (and so suffer from data loss) before the data is persisted to the backing store. This result in the data being lost.

What is it good for?#
The write-back policy is the best performer for mixed workloads as both read and write I/O have similar response time levels. In reality, you can add resiliency (e.g. by duplicating writes) to reduce the likelihood of data loss.

Recover system with missing initramfs


Issue observed in Lenevo systemx Avago megaraid sas-mfi bios - server raid M520, while running update-initramfs (corrupted)

/boot is using /dev/sda2

The root partition is using LVM-root in this server(Ubuntu 16.04):-

mount /dev/mapper/lvm-root /mnt (which was using PV sda3)

sudo mount --bind /dev /mnt/dev
sudo mount --bind /dev/pts /mnt/dev/pts
sudo mount --bind /proc /mnt/proc
sudo mount --bind /sys /mnt/sys

chroot /mnt

mount /dev/sda2 /mnt/boot  #sda2 has /boot files

///////////////////////// This was optional in this case  ////////////////////////////

check and install grub on the boot partition

This time you only need to add the drive letter (usually a) to replace X, for example: grub-install /dev/sda, grub-install –recheck /dev/sda.

grub-install /dev/sda2
grub-install --recheck /dev/sda2
update-grub

/////////////////////////////////////////////////////////////////////////////////////
update-initramfs -u -k all


/////////////////////

reboot




ubuntu boot from particular kernel version

If kernel version to boot from Linux 4.15.0-32-generic
Get menuentries

root@Ubuntu9-141:~# grep -A100 submenu  /boot/grub/grub.cfg |grep menuentry
submenu 'Advanced options for Ubuntu GNU/Linux' $menuentry_id_option 'gnulinux-advanced-b56fcaaa00cf79aa' {
menuentry 'Ubuntu GNU/Linux, with Linux 4.15.0-54-generic' --class Ubuntu --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-4.15.0-54-generic-advanced-b56fcaaa00cf79aa' {
menuentry 'Ubuntu GNU/Linux, with Linux 4.15.0-54-generic (recovery mode)' --class Ubuntu --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-4.15.0-54-generic-recovery-b56fcaaa00cf79aa' {
menuentry 'Ubuntu GNU/Linux, with Linux 4.15.0-50-generic' --class Ubuntu --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-4.15.0-50-generic-advanced-b56fcaaa00cf79aa' {
menuentry 'Ubuntu GNU/Linux, with Linux 4.15.0-50-generic (recovery mode)' --class Ubuntu --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-4.15.0-50-generic-recovery-b56fcaaa00cf79aa' {
menuentry 'Ubuntu GNU/Linux, with Linux 4.15.0-32-generic' --class Ubuntu --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-4.15.0-32-generic-advanced-b56fcaaa00cf79aa' {
menuentry 'Ubuntu GNU/Linux, with Linux 4.15.0-32-generic (recovery mode)' --class Ubuntu --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-4.15.0-32-generic-recovery-b56fcaaa00cf79aa' {



Edit /etc/default/grub

GRUB_DEFAULT='gnulinux-advanced-b56fcaaa00cf79aa>gnulinux-4.15.0-32-generic-advanced-b56fcaaa00cf79aa'


Update grub

root@Ubuntu9-141:~# update-grub
Generating grub configuration file ...
Found linux image: /boot/vmlinuz-4.4.0-59-generic
Found initrd image: /boot/initrd.img-4.4.0-59-generic
Found linux image: /boot/vmlinuz-3.19.0-73-generic
Found initrd image: /boot/initrd.img-3.19.0-73-generic
Found linux image: /boot/vmlinuz-3.19.0-43-generic
Found initrd image: /boot/initrd.img-3.19.0-43-generic
Found memtest86+ image: /memtest86+.elf
Found memtest86+ image: /memtest86+.bin
done
...........

reboot to verify





Time Synchronization with NTP and systemd

I recently ran into problems with generating TOTP 2-factor codes on my laptop. The fact that some of the codes would work and some wouldn't suggested a problem with time keeping on my laptop.
This was surprising since I've been running NTP for a many years and have therefore never had to think about time synchronization. After realizing that ntpd had stopped working on my machine for some reason, I found that systemd provides an easier way to keep time synchronized.

The new systemd time synchronization daemon

On a machine running systemd, there is no need to run the full-fledged ntpd daemon anymore. The built-in systemd-timesyncd can do the basic time synchronization job just fine.
However, I noticed that the daemon wasn't actually running:
$ systemctl status systemd-timesyncd.service 
● systemd-timesyncd.service - Network Time Synchronization
   Loaded: loaded (/lib/systemd/system/systemd-timesyncd.service; enabled; vendor preset: enabled)
  Drop-In: /lib/systemd/system/systemd-timesyncd.service.d
           └─disable-with-time-daemon.conf
   Active: inactive (dead)
Condition: start condition failed at Thu 2017-08-03 21:48:13 PDT; 1 day 20h ago
     Docs: man:systemd-timesyncd.service(8)
referring instead to a mysterious "failed condition". Attempting to restart the service did provide more details though:
$ systemctl restart systemd-timesyncd.service 
$ systemctl status systemd-timesyncd.service 
● systemd-timesyncd.service - Network Time Synchronization
   Loaded: loaded (/lib/systemd/system/systemd-timesyncd.service; enabled; vendor preset: enabled)
  Drop-In: /lib/systemd/system/systemd-timesyncd.service.d
           └─disable-with-time-daemon.conf
   Active: inactive (dead)
Condition: start condition failed at Sat 2017-08-05 18:19:12 PDT; 1s ago
           └─ ConditionFileIsExecutable=!/usr/sbin/ntpd was not met
     Docs: man:systemd-timesyncd.service(8)
The above check for the presence of /usr/sbin/ntpd points to a conflict between ntpd and systemd-timesyncd. The solution of course is to remove the former before enabling the latter:
apt purge ntp

Enabling time synchronization with NTP

Once the ntp package has been removed, it is time to enable NTP support in timesyncd.
Start by choosing the NTP server pool nearest you and put it in /etc/systemd/timesyncd.conf. For example, mine reads like this:
[Time]
NTP=time.cloudflare.com
before restarting the daemon:
systemctl restart systemd-timesyncd.service 
That may not be enough on your machine though. To check whether or not the time has been synchronized with NTP servers, run the following:
$ timedatectl status
...
 Network time on: yes
NTP synchronized: no
 RTC in local TZ: no
If NTP is not enabled, then you can enable it by running this command:
timedatectl set-ntp true
Once that's done, everything should be in place and time should be kept correctly:
$ timedatectl status
...
 Network time on: yes
NTP synchronized: yes
 RTC in local TZ: no

move /boot on zfs on root to ext4

# modprobe zfs
# zpool import -a -N -R /mnt
Mount the root pool into /mnt

# zfs mount LP_Pool/ROOT/lp_root
mount -t proc /proc /mnt/proc
mount --rbind /dev /mnt/dev
mount --rbind /sys /mnt/sys

chroot /mnt /bin/bash --login
export ZPOOL_VDEV_NAME_PATH=YES
grub-probe /

sgdisk -p /dev/sdx
#creating
sgdisk -n 1:2048:4095 -c 1:"BIOS Boot Partition" -t 1:ef02 /dev/sdx

#below command will create new partition number "2" of size 3219MB 
sgdisk -n 2:4096:6291456 -c 2:"Linux /boot" -t 2:8300 /dev/sdx

# For  1605 MB
#sgdisk -n 2:4096:3291456 -c 2:"Linux /boot" -t 2:8300 /dev/sdx

mkfs.ext4 /dev/sdx2 and mount it on /newboot

mount -t ext4 /boot/ /newboot/
copy /boot/* to /newboot
rename /newboot to /boot

get UUID of /dev/sdx2 and append to fstab as below:-

UUID=1b1d424c-9fd1-4fd2-aba7-80252f1a8d64 /boot           ext4    defaults        0       2

grub-install /dev/sdx
update-grub2
grub-mkdevicemap
update-initramfs -u -k all

exit from chroot
# zfs unmount -a
# zfs set mountpoint=/ LP_Pool/ROOT/lp_root
# reboot

https://www.rodsbooks.com/gdisk/sgdisk-walkthrough.html

https://pve.proxmox.com/wiki/ZFS:_Tips_and_Tricks

Boot fails and goes into busybox

If booting fails with something like
No pool imported. Manually import the root pool
at the command prompt and then exit.
Hint: try: zpool import -R /rpool -N rpool
is because zfs is invoked too soon (it has happen sometime when connecting a SSD for future ZIL configuration). To prevent it there have been some suggestions in the forum. Try to boot following the suggestions of busybox or searching the forum, and try ONE of the following:
a) edit /etc/default/grub and add "rootdelay=10" at GRUB_CMDLINE_LINUX_DEFAULT (i.e. GRUB_CMDLINE_LINUX_DEFAULT="rootdelay=10 quiet") and then issue a # update-grub
b) edit /etc/default/zfs, set ZFS_INITRD_PRE_MOUNTROOT_SLEEP='4', and then issue a "update-initramfs -k 4.2.6-1-pve -u"


nxlog iis

Panic Soft
### Start off with Definitions ###
define ROOT C:\Program Files (x86)\nxlog
define CERTDIR %ROOT%\cert
define CONFDIR %ROOT%\conf
define LOGDIR %ROOT%\data
define LOGFILE %LOGDIR%\nxlog.log
LOGFILE %LOGDIR%\nxlog.log

Moduledir %ROOT%\modules
CacheDir %ROOT%\data
Pidfile %ROOT%\data\nxlog.pid
SpoolDir %ROOT%\data
LogLevel INFO
#PersistLogqueue TRUE
#SyncLogqueue TRUE
#CacheFlushInterval always
#CacheSync TRUE

<Extension _syslog>
Module xm_syslog
</Extension>

<Extension xml>
Module xm_xml
</Extension>

<Extension _exec>
Module xm_exec
</Extension>

<Extension _json>
Module xm_json
</Extension>

<Extension w3c>
Module xm_csv
Fields $date, $time, $s-ip, $cs-method, $cs-uri-stem, $cs-uri-query, $s-port, $cs-username, $c-ip, $cs(User-Agent), $sc-status, $sc-substatus, $sc-win32-status, $time-taken
FieldTypes string, string, string, string, string, string, integer, string, string, string, integer, integer, integer,integer
Delimiter ' '
QuoteChar '"'
EscapeControl FALSE
UndefValue -
</Extension>

<Extension _fileop>
Module xm_fileop

# Check the size of our log file every hour and rotate if it is larger than 5Mb
<Schedule>
Every 1 hour
Exec if (file_exists('%LOGFILE%') and (file_size('%LOGFILE%') >= 5M)) file_cycle('%LOGFILE%', 8);
</Schedule>
</Extension>



### Get IIS Exchange logs ###

<Input IIS_log>
Module im_file
File "D:\\Inetlogs\\W3SVC1\\\\u_ex*.log"
InputType LineBased
SavePos TRUE
#Drop info legend lines
Exec if $raw_event =~ /^\#/ drop();\
else\
{\
w3c->parse_csv();\
$EventTime = parsedate($date + " " +$time);\
$date = strftime($EventTime, "%Y-%m-%d");\
}
Exec $Message = $raw_event;\
#Exec $date = strftime($EventTime, "'%Y-%m-%d"); $time = strftime($EventTime, "%H:%M:%S");
</Input>


### Define the output that goes to SIEM for analysis ###

<Output logcontents1>
Module om_tcp
Host SIEM_IP
Port 514
Exec $Hostname = hostname_fqdn();
Exec to_json();
</Output>

### Tie together inputs to outputs ###

<Route r_lp2>
Path IIS_log => logcontents1
</Route>

------------------------------------------------
To pull windows event log All you need to do is add the log name into the query list.

Query <QueryList>\
<Query Id="0">\
<Select Path="Application">*</Select>\
<Select Path="System">*</Select>\
<Select Path="Security">*</Select>\
<Select Path="DNS Server">*</Select>\
<Select Path="Directory Service">*</Select>\
<Select Path="DFS Replication">*</Select>\
<Select Path="Active Directory Web Services">*</Select>\
</Query>\
</QueryList>
To find the name of the log you want to add the easiest way is to

open event viewer

select custom view

select the event logs you want from the dropdown

switch to the XML tab

It will have a very similar look but you have to make it look exactly like my code above. Note the backslash at the end.



rsync through jumphost handy command

rsync -azvP -e 'ssh -A -J user1@10.99.1.50:22'  user1@192.168.20.163:/opt/backup/configuration/zipped/configuration_2020-03-19-07-47-44.backup  /path/local/

macos spotlight indexing problem

Intermittent stopping of spotlight on macos mojave 2020/03/26

urgens-MacBook-Pro:~ urgensherpa$ sudo mdutil -a -i off
/:
2020-03-26 12:40:18.654 mdutil[750:14995] mdutil disabling Spotlight: / -> kMDConfigSearchLevelFSSearchOnly
Indexing disabled.

urgens-MacBook-Pro:~ urgensherpa$ sudo launchctl unload -w /System/Library/LaunchDaemons/com.apple.metadata.mds.plist
/System/Library/LaunchDaemons/com.apple.metadata.mds.plist: Operation not permitted while System Integrity Protection is engaged

urgens-MacBook-Pro:~ urgensherpa$ sudo launchctl load -w /System/Library/LaunchDaemons/com.apple.metadata.mds.plist

/System/Library/LaunchDaemons/com.apple.metadata.mds.plist: service already loaded

urgens-MacBook-Pro:~ urgensherpa$ sudo mdutil -a -i on
/:
Indexing enabled. 

ZFS: Performance Tuning for Scrubs and Resilvers

Jason Matthews


My environment is write heavy. I have hundreds of millions of phones sending me events continously. They are like billions and billions of star like bubbles invading my spinning rust.
Consequently, when a disk fails or takes on excessive CRC errors a resilver job is kicked off. Similarly, I scrub the storage pools monthly and occasionally turn up a bad disk that is silently corrupting. The problem is the resilver or scrub job reports that it needs two or three months to complete, with the default settings. That doesn’t work for me. If you are reading this, you might have a similar problem.
I would prefer to throttle the inbound data and repair the system to optimal data redundancy. Around here, data integrity is everything. To complete the scrubs and resilvers quickly, I have had to tweak ZFS in an Evil way. First, we’ll talk about what to put into /etc/system, then I will show you how to make these changes take effect immediately.
The /etc/system changes are straight forward:

* Prioritize resilvering by setting the delay to zero
set zfs:zfs_resilver_delay = 0
* Prioritize scrubs by setting the delay to zero
set zfs:zfs_scrub_delay = 0
* set maximum number of inflight IOs to a reasonable value - this number will vary for your environment
set zfs:zfs_top_maxinflight = 128
* resilver for five seconds per TXG
set zfs:zfs_resilver_min_time_ms = 5000

The default value here is three seconds. By giving resilver five seconds per transaction group we can rebuild the mirror more quickly.
Now, to make these changes take effect immediately so you dont have to reboot, you can execute the following:

echo zfs_resilver_delay/w0 | mdb -kw
echo zfs_scrub_delay/w0 |mdb -kw
echo zfs_top_maxinflight/w7f |mdb -kw
echo zfs_resilver_min_time_ms/w1388 |mdb -kw
These variables and more can be found in dsl_scan.c
These modifications speed up my scrub/resilver operations from taking a approximately two months, to approximately two to three hours depending on the individual system.

zfs move boot to ext4

move boot to ext4
  1. Create your ext partition
  2. Mount it somewhere temporarily
  3. rsync -avPX /boot/. /<tempboot>/.
  4. unmount /<tempboot>
  5. Move /boot to another location on zfs (eg. /bootbackup)
  6. Recreate /boot mountpoint and mount your new partition there
  7. Add the new boot ext4 partition to /etc/fstab
UUID=1b1d424c-9fd1-4fd2-aba7-80252f1a8d64 /boot           ext2    defaults        0       2
  1. Re-run update-grub (maybe even update-initramfs for good measure)
  2. reboot
 The /bootbackup allows you to a location to periodically rsync your /boot ext4 into the pool for recovery purposes.

after reboot you might have to manually select the second partition as boot device
#############
# zpool import -a -N -R /mnt
Mount the root pool into /mnt
# zfs mount LP_Pool/ROOT/lp_root
mount -t proc /proc /mnt/proc
mount --rbind /dev /mnt/dev
mount --rbind /sys /mnt/sys
chroot /mnt /bin/bash --login

sgdisk -p /dev/sdx
#creating 
sgdisk -n 1:2048:4095 -c 1:"BIOS Boot Partition" -t 1:ef02 /dev/sdx
#below command will create new partition number "2" of size 3219MB 
sgdisk -n 2:4096:6291456 -c 2:"Linux /boot" -t 2:8300 /dev/sdx 

mkfs.ext4 /dev/sdx2 and mount it on /newboot
copy /boot/* to /newboot
rename /newboot to /boot

get UUID of /dev/sdx2 and change fstab as below:-

UUID=1b1d424c-9fd1-4fd2-aba7-80252f1a8d64 /boot           ext4    defaults        0       2

grub-install /dev/sdx
update-grub2
grub-mkdevicemap
update-initramfs -u -k all

exit from chroot

# zfs unmount -a
# zfs set mountpoint=/ LP_Pool/ROOT/lp_root
# reboot












############

UPDATE (2020-01-27): The SuperMicro X10SRH-CLN4F motherboard boots just fine from a root-on-ZFS disk in UEFI mode from the built-in SAS HBA. The only required change is the last step in the description below; to add a delay before attempting to mount ZFS volumes at boot-time.
There is a potential drawback to installing Proxmox in root-on-ZFS mode in a UEFI system: The drive gets partitioned, so ZFS doesn’t get uninhibited access to the entire block storage. This may or may not make a difference for performance, but in terms of speed on an SSD solution, I haven’t really seen any cause for concern for my real-world use case. An alternative would be to install the underlying operating system to a separate physical drive.
Also note that the workaround below works on a single vFAT volume. Since FAT doesn’t support symlinks, kernel or initramfs updates in Proxmox/Debian will require some manual work, which most sane people would likely wish to avoid.
I’m leaving the rest of my article intact for posterity:

My workaround was to place /boot – not the system – on a USB stick connected directly to the motherboard.

Process

After installation, reboot with the Proxmox installation medium, but select Install Proxmox VE (Debug mode).
When the first shell appears, Ctrl+D to have the system load the necessary drivers.
Check the name of the USB drive.
lsblk
Partition it.
cfdisk /dev/sdb
Clear the disk, create an EFI System partition and write the changes. Then apply a FAT to the new partition
mkfs.vfat /dev/sdb1
Prepare to chroot into the installed Proxmox instance
mkdir /media/rescue
zpool import -fR /media/rescue rpool
mount -o bind /dev /media/rescue/dev
mount -o bind /sys /media/rescue/sys
mount -o bind /dev /media/rescue/dev
chroot /media/rescue
Make room for the new /boot
mv /boot /boot.bak
Edit /etc/fstab and add the following:
/dev/sdb1 /boot vfat defaults 0 0
Make the stick bootable
mount -a
grub-install --efi-directory=/boot/efi /dev/sdb
update-grub
grub-mkconfig -o /boot/grub/grub.cfg
Exit the chroot, unmount the ZFS file system (zfs export rpool)and reboot
In my specific case I had a problem where I got stuck in a shell with the ZFS pool not mountable.
/sbin/zpool import -Nf rpool
Exit to continue the boot process. Then edit /etc/default/zfs and edit a delay before attempting to boot the file system.
ZFS_INITRD_PRE_MOUNTROOT_SLEEP=15
Then apply the new configuration:
update-initramfs -u