27.08.2015 Views

Advanced Bash−Scripting Guide

Advanced Bash-Scripting Guide - Nicku.org

Advanced Bash-Scripting Guide - Nicku.org

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>Advanced</strong> <strong>Bash−Scripting</strong> <strong>Guide</strong><br />

Example 13−6. Creating a filesystem in a file<br />

SIZE=1000000 # 1 meg<br />

head −c $SIZE < /dev/zero > file # Set up file of designated size.<br />

losetup /dev/loop0 file<br />

# Set it up as loopback device.<br />

mke2fs /dev/loop0<br />

# Create filesystem.<br />

mount −o loop /dev/loop0 /mnt # Mount it.<br />

# Thanks, S.C.<br />

mkswap<br />

Creates a swap partition or file. The swap area must subsequently be enabled with swapon.<br />

swapon, swapoff<br />

Enable / disable swap partitition or file. These commands usually take effect at bootup and shutdown.<br />

mke2fs<br />

Create a Linux ext2 filesystem. This command must be invoked as root.<br />

Example 13−7. Adding a new hard drive<br />

#!/bin/bash<br />

# Adding a second hard drive to system.<br />

# Software configuration. Assumes hardware already mounted.<br />

# From an article by the author of this document.<br />

# in issue #38 of "Linux Gazette", http://www.linuxgazette.com.<br />

ROOT_UID=0<br />

E_NOTROOT=67<br />

# This script must be run as root.<br />

# Non−root exit error.<br />

if [ "$UID" −ne "$ROOT_UID" ]<br />

then<br />

echo "Must be root to run this script."<br />

exit $E_NOTROOT<br />

fi<br />

# Use with extreme caution!<br />

# If something goes wrong, you may wipe out your current filesystem.<br />

NEWDISK=/dev/hdb<br />

# Assumes /dev/hdb vacant. Check!<br />

MOUNTPOINT=/mnt/newdisk # Or choose another mount point.<br />

fdisk $NEWDISK<br />

mke2fs −cv $NEWDISK1 # Check for bad blocks & verbose output.<br />

# Note: /dev/hdb1, *not* /dev/hdb!<br />

mkdir $MOUNTPOINT<br />

chmod 777 $MOUNTPOINT # Makes new drive accessible to all users.<br />

# Now, test...<br />

# mount −t ext2 /dev/hdb1 /mnt/newdisk<br />

# Try creating a directory.<br />

# If it works, umount it, and proceed.<br />

# Final step:<br />

# Add the following line to /etc/fstab.<br />

# /dev/hdb1 /mnt/newdisk ext2 defaults 1 1<br />

Chapter 13. System and Administrative Commands 229

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!