Solved

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

Posted on 2014-03-08
23
758 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 77

Expert Comment

by:arnold
ID: 39915994
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
ID: 39916012
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 77

Expert Comment

by:arnold
ID: 39916040
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
Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
LVL 77

Expert Comment

by:arnold
ID: 39916044
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
ID: 39916062
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 77

Expert Comment

by:arnold
ID: 39916139
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
ID: 39916405
I will test this and get back to you thank you!
0
 
LVL 1

Author Comment

by:Peter Kuczynski
ID: 39918938
# 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 77

Expert Comment

by:arnold
ID: 39919161
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 62

Expert Comment

by:gheist
ID: 39919191
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
ID: 39919299
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
 
LVL 62

Expert Comment

by:gheist
ID: 39919341
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 77

Expert Comment

by:arnold
ID: 39919361
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
ID: 39919398
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 62

Expert Comment

by:gheist
ID: 39919636
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
ID: 39920308
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 62

Expert Comment

by:gheist
ID: 39920349
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
ID: 39920572
again can you type the command please
0
 
LVL 62

Expert Comment

by:gheist
ID: 39920681
mkswap /dev/sdb && swapon /dev/sdb
0
 
LVL 1

Author Comment

by:Peter Kuczynski
ID: 39921043
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 62

Expert Comment

by:gheist
ID: 39921195
[ ! -b /dev/sdb ] && mkswap && swapon
0
 
LVL 77

Accepted Solution

by:
arnold earned 500 total points
ID: 39921237
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
ID: 39921254
Excellent! Thanks! I appreciate the straight-forward concise answer to my question.
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Regular patching is part of a system administrator's tasks. However, many patches require that the system be in single-user mode before they can be installed. A cluster patch in particular can take quite a while to apply if the machine…
Every server (virtual or physical) needs a console: and the console can be provided through hardware directly connected, software for remote connections, local connections, through a KVM, etc. This document explains the different types of consol…
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.

803 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