Monday, April 18, 2011

Resizing swap partition with LVM from the command line

I've been tweaking swap partitions a lot recently as a template virtual machine I created only reserved 512MB of space for a swap partition, where this should really have been at least 2GB for efficient swap usage.

It would seem that mostly other Linux users just boot to a LiveCD and use gparted, but using a GUI interface on a Virtual Machine's remote console is like playing pin-the-tail-on-the-donkey, with a real donkey, who's rather pissed off about you having his tail and all. So, doing it from a command line is a requisite here.

I've used this method on CentOS 5.5, and it should also work with other distributions, you could probably just use the CentOS DVD as well, but I suspect the rescue mode on any distribution's install disc will do the job.

The following method repartitions a 40GB drive (a virtual drive in this case, should work just fine on real ones) from 39.5GB ext3 file system and 512MB swap to 35GB ext3 file system and 2.5GB swap. Note that there is some unallocated space, that's because I'm being paranoid and I do not want the partitions to cross streams and rip a breach in the space time fabric and all my bits to leak out. If you want to tweak the numbers and risk your file system's integrity, you do that, but remember I didn't, I did something safer.

Just to keep things exciting, these drives use LVM (Logical Volume Management), which is awkward, until you get used to it, but gives you a lot more flexibility when it comes to mangling your file systems into something completely unmanageable.

Before you start, clearly identify your partitions, in this case /dev/VolGroup00/Logvol00 is my ext3 partion with the server's file system, and /dev/VolGroup00/LogVol01 is the swap space. Decide how much you want to shrink one, and grow the other. Be sure you're using ext2, ext3, or ext4. Make sure you have 20min to an hour to sit in front of your workstation while the file system is resized, and if you do find that you've been interrupted and CentOS has rebooted, you should be able to restart, repeat the lvm vgchange -a y command after booting to rescue mode and continue from where you left off.
  • Boot from the CentOS 5.5 installation DVD 1, instead of selecting install type in linux rescue and press enter.
  • When prompted, don't load your network drivers, you won't need them.
  • Skip mounting your local drives, we want them for ourselves.
  • Once you've got to a command prompt you're ready to go:

$ lvm vgchange -a y
$ e2fschk -f /dev/VolGroup00/LogVol00
$ resize2fs /dev/VolGroup00/LogVol00 35G
$ lvm lvreduce /dev/VolGroup00/LogVol00 -L 36G
$ lvm lvresize /dev/VolGroup00/LogVol01 -L +2G
$ mkswap /dev/VolGroup00/LogVol01
  • Reboot, and all should be well and you should now have 2GB more swap space.

Tuesday, April 12, 2011

Can haz syntax!

According to the instructions here, I should now have syntax highlighting.

It looks like the Bash/Shell brush isn't included (so I added it):
$ sudo rm -rf /


Some of my horriblest JavaScript from a 5k challenge
var r=true;function st(sz){sz--;
var sc=[[0,0],[1,0],[1,2],[3,1],[4,0],[5,0],[6,0]];//edit this for a new pattern
sc=cp(sc,sz);document.getElementById('tt').innerHTML=mt(sc,sz,sz,sz,sz);rl(sc,(sz),0);}
function h(){if(r){r=false;document.getElementById('tb').disabled=true;document.getElementById('tb').value="Reload page to resart";}}
function cp(p,sz){sz=sz/2;var np=[];var lx=0;var bx=0;var ly=0;var by=0;for(i=0;i<p.length;i++){var px=p[i][0];var py=p[i][1];lx=Math.min(lx,px);bx=Math.max(bx,px);ly=Math.min(ly,py);by=Math.max(by,py);}var dx=Math.round(sz-(bx-lx+1)/2);var dy=Math.round(sz-(by-ly+1)/2);for(i=0;i<p.length;i++){np.push([p[i][0]+dx,p[i][1]+dy]);}return np;}
function rl(cs,sz,it){document.getElementById('it').innerHTML="Iteration:"+it+" Population:"+cs.length;it++;t=setTimeout("this.rc()",0);this.rc=function(){if(r){rl(uc(cs,sz),sz,it);}}}
function mt(cs,sz,x,y){var c=cc(cs,sz,x,y);var sr='<tr>';var er='</tr>';if(x==sz&&y==sz){return "<table>"+sr+c+mt(cs,sz,x-1,y);}else{if(x==sz){return sr+c+mt(cs,sz,x-1,y);}else if(x>0){return c+mt(cs,sz,x-1,y);}else{if(y > 0){return c+er+mt(cs,sz,sz,y-1);}else{return c+er+"</table>";}}}}
function cc(cs,sz,x,y){var sd='<td id="X'+x+'Y'+y+'" class="';var ed='"></td>';if(ii(x,y,cs)){return sd+'l'+ed;}else{return sd+'d'+ed;}}
function ii(x,y,cs){var r=false;if(cs.length >0){for(k=0;k<cs.length;k++){if(cs[k][0]==x&&cs[k][1]==y){r=true;}}}return r;}
function cl(ls,cs){var n=0;while(ls.length >0){var co=ls.pop();if(ii(co[0],co[1],cs)){n++;}}return n;}
function nb(x,y,sz){var ls=[];for(i=-1;i<2;i++){for(j=-1;j<2;j++){var nx=ib(x+i,sz);var ny=ib(y+j,sz);if((x!=nx)||(y!=ny)){ls.push([nx,ny]);}}}return ls;}
function ib(r,sz){if(r<0){r=sz;}if(r>sz){r=0;}return r;}
function uc(cs,sz){var nc=[];var ac=[];for(l=0;l<cs.length;l++){var ls=nb(cs[l][0],cs[l][1],sz);ls.push(cs[l]);while(ls.length >0){var co=ls.pop();if(!ii(co[0],co[1],ac)){ac.push(co);}}}while(ac.length >0){var co=ac.pop(0);var m=cl(nb(co[0],co[1],sz),cs);var c='d';if(m==2&&ii(co[0],co[1],cs)){nc.push(co);c='l';}else if(m==3){nc.push(co);c='l';}document.getElementById('X'+co[0]+'Y'+co[1]).className=c;}return nc;}

Relocated

I have blogs, more than one. More and more there is increasing hate for LiveJournal and the awesome invasiveness of Facebook. More and more, I like blogger, though I think I'm going to have to figure out how to do without, or replicate, the fancy formatting tools I use for mediawiki or Confluence.