Creating & Mounting new drives in Ubuntu / Azure

Creating & Mounting new drives in Ubuntu / Azure

Leading note: The drive creation is based on using Azure with an Ubuntu 14.04 LTS release. This stuff doesn't change very often but sometimes ubuntu changes the advice and occasionally includes features or options that might not exist in other releases.

Create a Site & A disk (or more)

Ok, this bits all azure based, so hang fire or skip to the next bit if you've done this, or are in some other VM/cloud place or ssh'd into a hardware box.

Partitioning, Formatting and Mounting the drive(s)

You've added the drives, your machines ticking along nicely, so SSH in and let's get going.

Now I usually type sudo su which basically chucks you into root mode; if you're uncomfortable with that just imagine I typed sudo before each command.

First, type blkid

Now before you ATTACH your new drive blkid looks like this

/dev/sr0: LABEL="rd_rdfe_stable.150630-2348" TYPE="udf"
/dev/sda1: LABEL="cloudimg-rootfs" UUID="3156d3f3-c771-498f-89f3-c204d36c46d6" TYPE="ext4"
/dev/sdb1: UUID="ab77eb25-3fe1-401f-a0da-60a45169b168" TYPE="ext4"

sda1 is the system drive, never store anything you want to keep here (other than the configs etc for services).

sdb1 is a temporary drive azure attaches to each machine by default. Usually about 69G and linked to /mnt; the size increases with VM size. You can read about that here http://blogs.msdn.com/b/mast/archive/2013/12/07/understanding-the-temporary-drive-on-windows-azure-virtual-machines.aspx

Typing lshw -C disk will display all the attached disks... this is more accurate as blkid does not tend to pick-up new drives without a reboot or you doing something to alter the state.

After typing lshw -C disk we see (at the bottom of the list)

  *-disk
       description: SCSI Disk
       physical id: 0.0.0
       bus info: [email protected]:0.0.0
       logical name: /dev/sdc
       size: 60GiB (64GB)
       configuration: sectorsize=512

Create a partition

Type fdisk /dev/sdc
Various noise will appear followed by a prompt; hitting m shows you the options.

Command (m for help): m
Command action
   a   toggle a bootable flag
   b   edit bsd disklabel
   c   toggle the dos compatibility flag
   d   delete a partition
   l   list known partition types
   m   print this menu
   n   add a new partition
   o   create a new empty DOS partition table
   p   print the partition table
   q   quit without saving changes
   s   create a new empty Sun Disklabel
   t   change a partition's system id
   u   change display/entry units
   v   verify the partition table
   w   write table to disk and exit
   x   extra functionality (experts only)

We want a new partition; so press n followed by hitting enter.
Then p and hit enter again for a primary partition.
Then assuming you don't want to mess with the defaults hit enter for the following...

  • Partition number (1-4, default 1):
  • First sector (2048-125829119, default 2048):
  • Last sector, +sectors or +size{K,M,G} (2048-125829119, default 125829119):

You'll then be presented with the Command (m for help): prompt again; press w and hit enter to write your changes.

The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

Cool, we've made a partition, now we need to format it.

Type mkfs -t ext4 /dev/sdc1 and hit enter.

mke2fs 1.42.9 (4-Feb-2014)
Discarding device blocks: done
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
3932160 inodes, 15728384 blocks
786419 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=4294967296
480 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
        32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
        4096000, 7962624, 11239424

Allocating group tables: done
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done

Now we can mount it; first, create a folder (path) you want to mount to; in this instance, I'll create a folder called data in the root and mount that to sdc1 (our new drive sdc, partition 1).

mkdir /data
mount /dev/sdc1 /data

You can now use this drive by going to the data folder at the root. If you reboot the machine at this point it will unmount the drive. Which takes us to...

Make that mount more permanent with FSTAB

The new ubuntu recommendation is that you use the UUID from this output rather than the path, so that's what's demonstrated here.

We've altered the world and typing blkid will now show us our new goodness.

[email protected]:/# blkid
/dev/sr0: LABEL="rd_rdfe_stable.150630-2348" TYPE="udf"
/dev/sda1: LABEL="cloudimg-rootfs" UUID="3156d3f3-c771-498f-89f3-c204d36c46d6" TYPE="ext4"
/dev/sdb1: UUID="ab77eb25-3fe1-401f-a0da-60a45169b168" TYPE="ext4"
/dev/sdc1: UUID="cd5aaf52-5483-4f9d-a0d6-836f26c23a09" TYPE="ext4"

_Random tip as well as blkid you can also type ls -l /dev/disk/by-uuid which lists the drives + uuids`

Make sure you enter your own UUID, copy + paste fatigue could kick your arse here

So we want to edit fstab, type nano /etc/fstab to open in nano. You can open in VI if you prefer, or whatever.
The file will look like this...

# CLOUD_IMG: This file was created/modified by the Cloud Image build process
UUID=3156d3f3-c771-498f-89f3-c204d36c46d6       /        ext4   defaults,discard        0 0

# CLOUD_IMG: This file was created/modified by the Cloud Image build process
# The following is used to dynamically configured additional
# NICs. Do not remove unless you know what you are doing.
none /etc/network/interfaces.dynamic.d tmpfs   nodev,noexec,nosuid,size=64K 0 0
/dev/sdb1       /mnt    auto    defaults,nobootwait,comment=cloudconfig 0       2

Add a line at the end like this (but with your UUID)

UUID=cd3aaf52-5483-4f9d-a0d6-836f26c23a09 /data ext4 defaults 0 1

The format of this is pretty simple.

  • UUID or you can put /dev/sdb1 format, identifies the drive.
  • /data is the mountpoint
  • ext4 is the file-system type we specified above when formatting.
  • defaults is an option shortcut, so it sets these options rw, suid, dev, exec, auto, nouser, async. You can replace it with any of the [mount-options](http://man7.org/linux/man-pages/man8/mount.8.html#FILESYSTEM-INDEPENDENT_MOUNT OPTIONS) normally available
  • dump flag - Enable or disable backing up of the device/partition (the command dump). This field is usually set to 0, which disables it.
  • pass order - Controls the order in which fsck checks the device/partition for errors at boot time. The root device should be 1. Other partitions should be 2, or 0 to disable checking.

Save and exit the file... esc then :wq in vim or CTRL+X then enter in nano/pico

Typing umount -a will mount all available drives in fstab, in this case they're all mounted bar our new one so go nuts.

Recovering from failure

Your config is wrong or your external drive is buggered, but you still want your VM to start or you can't really fix it... more so in azure where VM's don't have a console emulator so you can diagnose issues (pay your support bill and watch as they can't tell either). So we need to alter the default settings in fstab for our new drive so we boot regardless of its presence.

This is where the aptly names nofail comes in.

fsck normally does not check whether the device actually exists before calling a file system specific checker. Therefore non-existing devices may cause the system to enter file system repair mode during boot if the filesystem specific checker returns a fatal error. The /etc/fstab mount option nofail may be used to have fsck skip non-existing devices. fsck also skips non-existing devices that have the special file system type auto

References

Chris McKee

Chris McKee

https://chrismckee.co.uk

Software Engineer, Web Front/Backend/Architecture; all-round tech obsessed geek. I hate unnecessary optimism