======Linux Disk Administraton==========
=====List Local Disk Size=====
fdisk -l | grep "Disk /dev"
=====Shred Partition=====
shred -vfz -n 1 /dev/sdb5
=====Shred Drive=====
shred -vfz -n 1 /dev/sdb
=====Add New Hard Drive Without Rebooting=====
Add the hard drive and then run the following (assuming you added the hard drive to SCSI channel 2).
This allows the "Hot" adding of new drives
echo "- - -" > /sys/class/scsi_host/host0/scan
echo "- - -" > /sys/class/scsi_host/host1/scan
echo "- - -" > /sys/class/scsi_host/host2/scan
echo "- - -" > /sys/class/scsi_host/host3/scan
echo "- - -" > /sys/class/scsi_host/host4/scan
echo "- - -" > /sys/class/scsi_host/host5/scan
echo "- - -" > /sys/class/scsi_host/host6/scan
fdisk -l
=====Useful df=====
The df command lists disks mounted on the system. To make the output more readable, you can use some of the following commands.
The following command is the most basic in human readable form
df -h
However, this often splits a line in two if the device name is to long. To prevent this, use the P parameter as well as the column command to make the output appear in pretty columns.
df -hP |column -t
Use grep to filter out any type of filesystem we are not interested. In the following case, we only consider local filesystems that are not virtual (e.g. ramfs) and are not network mounts
df -hP |column -t | grep "Filesystem\|/dev/sd.*\|/dev/mapper"
We then want to sort the results by size. We use the sort command. The -V parameter says to sort numbers naturally rather than putting 4% between 22% and 55%. The -k5 parameter says to sort based on the fifth column.
df -hP |column -t | grep "Filesystem\|/dev/sd.*\|/dev/mapper" | sort -k5 -V
Now we want to only want to see the drive name and the % of use. In addition, we want to strip out the % symbol. We now have the column command at the end as that is the only place we need it.
df -hP | grep "/dev/sd.*\|/dev/mapper" | awk '{ print $1 " " $5}' | sed 's/%//g' | column -t
Now we want to only show drives that are 80% full or more. Further more, we want the % used to now be the first column. The column command has been removed but could be added to the end if wanted. We also have put the value of 80 into an environment variable. This makes it easy to set in a script before running the command.
LEVEL=80; df -hP | grep "/dev/sd.*\|/dev/mapper" | awk '{ print $1 " " $5}' | sed 's/%//g' | awk -v ALEVEL=$LEVEL -F ' ' 'BEGIN {OFS=","} { if ($2 >= ALEVEL) print $2 " " $1}'
Finally, this command runs on a remote system. This involves an intricate set of escape characters to get the variable reading working.
LEVEL=80; ssh username@target.example.com " df -hP |column -t | grep \"/dev/sd.*\|/dev/mapper\" | awk '"'{ print $1 " " $5}'"' | sed '"'s/%//g'"' | awk -v ALEVEL=$LEVEL '"'BEGIN {OFS=","} { if ($2 >= ALEVEL) print $2 " " $1}'"' "
=====Add Swap Space=====
This shows you how to create a swap partition on a drive and start using it.
'''WARNING'''. This assumes that /dev/sdb is not already being used for anything else. You will DESTROY DATA if /dev/sdb is being used. Only run this command if you know what you are doing.
Assuming a 4 GB size for the /dev/sdb, make a primary partition covering the whole disk
fdisk /dev/sdb
n
p
1
t
82
w
Then make the new partition a swap partitionmkswap -f /dev/sdb1and start using itswapon -va
You may have to edit /etc/fstab to replace a UUID with /dev/sdb.
=====Remount Partition=====
The following example shows how to remount a partition /dev/sdb2 as read only (ro). Replace ro with rw to remount as read/writable.
mount -o remount,rw /dev/sdb2 /test/dir
=====Change LUKS Password=====
From [http://www.ehow.com/how_7428906_change-luks-password.html here]
# Open a root prompt
# Type the following, changing "sda2" to the name of the encrypted partition.cryptsetup luksAddKey /dev/sda2
# Enter your current LUKS password when prompted so that LUKS can unlock a password slot. Enter a new password when prompted and type it again for verification to set the new password.
# Type the following, changing "sda2" to the name of the encrypted partition. Enter your original password when prompted. LUKS deletes the password so that only the new one works.cryptsetup luksKillSlot /dev/sda2 0
=====Mount LUKS Partition=====
I found the following useful when mounting a second encrypted file system under CentOS. It is probably very similar for Ubuntu. We are assuming that the encrypted partition is on /dev/sda2. We are also assuming that the mount point exists (in this case /mnt/data)
# Unlock the partitioncryptsetup luksOpen /dev/sda2 myDecryptedPartition
# Mount the unlocked partitionmount /dev/mapper/myDecryptedPartition /mnt/data
The following commands are notes are for when you need to add the encrypted partition into a LVM system. I've not tried it so I've left it as one large note.
pvscan
lvscan
lvchange -a y /dev/basevg/lv_home
mount /dev/basevg/lv_home /mnt/data
umount /media/my_ssd
lvchange -a n /dev/basevg/lv_home
cryptsetup luksClose /data
The pvscan really just confirms the drive is unlocked and available. The lvscan tells you what is under lvm control. The lvchange changes a specific volume to active (otherwise you can not mount it).
=====Mount a TrueCrypt Volume=====
Assuming the TrueCrypt Volume is /dev/sdb and you want to mount it at /mnt/data
truecrypt -t -k "" --protect-hidden=no --fs-options=force /dev/sdc /mnt/data
=====Partition Sizing=====
When creating filesystems with the CentOS 6 installer, you have to specify the size in MB. However, once installed, the 'df -h' command will show slightly different sizes. The following table shows the number of MB you need to give at installer time to get the desired size from 'df -h' once install is complete
^ Desired ^ What you put during install ^
| 200M| 206M|
| 1G| 1040M|
| 2G| 2052M|
| 3G| 3076M|
| 4G| 4120M|
| 8G| 8300M|
=====SSD Tips=====
Edit the mounting options for the SSD partitions by adding discard,noatime,nodiratime to the mount options.
Also, add the following to make the filesystems reside in RAM.
tmpfs /tmp tmpfs defaults,noatime,mode=1777 0 0
tmpfs /var/tmp tmpfs defaults,noatime,mode=1777 0 0
tmpfs /var/log tmpfs defaults,noatime,mode=0755 0 0
tmpfs /var/log/apt tmpfs defaults,noatime 0 0
Open Firefox and go to about:config in the address bar
* keyword.enabled (disable this)
* browser.tabs.warnOnClose (disable this)
* browser.warnOnQuit (disable this)
* browser.tabs.loadDivertedInBackground (enable this)
Secondly, right-click and select New->String. Add 'browser.cache.disk.parent_directory'. Set the value to tmp
=====Network Mounts=====
su -
FILENAME=/etc/username.cred
# Name of local user that should have "owner" rights
LOCAL_USERNAME=username
# Name of local group that should have "group" rights
LOCAL_GROUP=groupname
# Name of domain user to use to access file share
DOMAIN_USERNAME=username
# Domain user's password
PASSWORD=pa55w0rd
DOMAIN=example
echo "username=$DOMAIN_USERNAME" > $FILENAME
echo "password=$PASSWORD" >> $FILENAME
echo "domain=$DOMAIN" >> $FILENAME
chmod 400 $FILENAME
echo "//fileserver.example.com/share/folder /local/mount/point cifs credentials=/etc/$FILENAME,uid=$LOCAL_USERNAME,gid=$LOCAL_GROUP 0 0' >> $FILENAME
=====Configure Disk Quotas======
SELinux is an issue when it comes to setting quotas. Replace /home/home with the directory structure to have quotas.
chcon --reference=/var /home/home