Linux and Unix Disk How To

From KnowWiki
Jump to: navigation, search

Contents

[edit] How to sort FAT file system contents for a media player

You can do it with the fatsort tool from the Ubuntu repository. Unfortunately, it is ancient and has a lot of bugs causing segmentation faults. You could also try YAFS, which is newer, but requires compilation from source.

First, find out the device name for your FAT disk. Unmount it. Run 'mount' command to see which device it is. Then install fatsort:

sudo aptitude install fatsort

View your fat system with

sudo fatsort -l /dev/sdc1

And then sort it with

sudo fatsort /dev/sdc1

For YAFS, download the source and unpack it. Install the xerces C lib:

sudo aptitude libxerces-c2-dev

Now compile:

cp Makefile_unix Makefile
make

Once compiled, start by generate the listing for your file system:

sudo ./yafs -d /dev/sdc1 -r -f layout.xml

Edit it with the file editor and change the order as you wish. Write the order back:

sudo ./yafs -d /dev/sdc1 -r -f layout.xml

YAFS has bugs too though. If an ampersand shows up in the short name in layout.xml yafs will crash. To fix that change the XML file with the following command:

sed -e 's/(<short_name>.*)&/$1&/g' layout.xml

[edit] How to Make an ISO Image Using DD

Put the media in the drive. Do not mount it, if automounts then unmount it. For DVD

dd if=/dev/dvd of=image.iso

For CD

dd if=/dev/cdrom of=image.iso

[edit] How to check disk for bad sectors

Unmount your FS or reboot into a recovery linux OS like Parted Magic.

e2fsck -cc /dev/sdf1

-cc runs a non-destructive read-write test. Results are saved into a bad blocks inode on the file system. You can later read its contents by running

dumpe2fs -b /dev/sdf1

Alternatively you could use badblocks that would produce the a text file output from the same non-destructive read-write test and stats visible on stdout.

nohup sudo badblocks -snv -o badblocks.log /dev/sdf1

Just know that the size of the blocks in the log, and therefore the naming might be different from what fsck thinks the size should be.

[edit] How to check file suspected for containing bad sectors

shred -vn 1 filename

[edit] How to change UUID of a cloned partition

uuidgen

and apply it:

sudo tune2fs /dev/sdc7 -U some-uid

or use the word 'random' to generate it on the fly

sudo tune2fs /dev/sdc7 -U random

reboot. View it with

blkid

[edit] How to see progress of a dd command

sudo kill -USR1 `pidof dd`

[edit] How to check what application/process/thread is accessing the disk

So you see that light blinking all the while you are not doing anything and are curious of what that might mean? You could check it in real time by running

sudo iotop -ao

Keep it running for awhile to monitor your system.

To show currently opened files with their associated processes run the following command:

lsof / | grep "REG" | grep -v "mem" | awk '{print $9}' | sort | uniq

Substitude $1 for $9 to see all processes that currently have files open. Keep in mind that multiple processes may have the same file open just as one process can have multiple files open.

[edit] How to clone a disk

The following command clones a disk fast and byte-by-byte so that even blkid remains the same. Before running make sure you've typed in correct disks (and careful copy/pasting 'enter') as there will not be a 'are you sure' prompt

sudo ddrescue -v /dev/sda /dev/sdb

You can get it from

sudo aptitude install ddrescue

[edit] How to spin down an HDD to check its noise level

hdparm -Y /dev/sdx

[edit] How to check or change how many times a FS was mounted and when the next fsck on boot will run

sudo dumpe2fs -h /dev/sdb1 | grep -i 'mount count'

To change it use

sudo tune2fs -c 50 /dev/hda1

[edit] How to restore names from lost+found

find ./lost+found -exec file -b {} \; > filetypes.txt
cat filetypes.txt | sort | uniq > filetypes_uniq.txt
#!/bin/bash
#
# Usage:
# Edit SOURCE and TARGET folders below, then
# ./find.sh filetype
#
 
# where to copy from (SOURCE) and copy to (TARGET). TARGET will be created if non existing
SOURCE="/home/jane/lost+found"
TARGET="/home/jane/recover"
 
# file name prefix "#"
FPREFIX="#"
 
# minimum filesize
FSIZE="100k"
 
if [ arg$1 != arg ]; then
  filetype=$1
else
  echo "Error: no filetype specified"
  echo "Usage: $0 [filetype]"
  exit
fi
 
# file extension
fext=".`echo $filetype |tr "[:upper:]" "[:lower:]"`"
 
# create target folder
targetfolder=$TARGET/$filetype
mkdir -p $targetfolder
 
FILES=`find "${SOURCE}" -name "${FPREFIX}*" -size +${FSIZE} -exec ls {} \;`
for file in $FILES
do
 
  fname=`basename "$file"`
  targetfile=${targetfolder}/${fname}${fext}
  check=`file "${file}" | grep -q "${filetype}"`
 
  if  $? -eq 0 
  then
    echo "${file} is ${filetype}, copying to ${targetfile}"
    cp "$file" "$targetfile"
  fi
 
done

[edit] How to force fsck on next boot

By creating /forcefsck file you will force the Linux system (or rc scripts) to perform a full file system check.

sudo touch /forcefsck

Using the shutdown command:

shutdown -rF now

The -F option force fsck on reboot.

[edit] How to mount an ISO

On Linux

sudo mount -o loop /yourisofile.iso /mount/point

On AIX

  1. Get the size of the image.
ls -al
-rw-r--r-- 1 root system 374575104 Apr 29 02:59 fim_console_aix_5.iso

It's roughly about 360MB, but we need a number with an increment of 128, so 384MB (always larger) sounds good.

  1. Make a logical volume of the appropriate size for this image. Ensure there is enough space allocated on physical volume hdisk0 (in this case). Try it and if it fails it will inform you that there is not enough space. At that point, increase the size if necessary.
mklv -y cdlv -s n -L /dev/cdlv rootvg 384M hdisk0
  1. Create the pseudo device with 'dd'. Again, ensure that the partition has enough space for pseudo device, /dev/cdlv in this case. This may take along time and will create 2 'dd' processes.
dd if=/opt/software/ISO/fim_console_aix_5.iso of=/dev/cdlv
  1. Mount the device like a cdrom in AIX. Ensure the mount point exists.
mount -v cdrfs -o ro /dev/cdlv /mnt/iso
  1. cd into the directory /mnt/iso

After installing the product you can remove the volume with rmlv [1]

[edit] How to move OS to a different disk or partition

Just copy all the os files and then

  1. redo grub if you want to boot of that partition
  2. sudo blkid
  3. change fstab to mount appropriate disks
  4. Check that swap line UUID from /etc/fstab matches swap UUID from step 2, if not change fstab.
  5. Check that the UUID in /etc/initramfs-tools/conf.d/resume matches the swap UUID from step 2, if not change resume file.
  6. sudo update-initramfs -u
  7. Restart

[edit] How to remount a file system with rw permissions

mount -o remount,rw,noatime -n /dev/root /

To go back :

mount -o remount,ro -n /dev/root /
Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox