Solved

create swap partion /dev/sdb1 on ssd at amazon ec2

Posted on 2014-03-08
23
747 Views
Last Modified: 2014-03-11
I have a rhel  linux template vm on amazon ec2 which will need to format and mount swap on /dev/sdb which is a amazon ssd disk
This will need to be a script as if this vm is ever restarted, it may move to another amazon host and ssd so it will need to repeat process of formating /dev/sdb and mounting it as swap.
So steps without a script would follow...
fdisk /deb/sdb
n
p
1
enter
enter
t
82
w
mkswap /dev/sdb
swapon /dev/sdb

then make the fstab entry, although it's probably unnecessary
/dev/sdb1 swap swap defaults 0 0

I am attaching one possible script I found on the internet as a start point.

Thank you!
swap.sh.txt
0
Comment
Question by:Peter Kuczynski
  • 10
  • 7
  • 6
23 Comments
 
LVL 76

Expert Comment

by:arnold
Comment Utility
I am not sure what exactly you are after, a disk partitioned with a partition of type swap will be auto-mapped during bootup.

Using an SSD for swap is an expensive .....
presumably the reason for formating is to clear the SSD of all prior data.

SWAP is a resource in the system that one does not want used. But is there in the event the memory requirement of the system exceed the installed RAM.
0
 
LVL 1

Author Comment

by:Peter Kuczynski
Comment Utility
I am a after fdisk ing and mounting /dev/sdb as swap.

I do agree about cost, but it's not the point of discussion  here.
I'm after formatting and mounting /dev/sdb as swap for a new vm using a script.

On amazons ec2 a m2.medium class vm with /dev/sdb ssd drive, the vm can be vmotioned after a reboot by amamzon to another host and will need to reformat the /dev/sdb partition again as swap, which is why this needs to be scripted.

Perhaps my simple .sh script below will do the job, I just tested it on a vm and it did work, just let me know your thoughts.
swap.sh.test.txt
0
 
LVL 76

Expert Comment

by:arnold
Comment Utility
You do not need to repetitively format it, it is defined once. as long as the VM no matter what host it is on, has the reference, it will be mounted. Presumably the VM is moved between hosts within the amazon EC2 cloud.

/etc/fstab it will mount
i.e. you partition /dev/sdb to be the entire swap.  no matter to which system you connect it it will always have the single partition /dev/sdb1 as swap.
0
 
LVL 76

Expert Comment

by:arnold
Comment Utility
Another option if you must use a script is to test for the presence of /dev/sdb then check whether it is partitioned as you need
fdisk -l /dev/sdb
parted -l /dev/sdb

this is to avoid the repettive formats when unnecessary.
0
 
LVL 1

Author Comment

by:Peter Kuczynski
Comment Utility
Can you help me script the IF ELSE statement to check for the presence of /dev/sdb as a swap partition, then if it is not there , to create one. I assume you looked at my simple script and it was ok?

BTW, the SSD disk [/dev/sdb] unlike /dev/sda does not move with the vm in the amazon cloud, but stays on the host [direct storage], so if the vm is ever moved [vmotion] to another host, and rebooted, it will need to fdisk /dev/sdb again.
0
 
LVL 76

Expert Comment

by:arnold
Comment Utility
I see.

swap can be added at any time, so you would place the script/commands in /etc/rc.local

A simpler way is to have within your VM a layout of the /dev/sdb
man sfdisk
sfdisk -d /dev/sdb > swap_SSD_layout.FMT

The VM has a swap partition  as part of the VM?


the partition creation part will be simple
sfdisk /dev/sdb < /path/to/where/swap_SSD_layout.FMT
 
now the testing.
if test -e "/dev/sdb"; then
    echo "we may have a disk"
   validate
  else
   echo "we do not have a disk we expect"
  generated notification
fi

Is it always the case that sdb is the one allocated to you?

Once /dev/sdb is present, using sfdisk -d /dev/sdb the script can confirm the layour of the disk to make sure you do not overwrite something else.
post the sfdisk -d /dev/sdb that you currently have?


Not sure whether you actually have to partition the SSD if you plan to use for swap .

swapon /dev/sdb might also work.

........
0
 
LVL 1

Author Comment

by:Peter Kuczynski
Comment Utility
I will test this and get back to you thank you!
0
 
LVL 1

Author Comment

by:Peter Kuczynski
Comment Utility
# sfdisk -d /dev/sdb
# partition table of /dev/sdb
unit: sectors

/dev/sdb1 : start=       63, size= 62910477, Id=83
/dev/sdb2 : start= 62910540, size= 41945715, Id=83
/dev/sdb3 : start=        0, size=        0, Id= 0
/dev/sdb4 : start=        0, size=        0, Id= 0
#


can you help with this validation statement

if test -e "/dev/sdc"; then
    echo "we may have a disk"
   validate
  else
   echo "we do not have a disk we expect"
  generated notification
fi
0
 
LVL 76

Expert Comment

by:arnold
Comment Utility
The partition layout you posted uses two Linux type partition (Type=83)
Swap is type 82.

as far as comparison, one thing is to compare one for one with the original

using cksum or md5sum
if the results are identical they will have identical cksum/md5sum.

if the one time you get /dev/sdb and another /dev/sdc they will not match.

One option is to look for type=82
0
 
LVL 61

Expert Comment

by:gheist
Comment Utility
If you want just indication that you are about to run out of memory i'd suggest using zram swap...
0
 
LVL 1

Author Comment

by:Peter Kuczynski
Comment Utility
I feel I'm not communicating my question properly. Let me ask another way, given this script, is it OK, or would you change anything. PS it does work.
#!/bin/bash
# This script formats and mounts all available Instance Store devices


fdisk -u -p /dev/sdb
n
p
1

w


mkswap /dev/sdb1

swapon /dev/sdb1

#EOF
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 61

Expert Comment

by:gheist
Comment Utility
mkswap owerwrites everything done by fdisk

mkswap /dev/sdb && swapon /dev/sdb

has same effect as all the fdick juggling followed by them.

especially if swap starts at disk block 0 alll writes are well aligned for ssd
0
 
LVL 76

Expert Comment

by:arnold
Comment Utility
If you are going on the premise the /dev/sdb is always going to be the SSD for the VM, entries in rc.local with mkswap and swapon is all that you need.

each time after boot the data will be "flushed"
0
 
LVL 1

Author Comment

by:Peter Kuczynski
Comment Utility
now were talking, thanks gheist

can you also help me with the if else statment. Basically if I reboot the vm and swap is still there, dont run the rest of the script

#!/bin/bash
# This script formats and mounts all available Instance Store devices

{
if [ ! -f /scripts/alert ]; then
    echo "File not found!"
    exit 0
fi
}

mkswap /dev/sdb && swapon /dev/sdb
#END
0
 
LVL 61

Expert Comment

by:gheist
Comment Utility
mkswap takes no time compared ro square bracket command
you might want to put /tmp in tmpfs for it to benefit from ssd swap
0
 
LVL 1

Author Comment

by:Peter Kuczynski
Comment Utility
thanks
can you provide the command for that as well for the if statement, which should check for the presence of swap, and if it exists, not run mkswap
0
 
LVL 61

Expert Comment

by:gheist
Comment Utility
first part of command fails if there is no device
so shorthand if (&&) takes care second part never gets executed.
0
 
LVL 1

Author Comment

by:Peter Kuczynski
Comment Utility
again can you type the command please
0
 
LVL 61

Expert Comment

by:gheist
Comment Utility
mkswap /dev/sdb && swapon /dev/sdb
0
 
LVL 1

Author Comment

by:Peter Kuczynski
Comment Utility
I already had that from arnold above : )
what i need is a statement which tests for swap and then exist gracefully if swap is found

{
if [ ! -f /scripts/alert ]; then
    echo "File not found!"
    exit 0
fi
}
0
 
LVL 61

Expert Comment

by:gheist
Comment Utility
[ ! -b /dev/sdb ] && mkswap && swapon
0
 
LVL 76

Accepted Solution

by:
arnold earned 500 total points
Comment Utility
I think gheist typo-ed, the test should be for whether the device exists and is a block device.
[ -b /dev/sdb ] && mkswap /dev/sdb && swapon /dev/sdb

I would caution you that should your VM get additonal storage allocations, you might be end up overwriting data.

You can not test for a file within swap space about its existence.
0
 
LVL 1

Author Closing Comment

by:Peter Kuczynski
Comment Utility
Excellent! Thanks! I appreciate the straight-forward concise answer to my question.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

When you do backups in the Solaris Operating System, the file system must be inactive. Otherwise, the output may be inconsistent. A file system is inactive when it's unmounted or it's write-locked by the operating system. Although the fssnap utility…
I promised to write further about my project, and here I am.  First, I needed to setup the Primary Server.  You can read how in this article: Setup FreeBSD Server with full HDD encryption (http://www.experts-exchange.com/OS/Unix/BSD/FreeBSD/A_3660-S…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now