Monday, December 8, 2014

Getting Yesterdays or Tomorrows Day With Bash Shell Date Command

Getting Yesterdays or Tomorrows Day With Bash Shell Date Command


When invoked without arguments, the date command displays the current date and time. Depending on the options specified, date will set the date and time or print it in a user defined way. I've seen many sysadmin writing perl scripts for calculating relative date such as yesterdays or tomorrows day. You can use GNU date command, which is designed to handle relative date calculation such as:
  • 1 Year
  • 2 Days
  • 2 Days ago
  • 5 Years
The syntax is as follows:
 
date  --date="STRING"
date  --date="next Friday"
date  --date="2 days ago"
 
The --date=STRING is a human readable format such as "next Thursday" or "1 month ago". A date string may contain items indicating calendar date, time of day, time zone, day of week, relative time, relative date, and numbers.

Why Use Relative GNU/date Formats?

  • Ease of use
  • Write your own shell scripts
  • Automate task using cron (example run a job on last day of the month or Nth day of the month or 3rd Friday and so on)

Examples

First, to display today's date, enter:
$ date
Sample outputs:
Wed Jun 15 04:47:45 IST 2011
To display yesterday's date, enter:
$ date --date="1 days ago"
OR
$ date --date="1 day ago"
OR
$ date --date="yesterday"
OR
$ date --date="-1 day"

Adding Disks under Linux

Adding Disks under Linux

Once the disk has been physically connected, the system should detect it during the boot process. The dmesg command can be used to check the boot messages:

  Vendor: HP        Model: C3724S            Rev: 5173
  Type:   Direct-Access                      ANSI SCSI revision: 02
Detected scsi disk sdc at scsi0, id 3, lun 0

Device nodes

It may be necessary to create device nodes for the new disk. Under Linux, device nodes use the format /dev/[r]sdXN, where X is a letter specifying the drive number (with a being the lowest SCSI ID, bbeing the next highest, etc), and N is the partition number on that drive. The MAKEDEV command is used to create new device nodes. For example, to create a device node for the third SCSI disk:

# cd /dev; MAKEDEV sdc

It is important to note that disk ordering is done at boot time, so there is not a direct mapping between the letter used to identify the drive and the SCSI ID of that drive. If the SCSI ID of the new drive is lower than that of one of the existing drives, the naming scheme for all of the higher-numbered will be changed, possibly causing problems in the /etc/fstab configuration.

Partitioning

Partitioning and labeling a disk under Linux is done with the fdisk command. There is also a screen-oriented version called cfdisk, which has essentially the same commands.
For instructions on how to use fdisk to partition a drive, see the Linux Installation and Getting Started guide, particularly the section on Creating Linux partitions

Making new filesystems

To create a new filesystem on the disk, use the mkfs command:
# mkfs -t ext2 /dev/sdc1
This will create an ext2 type filesystem on the third SCSI disk, first partition.
To check the new filesystem for integrity, use fsck# fsck -f -y /dev/sdc1 The -f option forces fsck to check the new filesystem, even if it is considered to be clean. The -y option tells fsck to assume a "yes" answer to any questions normally posed to the user.

Adding Disks under Solaris

Adding Disks under Solaris

Once the disk has been physically installed, the system should recognize a new device on the SCSI bus. After powering up the system, hold down the Stop key (on some Suns, this is labeled L1), and hit the A key to enter the boot monitor.
At the boot monitor, probe-scsi can be used to list the SCSI devices the system recognizes:

Type 'go' to resume
Type help for more information
ok probe-scsi
   .
          .
Target 5
  Unit 0  Disk     HP        C37245       5153
          .
          .

Note: on some older Suns, it may be necessary to enter "n" at the boot monitor to enter the newer command mode before probing for disks.
After verifying that the new disk is recognized by the system, reboot the machine by issuing "boot -r" from the boot monitor. The -r option tells the system to reconfigure for the new device.
During the boot process, the new disk should be recognized and a message should be printed to the console. (On some Suns, it may not be printed to the screen, but will be written to the system log -- in this case, the dmesg command should be used to review the boot messages). The messages should be similar to this:

   sd5 at esp0: target 5 lun 0
   sd5 is /iommu@f,e0000000/sbus@f,e0001000/espdma@f,400000/esp@f,800000/sd@5,0
   WARNING: /iommu@f,e0000000/sbus@f,e0001000/espdma@f,400000/esp@f,800000/sd@5,0 (sd5):  
    corrupt label - wrong magic number
    Vendor 'HP', product 'C3724S', 2354660 512 byte blocks 

In this example, the disk is located on controller 0, SCSI ID 5. The "corrupt label" warning means that the disk doesn't have a Solaris label on it yet.

Device nodes

The correct device nodes for the disk are automatically added when a "boot -r" is issued. If the system hasn't been rebooted using the -r option, here is a script that will configure the system for the new disk.

Formatting, Partitioning and Labeling

The format utility is used to format, partition, and label disks. It is menu driven. The raw disk device is given as an argument; if no argument is given, format will print a list of available disks and ask the user to pick one.

# format /dev/rdsk/c0t5d0s2
selecting /dev/rdsk/c0t5d0s2
[disk formatted]
 
 
FORMAT MENU:
        disk       - select a disk
        type       - select (define) a disk type
        partition  - select (define) a partition table
        current    - describe the current disk
        format     - format and analyze the disk
        repair     - repair a defective sector
        label      - write label to the disk
        analyze    - surface analysis
        defect     - defect list management
        backup     - search for backup labels
        verify     - read and display labels
        save       - save new disk/partition definitions
        inquiry    - show vendor, product and revision
        volname    - set 8-character volume name
        quit

Typing format at the prompt will perform a low-level format on the disk. This is usually not necessary with a new disk, since they generally come pre-formatted, but may help to map out any additional defects the drive may have developed.
The next step is to partition the drive. Type partition at the prompt to switch to the partition menu:

format> partition

PARTITION MENU:
        0      - change `0' partition
        1      - change `1' partition
        2      - change `2' partition
        3      - change `3' partition
        4      - change `4' partition
        5      - change `5' partition
        6      - change `6' partition
        7      - change `7' partition
        select - select a predefined table
        modify - modify a predefined partition table
        name   - name the current table
        print  - display the current table
        label  - write partition map and label to the disk
        quit

Type in print to get a listing of the current partition table. Note that the second partition represents the entire disk:

partition> print
Current partition table (original):
Total disk cylinders available: 3361 + 2 (reserved cylinders)
 
Part      Tag    Flag     Cylinders        Size            Blocks
  0 unassigned    wm       0               0         (0/0/0)          0
  1 unassigned    wm       0               0         (0/0/0)          0
  2     backup    wu       0-3360          1.12GB    (3361/0/0) 2352700
  3 unassigned    wm       0               0         (0/0/0)          0
  4 unassigned    wm       0               0         (0/0/0)          0
  5 unassigned    wm       0               0         (0/0/0)          0
  6 unassigned    wm       0               0         (0/0/0)          0
  7 unassigned    wm       0               0         (0/0/0)          0
 

We will be splitting the disk up into two equal partitions, numbers 3 and 4. The first partition will span cylinders 0 through 1680, the second will span cylinders 1681 through 3360. The partition size can be specified in blocks, cylinders, or megabytes by using the bc, and mb suffixes when entering the size.

partition> 3
Part      Tag    Flag     Cylinders        Size            Blocks
  3 unassigned    wm       0               0         (0/0/0)          0
 
Enter partition id tag[unassigned]: 
Enter partition permission flags[wm]: 
Enter new starting cyl[0]: 0
Enter partition size[0b, 0c, 0.00mb]: 1680c
partition> 4
Enter partition id tag[unassigned]: 
Enter partition permission flags[wm]: 
Enter new starting cyl[0]: 1681
Enter partition size[0b, 0c, 0.00mb]: 1680c

Once the disk has been partitioned, the label should be written to the disk:

partition> label
Ready to label disk, continue? y

The new partition table can be printed from the format utility, or may be viewed using the prtvtoc command:

# prtvtoc /dev/rdsk/s0t5d0s2
* /dev/rdsk/c0t5d0s2 partition map
*
* Dimensions:
*     512 bytes/sector
*     140 sectors/track
*       5 tracks/cylinder
*     700 sectors/cylinder
*    3363 cylinders
*    3361 accessible cylinders
*
* Flags:
*   1: unmountable
*  10: read-only
*
* Unallocated space:
*       First     Sector    Last
*       Sector     Count    Sector 
*     1176000       700   1176699
*
*                          First     Sector    Last
* Partition  Tag  Flags    Sector     Count    Sector  Mount Directory
       2      5    01          0   2352700   2352699
       3      0    00          0   1176000   1175999
       4      0    00    1176700   1176000   2352699


Creating new filesystems

Finally, new filesystems can be created on the disk using the newfs command, and each filesystem is checked for integrity using fsck:

# newfs /dev/rdsk/c0t5d0s3
newfs: construct a new file system /dev/rdsk/c0t5d0s3: (y/n)? y
/dev/rdsk/c0t5d0s3:     1176000 sectors in 1680 cylinders of 5 tracks, 140 sectors
        574.2MB in 105 cyl groups (16 c/g, 5.47MB/g, 2624 i/g)
super-block backups (for fsck -F ufs -o b=#) at:
 32, 11376, 22720, 34064, 45408, 56752, 68096, 79440, 89632, 100976, 112320,
 123664, 135008, 146352, 157696, 169040, 179232, 190576, 201920, 213264,
 224608, 235952, 247296, 258640, 268832, 280176, 291520, 302864, 314208,
 325552, 336896, 348240, 358432, 369776, 381120, 392464, 403808, 415152,
 426496, 437840, 448032, 459376, 470720, 482064, 493408, 504752, 516096,
 527440, 537632, 548976, 560320, 571664, 583008, 594352, 605696, 617040,
 627232, 638576, 649920, 661264, 672608, 683952, 695296, 706640, 716832,
 728176, 739520, 750864, 762208, 773552, 784896, 796240, 806432, 817776,
 829120, 840464, 851808, 863152, 874496, 885840, 896032, 907376, 918720,
 930064, 941408, 952752, 964096, 975440, 985632, 996976, 1008320, 1019664,
 1031008, 1042352, 1053696, 1065040, 1075232, 1086576, 1097920, 1109264,
 1120608, 1131952, 1143296, 1154640, 1164832,
# fsck -y /dev/rdsk/c0t5d0s3
** /dev/rdsk/c0t5d0s3
** Last Mounted on 
** Phase 1 - Check Blocks and Sizes
** Phase 2 - Check Pathnames
** Phase 3 - Check Connectivity
** Phase 4 - Check Reference Counts
** Phase 5 - Check Cyl groups
2 files, 9 used, 551853 free (13 frags, 68980 blocks, 0.0% fragmentation)

Adding Disks with HP-UX

Adding Disks with HP-UX

Once the disk has been physically installed, the system should detect the device that a new device is available on the SCSI bus. After the power to the system has been restored, press the Esc key to get to the Boot PROM prompt where Boot_Admin> is offered. At the Boot_Admin> prompt, type search:

Boot_Admin> search [return]

This will return the list of scsi devices:

Device Path             Device Type
----------------        -----------------
scsi.6.0                HP C2247
scsi.2.0                TOSHIBA CD-ROM XM-340ITA

After verifying the disk drive is detected by the Boot PROM, continue the initialization sequence by entering boot. When the boot process has completed, ensure that the drive was detected by the kernel by reviewing the kernel messages in /var/adm/syslog/syslog.log. They should look somewhat similar to the following:
Mar 19 15:21:47 bud vmunix: 2/0/1.0 tgt
Mar 19 15:21:47 bud vmunix: 2/0/1.0.0 sdisk
Mar 19 15:21:47 bud vmunix: 2/0/1.1 tgt
Mar 19 15:21:47 bud vmunix: 2/0/1.1.0 sdisk
Mar 19 15:21:47 bud vmunix: 2/0/1.6 tgt
Mar 19 15:21:47 bud vmunix: 2/0/1.6.0 sdisk
This system located three disk drives on controller 0 located at the controller device address 1 and at SCSI IDs 0, 1 and 6. Additionally, the command /usr/sbin/ioscan may be run to show further information about the disk drives detected.
# /usr/sbin/ioscan -C disk
H/W Path   Class                Description
===========================================
2/0/1.0.0               disk    HP      C2247
2/0/1.1.0               disk    HP      C2247
2/0/1.6.0               disk    SEAGATE ST31200N
If the disk device is not found, determine if the device driver needed for the disk and interface are available. If any necessary device driver is absent from the kernel, the kernel will need to be rebuilt incorperating the new driver.
Here is a brief overview of the steps to rebuild a kernel.
  • Go to the build directory and from there, run the script, system_prep. This script will extract the system file from the current kernel.
    # cd /stand/build                          
    # /usr/lbin/sysadm/system_prep -v -s system
    
  • Modify the /stand/build/system file, adding the driver. Rebuild the kernel with the command:
    # /usr/sbin/mk_kernel -s system 
    
    The new kernel /stand/build/vmunix_test is created
  • Save the copy of the old kernel and move the new one in its place and reboot the system.
    # mv /stand/system /stand/system.old
    # mv /stand/vmunix /stand/vmunix.old
    # mv /stand/build/system /stand/system     
    # mv /stand/build/vmunix_test /stand/vmunix
    

Device nodes

During HP-UX boot-up, /sbin/insf is executed to create the character and block device special /dev files that allow communication with the disk. HP-UX uses a somewhat standard SVR4 device file naming system. For each disk device, the following special files are created:
  • Block device file:
    /dev/dsk/ccardttargetddevice
  • Charater device file:
    /dev/rdsk/ccardttargetddevice
Where card is the (SCSI) controller number, target is the SCSI ID number, and device is the logical unit number, or lun. The lun is 0 for the majority of devices. An example for a disk at controller 0, target 6, lun 0, is /dev/dsk/c0t6d0.
A disk is usually divided into file systems, areas that can hold files, or raw data areas such as swap. File systems are created in disk partitions or logical volumes. To view the size of the drive to be configured use the command, diskinfo.
# /usr/sbin/diskinfo /dev/rdsk/c0t6d0
SCSI describe of /dev/rdsk/c0t6d0:
             vendor: HP      
         product id: C3324A          
               type: direct access
               size: 1025730 Kbytes
   bytes per sector: 512
Though HP-UX does support standard (BSD) style filesystems, it is recommended that the logical volume manager (LVM) be used because of the following reasons.
  • They offer greater flexiblity for disk partitioning.
  • The size of logical volumes can be modified according to need.
  • Logical volumes can span disks.
More recommended reading about LVM is availbable in the Logical Volume White paper.

Creating traditional filesystems

To initialize the disk and check the disk for bad blocks the command, mediainit can be used. This is usually not necessary with HP-UX 10.x.
# mediainit /dev/rdsk/c0t1d0
The newfs command has been updated, from the 9.x version, to prompt for disk specific information, thereby overriding the information directives found in the, now obsolete, /etc/disktab file.
# newfs -F hfs /dev/rdsk/c0t5d0
mkfs (hfs): Warning - 121 sector(s) in the last cylinder are not allocated.
mkfs (hfs): /dev/rdsk/c0t5d0 - 990071 sectors in 2947 cylinders of 12 tracks, 28 sectors
1013.8Mb in 185 cyl groups (16 c/g, 5.51Mb/g, 832 i/g)
Super block backups (for fsck -b) at:
    16,   5424,  10832,  16240,  21648,  27056,  .....

Creating logical volume filesystems

The system administration tool, sam, can be used to perform most of the tasks involved with adding a new disk. Optionally, the commands to activate this disk can be given from the command line, which is demonstrated here. The physical disk drive should be added to either and an existing volume group or to a newly created volume group. These tasks are done with the commands/usr/sbin/vgextend and /usr/sbin/vgcreate respectively.
In either case pvcreate needs to run to initializes the device (a raw disk device) for use as a physical volume in a volume group.
# pvcreate /dev/rdsk/c0t5d0
Physical volume "/dev/rdsk/c0t5d0" has been successfully created.
Then to create a new volume group, the directory /dev/vg01 with the character special file called group, execute vgcreate.
#mkdir /dev/vg01
#mknod /dev/vg01/group c 64 0x010000
# vgcreate /dev/vg01 /dev/dsk/c0t5d0
Volume group "/dev/vg01" has been successfully created.
Volume Group configuration for /dev/vg01 has been saved in /etc/lvmconf/vg01.conf
Note that the last argument to the mknod command must be unique for each volume group that exists on the system.
Otherwise, to add or extend an existing volume group, executed vgextend.
# vgextend /dev/vg00 /dev/dsk/c0t5d0
Volume group "/dev/vg00" has been successfully extended.
Volume Group configuration for /dev/vg00 has been saved in /etc/lvmconf/vg00.conf
Utililizing the increased disk space in the volume group, create a single or multiple logical volumes that will contain filesystems. In this example a single logical volume and file system of 1 gigabyte is created.
# lvcreate -L 1000 /dev/vg00      
Logical volume "/dev/vg00/lvol9" has been successfully created with
character device "/dev/vg00/rlvol9".
Logical volume "/dev/vg00/lvol9" has been successfully extended.
Volume Group configuration for /dev/vg00 has been saved in /etc/lvmconf/vg00.conf

# newfs -F hfs /dev/vg00/rlvol9
mkfs (hfs): Warning - 608 sector(s) in the last cylinder are not allocated.
mkfs (hfs): /dev/vg00/rlvol9 - 1024000 sectors in 1642 cylinders of 16 tracks, 39 sectors
1048.6Mb in 103 cyl groups (16 c/g, 10.22Mb/g, 1600 i/g)
Super block backups (for fsck -b) at:
    16,  10040,  20064,  30088,  40112,  ....

Mounting the new filesystem

Add this filesystem to the ones that are automatically mounted by system initialization, by updating the file /etc/fstab with the following entry. It specifies the device and the mount point for the filesystem.
/dev/vg00/lvol9 /usr/local hfs defaults 0 2

Mount the filesystem using the command /usr/sbin/mount -a. Check the mount table to ensure the partition was mounted and check the filesystem size with the /usr/bin/bdf command.
# mount -a
# mount
/ on /dev/vg00/lvol1 defaults on Thu May 23 08:10:01 1996
/var on /dev/vg00/lvol8 defaults on Thu May 23 08:10:14 1996
/usr on /dev/vg00/lvol7 defaults on Thu May 23 08:10:15 1996
/tmp on /dev/vg00/lvol6 defaults on Thu May 23 08:10:15 1996
/opt on /dev/vg00/lvol5 defaults on Thu May 23 08:10:15 1996
/home on /dev/vg00/lvol4 defaults on Thu May 23 08:10:15 1996
/usr/local on /dev/vg00/lvol9 defaults on Thu May 23 08:37:17 1996
# bdf /usr/local
Filesystem          kbytes    used   avail %used Mounted on
/dev/vg00/lvol9    1001729       9  901547    0% /usr/local

  How to Change Instance Type & Security Group of EC2 in AWS By David Taylor Updated April 29, 2023 EC2 stands for Elastic Compute Cloud...