Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 848
  • Last Modified:

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

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
Peter Kuczynski
Asked:
Peter Kuczynski
  • 10
  • 7
  • 6
1 Solution
 
arnoldCommented:
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
 
Peter KuczynskiLead Cloud EngineerAuthor Commented:
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
 
arnoldCommented:
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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
arnoldCommented:
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
 
Peter KuczynskiLead Cloud EngineerAuthor Commented:
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
 
arnoldCommented:
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
 
Peter KuczynskiLead Cloud EngineerAuthor Commented:
I will test this and get back to you thank you!
0
 
Peter KuczynskiLead Cloud EngineerAuthor Commented:
# 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
 
arnoldCommented:
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
 
gheistCommented:
If you want just indication that you are about to run out of memory i'd suggest using zram swap...
0
 
Peter KuczynskiLead Cloud EngineerAuthor Commented:
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
 
gheistCommented:
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
 
arnoldCommented:
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
 
Peter KuczynskiLead Cloud EngineerAuthor Commented:
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
 
gheistCommented:
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
 
Peter KuczynskiLead Cloud EngineerAuthor Commented:
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
 
gheistCommented:
first part of command fails if there is no device
so shorthand if (&&) takes care second part never gets executed.
0
 
Peter KuczynskiLead Cloud EngineerAuthor Commented:
again can you type the command please
0
 
gheistCommented:
mkswap /dev/sdb && swapon /dev/sdb
0
 
Peter KuczynskiLead Cloud EngineerAuthor Commented:
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
 
gheistCommented:
[ ! -b /dev/sdb ] && mkswap && swapon
0
 
arnoldCommented:
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
 
Peter KuczynskiLead Cloud EngineerAuthor Commented:
Excellent! Thanks! I appreciate the straight-forward concise answer to my question.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

  • 10
  • 7
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now